Similar Terms
Many times you may need to find terms that have a similar meaning to another term. A few examples:
- You've implemented a document editing interface and want to recommend alternative wordings to the user.
- You're trying to group product listings by similar attributes, some users either don't add tags or use different but synonymous tags.
- You're trying to optimize ad copy and want to try replacing some words with possibly catchier alternatives.
In these cases, it can be helpful to look for similar terms. The usual method for this is by using a thesaurus. Sapling offers this capability along with semantic matching to find similar terms that a thesaurus would miss.
You can test out this endpoint at our utilities page as well:
Similar Terms POST
Sample Code
- cURL
- JavaScript
- Python
curl -X POST https://api.sapling.ai/api/v1/thesaurus \
-H "Content-Type: application/json" \
-d '{
"key":"<api-key>",
"query":"pet",
}'
import axios from 'axios';
async function run(query) {
try {
const response = await axios.post(
'https://api.sapling.ai/api/v1/thesaurus',
{
key: '<api-key>',
query,
},
);
const {status, data} = response;
} catch (err) {
const { msg } = err.response.data;
}
}
run('pet');
import requests
key = '<api-key>'
url = 'https://api.sapling.ai/api/v1/thesaurus'
data = {
'key': key,
'query': 'pet',
}
try:
resp = requests.post(url, json=data)
resp_json = resp.json()
if 200 <= resp.status_code < 300:
print(resp_json)
else:
print('Error: ', resp_json)
except Exception as e:
print('Error: ', e)
Sample Response
{
"neighbors": [
[
0.8128582835197449,
"dog"
],
[
0.7802609205245972,
"cat"
],
[
0.7524483799934387,
"puppy"
],
[
0.7283433079719543,
"animal"
]
],
"synonyms": [
"darling",
"favorite",
"favourite",
"dearie",
"deary",
"ducky",
"positron_emission_tomography",
"favored",
"best-loved",
"preferred",
"preferent"
]
}
Request Parameters
https://api.sapling.ai/api/v1/thesaurus
HTTP method: POST
key: String
32-character API key
query: String
Term to find similar terms for.
Response Parameters
JSON containing similar terms from a lexical database (WordNet) as well as from a semantic database (terms matched using embedding similarity).
The semantically similar terms are in the neighbors
field.
Each neighbor also includes a floating point value indicating
the similarity from 0 to 1 (with 1 being most similar).
The synonyms from the lexical database are provided in a list
under the synonyms
field.
Output structure:
{
"neighbors": [
[
0.8440492153167725,
"puppy"
],
[
0.8339642882347107,
"cat"
],
[
0.8266059160232544,
"pup"
],
[
0.8128583431243896,
"pet"
]
],
"synonyms": [
"domestic_dog",
"Canis_familiaris",
"frump",
...
]
}