String distance and similarity metrics for Rust.
String distance and similarity — seven public metrics across four algorithms.
use ;
assert_eq!;
assert_eq!;
assert_eq!;
assert_eq!;
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 |
|---|---|---|
[fn@levenshtein], [fn@damerau_levenshtein] |
0.. |
distance — lower is closer |
[fn@hamming] |
-1, 0.. |
distance — lower is closer; -1 means incomparable |
[fn@jaro], [fn@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).