Skip to content

About

A library implementing different string similarity and distance measures using Python.

Topics

Resources

Stars

1.0k stars

Watchers

20 watching

Forks

Latest commit

 

History

111 Commits

Folders and files

Repository files navigation

python-string-similarity

CI Docs PyPI version Python

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.

Download

pip install -U strsimpy

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

Quick start

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

All public classes can be imported from the package root, and the per-module imports (from strsimpy.levenshtein import Levenshtein) remain supported.

Overview

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

Important semantics

  • Identical strings return the float 0.0 for historical edit-distance classes such as Levenshtein, and 1.0 for similarity classes.
  • Shingle-based metrics normalize whitespace before shingling.
  • Tversky uses shingle sets and is asymmetric when alpha != beta.
  • Tanimoto is the standard set measure (equivalent to Jaccard), not TextDistance's log2 variant.
  • RatcliffObershelp follows difflib tie-breaking and can be asymmetric on repeated patterns.
  • Alignment classes expose raw score() and alignment() values; they are not distance/similarity classes.
  • NormalizedCompressionDistance ships only an RLE compressor; values are compressor-dependent and identical strings need not score 0.

See the compatibility guide for the complete list.

Implementation

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.

Development

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 build

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

Changelog

0.5.0

  • Added token/set metrics: Tversky, Tanimoto, Bag and MongeElkan.
  • Added edit-based metrics: MLIPNS and StrCmp95.
  • Added RatcliffObershelp with difflib-compatible tie-breaking.
  • Added sequence alignment scores: NeedlemanWunsch, SmithWaterman and Gotoh (score + alignment path).
  • Added NormalizedCompressionDistance with an RLE compressor.
  • Documented semantics, limitations and divergences from TextDistance.

See the documentation changelog and GitHub Releases for earlier versions.

Users

Using this library in your project and want to be mentioned here? Drop us a line!

About

A library implementing different string similarity and distance measures using Python.

Topics

Resources

Stars

1.0k stars

Watchers

20 watching

Forks

Releases

Packages

Used by

Contributors

Languages