Skip to content

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

feat: add `harper-cli forms` and `just getforms`

hippietrail006ba3d5c1e8
diff --git a/Cargo.lock b/Cargo.lock
index 96c33f6f3..c3da4d2b3 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -607,6 +607,7 @@ dependencies = [
"harper-core",
"harper-literate-haskell",
"harper-typst",
+ "hashbrown 0.15.2",
"serde",
"serde_json",
]
diff --git a/harper-cli/Cargo.toml b/harper-cli/Cargo.toml
index 610aa05cc..d94463e41 100644
--- a/harper-cli/Cargo.toml
+++ b/harper-cli/Cargo.toml
@@ -14,6 +14,7 @@ harper-literate-haskell = { path = "../harper-literate-haskell", version = "0.19
harper-core = { path = "../harper-core", version = "0.19.0" }
harper-comments = { path = "../harper-comments", version = "0.19.0" }
harper-typst = { path = "../harper-typst", version = "0.19.0" }
+hashbrown = "0.15.2"
serde = { version = "1.0.214", features = ["derive"] }
serde_json = "1.0.138"
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rs
index caea919c0..aea07a182 100644
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@ -1,6 +1,6 @@
#![doc = include_str!("../README.md")]
-use std::collections::{BTreeMap, HashMap};
+use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use anyhow::format_err;
@@ -9,8 +9,13 @@ use clap::Parser;
use harper_comments::CommentParser;
use harper_core::linting::{LintGroup, LintGroupConfig, Linter};
use harper_core::parsers::{Markdown, MarkdownOptions};
-use harper_core::{remove_overlaps, Dictionary, Document, FstDictionary, TokenKind};
+use harper_core::spell::hunspell::parse_default_attribute_list;
+use harper_core::spell::hunspell::word_list::parse_word_list;
+use harper_core::{
+ remove_overlaps, CharString, Dictionary, Document, FstDictionary, TokenKind, WordMetadata,
+};
use harper_literate_haskell::LiterateHaskellParser;
+use hashbrown::HashMap;
use serde::Serialize;
/// A debugging tool for the Harper grammar checker.
@@ -41,6 +46,8 @@ enum Args {
},
/// Get the metadata associated with a particular word.
Metadata { word: String },
+ /// Get all the forms of a word using the affixes.
+ Forms { word: String },
/// Emit a decompressed, line-separated list of the words in Harper's dictionary.
Words,
/// Print the default config with descriptions.
@@ -156,7 +163,7 @@ fn main() -> anyhow::Result<()> {
word_str.clear();
word_str.extend(word);
- println!("{}", word_str);
+ println!("{:?}", word_str);
}
Ok(())
@@ -169,6 +176,22 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
+ Args::Forms { word } => {
+ let hunspell_word_list = format!("1\n{word}");
+ let words = parse_word_list(&hunspell_word_list.to_string()).unwrap();
+
+ let attributes = parse_default_attribute_list();
+
+ let mut expanded: HashMap<CharString, WordMetadata> = HashMap::new();
+
+ attributes.expand_marked_words(words, &mut expanded);
+
+ expanded.keys().for_each(|form| {
+ let string_form: String = form.iter().collect();
+ println!("{}", string_form);
+ });
+ Ok(())
+ }
Args::Config => {
#[derive(Serialize)]
struct Config {
diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rs
index ccf9df6cb..4be7f7c4d 100644
--- a/harper-core/src/lib.rs
+++ b/harper-core/src/lib.rs
@@ -16,7 +16,7 @@ pub mod parsers;
pub mod patterns;
mod punctuation;
mod span;
-mod spell;
+pub mod spell;
mod sync;
mod title_case;
mod token;
diff --git a/harper-core/src/spell/hunspell/mod.rs b/harper-core/src/spell/hunspell/mod.rs
index b5a3b48f3..2e75fb7c2 100644
--- a/harper-core/src/spell/hunspell/mod.rs
+++ b/harper-core/src/spell/hunspell/mod.rs
@@ -3,7 +3,7 @@ mod attribute_list;
mod error;
mod expansion;
mod matcher;
-mod word_list;
+pub mod word_list;
pub use attribute_list::AttributeList;
use attribute_list::HumanReadableAttributeList;
diff --git a/harper-core/src/spell/mod.rs b/harper-core/src/spell/mod.rs
index 70c67eb0a..68ba37791 100644
--- a/harper-core/src/spell/mod.rs
+++ b/harper-core/src/spell/mod.rs
@@ -12,7 +12,7 @@ pub use self::merged_dictionary::MergedDictionary;
mod dictionary;
mod fst_dictionary;
mod full_dictionary;
-mod hunspell;
+pub mod hunspell;
mod merged_dictionary;
#[derive(PartialEq, Debug)]
diff --git a/justfile b/justfile
index 924762344..48528580f 100644
--- a/justfile
+++ b/justfile
@@ -256,6 +256,9 @@ userdictoverlap:
# Get the metadata associated with a particular word in Harper's dictionary as JSON.
getmetadata word:
cargo run --bin harper-cli -- metadata {{word}}
+# Get all the forms of a word using the affixes.
+getforms word:
+ cargo run --bin harper-cli -- forms {{word}}
bump-versions: update-vscode-linters
#! /bin/bash