The Text Similarity API computes the similarity score between two pieces of text. It uses state-of-the-art NLP machine learning models to first embed (see our Embddings API) the texts into 768-dimension vectors, and then computes the cosine similarity between the two vectors.
HTTP POST
Returns a similarity score between 0 and 1 (1 is similar and 0 is dissimilar) of two given texts.
text_1
(required) - first input text. Maximum 5000 characters.
text_2
(required) - second input text. Maximum 5000 characters.
X-Api-Key
(required) - API Key associated with your account.
Live Demo!
https://api.api-ninjas.com/v1/textsimilarity
text_1:
text_2:
{
"similarity": 0.7749438285827637
}
import requests
body = { 'text_1': 'This is an example sentence.', 'text_2': 'This is just another sample sentence.' }
api_url = 'https://api.api-ninjas.com/v1/textsimilarity'
response = requests.post(api_url, headers={'X-Api-Key': 'YOUR_API_KEY'}, json=body)
if response.status_code == requests.codes.ok:
print(response.text)
else:
print("Error:", response.status_code, response.text)
$.ajax({
method: 'POST',
url: 'https://api.api-ninjas.com/v1/textsimilarity',
headers: { 'X-Api-Key': 'YOUR_API_KEY'},
data: JSON.stringify({ "text_1": "This is an example sentence.", "text_2": "This is just another sample sentence." }),
success: function(result) {
console.log(result);
},
error: function ajaxError(jqXHR) {
console.error('Error: ', jqXHR.responseText);
}
});