---
title: "Choosing an algorithm"
description: "A task-first decision table for strsimpy 0.5.0, with normalized, raw, metric and non-metric measures compared."
---

> Documentation Index
> Fetch the complete documentation index at: https://luozhouyang.github.io/python-string-similarity/llms.txt
> Use this file to discover all available pages before exploring further.

# Choosing an algorithm

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`](https://luozhouyang.github.io/python-string-similarity/algorithms/sequence-similarity/), [`NormalizedLevenshtein`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/), [`Damerau`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/), [`StrCmp95`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/) | similarity and distance | Yes | Damerau only | Jaro-Winkler and Strcmp95 are designed for short strings; Damerau also handles adjacent transpositions. |
| Raw edit counts | [`Levenshtein`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/), [`Damerau`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/), [`OptimalStringAlignment`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/), [`LongestCommonSubsequence`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/) | 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`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/), [`JaroWinkler`](https://luozhouyang.github.io/python-string-similarity/algorithms/sequence-similarity/), [`MetricLCS`](https://luozhouyang.github.io/python-string-similarity/algorithms/sequence-similarity/), [`RatcliffObershelp`](https://luozhouyang.github.io/python-string-similarity/algorithms/sequence-similarity/), [`NGram`](https://luozhouyang.github.io/python-string-similarity/algorithms/shingle-based/), [`MLIPNS`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/), [`StrCmp95`](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/) | distance, similarity or both | Yes | MetricLCS only | `NGram` and `MetricLCS` provide only `distance()`; MLIPNS is a binary heuristic. |
| Set or profile similarity | [`QGram`](https://luozhouyang.github.io/python-string-similarity/algorithms/shingle-based/), [`Cosine`](https://luozhouyang.github.io/python-string-similarity/algorithms/shingle-based/), [`Jaccard`](https://luozhouyang.github.io/python-string-similarity/algorithms/shingle-based/), [`SorensenDice`](https://luozhouyang.github.io/python-string-similarity/algorithms/shingle-based/), [`OverlapCoefficient`](https://luozhouyang.github.io/python-string-similarity/algorithms/shingle-based/), [`Tversky`](https://luozhouyang.github.io/python-string-similarity/algorithms/shingle-based/), [`Tanimoto`](https://luozhouyang.github.io/python-string-similarity/algorithms/shingle-based/) | 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`](https://luozhouyang.github.io/python-string-similarity/algorithms/token-based/), [`Bag`](https://luozhouyang.github.io/python-string-similarity/algorithms/token-based/) | 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`](https://luozhouyang.github.io/python-string-similarity/algorithms/alignment/), [`SmithWaterman`](https://luozhouyang.github.io/python-string-similarity/algorithms/alignment/), [`Gotoh`](https://luozhouyang.github.io/python-string-similarity/algorithms/alignment/) | 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`](https://luozhouyang.github.io/python-string-similarity/algorithms/compression/) | 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`](https://luozhouyang.github.io/python-string-similarity/algorithms/sequence-similarity/) | similarity and distance | Yes | No | The formula behind `difflib.SequenceMatcher.ratio()`; tie-breaking can make it asymmetric. |
| Experimental, human-oriented | [`SIFT4`](https://luozhouyang.github.io/python-string-similarity/algorithms/experimental/) | integer distance | No | No | Heuristic; accepts `maxoffset` and an options dictionary with tokenizers and evaluators. |

> **A named score is not a calibrated probability**
>
> Normalized similarities are convenient for ranking, but a value of `0.8` from
> Jaro-Winkler does not mean the same thing as `0.8` from Jaccard or
> Monge-Elkan. Do not mix scales in one ranked list unless the application
> defines how to calibrate them.

## 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](https://luozhouyang.github.io/python-string-similarity/guides/performance/).

## A minimal example

```python
from strsimpy import JaroWinkler, Levenshtein

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

```text
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](https://luozhouyang.github.io/python-string-similarity/guides/compatibility/) explains the set,
  alignment, compression and token definitions that differ from other
  libraries.
- [Performance](https://luozhouyang.github.io/python-string-similarity/guides/performance/) gives complexity and memory by family,
  plus profile reuse and Unicode details.
- The [API reference](https://luozhouyang.github.io/python-string-similarity/reference/api/) has constructors, return types, ABC
  registrations and pickling behavior for every class.

Source: https://luozhouyang.github.io/python-string-similarity/guides/choosing-an-algorithm/index.mdx
