---
title: "Algorithms"
description: "Choose a string similarity, distance, alignment, or compression measure and read its exact semantics."
---

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

# Algorithms

`strsimpy` groups its algorithms by the kind of comparison they perform. The pages below cover formulas, complexity, empty-input behavior, and known limitations for every public class.

- [**Edit distance**](https://luozhouyang.github.io/python-string-similarity/algorithms/edit-distance/) — Levenshtein, Weighted Levenshtein, Damerau, Optimal String Alignment, MLIPNS, and Strcmp95.
- [**Sequence similarity**](https://luozhouyang.github.io/python-string-similarity/algorithms/sequence-similarity/) — Jaro-Winkler, Longest Common Subsequence, Metric LCS, and Ratcliff-Obershelp.
- [**Shingle-based**](https://luozhouyang.github.io/python-string-similarity/algorithms/shingle-based/) — N-Gram, Q-Gram, Cosine, Jaccard, Sorensen-Dice, Overlap, Tversky, and Tanimoto.
- [**Token-based**](https://luozhouyang.github.io/python-string-similarity/algorithms/token-based/) — Bag distance and Monge-Elkan for token or character multisets.
- [**Sequence alignment**](https://luozhouyang.github.io/python-string-similarity/algorithms/alignment/) — Needleman-Wunsch, Smith-Waterman, and Gotoh scores with optional alignment paths.
- [**Compression**](https://luozhouyang.github.io/python-string-similarity/algorithms/compression/) — Normalized compression distance with a run-length encoder.
- [**Experimental**](https://luozhouyang.github.io/python-string-similarity/algorithms/experimental/) — SIFT4, its options, and its tokenizers.

## Choosing a measure

Use the [selection guide](https://luozhouyang.github.io/python-string-similarity/guides/choosing-an-algorithm/) for a task-oriented comparison. In short:

- Use **Levenshtein** or **Damerau** when you need an edit count, and choose the normalized variants when you need a 0-1 score.
- Use **Jaro-Winkler** or **Strcmp95** for short names and typo-heavy record linkage.
- Use **Jaccard**, **Cosine**, **Tversky**, or **Tanimoto** for documents represented as shingles or sets.
- Use **Monge-Elkan** for token sequences such as names or short addresses.
- Use the **alignment** classes when you need the alignment path or affine gap scoring, and **Ratcliff-Obershelp** when you want a Gestalt/difflib-style ratio.
- Use **NormalizedCompressionDistance** only when a compressor-based distance is appropriate and its limitations are acceptable.

## Properties at a glance

| Class | Kind | Normalized | Metric | Notes |
| --- | --- | --- | --- | --- |
| `Levenshtein` | distance | No | Yes | Uniform edit costs |
| `NormalizedLevenshtein` | distance + similarity | Yes | No | Edit count divided by the longer length |
| `WeightedLevenshtein` | distance | No | No | Callables define insertion, deletion, and substitution costs |
| `Damerau` | distance | No | Yes | Unrestricted adjacent transpositions |
| `OptimalStringAlignment` | distance | No | No | Restricted transpositions; exposes `alignment()` |
| `JaroWinkler` | similarity + distance | Yes | No | Prefix-boosted Jaro score |
| `MLIPNS` | similarity + distance | Yes | No | Binary product-name heuristic |
| `Strcmp95` | similarity + distance | Yes | No | Census Jaro-Winkler variant |
| `LongestCommonSubsequence` | distance | No | No | Insertions and deletions only |
| `MetricLCS` | distance | Yes | Yes | Normalized LCS distance |
| `RatcliffObershelp` | similarity + distance | Yes | No | Gestalt pattern matching |
| `NGram` | distance | Yes | No | Kondrak's normalized n-gram distance |
| `QGram` | distance | No | No | L1 profile distance |
| `Cosine` | similarity + distance | Yes | No | Profile cosine |
| `Jaccard` | similarity + distance | Yes | Yes | Shingle sets |
| `SorensenDice` | similarity + distance | Yes | No | Shingle sets |
| `OverlapCoefficient` | similarity + distance | Yes | No | Shingle sets |
| `Tversky` | similarity + distance | Yes | No | Asymmetric when `alpha != beta` |
| `Tanimoto` | similarity + distance | Yes | Yes | Standard set Tanimoto (Jaccard) |
| `Bag` | distance | No | Yes | Character multiset distance |
| `MongeElkan` | similarity + distance | Yes | No | Token sequences |
| `NeedlemanWunsch` | score + alignment | No | No | Global, linear gap |
| `SmithWaterman` | score + alignment | No | No | Local, linear gap |
| `Gotoh` | score + alignment | No | No | Global, affine gap |
| `NormalizedCompressionDistance` | distance | No | No | RLE compressor only |
| `SIFT4` | distance | No | No | Experimental |

> **Normalized is not the same as metric**
>
> A normalized value lies between 0 and 1. A metric additionally satisfies the triangle inequality. Normalized Levenshtein, Jaro-Winkler, Tversky, and Ratcliff-Obershelp are not metrics. See [Choosing an algorithm](https://luozhouyang.github.io/python-string-similarity/guides/choosing-an-algorithm/) for the details.

Source: https://luozhouyang.github.io/python-string-similarity/algorithms/index.mdx
