Expand description
String distance and similarity metrics for Rust.
String distance and similarity — seven public metrics across four algorithms.
use verbora_distance::{levenshtein, jaro_winkler, dice_coefficient, hamming};
assert_eq!(levenshtein("kitten", "sitting", &Default::default()), 3.0);
assert_eq!(dice_coefficient("abc", "abc"), 1.0);
assert_eq!(hamming("karolin", "kathrin", false), 3);
assert_eq!(jaro_winkler("abc", "abc", &Default::default()), 1.0);§Conventions differ between metrics
The metrics deliberately do not share a single direction convention, and this crate does not “fix” that, since doing so would change every caller’s results:
| Metric | Range | Direction |
|---|---|---|
levenshtein, damerau_levenshtein | 0.. | distance — lower is closer |
hamming | -1, 0.. | distance — lower is closer; -1 means incomparable |
jaro, jaro_winkler | 0..=1 | similarity — higher is closer |
dice_coefficient | 0..=1, or NaN | similarity — higher is closer |
The verbora_core::StringMetric implementations below record which
direction each one uses, so generic code can adapt without any metric
changing its output.
§Unicode
Every metric here indexes text by UTF-16 code unit —
because that is observable in the results. See units for the mechanism
and for the ASCII fast path that keeps it free on ordinary input.
§Batch computation (feature = parallel)
Every metric above is a pure, stateless free function, so scoring many
independent pairs is embarrassingly parallel with zero coordination cost.
With the parallel feature enabled, [par_levenshtein_batch],
[par_damerau_levenshtein_batch], [par_jaro_winkler_batch],
[par_dice_coefficient_batch] and [par_hamming_batch] fan a batch of
pairs out across a rayon thread pool. Each is exactly
pairs.par_iter().map(<the sequential function>).collect() — see the
individual function docs for cost trade-offs and when a plain sequential
loop is the better choice (usually: for small batches or short strings).
Re-exports§
pub use dice::dice_coefficient;pub use hamming::INCOMPARABLE;pub use hamming::hamming;pub use hamming::hamming_checked;pub use jaro_winkler::jaro;pub use jaro_winkler::jaro_winkler;pub use levenshtein::SearchResult;pub use levenshtein::damerau_levenshtein;pub use levenshtein::damerau_levenshtein_search;pub use levenshtein::levenshtein;pub use levenshtein::levenshtein_search;
Modules§
- dice
- Sørensen–Dice coefficient, ported from
The reference
dice_coefficient. - hamming
- Hamming distance, ported from the reference
hamming_distance. - jaro_
winkler - Jaro and Jaro–Winkler similarity, ported from
The reference
jaro-winkler_distance. - levenshtein
- Levenshtein and Damerau–Levenshtein distance, ported from
The reference
levenshtein_distance. - units
- Text-unit dispatch: the mechanism that buys exact the reference parity without paying for it on ordinary input.
Structs§
- Damerau
Levenshtein - Damerau–Levenshtein distance as a
StringMetric. - Dice
- Sørensen–Dice coefficient as a
StringMetric. - Hamming
- Hamming distance as a
StringMetric. - Jaro
Winkler - Jaro–Winkler similarity as a
StringMetric. - Levenshtein
- Levenshtein distance as a
StringMetric.