#levenshtein #string-similarity #hamming #similarity #string #jaro

strsim

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

24 releases

Uses old Rust 2015

0.11.1 Apr 2, 2024
0.11.0 Jan 7, 2024
0.10.1 Jan 7, 2024
0.10.0 Jan 31, 2020
0.2.2 Mar 29, 2015

#5 in Algorithms

Download history 1708399/week @ 2024-01-12 1819553/week @ 2024-01-19 1900790/week @ 2024-01-26 1874765/week @ 2024-02-02 2109698/week @ 2024-02-09 2098742/week @ 2024-02-16 2266487/week @ 2024-02-23 2342059/week @ 2024-03-01 2249936/week @ 2024-03-08 2311486/week @ 2024-03-15 2363309/week @ 2024-03-22 2352547/week @ 2024-03-29 2509504/week @ 2024-04-05 2562977/week @ 2024-04-12 2595838/week @ 2024-04-19 2026851/week @ 2024-04-26

10,164,199 downloads per month
Used in 1,635 crates (491 directly)

MIT license

43KB
965 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 project:

cargo add strsim

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