Skip to content

Choosing an algorithm

A task-first decision table for strsimpy 0.5.0, with normalized, raw, metric and non-metric measures compared.

This guide maps common jobs to the measures in strsimpy 0.5.0. The decision table starts from the task; the notes below explain the columns and the trade-offs that are easy to miss.

Two axes that matter

Normalized or raw. A normalized measure is registered under NormalizedStringDistance or NormalizedStringSimilarity and is designed to return a value in [0.0, 1.0]. A raw measure returns an algorithm-dependent value: an edit count, a set difference, or an alignment score. Two exceptions are worth remembering:

  • MLIPNS is normalized but binary: it returns only 0.0 or 1.0.
  • NormalizedCompressionDistance has “normalized” in its name but is not a NormalizedStringDistance; its values are not guaranteed to lie in [0, 1].

Metric or non-metric. A metric distance is non-negative, zero only for identical inputs, symmetric, and obeys the triangle inequality d(x, y) <= d(x, z) + d(z, y). Only distances can be metric; a similarity such as Jaccard becomes a metric distance through 1 - similarity. The MetricStringDistance ABC marks the measures whose distance is a metric. That property matters for nearest-neighbor indexes, pruning and threshold search.

Decision table

Task Measures Output Normalized? Metric? Notes
Typo correction, short names JaroWinkler, NormalizedLevenshtein, Damerau, StrCmp95 similarity and distance Yes Damerau only Jaro-Winkler and Strcmp95 are designed for short strings; Damerau also handles adjacent transpositions.
Raw edit counts Levenshtein, Damerau, OptimalStringAlignment, LongestCommonSubsequence distance; OSA also has alignment() No Levenshtein and Damerau only Use Levenshtein by default; Damerau when swaps count as one edit; LCS when only insertions and deletions are allowed.
Normalized similarity NormalizedLevenshtein, JaroWinkler, MetricLCS, RatcliffObershelp, NGram, MLIPNS, StrCmp95 distance, similarity or both Yes MetricLCS only NGram and MetricLCS provide only distance(); MLIPNS is a binary heuristic.
Set or profile similarity QGram, Cosine, Jaccard, SorensenDice, OverlapCoefficient, Tversky, Tanimoto QGram distance; the others similarity and distance QGram no; the others yes Jaccard and Tanimoto QGram and Cosine keep shingle counts; the rest use sets. Tversky is asymmetric when alpha != beta.
Token or sentence MongeElkan, Bag Monge-Elkan similarity and distance; Bag distance Monge-Elkan yes; Bag no Bag only Monge-Elkan compares whitespace-separated tokens; Bag compares character multisets and ignores order.
Sequence alignment NeedlemanWunsch, SmithWaterman, Gotoh raw score and alignment path No No Global, local, and affine-gap alignment. Scores can be negative and are not comparable across scoring parameters.
Compression based NormalizedCompressionDistance distance No, despite the name No Only the RLE compressor is available. Values are compressor-dependent and identical inputs are not guaranteed to score 0.
Diff or Gestalt matching RatcliffObershelp similarity and distance Yes No The formula behind difflib.SequenceMatcher.ratio(); tie-breaking can make it asymmetric.
Experimental, human-oriented SIFT4 integer distance No No Heuristic; accepts maxoffset and an options dictionary with tokenizers and evaluators.

Choosing a true metric

If you need the triangle inequality for an index, pruning or a threshold search, start with the measures registered as MetricStringDistance:

  • Levenshtein and Damerau for raw edit distances.
  • MetricLCS for a normalized distance based on the longest common subsequence.
  • Jaccard and Tanimoto for set-based shingle distances. Their similarities are in [0, 1]; the metric is the distance 1 - similarity.
  • Bag for a cheap character-multiset metric.

OptimalStringAlignment, LongestCommonSubsequence, WeightedLevenshtein, NormalizedLevenshtein, JaroWinkler, StrCmp95, NGram, QGram, Cosine, SorensenDice, OverlapCoefficient, Tversky, RatcliffObershelp, MongeElkan and MLIPNS are not metric distances.

Choosing a symmetric score

Most measures are symmetric, but three need attention:

  • Tversky is asymmetric when alpha != beta. The first string’s extra shingles are weighted by alpha; the second string’s by beta.
  • MongeElkan averages over the tokens of the first argument by default. Use MongeElkan(symmetric=True) to average both directions.
  • RatcliffObershelp uses difflib-style tie-breaking, so repeated patterns can produce different values for the two argument orders.

If argument order must not affect the result, use the symmetric option or pick a measure that is symmetric by construction.

Cheap candidate generation

When comparing many pairs, compute a cheap bound before the expensive measure:

  • The absolute length difference is a lower bound for Levenshtein distance.
  • Bag distance is a cheap metric lower bound and filter for edit distance.
  • QGram distance is a lower bound on Levenshtein distance and costs O(m + n) once the profiles exist.
  • Shingle profiles can be built once per string and compared repeatedly. Use QGram.distance_profile() or Cosine.similarity_profiles(); see Performance.

A minimal example

from strsimpy import JaroWinkler, Levenshtein

print(JaroWinkler().similarity("MARTHA", "MARHTA"))
print(Levenshtein().distance("kitten", "sitting"))
0.9611111111111111
3

The first value is a normalized similarity; the second is a raw edit count. The next step depends on what the application does with the number: rank candidates, apply a fixed threshold, or explain the edits.

Next steps

  • Semantics and divergences explains the set, alignment, compression and token definitions that differ from other libraries.
  • Performance gives complexity and memory by family, plus profile reuse and Unicode details.
  • The API reference has constructors, return types, ABC registrations and pickling behavior for every class.

Type to search…

↑↓ navigate↵ selectEsc close