Skip to content

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

feat: API now allows adding words to the dictionary

Elijah Potter7f6ad584b7cc
diff --git a/Cargo.lock b/Cargo.lock
index a7e08e2d3..ccf9383f3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -529,6 +529,7 @@ checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604"
dependencies = [
"ahash",
"allocator-api2",
+ "serde",
]
[[package]]
@@ -1081,6 +1082,9 @@ name = "smallvec"
version = "1.12.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2593d31f82ead8df961d8bd23a64c2ccf2eb5dd34b0a34bfb4dd54011c72009e"
+dependencies = [
+ "serde",
+]
[[package]]
name = "socket2"
diff --git a/harper-core/Cargo.toml b/harper-core/Cargo.toml
index d14f3b205..537368e6e 100644
--- a/harper-core/Cargo.toml
+++ b/harper-core/Cargo.toml
@@ -8,14 +8,14 @@ readme = "README.md"
homepage = "https://github.com/chilipepperhott/harper"
[dependencies]
-hashbrown = "0.14.3"
+hashbrown = { version = "0.14.3", features = ["serde"] }
is-macro = "0.3.0"
itertools = "0.11.0"
once_cell = "1.19.0"
paste = "1.0.14"
pulldown-cmark = "0.9.3"
serde = { version = "1.0.190", features = ["derive"] }
-smallvec = "1.12.0"
+smallvec = { version = "1.12.0", features = ["serde"] }
thiserror = "1.0.56"
[dev-dependencies]
diff --git a/harper-core/benches/parse_demo.rs b/harper-core/benches/parse_demo.rs
index 3e436233d..3f28cbc29 100644
--- a/harper-core/benches/parse_demo.rs
+++ b/harper-core/benches/parse_demo.rs
@@ -15,7 +15,7 @@ fn parse_demo(bencher: Bencher) {
#[divan::bench]
fn create_lint_set(bencher: Bencher) {
- let dictionary = Dictionary::new();
+ let dictionary = Dictionary::create_from_curated();
bencher.bench_local(|| {
let _lint_set = LintSet::new().with_standard(dictionary.clone());
@@ -24,7 +24,7 @@ fn create_lint_set(bencher: Bencher) {
#[divan::bench]
fn lint_demo(bencher: Bencher) {
- let dictionary = Dictionary::new();
+ let dictionary = Dictionary::create_from_curated();
let mut lint_set = LintSet::new().with_standard(dictionary);
let document = Document::new_markdown(black_box(DEMO));
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index f1ca2371a..d00170173 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -41327,7 +41327,7 @@ serge/M
sergeant/MS
serial/SMY
serialization/SM
-serialize/GDSB
+serialize/GDSBR
series/M
serif/MS
serigraph/M
diff --git a/harper-core/src/linting/matcher.rs b/harper-core/src/linting/matcher.rs
index ee1d2864b..03b1b87bb 100644
--- a/harper-core/src/linting/matcher.rs
+++ b/harper-core/src/linting/matcher.rs
@@ -93,6 +93,7 @@ pub struct Matcher {
impl Matcher {
pub fn new() -> Self {
let mut triggers = pt! {
+ "wordlist" => "word list",
"the","challenged" => "that challenged",
"stdin" => "standard input",
"stdout" => "standard output",
diff --git a/harper-core/src/spell/dictionary.rs b/harper-core/src/spell/dictionary.rs
index ca6441f8c..50cb66269 100644
--- a/harper-core/src/spell/dictionary.rs
+++ b/harper-core/src/spell/dictionary.rs
@@ -33,19 +33,9 @@ fn uncached_inner_new() -> Dictionary {
let words = attr_list.expand_marked_words(word_list).unwrap();
let mut words: Vec<DictWord> = words.into_iter().collect();
- words.sort_by_key(|a| a.len());
-
- let mut word_len_starts = vec![0, 0];
-
- for (index, len) in words.iter().map(SmallVec::len).enumerate() {
- if word_len_starts.len() == len {
- word_len_starts.push(index);
- }
- }
-
Dictionary {
word_set: HashSet::from_iter(words.iter().cloned()),
- word_len_starts,
+ word_len_starts: Dictionary::create_len_starts(&mut words),
words,
}
}
@@ -53,10 +43,37 @@ fn uncached_inner_new() -> Dictionary {
static DICT: Lazy<Dictionary> = Lazy::new(uncached_inner_new);
impl Dictionary {
- pub fn new() -> Self {
+ /// Create a dictionary from the curated Hunspell dictionary included
+ /// in the Harper binary.
+ pub fn create_from_curated() -> Self {
DICT.clone()
}
+ /// Appends words to the dictionary.
+ /// It is significantly faster to append many words with one call than many
+ /// distinct calls to this function.
+ pub fn append_words(&mut self, words: &mut [DictWord]) {
+ self.words.extend(words.iter().cloned());
+ self.word_set.extend(words.iter().cloned());
+ self.word_len_starts = Self::create_len_starts(&mut self.words);
+ }
+
+ /// Create a lookup table for finding words of a specific length in a word list.
+ /// NOTE: This function will sort the original word list by its length.
+ /// If the word list's order is changed after creating the lookup, it will no longer be valid.
+ fn create_len_starts(words: &mut [DictWord]) -> Vec<usize> {
+ words.sort_by_key(|a| a.len());
+ let mut word_len_starts = vec![0, 0];
+
+ for (index, len) in words.iter().map(SmallVec::len).enumerate() {
+ if word_len_starts.len() == len {
+ word_len_starts.push(index);
+ }
+ }
+
+ word_len_starts
+ }
+
/// Iterate over all the words in the dictionary of a given length
pub fn words_with_len_iter(&self, len: usize) -> Box<dyn Iterator<Item = &'_ [char]> + '_> {
if len == 0 || len >= self.word_len_starts.len() {
diff --git a/harper-ls/src/backend.rs b/harper-ls/src/backend.rs
index 99b06ee6d..0b4bf7b19 100644
--- a/harper-ls/src/backend.rs
+++ b/harper-ls/src/backend.rs
@@ -77,7 +77,7 @@ impl Backend {
}
pub fn new(client: Client) -> Self {
- let dictionary = Dictionary::new();
+ let dictionary = Dictionary::create_from_curated();
let linter = Mutex::new(LintSet::new().with_standard(dictionary));
Self {
diff --git a/harper-serve/src/main.rs b/harper-serve/src/main.rs
index f604127b0..62f8ac23e 100644
--- a/harper-serve/src/main.rs
+++ b/harper-serve/src/main.rs
@@ -92,7 +92,7 @@ async fn lint(Json(payload): Json<LintRequest>) -> (StatusCode, Json<LintRespons
let document = Document::new_markdown(&text);
- let dictionary = Dictionary::new();
+ let dictionary = Dictionary::create_from_curated();
let mut linter = LintSet::new().with_standard(dictionary);
let lints = linter.lint(&document);
diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rs
index aa4841f3a..0e3756432 100644
--- a/harper-wasm/src/lib.rs
+++ b/harper-wasm/src/lib.rs
@@ -6,7 +6,7 @@ use serde::Serialize;
use wasm_bindgen::{prelude::wasm_bindgen, JsValue};
static LINTER: Lazy<Mutex<LintSet>> =
- Lazy::new(|| Mutex::new(LintSet::new().with_standard(Dictionary::new())));
+ Lazy::new(|| Mutex::new(LintSet::new().with_standard(Dictionary::create_from_curated())));
/// Create the serializer that preserves types across the JavaScript barrier
fn glue_serializer() -> serde_wasm_bindgen::Serializer {