2 releases

0.1.1 Jul 14, 2021
0.1.0 Jul 14, 2021

#1713 in Algorithms

Download history 434/week @ 2024-06-17 474/week @ 2024-06-24 293/week @ 2024-07-01 652/week @ 2024-07-08 412/week @ 2024-07-15 497/week @ 2024-07-22 560/week @ 2024-07-29 770/week @ 2024-08-05 703/week @ 2024-08-12 520/week @ 2024-08-19 489/week @ 2024-08-26 466/week @ 2024-09-02 479/week @ 2024-09-09 425/week @ 2024-09-16 497/week @ 2024-09-23 421/week @ 2024-09-30

1,856 downloads per month
Used in 14 crates (12 directly)

MIT/Apache

1MB
234 lines

Contains (WOFF font, 190KB) target/doc/FiraSans-Medium.woff, (WOFF font, 135KB) target/doc/FiraSans-Medium.woff2, (WOFF font, 185KB) target/doc/FiraSans-Regular.woff, (WOFF font, 130KB) target/doc/FiraSans-Regular.woff2, (WOFF font, 69KB) SourceCodePro-Regular.ttf.woff, (WOFF font, 69KB) SourceCodePro-Semibold.ttf.woff and 4 more.

Rust Fuzzy Search | Documentation

Fuzzy search with trigrams implemented in Rust

This crate implements Fuzzy Searching with trigrams

Fuzzy searching allows to compare strings by similarity rather than by equality:
Similar strings will get a high score (close to 1.0f32) while dissimilar strings will get a lower score (closer to 0.0f32).

Fuzzy searching tolerates changes in word order:
ex. "John Dep" and "Dep John" will get a high score.

The Algorithm used is taken from : https://dev.to/kaleman15/fuzzy-searching-with-postgresql-97o

Basic idea:

  1. From both strings extracts all groups of 3 adjacent letters.
    ("House" becomes [' H', ' Ho', 'Hou', 'ous', 'use', 'se ']).
    Note the 2 spaces added to the head of the string and the one on the tail, used to make the algorithm work on zero length words.

  2. Then counts the number of trigrams of the first words that are also present on the second word and divide by the number of trigrams of the first word.

Example: Comparing 2 strings

fn test () {
   use rust_fuzzy_search::fuzzy_compare;
   let score : f32 = fuzzy_compare("kolbasobulko", "kolbasobulko");
   println!("score = {:?}", score);
}

Example: Comparing a string with a list of strings and retrieving only the best matches

 fn test() {
     use rust_fuzzy_search::fuzzy_search_best_n;
     let s = "bulko";
     let list : Vec<&str> = vec![
         "kolbasobulko",
         "sandviĉo",
         "ŝatas",
         "domo",
         "emuo",
         "fabo",
         "fazano"
     ];
     let n : usize = 3;
     let res : Vec<(&str, f32)> = fuzzy_search_best_n(s,&list, n);
     for (_word, score) in res {
         println!("{:?}",score)
     }
 }

Example: if you have a Vec of Strings you need to convert it to a list of &str

fn works_with_strings() {
    use rust_fuzzy_search::fuzzy_search;
    let s = String::from("varma");
    let list: Vec<String> = vec![String::from("varma vetero"), String::from("varma ĉokolado")];
    fuzzy_search(&s, &list.iter().map(String::as_ref).collect::<Vec<&str>>());
}

The crate exposes 5 main functions:

Usage

To use this crate, first add this to your Cargo.toml:

[dependencies]
rust-fuzzy-search = { git = "https://gitlab.com/EnricoCh/rust-fuzzy-search"}

Next, add this to your crate:

extern crate rust_fuzzy_search;

use rust_fuzzy_search::*;

fn main() {
// ...
}

Building

To build the library use cargo build

Testing

To test the library use cargo test

License

Licensed under either of

MIT License Apache 2.0 License

Contribution

Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

No runtime deps