11 unstable releases (3 breaking)

0.4.4 Oct 6, 2019
0.4.3 Oct 6, 2019
0.3.0 Oct 6, 2019
0.2.4 Oct 6, 2019
0.1.0 Oct 5, 2019

#967 in Text processing

Download history 1573/week @ 2024-03-14 1314/week @ 2024-03-21 1267/week @ 2024-03-28 1484/week @ 2024-04-04 1769/week @ 2024-04-11 1655/week @ 2024-04-18 1602/week @ 2024-04-25 685/week @ 2024-05-02 1112/week @ 2024-05-09 1448/week @ 2024-05-16 848/week @ 2024-05-23 1072/week @ 2024-05-30 1109/week @ 2024-06-06 1365/week @ 2024-06-13 1434/week @ 2024-06-20 1340/week @ 2024-06-27

5,483 downloads per month
Used in 7 crates (5 directly)

Apache-2.0

13KB
216 lines

trigram

Build Status License Documentation

This Rust crate contains functions for fuzzy string matching.

It exports two functions. The similarity function returns the similarity of two strings, and the find_words_iter function returns an iterator of matches for a smaller string (needle) in a larger string (haystack).

The similarity of strings is computed based on their trigrams, meaning their 3-character substrings: https://en.wikipedia.org/wiki/Trigram.

Trying it out

Here is how to run the examples:

$ cargo run --example similarity color colour
...
0.44444445

$ cargo run --example find_words_iter
bufalo
buffalow
Bungalo
biffalo
buffaloo
huffalo
snuffalo
fluffalo

Usage

Add this to your Cargo.toml:

[dependencies]
trigram = "0.2.2"

and call it like this:

use trigram::similarity;

fn main() {
	println!("{}", similarity(&"rustacean", &"crustacean"));
}

Background

The similarity function in this crate is a reverse-engineered approximation of the similarity function in the Postgresql pg_trgm extension: https://www.postgresql.org/docs/9.1/pgtrgm.html. It gives exactly the same answers in many cases, but may disagree in others (none known). If you find a case where the answers don't match, please file an issue about it!

A good introduction to the Postgres version of this is given on Stack Overflow: https://stackoverflow.com/a/43161051/484529.

Dependencies

~2.2–3MB
~54K SLoC