Repository navigation
use fuzzy_dir #69
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
use fuzzy_dir #69
Changes from 9 commits
96e5481
9ace461
b6e9f02
168d2b2
b7a0402
69e08c5
d35658e
9d6bbfc
17c4f32
52a8c36
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,53 +1,66 @@ | ||
| use std::{collections::HashMap, iter}; | ||
|
|
||
| pub type Score = u16; | ||
|
|
||
| const SCORE_PATTERN_FULL_MATCH: Score = 60; | ||
| const SCORE_PATTERN_CONTAINED: Score = 10; | ||
| const SCORE_CHAR_STARTING: Score = 5; | ||
| const SCORE_CHAR_HIT: Score = 1; | ||
| const PENALTY_CHAR_MISS: Score = 5; | ||
|
|
||
| /// Creates a score of how much the input and the pattern match | ||
| /// | ||
| /// The higher the score the better. There is no max score. | ||
| pub fn fuzzy_match_score(input: &str, pattern: &str) -> i32 { | ||
| let mut score = 0; | ||
| if input | ||
| .to_lowercase() | ||
| .contains(pattern.to_lowercase().as_str()) | ||
| { | ||
| score += 10; | ||
| } | ||
| let mut dir_name_mut = input.to_string(); | ||
| for c in pattern.chars() { | ||
| if dir_name_mut.to_lowercase().contains(c.to_ascii_lowercase()) { | ||
| score += 1; | ||
| // strip the char to avoid multiple matches | ||
| dir_name_mut = dir_name_mut.replacen(c, "", 1); | ||
| pub fn fuzzy_match_score(input: &str, pattern: &str) -> Score { | ||
| let input = input.to_lowercase(); | ||
| let pattern = pattern.to_lowercase(); | ||
|
|
||
| let whole_pattern_score = { | ||
| if input == pattern { | ||
| SCORE_PATTERN_FULL_MATCH | ||
| } else if input.contains(&pattern) { | ||
| SCORE_PATTERN_CONTAINED | ||
| } else { | ||
| score -= 5; | ||
| 0 | ||
| } | ||
| }; | ||
|
|
||
| let starting_score = SCORE_CHAR_STARTING | ||
| * iter::zip(input.chars(), pattern.chars()) | ||
| .take_while(|(inp, pat)| inp == pat) | ||
| .count() as Score; | ||
|
|
||
| let (char_occurrence_score, char_miss_penalty) = { | ||
| let input = freqmap(&input); | ||
| let pattern = freqmap(&pattern); | ||
|
|
||
| let hits = pattern | ||
| .iter() | ||
| .map(|(char, &amt_in_pat)| { | ||
| let amt_in_inp = input.get(char).copied().unwrap_or_default(); | ||
| amt_in_pat.min(amt_in_inp) | ||
| }) | ||
| .sum::<Score>(); | ||
| let misses = pattern.values().sum::<Score>() - hits; | ||
|
|
||
| (SCORE_CHAR_HIT * hits, PENALTY_CHAR_MISS * misses) | ||
| }; | ||
|
|
||
| (whole_pattern_score + starting_score + char_occurrence_score).saturating_sub(char_miss_penalty) | ||
| } | ||
|
|
||
| // Doesn't technically need to be score as this is some abstract char count but it's convenient | ||
| fn freqmap(str: &str) -> HashMap<char, Score> { | ||
| let mut map = HashMap::with_capacity(str.len()); | ||
| for char in str.chars() { | ||
| *map.entry(char).or_default() += 1; | ||
| } | ||
| if input.to_lowercase() == pattern.to_lowercase() { | ||
| score += 50; | ||
| } | ||
| if score < 0 { | ||
| score = 0; | ||
| } | ||
| score | ||
| map | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::fuzzy_match_score as score; | ||
|
|
||
| #[test] | ||
| #[should_panic] | ||
| fn not_yet_implemented() { | ||
| // TODO: Should give more points if parts of query match | ||
| // beginning parts of input parts | ||
| assert!(score("test", "tt") > score("test", "t")); | ||
| assert!(score("test-abc", "ta") > score("test-abc", "te")); | ||
| assert!(score("test abc", "ta") > score("test abc", "te")); | ||
| assert!(score("test_abc", "ta") > score("test_abc", "te")); | ||
|
|
||
| assert!(score("test_abc_a", "taa") > score("test_abc_a", "te")); | ||
| assert!(score("test_abc_a", "taa") > score("test_abc_a", "tea")); | ||
|
|
||
| assert!(score("testAbc", "ta") > score("testAbc", "te")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_simple() { | ||
| assert_eq!(score("test", "test"), score("test", "test")); | ||
|
|
@@ -63,6 +76,19 @@ mod tests { | |
| assert!(score("helloworld", "hello") > score("helloworld", "hellohello")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_starting_with() { | ||
| assert!(score("test", "t") > score("test", "tt")); | ||
| assert!(score("test-abc", "te") > score("test-abc", "ta")); | ||
| assert!(score("test abc", "te") > score("test abc", "ta")); | ||
| assert!(score("test_abc", "te") > score("test_abc", "ta")); | ||
|
|
||
| assert!(score("test_abc_a", "te") > score("test_abc_a", "taa")); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. taa should have more value my thought process behind this was that if you have multiple words you just type the first letter "test abc abc" taa but now for both te is better. ig you could argue that te and ta for "test abc" can give the same value, but not better imo |
||
| assert!(score("test_abc_a", "tea") > score("test_abc_a", "taa")); | ||
|
|
||
| assert!(score("testAbc", "te") > score("testAbc", "tA")); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_negative_queries() { | ||
| assert!(score("helloworld", "ellovvvv") == 0); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -100,7 +100,10 @@ fn test_absolute() { | |
| #[test] | ||
| fn test_nonexisting() { | ||
| let env = TempEnv::new(); | ||
| assert!(env.resolve_query("test zzzzzzzzz zzzzzzzzz").is_empty()); | ||
| assert_eq!( | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just curious, is there any difference here? Or just readability?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| env.resolve_query("test zzzzzzzzz zzzzzzzzz"), | ||
| Vec::<PathBuf>::new() | ||
| ); | ||
| } | ||
|
|
||
| #[test] | ||
|
|
@@ -183,10 +186,11 @@ fn test_dir_skip() { | |
| let env = TempEnv::new(); | ||
|
|
||
| assert_eq!( | ||
| env.resolve_query("test gamma - u"), | ||
| env.resolve_query("test alpha - eps"), | ||
| vec!( | ||
| env.abs_path("test/gamma/sigm#a/upsilon3"), | ||
| env.abs_path("test/gamma/sigma9/t@u0"), | ||
| env.abs_path("test/alpha/beta/epsil@on8"), | ||
| env.abs_path("test/alpha/betabeta/epsil#on"), | ||
| env.abs_path("test/alpha/betabeta/epsil0n"), | ||
| ) | ||
| ); | ||
| assert_eq!( | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test has two tt, why shouldn't it score higher?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hm, actually ok, fair
I'll replace it with the length of common starting chars instead
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it already worked like that lol
this is because t is contained in test and tt is not