Skip to main content

Crate lev

Crate lev 

Source
Expand description

An extremely fast Levenshtein distance and similarity ratio.

  • distance – the Levenshtein edit distance between two strings.
  • ratio – the indel similarity score in [0.0, 1.0].
assert_eq!(lev::distance("kitten", "sitting"), 3);
assert_eq!(lev::ratio("abc", "abc"), 1.0);

Characters are counted as Unicode scalar values (chars), so an accented letter or an emoji counts as one character.

Both functions use bit-parallel algorithms (Myers 1999, Hyyrö 2003) after stripping common prefixes and suffixes, and a diagonal band for long, similar strings. On x86-64 the hot path is compiled a second time for x86-64-v2 and picked at run time.

The same code backs the lev-rs Python package. Its bindings are behind the python feature, which only the Python build enables; Python 3.10 through 3.15 are supported, including free-threaded builds (PEP 703).

Functions§

distance
Levenshtein edit distance between two strings.
ratio
Calculate how similar two strings are, as a score from 0.0 to 1.0.