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:
MLIPNSis normalized but binary: it returns only0.0or1.0.NormalizedCompressionDistancehas “normalized” in its name but is not aNormalizedStringDistance; 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:
LevenshteinandDameraufor raw edit distances.MetricLCSfor a normalized distance based on the longest common subsequence.JaccardandTanimotofor set-based shingle distances. Their similarities are in[0, 1]; the metric is the distance1 - similarity.Bagfor 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:
Tverskyis asymmetric whenalpha != beta. The first string’s extra shingles are weighted byalpha; the second string’s bybeta.MongeElkanaverages over the tokens of the first argument by default. UseMongeElkan(symmetric=True)to average both directions.RatcliffObershelpusesdifflib-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.
Bagdistance is a cheap metric lower bound and filter for edit distance.QGramdistance is a lower bound on Levenshtein distance and costsO(m + n)once the profiles exist.- Shingle profiles can be built once per string and compared repeatedly. Use
QGram.distance_profile()orCosine.similarity_profiles(); see Performance.
A minimal example
from strsimpy import JaroWinkler, Levenshtein
print(JaroWinkler().similarity("MARTHA", "MARHTA"))
print(Levenshtein().distance("kitten", "sitting"))0.9611111111111111
3The 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.