Crates.io | ngrammatic |
lib.rs | ngrammatic |
version | 0.4.0 |
source | src |
created_at | 2017-11-06 05:17:24.374647 |
updated_at | 2022-05-02 18:42:46.217004 |
description | Character-oriented ngram generator and fuzzy matching library. |
homepage | https://github.com/compenguy/ngrammatic |
repository | https://github.com/compenguy/ngrammatic |
max_upload_size | |
id | 38311 |
size | 38,699 |
This crate provides fuzzy search/string matching using N-grams.
This implementation is character-based, rather than word based, matching solely based on string similarity.
Licensed under the MIT license.
https://docs.rs/ngrammatic/latest/ngrammatic/
This crate is published on crates.io.
To use it, add this to your Cargo.toml:
[dependencies]
ngrammatic = "0.3.4"
To do fuzzy matching, build up your corpus of valid symbols like this:
use ngrammatic::{CorpusBuilder, Pad};
let mut corpus = CorpusBuilder::new()
.arity(2)
.pad_full(Pad::Auto)
.finish();
// Build up the list of known words
corpus.add_text("pie");
corpus.add_text("animal");
corpus.add_text("tomato");
corpus.add_text("seven");
corpus.add_text("carbon");
// Now we can try an unknown/misspelled word, and find a similar match
// in the corpus
let word = String::from("tomacco");
if let Some(top_result) = corpus.search(word, 0.25).first() {
if top_result.similarity > 0.99 {
println!("✔ {}", top_result.text);
} else {
println!("❓{} (did you mean {}? [{:.0}% match])",
word,
top_result.text,
top_result.similarity * 100.0);
}
} else {
println!("🗙 {}", word);
}