Skip to content

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

fix(fst-dict): put methods back in `Dictionary` trait

Elijah Potterfc0f4642d487
diff --git a/harper-core/src/spell/dictionary.rs b/harper-core/src/spell/dictionary.rs
index 3fbcb0703..44100e32e 100644
--- a/harper-core/src/spell/dictionary.rs
+++ b/harper-core/src/spell/dictionary.rs
@@ -31,6 +31,12 @@ pub trait Dictionary {
/// If the word isn't in the dictionary, the resulting metadata will be
/// empty.
fn get_word_metadata_str(&self, word: &str) -> WordMetadata;
+
+ /// Iterate over the words in the dictionary.
+ fn words_iter(&self) -> impl Iterator<Item = &'_ [char]>;
+
+ /// Iterate over all the words in the dictionary of a given length
+ fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + '_>;
}
#[cfg(feature = "concurrent")]
@@ -62,4 +68,10 @@ pub trait Dictionary: Send + Sync {
/// If the word isn't in the dictionary, the resulting metadata will be
/// empty.
fn get_word_metadata_str(&self, word: &str) -> WordMetadata;
+
+ /// Iterate over the words in the dictionary.
+ fn words_iter(&self) -> impl Iterator<Item = &'_ [char]>;
+
+ /// Iterate over all the words in the dictionary of a given length
+ fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + '_>;
}
diff --git a/harper-core/src/spell/fst_dictionary.rs b/harper-core/src/spell/fst_dictionary.rs
index 5b46e2a4f..6545f56f1 100644
--- a/harper-core/src/spell/fst_dictionary.rs
+++ b/harper-core/src/spell/fst_dictionary.rs
@@ -108,4 +108,13 @@ impl Dictionary for FstDictionary {
.map(|(word, i)| (word.as_slice(), i as u8, self.metadata[i as usize]))
.collect()
}
+
+ fn words_iter(&self) -> impl Iterator<Item = &'_ [char]> {
+ self.words.iter().map(|v| v.as_slice())
+ }
+
+ fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + '_> {
+ // Could be _way_ faster.
+ Box::new(self.words_iter().filter(move |w| w.len() == len))
+ }
}
diff --git a/harper-core/src/spell/full_dictionary.rs b/harper-core/src/spell/full_dictionary.rs
index e6c0c1ca4..02945ce89 100644
--- a/harper-core/src/spell/full_dictionary.rs
+++ b/harper-core/src/spell/full_dictionary.rs
@@ -117,26 +117,6 @@ impl FullDictionary {
word_len_starts
}
-
- fn words_iter(&self) -> impl Iterator<Item = &'_ [char]> {
- self.words.iter().map(|v| v.as_slice())
- }
-
- /// Iterate over all the words in the dictionary of a given length
- fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + '_> {
- if len == 0 || len >= self.word_len_starts.len() {
- return Box::new(std::iter::empty());
- }
-
- let start = self.word_len_starts[len];
- let end = if len + 1 == self.word_len_starts.len() {
- self.words.len()
- } else {
- self.word_len_starts[len + 1]
- };
-
- Box::new(self.words[start..end].iter().map(|v| v.as_slice()))
- }
}
impl Default for FullDictionary {
@@ -246,6 +226,25 @@ impl Dictionary for FullDictionary {
let word: Vec<_> = word.chars().collect();
self.fuzzy_match(&word, max_distance, max_results)
}
+
+ fn words_iter(&self) -> impl Iterator<Item = &'_ [char]> {
+ self.words.iter().map(|v| v.as_slice())
+ }
+
+ fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + '_> {
+ if len == 0 || len >= self.word_len_starts.len() {
+ return Box::new(std::iter::empty());
+ }
+
+ let start = self.word_len_starts[len];
+ let end = if len + 1 == self.word_len_starts.len() {
+ self.words.len()
+ } else {
+ self.word_len_starts[len + 1]
+ };
+
+ Box::new(self.words[start..end].iter().map(|v| v.as_slice()))
+ }
}
// Computes the Levenstein edit distance between two patterns.
diff --git a/harper-core/src/spell/merged_dictionary.rs b/harper-core/src/spell/merged_dictionary.rs
index 7b1b3dc0d..3933e9167 100644
--- a/harper-core/src/spell/merged_dictionary.rs
+++ b/harper-core/src/spell/merged_dictionary.rs
@@ -76,6 +76,18 @@ where
found_metadata
}
+ fn words_iter(&self) -> impl Iterator<Item = &'_ [char]> {
+ self.children.iter().flat_map(|c| c.words_iter())
+ }
+
+ fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + '_> {
+ Box::new(
+ self.children
+ .iter()
+ .flat_map(move |c| c.words_with_len_iter(len)),
+ )
+ }
+
fn contains_word_str(&self, word: &str) -> bool {
let chars: CharString = word.chars().collect();
self.contains_word(&chars)