# lev

`lev` is an extremely fast Python library for comparing strings, written in Rust. 🦀 It computes the [Levenshtein distance](<https://en.wikipedia.org/wiki/Levenshtein_distance>) and a similarity ratio based on the [longest common subsequence](<https://en.wikipedia.org/wiki/Longest_common_subsequence>).

## Installation

Install from [PyPI](<https://pypi.org/project/lev-rs/>):

```
uv add lev-rs
```

or with pip:

```
pip install lev-rs
```

Wheels are available for Python 3.10 through 3.15, including free-threaded Python (3.13t+), on Linux, macOS, and Windows.

## Usage

```
import lev

lev.distance("kitten", "sitting")  # 3
lev.distance("résumé", "resume")  # 2
lev.distance("日本語", "日本")  # 1

lev.ratio("kitten", "sitting")  # 0.615...
lev.ratio("abc", "xyz")  # 0.0
lev.ratio("", "")  # 1.0
```

`lev.ratio` returns the same values as `rapidfuzz.fuzz.ratio(s1, s2) / 100` and `Levenshtein.ratio`.

See the [API Reference](<https://KarelZe.github.io/lev/api/index.md>) for details.

### Rust

`lev` is also published as the [`lev-rs`](<https://crates.io/crates/lev-rs>) crate:

```
cargo add lev-rs
```

```
assert_eq!(lev::distance("kitten", "sitting"), 3);
assert_eq!(lev::ratio("abc", "xyz"), 0.0);
```

The crate requires Rust 1.91 or newer. See [docs.rs](<https://docs.rs/lev-rs>) for details.

## Benchmarks

`lev` is compared against the fastest Levenshtein libraries for Python: [rapidfuzz](<https://github.com/rapidfuzz/RapidFuzz>), [polyleven](<https://github.com/fujimotos/polyleven>), [editdistance](<https://github.com/roy-ht/editdistance>), and [edlib](<https://github.com/Martinsos/edlib>). Each plot shows the total time for 1,000 calls (lower is better).

> [!NOTE]
>
> Plots were generated by GitHub Actions on Ubuntu 24.04 with Python 3.13. Reproduce them locally with [`uv run scripts/benchmark.py`](<https://github.com/KarelZe/lev/blob/main/scripts/benchmark.py>).

### ASCII

![ASCII benchmark – light](<https://KarelZe.github.io/lev/assets/benchmark_ascii_light.svg#only-light>) ![ASCII benchmark – dark](<https://KarelZe.github.io/lev/assets/benchmark_ascii_dark.svg#only-dark>)

### Latin-1

![Latin-1 benchmark – light](<https://KarelZe.github.io/lev/assets/benchmark_latin_1_light.svg#only-light>) ![Latin-1 benchmark – dark](<https://KarelZe.github.io/lev/assets/benchmark_latin_1_dark.svg#only-dark>)

### CJK

![CJK benchmark – light](<https://KarelZe.github.io/lev/assets/benchmark_cjk_light.svg#only-light>) ![CJK benchmark – dark](<https://KarelZe.github.io/lev/assets/benchmark_cjk_dark.svg#only-dark>)

### Emoji

![Emoji benchmark – light](<https://KarelZe.github.io/lev/assets/benchmark_emoji_light.svg#only-light>) ![Emoji benchmark – dark](<https://KarelZe.github.io/lev/assets/benchmark_emoji_dark.svg#only-dark>)

### Realistic Text

A short customer support message with a few natural typos.

![Realistic text benchmark – light](<https://KarelZe.github.io/lev/assets/benchmark_realistic_light.svg#only-light>) ![Realistic text benchmark – dark](<https://KarelZe.github.io/lev/assets/benchmark_realistic_dark.svg#only-dark>)

### Benchmark Scripts

Reproduce benchmarks locally using the provided scripts:

```
# Compare against other libraries:
uv run scripts/benchmark.py

# Benchmark scaling across string lengths:
uv run scripts/benchmark_by_length.py

# Benchmark speedup across released versions:
uv run scripts/benchmark_by_version.py
```
