2 unstable releases

0.2.2 Dec 7, 2023
0.1.0 Nov 29, 2020

#738 in Text processing

36 downloads per month

MIT license

16KB
249 lines

group-similar

Crate allowing values to be grouped based on Jaro-Winkler distance.

License

Copyright 2020 Josh Clayton. See the LICENSE.


lib.rs:

This crate enables grouping values based on string similarity via Jaro-Winkler distance and complete-linkage clustering.

Example: Identify likely repeated merchants based on merchant name

use group_similar::{Config, Threshold, group_similar};

#[derive(Eq, PartialEq, std::hash::Hash, Debug, Ord, PartialOrd)]
struct Merchant {
  id: usize,
  name: String
}


impl std::fmt::Display for Merchant {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.name)
    }
}

impl AsRef<str> for Merchant {
    fn as_ref(&self) -> &str {
        &self.name
    }
}

let merchants = vec![
    Merchant {
        id: 1,
        name: "McDonalds 105109".to_string()
    },
    Merchant {
        id: 2,
        name: "McDonalds 105110".to_string()
    },
    Merchant {
        id: 3,
        name: "Target ID1244".to_string()
    },
    Merchant {
        id: 4,
        name: "Target ID125".to_string()
    },
    Merchant {
        id: 5,
        name: "Amazon.com TID120159120".to_string()
    },
    Merchant {
        id: 6,
        name: "Target".to_string()
    },
    Merchant {
        id: 7,
        name: "Target.com".to_string()
    },
];

let config = Config::jaro_winkler(Threshold::default());
let results = group_similar(&merchants, &config);

assert_eq!(results.get(&merchants[0]), Some(&vec![&merchants[1]]));
assert_eq!(results.get(&merchants[2]), Some(&vec![&merchants[3], &merchants[5], &merchants[6]]));
assert_eq!(results.get(&merchants[4]), Some(&vec![]));

Dependencies

~5–15MB
~161K SLoC