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.

typescript
import { compareStrings } from "@matchforge/core";

const score = compareStrings("Pavan Sharma", "Pavan S Sharma");

console.log(score);
// Similarity score between 0 and 1

Similarity methods

MatchForge provides nine methods. Each uses a different approach to measure similarity between strings.

MethodDescription
levenshteinMeasures similarity using insertions, deletions, and substitutions.
damerau-levenshteinAlso considers adjacent character transpositions.
jaroCompares matching characters and their ordering.
jaro-winklerExtends Jaro similarity by giving additional weight to common prefixes.
jaccardCompares the overlap between character q-gram sets.
diceMeasures q-gram overlap using the Dice coefficient.
cosineCompares strings using cosine similarity.
soundexCompares phonetic codes based on pronunciation.
metaphoneUses 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()`.

typescript
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 practices

Consider 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.

01

Normalize consistently

Apply consistent normalization settings when comparing values from different datasets.

02

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.

03