Sentence Similarity
This endpoint will receive a series of texts from you, encode them using a BERT-derived model, and produce a series of scores using a Cosine Similarity function to rank them.
This functionality is designed to work best with short sentences.
Constraints
Limits
The maximum length accepted is 512 characters per sentence, and up to 20 sentences per request.
Invokation
- cURL
- Python
- PHP
curl -L -G 'http://api.textkit.ai/detect/similarity' \
--data-urlencode 'text=yeah, man! Go!' \
--header 'Content-Type: application/json' \
--data-raw '[
"the color is red",
"i want to unsubscribe",
"delete me from your emails",
"don'\''t send me anything anymore"
]'
import requests
url = "https://api.textkit.ai/detect/similarity"
payload = json.dumps([
"the color is red",
"i want to unsubscribe",
"delete me from your emails",
"don't send me anything anymore"
])
headers = {
'X-API-Key': 'your_api_key_here',
'Content-Type': 'application/json'
}
response = requests.request("GET", url, headers=headers, data=payload)
print(response.text)
<?php
$curl = curl_init();
$url = "https://api.textkit.ai/detect/similarity"
curl_setopt_array($curl, array(
CURLOPT_URL => $url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_POSTFIELDS =>'[
"the color is red",
"i want to unsubscribe",
"delete me from your emails",
"don\'t send me anything anymore"
]',
CURLOPT_CUSTOMREQUEST => 'GET',
CURLOPT_HTTPHEADER => array(
'X-API-Key: your_api_key_here',
'Content-Type: application/json'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
Response
{
"time_ms": 109,
"scores": [
{
"score": 0.526,
"sentences": [
"delete me from your emails",
"don't send me anything anymore"
]
},
{
"score": 0.496,
"sentences": [
"i want to unsubscribe",
"delete me from your emails"
]
},
{
"score": 0.26,
"sentences": [
"i want to unsubscribe",
"don't send me anything anymore"
]
},
{
"score": 0.097,
"sentences": [
"the color is red",
"don't send me anything anymore"
]
},
{
"score": 0.052,
"sentences": [
"the color is red",
"i want to unsubscribe"
]
},
{
"score": -0.013,
"sentences": [
"the color is red",
"delete me from your emails"
]
}
]
}
Field | Meaning |
---|---|
score | The higher the number, the more similar the sentences are |
sentences | the 2 sentences being compared which resulted in the computed score |