Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(fst-dict): create new struct `FuzzyMatchResult` returned from fuzzy find operations

Grant Lemons4359a72da081
diff --git a/harper-core/src/spell/dictionary.rs b/harper-core/src/spell/dictionary.rs
index 79cacf4e5..8689762de 100644
--- a/harper-core/src/spell/dictionary.rs
+++ b/harper-core/src/spell/dictionary.rs
@@ -1,5 +1,6 @@
use blanket::blanket;
+use super::FuzzyMatchResult;
use crate::WordMetadata;
#[blanket(derive(Arc))]
@@ -9,19 +10,9 @@ pub trait Dictionary: Send + Sync {
/// Check if the dictionary contains a given word.
fn contains_word_str(&self, word: &str) -> bool;
/// Gets best fuzzy match from dictionary
- fn fuzzy_match(
- &self,
- word: &[char],
- max_distance: u8,
- max_results: usize,
- ) -> Vec<(&[char], u8, WordMetadata)>;
+ fn fuzzy_match(&self, word: &[char], max_distance: u8, max_results: usize) -> Vec<FuzzyMatchResult>;
/// Gets best fuzzy match from dictionary
- fn fuzzy_match_str(
- &self,
- word: &str,
- max_distance: u8,
- max_results: usize,
- ) -> Vec<(&[char], u8, WordMetadata)>;
+ fn fuzzy_match_str(&self, word: &str, max_distance: u8, max_results: usize) -> Vec<FuzzyMatchResult>;
/// Get the associated [`WordMetadata`] for a given word.
/// If the word isn't in the dictionary, the resulting metadata will be
/// empty.
diff --git a/harper-core/src/spell/fst_dictionary.rs b/harper-core/src/spell/fst_dictionary.rs
index d3ac36e4a..0c03d9095 100644
--- a/harper-core/src/spell/fst_dictionary.rs
+++ b/harper-core/src/spell/fst_dictionary.rs
@@ -11,6 +11,7 @@ use std::{cell::RefCell, sync::Arc};
use crate::{CharString, CharStringExt, WordMetadata};
use super::Dictionary;
+use super::FuzzyMatchResult;
pub struct FstDictionary {
/// Underlying FullDictionary used for everything except fuzzy finding
@@ -144,7 +145,7 @@ impl Dictionary for FstDictionary {
word: &[char],
max_distance: u8,
max_results: usize,
- ) -> Vec<(&[char], u8, WordMetadata)> {
+ ) -> Vec<FuzzyMatchResult> {
self.fuzzy_match_str(&word.iter().collect::<String>(), max_distance, max_results)
}
@@ -153,7 +154,7 @@ impl Dictionary for FstDictionary {
word: &str,
max_distance: u8,
max_results: usize,
- ) -> Vec<(&[char], u8, WordMetadata)> {
+ ) -> Vec<FuzzyMatchResult> {
// Various transformations of the input
let chars: Vec<_> = word.chars().collect();
let misspelled_word_charslice = seq_to_normalized(&chars);
@@ -168,12 +169,10 @@ impl Dictionary for FstDictionary {
// Sort by edit distance
.sorted_unstable_by_key(|a| a.1)
.take(max_results)
- .map(|(index, dist)| {
- (
- self.full_dict.get_word(index as usize).as_slice(),
- dist,
- self.full_dict.get_metadata(index as usize).to_owned(),
- )
+ .map(|(index, edit_distance)| FuzzyMatchResult {
+ word: self.full_dict.get_word(index as usize).as_slice(),
+ edit_distance,
+ metadata: self.full_dict.get_metadata(index as usize).to_owned(),
})
.collect()
}
@@ -238,7 +237,7 @@ mod tests {
let results = dict.fuzzy_match_str("hello", 3, 100);
let is_sorted_by_dist = results
.iter()
- .map(|(_, dist, _)| dist)
+ .map(|fm| fm.edit_distance)
.tuple_windows()
.all(|(a, b)| a <= b);
diff --git a/harper-core/src/spell/full_dictionary.rs b/harper-core/src/spell/full_dictionary.rs
index 630234058..ac2333ae8 100644
--- a/harper-core/src/spell/full_dictionary.rs
+++ b/harper-core/src/spell/full_dictionary.rs
@@ -8,9 +8,11 @@ use itertools::Itertools;
use smallvec::{SmallVec, ToSmallVec};
use std::sync::Arc;
-use super::dictionary::Dictionary;
use crate::{CharString, CharStringExt, WordMetadata};
+use super::dictionary::Dictionary;
+use super::FuzzyMatchResult;
+
/// A full, fat dictionary.
/// All elements are stored in-memory.
#[derive(Debug, Clone, Eq, PartialEq)]
@@ -181,7 +183,7 @@ impl Dictionary for FullDictionary {
word: &[char],
max_distance: u8,
max_results: usize,
- ) -> Vec<(&[char], u8, WordMetadata)> {
+ ) -> Vec<FuzzyMatchResult> {
let misspelled_charslice = seq_to_normalized(word);
let shortest_word_len = if misspelled_charslice.len() <= max_distance as usize {
@@ -215,7 +217,11 @@ impl Dictionary for FullDictionary {
})
.sorted_unstable_by_key(|a| a.1)
.take(max_results)
- .map(|(word, dist)| (word, dist, self.get_word_metadata(word)))
+ .map(|(word, edit_distance)| FuzzyMatchResult {
+ word,
+ edit_distance,
+ metadata: self.get_word_metadata(word),
+ })
.collect()
}
@@ -224,7 +230,7 @@ impl Dictionary for FullDictionary {
word: &str,
max_distance: u8,
max_results: usize,
- ) -> Vec<(&[char], u8, WordMetadata)> {
+ ) -> Vec<FuzzyMatchResult> {
let word: Vec<_> = word.chars().collect();
self.fuzzy_match(&word, max_distance, max_results)
}
@@ -318,7 +324,7 @@ mod tests {
let results = dict.fuzzy_match_str("hello", 3, 100);
let is_sorted_by_dist = results
.iter()
- .map(|(_, dist, _)| dist)
+ .map(|fm| fm.edit_distance)
.tuple_windows()
.all(|(a, b)| a <= b);
diff --git a/harper-core/src/spell/merged_dictionary.rs b/harper-core/src/spell/merged_dictionary.rs
index 717e064f3..c455a2298 100644
--- a/harper-core/src/spell/merged_dictionary.rs
+++ b/harper-core/src/spell/merged_dictionary.rs
@@ -2,7 +2,7 @@ use std::sync::Arc;
use itertools::Itertools;
-use super::dictionary::Dictionary;
+use super::{dictionary::Dictionary, FuzzyMatchResult};
use crate::{CharString, WordMetadata};
/// A simple wrapper over [`Dictionary`] that allows
@@ -82,11 +82,11 @@ impl Dictionary for MergedDictionary {
word: &[char],
max_distance: u8,
max_results: usize,
- ) -> Vec<(&[char], u8, WordMetadata)> {
+ ) -> Vec<FuzzyMatchResult> {
self.children
.iter()
.flat_map(|d| d.fuzzy_match(word, max_distance, max_results))
- .sorted_by_key(|r| r.1)
+ .sorted_by_key(|r| r.edit_distance)
.take(max_results)
.collect()
}
@@ -96,11 +96,11 @@ impl Dictionary for MergedDictionary {
word: &str,
max_distance: u8,
max_results: usize,
- ) -> Vec<(&[char], u8, WordMetadata)> {
+ ) -> Vec<FuzzyMatchResult> {
self.children
.iter()
.flat_map(|d| d.fuzzy_match_str(word, max_distance, max_results))
- .sorted_by_key(|r| r.1)
+ .sorted_by_key(|r| r.edit_distance)
.take(max_results)
.collect()
}
diff --git a/harper-core/src/spell/mod.rs b/harper-core/src/spell/mod.rs
index fd68947e7..1614c832d 100644
--- a/harper-core/src/spell/mod.rs
+++ b/harper-core/src/spell/mod.rs
@@ -12,21 +12,33 @@ pub use self::merged_dictionary::MergedDictionary;
mod dictionary;
mod fst_dictionary;
mod full_dictionary;
+mod hunspell;
mod merged_dictionary;
-fn order_suggestions(matches: Vec<(&[char], u8, WordMetadata)>) -> Vec<&[char]> {
- let mut found: Vec<(&[char], u8, WordMetadata)> = Vec::with_capacity(matches.len());
+#[derive(PartialEq)]
+pub struct FuzzyMatchResult<'a> {
+ word: &'a [char],
+ edit_distance: u8,
+ metadata: WordMetadata,
+}
+
+impl<'a> PartialOrd for FuzzyMatchResult<'a> {
+ fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
+ self.edit_distance.partial_cmp(&other.edit_distance)
+ }
+}
+
+fn order_suggestions(matches: Vec<FuzzyMatchResult>) -> Vec<&[char]> {
+ let mut found: Vec<&FuzzyMatchResult> = Vec::with_capacity(matches.len());
// Often the longest and the shortest words are the most helpful, so lets push
// them first.
- let minmax = matches
- .iter()
- .position_minmax_by_key(|(word, _, _)| word.len());
+ let minmax = matches.iter().position_minmax_by_key(|fmr| fmr.word.len());
if let MinMaxResult::MinMax(a, b) = minmax {
if a == b {
- found.push(matches[a]);
+ found.push(&matches[a]);
} else {
- found.push(matches[a]);
- found.push(matches[b]);
+ found.push(&matches[a]);
+ found.push(&matches[b]);
}
// Push the rest
@@ -38,7 +50,7 @@ fn order_suggestions(matches: Vec<(&[char], u8, WordMetadata)>) -> Vec<&[char]>
.map(|v| v.1),
);
} else {
- found.extend(matches);
+ found.extend(&matches);
}
// Swap the lowest edit distance word with the shortest.
@@ -47,9 +59,9 @@ fn order_suggestions(matches: Vec<(&[char], u8, WordMetadata)>) -> Vec<&[char]>
}
// Let common words bubble up, but do not prioritize them over all else.
- found.sort_by_key(|(_, dist, metadata)| dist + if metadata.common { 0 } else { 1 });
+ found.sort_by_key(|fmr| fmr.edit_distance + if fmr.metadata.common { 0 } else { 1 });
- found.into_iter().map(|(word, _, _)| word).collect()
+ found.into_iter().map(|fmr| fmr.word).collect()
}
/// Get the closest matches in the provided [`Dictionary`] and rank them
@@ -60,7 +72,7 @@ pub fn suggest_correct_spelling<'a>(
max_edit_dist: u8,
dictionary: &'a impl Dictionary,
) -> Vec<&'a [char]> {
- let matches: Vec<(&[char], u8, WordMetadata)> = dictionary
+ let matches: Vec<FuzzyMatchResult> = dictionary
.fuzzy_match(misspelled_word, max_edit_dist, result_limit)
.into_iter()
.collect();
@@ -149,6 +161,8 @@ fn edit_distance(source: &[char], target: &[char]) -> u8 {
mod tests {
use itertools::Itertools;
+ use crate::spell::FuzzyMatchResult;
+
use super::{
edit_distance, order_suggestions, seq_to_normalized, suggest_correct_spelling_str,
Dictionary, FstDictionary, FullDictionary,
@@ -236,7 +250,11 @@ mod tests {
.filter_map(|word| {
let metadata = dict.get_word_metadata(word);
if metadata.common {
- Some((word, 0, metadata))
+ Some(FuzzyMatchResult {
+ word,
+ edit_distance: 0,
+ metadata,
+ })
} else {
None
}
@@ -249,7 +267,11 @@ mod tests {
if metadata.common {
None
} else {
- Some((word, 0, metadata))
+ Some(FuzzyMatchResult {
+ word,
+ edit_distance: 0,
+ metadata,
+ })
}
})
.take(3);