A library implementing string similarity and distance measures, written in Rust and exposed to Python through PyO3. It is inspired by tdebatty/java-string-similarity and keeps the original Python API.
Full documentation: https://luozhouyang.github.io/python-string-similarity/
The documentation site covers installation, quick start, every algorithm, selection guidance, the API reference and the known limitations. This README is a short overview.
pip install -U strsimpyPython 3.10+ is supported. Prebuilt abi3 wheels are published for:
- Linux: x86_64 and aarch64, both manylinux (glibc) and musllinux (Alpine)
- macOS: x86_64 and arm64
- Windows: x64
Installing from the source distribution requires a Rust toolchain (1.85 or
newer) and maturin; pip handles both automatically
when no wheel is available.
from strsimpy import (
JaroWinkler,
Levenshtein,
NeedlemanWunsch,
NormalizedCompressionDistance,
Tversky,
)
print(Levenshtein().distance("kitten", "sitting")) # 3
print(JaroWinkler().similarity("MARTHA", "MARHTA")) # 0.9611111111111111
print(Tversky(2, 1.0, 1.0).similarity("night", "nacht")) # 0.14285714285714285
print(NeedlemanWunsch().score("GATTACA", "GCATGCU")) # 0.0
print(NormalizedCompressionDistance().distance("aaaa", "aaab")) # 0.3333333333333333All public classes can be imported from the package root, and the per-module
imports (from strsimpy.levenshtein import Levenshtein) remain supported.
The algorithms fall into several families:
- Edit distance: Levenshtein, Damerau-Levenshtein, Optimal String Alignment, MLIPNS, Strcmp95
- Normalized similarity: Normalized Levenshtein, Jaro-Winkler, Metric LCS, Ratcliff-Obershelp
- Shingle-based: N-Gram, Q-Gram, Cosine, Jaccard, Sorensen-Dice, Overlap coefficient, Tversky, Tanimoto
- Token-based: Bag distance, Monge-Elkan
- Sequence alignment: Needleman-Wunsch, Smith-Waterman, Gotoh
- Compression: Normalized compression distance (RLE)
- Experimental: SIFT4
The docs contain the full comparison table, formulas, complexity and edge-case behavior:
| Topic | Documentation |
|---|---|
| Installation and first steps | Getting started |
| Edit distances | Edit distance algorithms |
| Jaro-Winkler, LCS, Ratcliff-Obershelp | Sequence similarity |
| N-Gram, Jaccard, Tversky, Tanimoto, ... | Shingle-based algorithms |
| Bag, Monge-Elkan | Token-based algorithms |
| Needleman-Wunsch, Smith-Waterman, Gotoh | Sequence alignment |
| Normalized compression distance | Compression |
| Choosing an algorithm | Selection guide |
| Semantics and known divergences | Compatibility guide |
| Full class and method reference | API reference |
- Identical strings return the float
0.0for historical edit-distance classes such asLevenshtein, and1.0for similarity classes. - Shingle-based metrics normalize whitespace before shingling.
Tverskyuses shingle sets and is asymmetric whenalpha != beta.Tanimotois the standard set measure (equivalent to Jaccard), not TextDistance'slog2variant.RatcliffObershelpfollowsdifflibtie-breaking and can be asymmetric on repeated patterns.- Alignment classes expose raw
score()andalignment()values; they are not distance/similarity classes. NormalizedCompressionDistanceships only an RLE compressor; values are compressor-dependent and identical strings need not score 0.
See the compatibility guide for the complete list.
The project is a Rust workspace with a thin Python packaging layer:
crates/strsimpy-core/ Pure Rust implementations of every algorithm
crates/strsimpy-py/ PyO3 bindings (the strsimpy._strsimpy extension module)
python/strsimpy/ Python package, base classes and per-module compatibility shims
tests/ Python integration tests
docs/ Nimbus documentation site (built with pnpm)
All algorithms operate on Unicode scalar values (code points), matching
Python's len(str) and indexing semantics. The Rust core has its own unit tests
and is usable as a normal Rust library.
Prerequisites: Python 3.10+, Rust 1.85+ and a C compiler. maturin drives the build.
# Build and install the extension in editable mode
pip install -e .
# Rust checks
cargo fmt --all
cargo clippy --workspace --all-targets -- -D warnings
cargo test --workspace
# Python checks
pytest
black --check python tests
ruff check --select RUF022 python tests
# Documentation site
cd docs
pnpm install
pnpm run dev
pnpm run buildCI runs the Rust checks and the Python test suite on Python 3.10-3.13. The release workflow builds abi3 wheels for Linux, macOS and Windows and publishes them to PyPI together with the source distribution. The docs workflow builds the Nimbus site and deploys it to GitHub Pages.
- Added token/set metrics:
Tversky,Tanimoto,BagandMongeElkan. - Added edit-based metrics:
MLIPNSandStrCmp95. - Added
RatcliffObershelpwith difflib-compatible tie-breaking. - Added sequence alignment scores:
NeedlemanWunsch,SmithWatermanandGotoh(score + alignment path). - Added
NormalizedCompressionDistancewith an RLE compressor. - Documented semantics, limitations and divergences from TextDistance.
See the documentation changelog and GitHub Releases for earlier versions.
- StringSimilarity.NET — a .NET port of java-string-similarity
Using this library in your project and want to be mentioned here? Drop us a line!