#fuzzy #match #search #text

bin+lib sublime_fuzzy

Fuzzy matching algorithm based on Sublime Text’s string search

7 releases (breaking)

Uses old Rust 2015

0.7.0 Dec 19, 2020
0.6.0 Dec 7, 2018
0.5.0 Mar 24, 2018
0.4.1 Aug 17, 2017
0.2.0 Jul 25, 2017

#210 in Text processing

Download history 2262/week @ 2021-09-28 2198/week @ 2021-10-05 2891/week @ 2021-10-12 2838/week @ 2021-10-19 1827/week @ 2021-10-26 1627/week @ 2021-11-02 534/week @ 2021-11-09 612/week @ 2021-11-16 477/week @ 2021-11-23 517/week @ 2021-11-30 858/week @ 2021-12-07 996/week @ 2021-12-14 537/week @ 2021-12-21 406/week @ 2021-12-28 1495/week @ 2022-01-04 1870/week @ 2022-01-11

4,356 downloads per month
Used in less than 8 crates

Custom license

36KB
787 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 that also match case (T -> T instead of t -> T) in case of a case-insensitive search get a bonus (bonus_match_case)
  • The distance between two matches will be multiplied with the penalty_distance penalty and subtracted from the score

The default scoring is configured 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 result = best_match("something", "some search thing");

assert!(result.is_some());

Match::continuous_matches returns an iter of consecutive matches. Based on those the input string can be formatted.

format_simple provides a simple formatting that wraps matches in tags:

use sublime_fuzzy::{best_match, format_simple};

let target = "some search thing";

let result = best_match("something", target).unwrap();

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

The weighting of the different factors can be adjusted:

use sublime_fuzzy::{FuzzySearch, Scoring};

// Or pick from one of the provided `Scoring::...` methods like `emphasize_word_starts`
let scoring = Scoring {
    bonus_consecutive: 128,
    bonus_word_start: 0,
    ..Scoring::default()
};

let result = FuzzySearch::new("something", "some search thing")
    .case_sensitive()
    .score_with(&scoring)
    .best_match();

assert!(result.is_some())

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

Documentation

Check out the documentation at docs.rs.

Dependencies

~175KB