| 1 | # strsim-rs |
| 2 | |
| 3 | [](https://crates.io/crates/strsim) |
| 4 | [](https://github.com/rapidfuzz/strsim-rs/blob/main/LICENSE) |
| 5 | [](https://github.com/rapidfuzz/strsim-rs/actions?query=branch%3Amain) |
| 6 | [](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] |
| 14 | - [Sørensen-Dice] |
| 15 | |
| 16 | The normalized versions return values between `0.0` and `1.0`, where `1.0` means |
| 17 | an exact match. |
| 18 | |
| 19 | There 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 |
| 24 | your project: |
| 25 | |
| 26 | ```sh |
| 27 | cargo add strsim |
| 28 | ``` |
| 29 | |
| 30 | ## Usage |
| 31 | |
| 32 | Go to [Docs.rs](https://docs.rs/strsim/) for the full documentation. You can |
| 33 | also clone the repo, and run `$ cargo doc --open`. |
| 34 | |
| 35 | ### Examples |
| 36 | |
| 37 | ```rust |
| 38 | extern crate strsim; |
| 39 | |
| 40 | use strsim::{hamming, levenshtein, normalized_levenshtein, osa_distance, |
| 41 | damerau_levenshtein, normalized_damerau_levenshtein, jaro, |
| 42 | jaro_winkler, sorensen_dice}; |
| 43 | |
| 44 | fn 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 | |
| 72 | Using the generic versions of the functions: |
| 73 | |
| 74 | ```rust |
| 75 | extern crate strsim; |
| 76 | |
| 77 | use strsim::generic_levenshtein; |
| 78 | |
| 79 | fn main() { |
| 80 | assert_eq!(2, generic_levenshtein(&[1, 2, 3], &[0, 2, 5])); |
| 81 | } |
| 82 | ``` |
| 83 | |
| 84 | ## Contributing |
| 85 | |
| 86 | If you don't want to install Rust itself, you can run `$ ./dev` for a |
| 87 | development CLI if you have [Docker] installed. |
| 88 | |
| 89 | Benchmarks require a Nightly toolchain. Run `$ cargo +nightly bench`. |
| 90 | |
| 91 | ## License |
| 92 | |
| 93 | [MIT](https://github.com/rapidfuzz/strsim-rs/blob/main/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/ |