Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(fst-dict): add a curated function and use in benches
Grant Lemonsf4a8bb1d3a9b
diff --git a/harper-core/benches/parse_demo.rs b/harper-core/benches/parse_demo.rsindex 7d49cf91d..f8c2b2f8f 100644--- a/harper-core/benches/parse_demo.rs+++ b/harper-core/benches/parse_demo.rs@@ -1,6 +1,6 @@use criterion::{black_box, criterion_group, criterion_main, Criterion};use harper_core::linting::{LintGroup, LintGroupConfig, Linter};-use harper_core::{Document, FullDictionary};+use harper_core::{Document, FstDictionary, FullDictionary};static DEMO: &str = include_str!("../../demo.md");@@ -23,7 +23,7 @@ fn lint_demo(c: &mut Criterion) {fn lint_demo_uncached(c: &mut Criterion) {c.bench_function("lint_demo_uncached", |b| {b.iter(|| {- let dictionary = FullDictionary::curated();+ let dictionary = FstDictionary::curated();let mut lint_set = LintGroup::new(LintGroupConfig::default(), dictionary.clone());let document = Document::new_markdown(black_box(DEMO), &dictionary);lint_set.lint(&document)diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rsindex 29a7b1015..2aa28baff 100644--- a/harper-core/src/lib.rs+++ b/harper-core/src/lib.rs@@ -26,7 +26,7 @@ use linting::Lint;pub use mask::{Mask, Masker};pub use punctuation::{Punctuation, Quote};pub use span::Span;-pub use spell::{Dictionary, FullDictionary, MergedDictionary};+pub use spell::{Dictionary, FstDictionary, FullDictionary, MergedDictionary};pub use sync::Lrc;pub use token::{FatToken, Token, TokenKind, TokenStringExt};pub use vec_ext::VecExt;diff --git a/harper-core/src/spell/fst_dictionary.rs b/harper-core/src/spell/fst_dictionary.rsindex e590ffc80..5b46e2a4f 100644--- a/harper-core/src/spell/fst_dictionary.rs+++ b/harper-core/src/spell/fst_dictionary.rs@@ -49,6 +49,14 @@ thread_local! {static DICT: Lrc<FstDictionary> = uncached_inner_new();}+impl FstDictionary {+ /// Create a dictionary from the curated dictionary included+ /// in the Harper binary.+ pub fn curated() -> Lrc<Self> {+ DICT.with(|v| v.clone())+ }+}+impl Dictionary for FstDictionary {fn contains_word(&self, word: &[char]) -> bool {self.word_map.contains_key(word.iter().collect::<String>())@@ -59,8 +67,16 @@ impl Dictionary for FstDictionary {}fn get_word_metadata(&self, word: &[char]) -> WordMetadata {- let index: usize = self.word_map.get(word.iter().collect::<String>()).unwrap() as usize;- self.metadata[index]+ let index: Option<usize> = self+ .word_map+ .get(word.iter().collect::<String>())+ .map(|i| i as usize);++ if let Some(i) = index {+ self.metadata[i]+ } else {+ WordMetadata::default()+ }}fn get_word_metadata_str(&self, word: &str) -> WordMetadata {diff --git a/harper-core/src/spell/mod.rs b/harper-core/src/spell/mod.rsindex d80ba3bdf..f196fdb7f 100644--- a/harper-core/src/spell/mod.rs+++ b/harper-core/src/spell/mod.rs@@ -3,6 +3,7 @@ use itertools::{Itertools, MinMaxResult};use crate::CharString;pub use self::dictionary::Dictionary;+pub use self::fst_dictionary::FstDictionary;pub use self::full_dictionary::FullDictionary;pub use self::merged_dictionary::MergedDictionary;