A Node.js Express server that provides string similarity comparison using multiple algorithms including TF-IDF, Jaro-Winkler, Levenshtein distance, Jaccard similarity, and cosine similarity.
- Multiple Similarity Algorithms: Combines 5 different similarity metrics for accurate results
- Weighted Scoring: Uses weighted combination of different similarity measures
- RESTful API: Simple POST endpoint for string comparison
- Flexible Input: Works with arrays of objects and custom property selection
- JSON String Parsing: Automatically parses JSON strings sent as inputObject
- Nested Object Access: Supports dot notation for accessing nested properties (e.g., 'practicetypes.practicetype_title')
- Detailed Results: Returns similarity scores with detailed breakdowns
npm installnpm startThe server will start on port 3031.
Compare a string against an array of objects.
Request Body:
{
"inputObject": {
"id": 1,
"title": "Hello world, this is a test",
"category": "Sample"
},
"inputElement": "title",
"arrayOfObjects": [
{ "id": 1, "text": "Hello world, this is a test" },
{ "id": 2, "text": "Hello world, this is a sample" },
{ "id": 3, "text": "Hi there, this is a test" }
],
"elementToCheck": "text"
}Alternative: JSON String as inputObject:
{
"inputObject": "{\"id\": 1, \"title\": \"Hello world, this is a test\", \"category\": \"Sample\"}",
"inputElement": "title",
"arrayOfObjects": [
{ "id": 1, "text": "Hello world, this is a test" },
{ "id": 2, "text": "Hello world, this is a sample" },
{ "id": 3, "text": "Hi there, this is a test" }
],
"elementToCheck": "text"
}Nested Object Access with Dot Notation:
{
"inputObject": {
"practicetypes": {
"practicetype_title": "Εξωτερική ακτινοθεραπεία με ακτίνες Χ",
"practicetype_number": 1
},
"practice_number": 1,
"practice_code": "Γ1"
},
"inputElement": "practicetypes.practicetype_title",
"arrayOfObjects": [
{ "PracticeTypeTitle": "Εξωτερική ακτινοθεραπεία με ακτίνες Χ" },
{ "PracticeTypeTitle": "Διαγνωστική ακτινοσκόπηση" }
],
"elementToCheck": "PracticeTypeTitle"
}Response:
{
"inputObject": {
"id": 1,
"title": "Hello world, this is a test",
"category": "Sample"
},
"inputString": "Hello world, this is a test",
"totalCompared": 3,
"resultsReturned": 3,
"results": [
{
"index": 0,
"score": 1.0,
"targetString": "Hello world, this is a test",
"originalObject": { "id": 1, "text": "Hello world, this is a test" }
},
{
"index": 2,
"score": 0.7234,
"targetString": "Hi there, this is a test",
"originalObject": { "id": 3, "text": "Hi there, this is a test" }
},
{
"index": 1,
"score": 0.6789,
"targetString": "Hello world, this is a sample",
"originalObject": { "id": 2, "text": "Hello world, this is a sample" }
}
],
"topMatch": {
"index": 0,
"score": 1.0,
"targetString": "Hello world, this is a test",
"originalObject": { "id": 1, "text": "Hello world, this is a test" }
}
}Health check endpoint.
Server information and usage instructions.
- String Similarity (Dice Coefficient): 30% weight - Wikipedia: Sørensen–Dice coefficient
- Jaro-Winkler Distance: 20% weight - Wikipedia: Jaro–Winkler distance
- Levenshtein Distance: 20% weight - Wikipedia: Levenshtein distance
- Jaccard Similarity: 15% weight - Wikipedia: Jaccard index
- TF-IDF Cosine Similarity: 15% weight - Wikipedia: TF-IDF and Wikipedia: Cosine similarity
Run the test script to verify the server functionality:
node test-server.jsFor development with auto-restart:
npm run devcurl -X POST http://localhost:3031/compare \
-H "Content-Type: application/json" \
-d '{
"inputObject": {
"id": 1,
"title": "Hello world",
"category": "Greeting"
},
"inputElement": "title",
"arrayOfObjects": [
{"id": 1, "text": "Hello world"},
{"id": 2, "text": "Hi there"},
{"id": 3, "text": "Goodbye world"}
],
"elementToCheck": "text"
}'The server includes comprehensive error handling for:
- Missing or invalid input parameters
- Empty arrays
- Invalid object structures
- Server errors
All errors return appropriate HTTP status codes and descriptive error messages.