#string #similarity #Hamming #Levenshtein #Jaro

strsim

Implementations of string similarity metrics. Includes Hamming, Levenshtein, OSA, Damerau-Levenshtein, Jaro, Jaro-Winkler, and Sørensen-Dice.

22 releases

0.10.0 Jan 31, 2020
0.9.3 Dec 13, 2019
0.9.2 May 9, 2019
0.8.0 Aug 19, 2018
0.2.2 Mar 29, 2015

#5 in Text processing

Download history 132084/week @ 2019-12-15 82194/week @ 2019-12-22 96750/week @ 2019-12-29 143819/week @ 2020-01-05 151431/week @ 2020-01-12 155505/week @ 2020-01-19 151104/week @ 2020-01-26 157152/week @ 2020-02-02 147835/week @ 2020-02-09 141450/week @ 2020-02-16 148038/week @ 2020-02-23 151689/week @ 2020-03-01 151250/week @ 2020-03-08 148228/week @ 2020-03-15 151875/week @ 2020-03-22 149641/week @ 2020-03-29

560,800 downloads per month
Used in 849 crates (257 directly)

MIT license

34KB
712 lines

strsim-rs

Crates.io Crates.io CI status unsafe forbidden

Rust implementations of string similarity metrics:

The normalized versions return values between 0.0 and 1.0, where 1.0 means an exact match.

There are also generic versions of the functions for non-string inputs.

Installation

strsim is available on crates.io. Add it to your Cargo.toml:

[dependencies]
strsim = "0.10.0"

Usage

Go to Docs.rs for the full documentation. You can also clone the repo, and run $ cargo doc --open.

Examples

extern crate strsim;

use strsim::{hamming, levenshtein, normalized_levenshtein, osa_distance,
             damerau_levenshtein, normalized_damerau_levenshtein, jaro,
             jaro_winkler, sorensen_dice};

fn main() {
    match hamming("hamming", "hammers") {
        Ok(distance) => assert_eq!(3, distance),
        Err(why) => panic!("{:?}", why)
    }

    assert_eq!(levenshtein("kitten", "sitting"), 3);

    assert!((normalized_levenshtein("kitten", "sitting") - 0.571).abs() < 0.001);

    assert_eq!(osa_distance("ac", "cba"), 3);

    assert_eq!(damerau_levenshtein("ac", "cba"), 2);

    assert!((normalized_damerau_levenshtein("levenshtein", "löwenbräu") - 0.272).abs() <
            0.001);

    assert!((jaro("Friedrich Nietzsche", "Jean-Paul Sartre") - 0.392).abs() <
            0.001);

    assert!((jaro_winkler("cheeseburger", "cheese fries") - 0.911).abs() <
            0.001);

    assert_eq!(sorensen_dice("web applications", "applications of the web"),
        0.7878787878787878);
}

Using the generic versions of the functions:

extern crate strsim;

use strsim::generic_levenshtein;

fn main() {
    assert_eq!(2, generic_levenshtein(&[1, 2, 3], &[0, 2, 5]));
}

Contributing

If you don't want to install Rust itself, you can run $ ./dev for a development CLI if you have Docker installed.

Benchmarks require a Nightly toolchain. Run $ cargo +nightly bench.

License

MIT

No runtime deps