Levenshtein
A fast Python C extension for Levenshtein edit distance, string similarity, and edit operations.
Repository Health
Technical Analysis
Levenshtein is a Python C extension module that provides fast computation of Levenshtein (edit) distance and related string metrics. Beyond raw edit distance, it returns the underlying edit operations, computes normalized string similarity, and can derive approximate median strings and set/sequence similarity, making it a compact toolkit for fuzzy string work.
Maintained by the rapidfuzz project, the current versions are implemented on top of the high-performance rapidfuzz-cpp backend, giving native-speed results while keeping a simple Python API. It is a common building block for spell checking, deduplication, record linkage, and any workflow that needs to measure how similar two strings are.
What You Get
distance()for Levenshtein (edit) distance between two stringsratio()and related functions for normalized string similarity- Edit-operation APIs (
editops,opcodes) describing how to transform one string into another - Approximate median-string and general string-averaging helpers
- Sequence and set similarity functions, all backed by a compiled extension
Common Use Cases
- Fuzzy matching and deduplication of names, addresses, or records
- Spell checking and autocorrect ranking by edit distance
- Computing similarity scores for search, clustering, or record linkage
- Deriving a representative median string from a set of noisy inputs
Under The Hood
Architecture - The Python-facing package lives in src/Levenshtein/, with __init__.py/__init__.pyi defining the public API and type stubs, a Cython layer (levenshtein_cpp.pyx) bridging to the C++ core in Levenshtein-c/_levenshtein.cpp, and a legacy StringMatcher.py compatibility shim. The vendored extern/ tree and CMakeLists.txt wire in the rapidfuzz-cpp backend that performs the actual distance and similarity computation.
Tech Stack - The distribution is a compiled extension: C++ core plus Cython glue, built via CMake (scikit-build style) as configured in pyproject.toml and CMakeLists.txt, targeting Python 3.10+. It depends on rapidfuzz’s C++ library for its algorithms and ships py.typed plus .pyi stubs for static typing.
Code Quality - The project is actively maintained under the rapidfuzz organization with a tests/ suite, CI via GitHub Actions, published documentation, a HISTORY changelog, and a SECURITY policy. Delegating algorithms to the shared rapidfuzz-cpp backend keeps the surface small and consistent.
API Design - The API is flat and predictable: standalone functions like distance, ratio, editops, and median take strings and return numbers or operation lists, with no objects to instantiate. This makes it easy to adopt as a drop-in for the classic python-Levenshtein package while gaining modern performance.