String comparison
String comparison measures how similar two strings are. MatchForge provides multiple similarity algorithms for comparing names, addresses, and other text values through a consistent API.
Basic usage
Use compareStrings() to compare two strings. It returns a similarity score between 0 and 1, where higher values indicate greater similarity. The default algorithm is Jaro-Winkler.
import { compareStrings } from "@matchforge/core";
const score = compareStrings("Pavan Sharma", "Pavan S Sharma");
console.log(score);
// Similarity score between 0 and 1Similarity methods
MatchForge provides nine methods. Each uses a different approach to measure similarity between strings.
| Method | Description |
|---|---|
levenshtein | Measures similarity using insertions, deletions, and substitutions. |
damerau-levenshtein | Also considers adjacent character transpositions. |
jaro | Compares matching characters and their ordering. |
jaro-winkler | Extends Jaro similarity by giving additional weight to common prefixes. |
jaccard | Compares the overlap between character q-gram sets. |
dice | Measures q-gram overlap using the Dice coefficient. |
cosine | Compares strings using cosine similarity. |
soundex | Compares phonetic codes based on pronunciation. |
metaphone | Uses phonetic encoding to compare similar-sounding words. |
Configure comparison
You can select a similarity method using the `method` option. You can also pass normalization options to preprocess both strings before comparison. This uses the same options available in `normalizeString()`.
import { compareStrings } from "@matchforge/core";
const score = compareStrings("José Sharma", "Jose Sharma", {
method: "jaro-winkler",
normalization: {
lowercase: true,
trim: true,
normalizeWhitespace: true,
removeAccents: true,
removePunctuation: false,
},
});
console.log(score);The normalization option applies preprocessing to both input strings before calculating similarity. If omitted, the default normalization behavior is used.
Important considerations
Best practicesConsider these points when selecting a similarity method.
Choose the appropriate method
Different algorithms handle character changes, ordering, and pronunciation differently. Select a method based on your data.
Normalize consistently
Apply consistent normalization settings when comparing values from different datasets.
Interpret scores carefully
A high similarity score does not always mean two records refer to the same entity. Choose an appropriate threshold for your application.