Skip to content

Commit

Permalink
search: Improve the search matcher
Browse files Browse the repository at this point in the history
Introduces fuzzy matcher to improve the theme search.
  • Loading branch information
darkowlzz committed Sep 7, 2021
1 parent 6957c63 commit 7379966
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
19 changes: 19 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ colored = "2.0.0"
git2 = "0.13.17"
edit-distance = "2.1.0"
tempfile = "3.2.0"
fuzzy-matcher = "0.3.7"
44 changes: 43 additions & 1 deletion src/operations/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use clap::Clap;
use colored::Colorize;
use edit_distance::edit_distance;
use errors::LeftError;
use fuzzy_matcher::skim::SkimMatcherV2;
use fuzzy_matcher::FuzzyMatcher;
use log::trace;

/* This function searches for themes, but does not update them by default
Expand Down Expand Up @@ -34,7 +36,7 @@ impl Search {
&theme.name,
edit_distance(&theme.name, &self.name)
);
if edit_distance(&theme.name, &self.name) <= 3 {
if Search::edit_distance_match(&theme.name, &self.name) {
let current = match theme.current {
Some(true) => "Current: ".bright_yellow().bold(),
_ => "".white(),
Expand Down Expand Up @@ -64,4 +66,44 @@ impl Search {

Ok(())
}

// Performs a match using edit_distance.
fn edit_distance_match(a: &str, b: &str) -> bool {
if edit_distance(a, b) <= 3 {
return true;
}
false
}

#[allow(dead_code)]
// Performs a using fuzzy matcher.
fn fuzzy_matcher_match(a: &str, b: &str) -> bool {
let matcher = SkimMatcherV2::default();
matcher.fuzzy_match(a, b).is_some()
}
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn test_match() {
// Temporary, to demonstrate mismatch. Will be removed later.
assert!(!Search::edit_distance_match("apple pie", "apple"));
assert!(!Search::edit_distance_match("apple pie", "pie"));
assert!(!Search::edit_distance_match("Windows XP", "windows"));
assert!(!Search::edit_distance_match("Windows XP", "xp"));
assert!(!Search::edit_distance_match("Windows XP", "zinbows"));
assert!(!Search::edit_distance_match("Soothe", "soo"));
assert!(Search::edit_distance_match("Soothe", "soohe"));

assert!(Search::fuzzy_matcher_match("apple pie", "apple"));
assert!(Search::fuzzy_matcher_match("apple pie", "pie"));
assert!(Search::fuzzy_matcher_match("Windows XP", "xp"));
assert!(Search::fuzzy_matcher_match("Windows XP", "windows"));
assert!(!Search::fuzzy_matcher_match("Windows XP", "zinbows"));
assert!(Search::fuzzy_matcher_match("Soothe", "soo"));
assert!(Search::fuzzy_matcher_match("Soothe", "soohe"));
}
}

0 comments on commit 7379966

Please sign in to comment.