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.
With default weighting.
| Pattern | Target string | Result
| --- | --- | ---
| scc
| SoccerCartoonController
| SoccerCartoonController
| something
| some search thing
| some search thing
Basic usage:
```rust use sublimefuzzy::bestmatch;
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:
```rust use sublimefuzzy::{bestmatch, format_simple};
let s = "some search thing"; let search = "something"; let result = best_match(search, s).unwrap();
asserteq!( formatsimple(&result, s, "", ""), "some search thing" ); ```
The weighting of the different factors can be adjusted:
```rust use sublime_fuzzy::{FuzzySearch, ScoreConfig};
let case_insensitive = true;
let mut search = FuzzySearch::new("something", "some search thing", case_insensitive);
let config = ScoreConfig { bonusconsecutive: 12, bonuswordstart: 64, bonuscoverage: 64, penalty_distance: 4, };
search.setscoreconfig(config);
println!("result: {:?}", search.best_match()); ```
Note: Any whitespace in the pattern ('something'
in the examples above) will be removed.
Check out the documentation at docs.rs.