Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor(core): use `MutableDictionary` to construct `FstDictionary`
Elijah Potter8af4c0e1b81c
diff --git a/harper-core/src/spell/fst_dictionary.rs b/harper-core/src/spell/fst_dictionary.rsindex 0027af964..947e39902 100644--- a/harper-core/src/spell/fst_dictionary.rs+++ b/harper-core/src/spell/fst_dictionary.rs@@ -1,8 +1,4 @@-use super::{- MutableDictionary, WordId,- hunspell::{parse_default_attribute_list, parse_default_word_list},- word_map::WordMap,-};+use super::{MutableDictionary, WordId};use fst::{IntoStreamer, Map as FstMap, Streamer, map::StreamWithState};use lazy_static::lazy_static;use levenshtein_automata::{DFA, LevenshteinAutomatonBuilder};@@ -16,7 +12,7 @@ use super::FuzzyMatchResult;/// An immutable dictionary allowing for very fast spellchecking.////// For dictionaries with changing contents, such as user and file dictionaries, prefer-/// [`super::MutableDictionary`].+/// [`MutableDictionary`].pub struct FstDictionary {/// Underlying [`super::MutableDictionary`] used for everything except fuzzy findingfull_dict: Arc<MutableDictionary>,@@ -26,29 +22,11 @@ pub struct FstDictionary {words: Vec<(CharString, WordMetadata)>,}-/// The uncached function that is used to produce the original copy of the-/// curated dictionary.-fn uncached_inner_new() -> Arc<FstDictionary> {- let word_list = parse_default_word_list().unwrap();- let attr_list = parse_default_attribute_list();-- // There will be at _least_ this number of words- let mut word_map = WordMap::with_capacity(word_list.len());- attr_list.expand_marked_words(word_list, &mut word_map);-- Arc::new(FstDictionary::new(- word_map- .into_iter()- .map(|entry| (entry.canonical_spelling, entry.metadata))- .collect(),- ))-}-const EXPECTED_DISTANCE: u8 = 3;const TRANSPOSITION_COST_ONE: bool = false;lazy_static! {- static ref DICT: Arc<FstDictionary> = uncached_inner_new();+ static ref DICT: Arc<FstDictionary> = Arc::new((*MutableDictionary::curated()).clone().into());}thread_local! {@@ -75,6 +53,8 @@ impl FstDictionary {(*DICT).clone()}+ /// Construct a new [`FstDictionary`] using a word list as a source.+ /// This can be expensive, so only use this if fast fuzzy searches are worth it.pub fn new(mut words: Vec<(CharString, WordMetadata)>) -> Self {words.sort_unstable_by(|(a, _), (b, _)| a.cmp(b));words.dedup_by(|(a, _), (b, _)| a == b);diff --git a/harper-core/src/spell/mutable_dictionary.rs b/harper-core/src/spell/mutable_dictionary.rsindex cc13a97c2..34e444118 100644--- a/harper-core/src/spell/mutable_dictionary.rs+++ b/harper-core/src/spell/mutable_dictionary.rs@@ -1,5 +1,5 @@use super::{- WordId,+ FstDictionary, WordId,hunspell::{parse_default_attribute_list, parse_default_word_list},word_map::{WordMap, WordMapEntry},};@@ -225,6 +225,18 @@ impl Dictionary for MutableDictionary {}}+impl From<MutableDictionary> for FstDictionary {+ fn from(dict: MutableDictionary) -> Self {+ let words = dict+ .word_map+ .into_iter()+ .map(|entry| (entry.canonical_spelling, entry.metadata))+ .collect();++ FstDictionary::new(words)+ }+}+#[cfg(test)]mod tests {use itertools::Itertools;