A TypeScript library for string similarity comparison
npm install node-string-similarityA TypeScript library for comparing strings using various similarity algorithms.
- Levenshtein Distance: Measures the minimum number of edits required to transform one string into another.
- Jaro-Winkler Distance: Gives more weight to matching characters at the start of the strings.
- Cosine Similarity: Measures similarity based on vector representations of strings.
- Dice's Coefficient: Measures overlap between two sets of bigrams.
- Batch Comparison: Compare one string against a list of strings.
- Threshold-Based Matching: Filter matches based on a similarity threshold.
- Unicode Support: Handles multi-byte characters and non-English languages.
- CLI Tool: Command-line interface for quick comparisons.
- RESTful API: Expose string similarity functionalities as HTTP endpoints.
- Database Integration: Utilities for finding similar entries in a database.
- Visualization: Highlight differences between two strings.
``bash`
npm install node-string-similarity
`typescript`
import {
compareStrings,
jaroWinklerDistance,
cosineSimilarity,
diceCoefficient,
batchCompareStrings,
findMatchesAboveThreshold,
} from "node-string-similarity";
`typescriptSimilarity: ${similarity}
const similarity = compareStrings("hello", "hell");
console.log();`
`typescriptJaro-Winkler Similarity: ${similarity}
const similarity = jaroWinklerDistance("hello", "hell");
console.log();`
`typescript`
const results = batchCompareStrings("hello", ["hi", "hey", "hello", "world"]);
console.log(results);
`typescript`
const matches = findMatchesAboveThreshold("hello", ["hi", "hey", "hello", "world"], 0.8);
console.log(matches);
`typescript
import { visualizeStringDifferences } from "node-string-similarity/visualization";
const visualization = visualizeStringDifferences("hello", "hell");
console.log(visualization);
`
`bash`
npm install -g node-string-similarity
`bash`
node-string-similarity compare "hello" "world"
node-string-similarity jaro-winkler "hello" "hell"
node-string-similarity cosine "hello" "world"
node-string-similarity dice "hello" "hell"
`bash`
node dist/api/server.js
- POST /compare
- POST /jaro-winkler
- POST /cosine
- POST /dice
#### Example Request
`json`
{
"str1": "hello",
"str2": "world"
}
#### Example Response
`json`
{
"similarity": 0.4
}
`typescript
import { findSimilarEntries } from "node-string-similarity/dbUtils";
const results = await findSimilarEntries("hello", "my_table", "my_column", 0.8);
console.log(results);
`
`typescript
import { insertStringEntry } from "node-string-similarity/dbUtils";
await insertStringEntry("my_table", "my_column", "hello");
`
Run the tests using Jest:
`bash``
npm test
Contributions are welcome! Please submit a pull request or open an issue for any bugs or feature requests.
This project is licensed under the MIT License.