9 releases

Uses old Rust 2015

0.4.0 May 2, 2022
0.3.5 Feb 4, 2022
0.3.4 Dec 28, 2021
0.3.2 Aug 18, 2020
0.2.1 Nov 6, 2017

#539 in Text processing

Download history 44/week @ 2023-11-20 21/week @ 2023-11-27 17/week @ 2023-12-04 16/week @ 2023-12-11 21/week @ 2023-12-18 12/week @ 2023-12-25 40/week @ 2024-01-01 37/week @ 2024-01-08 18/week @ 2024-01-15 27/week @ 2024-01-22 13/week @ 2024-01-29 33/week @ 2024-02-05 36/week @ 2024-02-12 49/week @ 2024-02-19 74/week @ 2024-02-26 44/week @ 2024-03-04

206 downloads per month
Used in 5 crates

MIT license

38KB
583 lines

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.

Documentation

https://docs.rs/ngrammatic/latest/ngrammatic/

Installation

This crate is published on crates.io.

To use it, add this to your Cargo.toml:

[dependencies]
ngrammatic = "0.3.4"

Usage

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);
}

No runtime deps