| 1 | # Rust Fuzzy Search | [Documentation](https://enricoch.gitlab.io/rust-fuzzy-search/rust_fuzzy_search/index.html) |
| 2 | |
| 3 | [<img src="https://img.shields.io/badge/Repository-GitLab-success?style=plastic&logo=gitlab">](https://gitlab.com/EnricoCh/rust-fuzzy-search) |
| 4 | [<img src="https://img.shields.io/badge/Documentation-Doc-success?style=plastic">](https://enricoch.gitlab.io/rust-fuzzy-search/rust_fuzzy_search/index.html) |
| 5 | [<img src="https://img.shields.io/gitlab/pipeline/EnricoCh/rust-fuzzy-search/main?style=plastic">](https://gitlab.com/EnricoCh/rust-fuzzy-search/badges/master/pipeline.svg) |
| 6 | [<img src="https://img.shields.io/badge/Language-Rust-success?style=plastic&logo=rust">](https://www.rust-lang.org/) |
| 7 | [<img src="https://badgen.net/crates/d/rust-fuzzy-search">](https://gitlab.com/EnricoCh/rust-fuzzy-search) |
| 8 | ## Fuzzy search with trigrams implemented in Rust |
| 9 | |
| 10 | |
| 11 | |
| 12 | This crate implements Fuzzy Searching with trigrams |
| 13 | |
| 14 | |
| 15 | Fuzzy searching allows to compare strings by similarity rather than by equality:\ |
| 16 | Similar strings will get a high score (close to `1.0f32`) while dissimilar strings will get a lower score (closer to `0.0f32`). |
| 17 | |
| 18 | Fuzzy searching tolerates changes in word order:\ |
| 19 | ex. `"John Dep"` and `"Dep John"` will get a high score. |
| 20 | |
| 21 | The Algorithm used is taken from : <https://dev.to/kaleman15/fuzzy-searching-with-postgresql-97o> |
| 22 | |
| 23 | Basic idea: |
| 24 | |
| 25 | 1. From both strings extracts all groups of 3 adjacent letters.\ |
| 26 | (`"House"` becomes `[' H', ' Ho', 'Hou', 'ous', 'use', 'se ']`).\ |
| 27 | 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. |
| 28 | |
| 29 | 1. 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. |
| 30 | |
| 31 | |
| 32 | Example: Comparing 2 strings |
| 33 | ```rust |
| 34 | fn test () { |
| 35 | use rust_fuzzy_search::fuzzy_compare; |
| 36 | let score : f32 = fuzzy_compare("kolbasobulko", "kolbasobulko"); |
| 37 | println!("score = {:?}", score); |
| 38 | } |
| 39 | ``` |
| 40 | |
| 41 | Example: Comparing a string with a list of strings and retrieving only the best matches |
| 42 | |
| 43 | ```rust |
| 44 | fn test() { |
| 45 | use rust_fuzzy_search::fuzzy_search_best_n; |
| 46 | let s = "bulko"; |
| 47 | let list : Vec<&str> = vec![ |
| 48 | "kolbasobulko", |
| 49 | "sandviĉo", |
| 50 | "ŝatas", |
| 51 | "domo", |
| 52 | "emuo", |
| 53 | "fabo", |
| 54 | "fazano" |
| 55 | ]; |
| 56 | let n : usize = 3; |
| 57 | let res : Vec<(&str, f32)> = fuzzy_search_best_n(s,&list, n); |
| 58 | for (_word, score) in res { |
| 59 | println!("{:?}",score) |
| 60 | } |
| 61 | } |
| 62 | ``` |
| 63 | Example: if you have a `Vec` of `String`s you need to convert it to a list of `&str` |
| 64 | ```rust |
| 65 | fn works_with_strings() { |
| 66 | use rust_fuzzy_search::fuzzy_search; |
| 67 | let s = String::from("varma"); |
| 68 | let list: Vec<String> = vec![String::from("varma vetero"), String::from("varma ĉokolado")]; |
| 69 | fuzzy_search(&s, &list.iter().map(String::as_ref).collect::<Vec<&str>>()); |
| 70 | } |
| 71 | ``` |
| 72 | |
| 73 | |
| 74 | The crate exposes 5 main functions: |
| 75 | - [fuzzy_compare](https://enricoch.gitlab.io/rust-fuzzy-search/rust_fuzzy_search/fn.fuzzy_compare.html) will take 2 strings and return a score representing how similar those strings are. |
| 76 | - [fuzzy_search](https://enricoch.gitlab.io/rust-fuzzy-search/rust_fuzzy_search/fn.fuzzy_search.html) applies [fuzzy_compare](https://enricoch.gitlab.io/rust-fuzzy-search/rust_fuzzy_search/fn.fuzzy_compare.html) to a list of strings and returns a list of tuples: (word, score). |
| 77 | - [fuzzy_search_sorted](https://enricoch.gitlab.io/rust-fuzzy-search/rust_fuzzy_search/fn.fuzzy_search_sorted.html) is similar to [fuzzy_search](https://enricoch.gitlab.io/rust-fuzzy-search/rust_fuzzy_search/fn.fuzzy_search.html) but orders the output in descending order. |
| 78 | - [fuzzy_search_threshold](https://enricoch.gitlab.io/rust-fuzzy-search/rust_fuzzy_search/fn.fuzzy_search_threshold.html) will take an additional `f32` as input and returns only tuples with score greater than the threshold. |
| 79 | - [fuzzy_search_best_n](https://enricoch.gitlab.io/rust-fuzzy-search/rust_fuzzy_search/fn.fuzzy_search_best_n.html) will take an additional `usize` arguments and returns the first `n` tuples. |
| 80 | |
| 81 | |
| 82 | ## Usage |
| 83 | |
| 84 | To use this crate, first add this to your Cargo.toml: |
| 85 | |
| 86 | ```toml |
| 87 | [dependencies] |
| 88 | rust-fuzzy-search = { git = "https://gitlab.com/EnricoCh/rust-fuzzy-search"} |
| 89 | ``` |
| 90 | Next, add this to your crate: |
| 91 | |
| 92 | ```rust |
| 93 | extern crate rust_fuzzy_search; |
| 94 | |
| 95 | use rust_fuzzy_search::*; |
| 96 | |
| 97 | fn main() { |
| 98 | // ... |
| 99 | } |
| 100 | ``` |
| 101 | |
| 102 | |
| 103 | ## Building |
| 104 | |
| 105 | To build the library use `cargo build` |
| 106 | |
| 107 | |
| 108 | ## Testing |
| 109 | |
| 110 | To test the library use `cargo test` |
| 111 | |
| 112 | |
| 113 | ## License |
| 114 | |
| 115 | Licensed under either of |
| 116 | |
| 117 | - Apache License, Version 2.0 ([LICENSE-APACHE](LICENSE-APACHE) or http://apache.org/licenses/LICENSE-2.0) |
| 118 | - MIT license ([LICENSE-MIT](LICENSE-MIT) or http://opensource.org/licenses/MIT) |
| 119 | |
| 120 |  |
| 121 |  |
| 122 | |
| 123 | # Contribution |
| 124 | |
| 125 | Unless you explicitly state otherwise, any contribution intentionally submitted |
| 126 | for inclusion in the work by you, as defined in the Apache-2.0 license, shall be |
| 127 | dual licensed as above, without any additional terms or conditions. |