Text Similarity
Compare two pieces of text and get a similarity score between 0 and 1 using cosine similarity on word-frequency vectors.
Overview
Use Cases
- Duplicate content detection
- Plagiarism checking
- Search and recommendation relevance scoring
- Document clustering and deduplication
- FAQ matching and chatbot response selection
Features
Cosine similarity on bag-of-words vectors
Normalised score between 0 (no overlap) and 1 (identical)
Case-insensitive, punctuation-agnostic tokenisation
Fast, in-memory computation β no external ML model required
API Endpoints
Compare Text Similarity
Compares two texts and returns a cosine similarity score.
POST
https://api.requiems.xyz/v1/ai/similarity
Parameters
| Name | Type | Required | Description |
|---|---|---|---|
| text1 | string |
Required | The first text to compare. |
| text2 | string |
Required | The second text to compare. |
Try it out
Live DemoRequest
The first text to compare.
The second text to compare.
Sending request...
Response Fields
| Field | Type | Description |
|---|---|---|
| similarity | number |
Cosine similarity score between the two texts, in the range [0, 1]. |
| method | string |
The algorithm used. Currently always 'cosine'. |
Code Examples
curl -X POST https://api.requiems.xyz/v1/ai/similarity \
-H "requiems-api-key: YOUR_API_KEY" \
-H "Content-Type: application/json" \
-d '{"text1": "The cat sat on the mat", "text2": "A cat was sitting on a mat"}'
import requests
url = "https://api.requiems.xyz/v1/ai/similarity"
headers = {
"requiems-api-key": "YOUR_API_KEY",
"Content-Type": "application/json"
}
payload = {
"text1": "The cat sat on the mat",
"text2": "A cat was sitting on a mat"
}
response = requests.post(url, headers=headers, json=payload)
data = response.json()
print(data["data"]["similarity"])
print(data["data"]["method"])
const response = await fetch('https://api.requiems.xyz/v1/ai/similarity', {
method: 'POST',
headers: {
'requiems-api-key': 'YOUR_API_KEY',
'Content-Type': 'application/json'
},
body: JSON.stringify({
text1: 'The cat sat on the mat',
text2: 'A cat was sitting on a mat'
})
});
const data = await response.json();
console.log(data.data.similarity);
console.log(data.data.method);
require 'net/http'
require 'json'
uri = URI('https://api.requiems.xyz/v1/ai/similarity')
request = Net::HTTP::Post.new(uri)
request['requiems-api-key'] = 'YOUR_API_KEY'
request['Content-Type'] = 'application/json'
request.body = {
text1: 'The cat sat on the mat',
text2: 'A cat was sitting on a mat'
}.to_json
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http|
http.request(request)
end
data = JSON.parse(response.body)
puts data['data']['similarity']
puts data['data']['method']
Error Responses
validation_failed
One or both text fields are missing or empty.
bad_request
The request body is missing or malformed.
internal_error
Unexpected server error.
Frequently Asked Questions
A score of 0 means the two texts share no words in common. A score of 1 means both texts have an identical word distribution.
No. Both texts are lowercased before comparison, so "Cat" and "cat" are treated as the same word.
No. Only alphanumeric characters are considered. Punctuation and whitespace are stripped before tokenisation.
Cosine similarity on term-frequency (bag-of-words) vectors. Each word becomes a dimension and its frequency is the weight. The score is computed as: similarity = (A Β· B) / (|A| Γ |B|), where A and B are the word-frequency vectors of the two texts.