Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
style(fst-dict): refactor some of FullDictionary fuzzy search
Grant Lemons56ab2260d2a2
diff --git a/harper-core/src/spell/fst_dictionary.rs b/harper-core/src/spell/fst_dictionary.rsindex 4b4373315..2f6d32a99 100644--- a/harper-core/src/spell/fst_dictionary.rs+++ b/harper-core/src/spell/fst_dictionary.rs@@ -86,14 +86,14 @@ impl Dictionary for FstDictionary {// Actual FST searchlet automaton = Levenshtein::new(&misspelled_word_string, max_distance as u32).unwrap();- let word_indexes_stream = self.word_map.search(automaton).into_stream().into_values();+ let word_indexes = self.word_map.search(automaton).into_stream().into_values();// Pre-allocated vectors for edit-distance calculation// 53 is the length of the longest word.let mut buf_a = Vec::with_capacity(53);let mut buf_b = Vec::with_capacity(53);- word_indexes_stream+ word_indexes.into_iter().sorted_unstable().dedup()diff --git a/harper-core/src/spell/full_dictionary.rs b/harper-core/src/spell/full_dictionary.rsindex 38ae04169..19fbc581a 100644--- a/harper-core/src/spell/full_dictionary.rs+++ b/harper-core/src/spell/full_dictionary.rs@@ -175,18 +175,14 @@ impl Dictionary for FullDictionary {max_results: usize,) -> Vec<(&[char], u8, WordMetadata)> {let misspelled_normalized = seq_to_normalized(word);- let misspelled_lower: Vec<char> = misspelled_normalized.to_lower().to_vec();+ let misspelled_lower_charslice = misspelled_normalized.to_lower();- // The length of the shortest word to look at.- let shortest_word_len = if misspelled_normalized.len() < max_distance as usize {- 1- } else {- misspelled_normalized.len() - max_distance as usize- };+ let shortest_word_len =+ std::cmp::max(1, misspelled_normalized.len() - max_distance as usize);+ let longest_word_len = misspelled_normalized.len() + max_distance as usize;- // Note how we look at the biggest words first.- let words_to_search = (shortest_word_len- ..misspelled_normalized.len() + max_distance as usize)+ // Get canidate words+ let words_to_search = (shortest_word_len..=longest_word_len).rev().flat_map(|len| self.words_with_len_iter(len));@@ -194,10 +190,12 @@ impl Dictionary for FullDictionary {// 53 is the length of the longest word.let mut buf_a = Vec::with_capacity(53);let mut buf_b = Vec::with_capacity(53);++ // Sort by edit-distancewords_to_search.filter_map(|word| {let dist = edit_distance_min_alloc(- &misspelled_lower,+ &misspelled_lower_charslice,&word.to_lower(),&mut buf_a,&mut buf_b,@@ -209,7 +207,7 @@ impl Dictionary for FullDictionary {None}})- .sorted_by_key(|a| a.1)+ .sorted_unstable_by_key(|a| a.1).take(max_results).map(|(word, dist)| (word, dist, self.get_word_metadata(word))).collect()diff --git a/harper-core/src/spell/mod.rs b/harper-core/src/spell/mod.rsindex bd0fcd3e6..dcf00369a 100644--- a/harper-core/src/spell/mod.rs+++ b/harper-core/src/spell/mod.rs@@ -48,7 +48,7 @@ fn order_suggestions(matches: Vec<(&[char], u8, WordMetadata)>) -> Vec<&[char]>}// Let common words bubble up.- found.sort_by_key(|(_, _, metadata)| if metadata.common { 0 } else { 1 });+ found.sort_by_key(|(_, dist, metadata)| dist + if metadata.common { 0 } else { 1 });found.into_iter().map(|(word, _, _)| word).collect()}@@ -154,6 +154,9 @@ mod tests {Dictionary, FstDictionary, FullDictionary,};+ const RESULT_LIMIT: usize = 100;+ const MAX_EDIT_DIST: u8 = 3;+fn assert_edit_dist(source: &str, target: &str, expected: u8) {let source: Vec<_> = source.chars().collect();let target: Vec<_> = target.chars().collect();@@ -172,17 +175,22 @@ mod tests {#[test]fn simple_edit_distance_1() {- assert_edit_dist("kitten", "sitting", 3)+ assert_edit_dist("kitten", "sitting", MAX_EDIT_DIST)}#[test]fn simple_edit_distance_2() {- assert_edit_dist("saturday", "sunday", 3)+ assert_edit_dist("saturday", "sunday", MAX_EDIT_DIST)}#[test]fn produces_no_duplicates() {- let results = suggest_correct_spelling_str("punctation", 100, 3, &FstDictionary::curated());+ let results = suggest_correct_spelling_str(+ "punctation",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );dbg!(&results, results.iter().unique().collect_vec());@@ -257,17 +265,38 @@ mod tests {}#[test]- fn full_dict_fuzzy_find_anout() {- let results = suggest_correct_spelling_str("anout", 100, 3, &FullDictionary::curated());+ fn issue_182() {+ let results = suggest_correct_spelling_str(+ "Im",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );+ let lowercase_results = suggest_correct_spelling_str(+ "im",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );dbg!(&results);+ dbg!(&lowercase_results);- assert!(results.iter().take(3).contains(&"about".to_string()));+ assert!(results.iter().take(3).contains(&"I'm".to_string()));+ assert!(lowercase_results+ .iter()+ .take(3)+ .contains(&"I'm".to_string()));}#[test]- fn full_dict_fuzzy_find_ello() {- let results = suggest_correct_spelling_str("ello", 100, 3, &FullDictionary::curated());+ fn fst_spellcheck_hvllo() {+ let results = suggest_correct_spelling_str(+ "hvllo",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );dbg!(&results);@@ -275,8 +304,13 @@ mod tests {}#[test]- fn full_dict_fuzzy_find_hello_capital() {- let results = suggest_correct_spelling_str("Hello", 100, 3, &FullDictionary::curated());+ fn full_spellcheck_hvllo() {+ let results = suggest_correct_spelling_str(+ "hvllo",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FullDictionary::curated(),+ );dbg!(&results);@@ -284,24 +318,41 @@ mod tests {}#[test]- fn issue_182() {- let results = suggest_correct_spelling_str("Im", 100, 3, &FstDictionary::curated());- let lowercase_results =- suggest_correct_spelling_str("im", 100, 3, &FstDictionary::curated());+ fn fst_spellcheck_common() {+ let results = suggest_correct_spelling_str(+ "aboot",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );dbg!(&results);- dbg!(&lowercase_results);- assert!(results.iter().take(3).contains(&"I'm".to_string()));- assert!(lowercase_results- .iter()- .take(3)- .contains(&"I'm".to_string()));+ assert!(results.iter().take(3).contains(&"about".to_string()));+ }++ #[test]+ fn full_spellcheck_common() {+ let results = suggest_correct_spelling_str(+ "aboot",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FullDictionary::curated(),+ );++ dbg!(&results);++ assert!(results.iter().take(3).contains(&"about".to_string()));}#[test]- fn spellcheck_hvllo() {- let results = suggest_correct_spelling_str("hvllo", 100, 3, &FstDictionary::curated());+ fn fst_spellcheck_match() {+ let results = suggest_correct_spelling_str(+ "hello",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );dbg!(&results);@@ -309,17 +360,27 @@ mod tests {}#[test]- fn spellcheck_common() {- let results = suggest_correct_spelling_str("aboot", 100, 3, &FstDictionary::curated());+ fn full_spellcheck_match() {+ let results = suggest_correct_spelling_str(+ "hello",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FullDictionary::curated(),+ );dbg!(&results);- assert!(results.iter().take(3).contains(&"about".to_string()));+ assert!(results.iter().take(3).contains(&"hello".to_string()));}#[test]- fn spellcheck_match() {- let results = suggest_correct_spelling_str("hello", 100, 3, &FstDictionary::curated());+ fn fst_spellcheck_capital() {+ let results = suggest_correct_spelling_str(+ "Hello",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );dbg!(&results);@@ -327,8 +388,13 @@ mod tests {}#[test]- fn spellcheck_capital() {- let results = suggest_correct_spelling_str("Hello", 100, 3, &FstDictionary::curated());+ fn full_spellcheck_capital() {+ let results = suggest_correct_spelling_str(+ "Hello",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FullDictionary::curated(),+ );dbg!(&results);@@ -337,9 +403,24 @@ mod tests {#[test]fn spellchecking_is_deterministic() {- let results1 = suggest_correct_spelling_str("hello", 100, 3, &FstDictionary::curated());- let results2 = suggest_correct_spelling_str("hello", 100, 3, &FstDictionary::curated());- let results3 = suggest_correct_spelling_str("hello", 100, 3, &FstDictionary::curated());+ let results1 = suggest_correct_spelling_str(+ "hello",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );+ let results2 = suggest_correct_spelling_str(+ "hello",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );+ let results3 = suggest_correct_spelling_str(+ "hello",+ RESULT_LIMIT,+ MAX_EDIT_DIST,+ &FstDictionary::curated(),+ );assert_eq!(results1, results2);assert_eq!(results1, results3);