1# strsim-rs
2
3[![Crates.io](https://img.shields.io/crates/v/strsim.svg)](https://crates.io/crates/strsim)
4[![Crates.io](https://img.shields.io/crates/l/strsim.svg?maxAge=2592000)](https://github.com/dguo/strsim-rs/blob/master/LICENSE)
5[![CI status](https://github.com/dguo/strsim-rs/workflows/CI/badge.svg)](https://github.com/dguo/strsim-rs/actions?query=branch%3Amaster)
6[![unsafe forbidden](https://img.shields.io/badge/unsafe-forbidden-success.svg)](https://github.com/rust-secure-code/safety-dance/)
7
8[Rust](https://www.rust-lang.org) implementations of [string similarity metrics]:
9  - [Hamming]
10  - [Levenshtein] - distance & normalized
11  - [Optimal string alignment]
12  - [Damerau-Levenshtein] - distance & normalized
13  - [Jaro and Jaro-Winkler] - this implementation of Jaro-Winkler does not limit the common prefix length
14  - [Sørensen-Dice]
15
16The normalized versions return values between `0.0` and `1.0`, where `1.0` means
17an exact match.
18
19There are also generic versions of the functions for non-string inputs.
20
21## Installation
22
23`strsim` is available on [crates.io](https://crates.io/crates/strsim). Add it to
24your `Cargo.toml`:
25```toml
26[dependencies]
27strsim = "0.10.0"
28```
29
30## Usage
31
32Go to [Docs.rs](https://docs.rs/strsim/) for the full documentation. You can
33also clone the repo, and run `$ cargo doc --open`.
34
35### Examples
36
37```rust
38extern crate strsim;
39
40use strsim::{hamming, levenshtein, normalized_levenshtein, osa_distance,
41             damerau_levenshtein, normalized_damerau_levenshtein, jaro,
42             jaro_winkler, sorensen_dice};
43
44fn main() {
45    match hamming("hamming", "hammers") {
46        Ok(distance) => assert_eq!(3, distance),
47        Err(why) => panic!("{:?}", why)
48    }
49
50    assert_eq!(levenshtein("kitten", "sitting"), 3);
51
52    assert!((normalized_levenshtein("kitten", "sitting") - 0.571).abs() < 0.001);
53
54    assert_eq!(osa_distance("ac", "cba"), 3);
55
56    assert_eq!(damerau_levenshtein("ac", "cba"), 2);
57
58    assert!((normalized_damerau_levenshtein("levenshtein", "löwenbräu") - 0.272).abs() <
59            0.001);
60
61    assert!((jaro("Friedrich Nietzsche", "Jean-Paul Sartre") - 0.392).abs() <
62            0.001);
63
64    assert!((jaro_winkler("cheeseburger", "cheese fries") - 0.911).abs() <
65            0.001);
66
67    assert_eq!(sorensen_dice("web applications", "applications of the web"),
68        0.7878787878787878);
69}
70```
71
72Using the generic versions of the functions:
73
74```rust
75extern crate strsim;
76
77use strsim::generic_levenshtein;
78
79fn main() {
80    assert_eq!(2, generic_levenshtein(&[1, 2, 3], &[0, 2, 5]));
81}
82```
83
84## Contributing
85
86If you don't want to install Rust itself, you can run `$ ./dev` for a
87development CLI if you have [Docker] installed.
88
89Benchmarks require a Nightly toolchain. Run `$ cargo +nightly bench`.
90
91## License
92
93[MIT](https://github.com/dguo/strsim-rs/blob/master/LICENSE)
94
95[string similarity metrics]:http://en.wikipedia.org/wiki/String_metric
96[Damerau-Levenshtein]:http://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance
97[Jaro and Jaro-Winkler]:http://en.wikipedia.org/wiki/Jaro%E2%80%93Winkler_distance
98[Levenshtein]:http://en.wikipedia.org/wiki/Levenshtein_distance
99[Hamming]:http://en.wikipedia.org/wiki/Hamming_distance
100[Optimal string alignment]:https://en.wikipedia.org/wiki/Damerau%E2%80%93Levenshtein_distance#Optimal_string_alignment_distance
101[Sørensen-Dice]:http://en.wikipedia.org/wiki/S%C3%B8rensen%E2%80%93Dice_coefficient
102[Docker]:https://docs.docker.com/engine/installation/
103