#fuzzy #match #search #text

sublime_fuzzy

Fuzzy matching algorithm based on Sublime Text’s string search. Check out the repository’s releases for a summarized changelog.

6 releases (breaking)

0.6.0 Dec 7, 2018
0.5.0 Mar 24, 2018
0.4.1 Aug 17, 2017
0.4.0 Jul 30, 2017
0.2.0 Jul 25, 2017

#71 in Text processing

Download history 8/week @ 2019-04-23 296/week @ 2019-04-30 258/week @ 2019-05-07 300/week @ 2019-05-14 225/week @ 2019-05-21 247/week @ 2019-05-28 441/week @ 2019-06-04 245/week @ 2019-06-11 246/week @ 2019-06-18 447/week @ 2019-06-25 296/week @ 2019-07-02 318/week @ 2019-07-09 264/week @ 2019-07-16 284/week @ 2019-07-23 175/week @ 2019-07-30

531 downloads per month
Used in 5 crates

Custom license

28KB
457 lines

sublime_fuzzy sublime_fuzzy on crates.io

Fuzzy matching algorithm based on Sublime Text's string search. Iterates through characters of a search string and calculates a score.

The score is based on several factors:

  • Word starts like the t in some_thing get a bonus (bonus_word_start)
  • Consecutive matches get an accumulative bonus for every consecutive match (bonus_consecutive)
  • Matches with higher coverage (targets some_release (lower) versus a_release (higher) with pattern release) will get a bonus multiplied by the coverage percentage (bonus_coverage)
  • The distance between two matches will be multiplied with the penalty_distance penalty and subtracted from the score

The default bonus/penalty values are set to give a lot of weight to word starts. So a pattern scc will match SoccerCartoonController, not SoccerCartoonController.

Match Examples

With default weighting.

Pattern Target string Result
scc SoccerCartoonController SoccerCartoonController
something some search thing some search thing

Usage

Basic usage:

use sublime_fuzzy::best_match;

let s = "some search thing";
let search = "something";
let result = best_match(search, s).unwrap();

println!("score: {:?}", result.score());

Match.continuous_matches() returns a list of consecutive matches ((start_index, length)). Based on those the input string can be formatted.

sublime_fuzzy provides a simple formatting function that wraps matches in tags:

use sublime_fuzzy::{best_match, format_simple};

let s = "some search thing";
let search = "something";
let result = best_match(search, s).unwrap();

assert_eq!(
    format_simple(&result, s, "<span>", "</span>"),
    "<span>some</span> search <span>thing</span>"
);

The weighting of the different factors can be adjusted:

use sublime_fuzzy::{FuzzySearch, ScoreConfig};

let case_insensitive = true;

let mut search = FuzzySearch::new("something", "some search thing", case_insensitive);

let config = ScoreConfig {
    bonus_consecutive: 12,
    bonus_word_start: 64,
    bonus_coverage: 64,
    penalty_distance: 4,
};

search.set_score_config(config);

println!("result: {:?}", search.best_match());

Note: Any whitespace in the pattern ('something' in the examples above) will be removed.

Documentation

Check out the documentation at docs.rs.

Dependencies

~215KB