Skip to content

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

feat: `harper-cli audit-dictionary` (#2136)

Andrew Dunbar19832424d654
diff --git a/harper-cli/src/main.rs b/harper-cli/src/main.rs
index a4e636859..f1af636d5 100644
--- a/harper-cli/src/main.rs
+++ b/harper-cli/src/main.rs
@@ -28,6 +28,7 @@ use harper_python::PythonParser;
use harper_stats::Stats;
use serde::Serialize;
+use serde_json::Value;
mod input;
use input::Input;
@@ -161,6 +162,11 @@ enum Args {
/// The directory containing the dictionary and affixes.
dir: PathBuf,
},
+ /// Audit the `dictionary.dict` file.
+ AuditDictionary {
+ /// The directory containing the dictionary and affixes.
+ dir: PathBuf,
+ },
/// Emit a decompressed, line-separated list of the compounds in Harper's dictionary.
/// As long as there's either an open or hyphenated spelling.
Compounds,
@@ -548,8 +554,6 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
Args::RenameFlag { old, new, dir } => {
- use serde_json::Value;
-
let dict_path = dir.join("dictionary.dict");
let affixes_path = dir.join("annotations.json");
@@ -668,6 +672,129 @@ fn main() -> anyhow::Result<()> {
Ok(())
}
+ Args::AuditDictionary { dir } => {
+ let annotations_path = dir.join("annotations.json");
+ let annotations_content = fs::read_to_string(&annotations_path)
+ .map_err(|e| anyhow!("Failed to read annotations: {e}"))?;
+ let annotations_json: Value = serde_json::from_str(&annotations_content)
+ .map_err(|e| anyhow!("Failed to parse annotations.json: {e}"))?;
+
+ let annotations = annotations_json
+ .as_object()
+ .ok_or_else(|| anyhow!("annotations.json is not an object"))?;
+
+ let (affixes, properties) = ["affixes", "properties"]
+ .iter()
+ .map(|key| {
+ annotations
+ .get(*key)
+ .and_then(Value::as_object)
+ .ok_or_else(|| {
+ anyhow!("Missing or invalid '{key}' key in annotations.json")
+ })
+ })
+ .collect::<Result<Vec<_>, _>>()
+ .map(|v| (v[0], v[1]))?;
+
+ let all_keys = affixes.keys().chain(properties.keys()).collect::<Vec<_>>();
+
+ let mut annotation_flag_count: HashMap<char, u32> = all_keys
+ .iter()
+ .filter_map(|key| key.chars().next()) // Get first char of each key
+ .map(|c| (c, 0))
+ .collect();
+
+ // let mut duplicate_flag_total = 0;
+ let mut duplicate_flags = std::collections::HashMap::new();
+ let mut unknown_flags = std::collections::HashMap::new();
+ let mut unused_flag_total = 0;
+
+ let dict_path = dir.join("dictionary.dict");
+ let dict_content = fs::read_to_string(&dict_path)
+ .map_err(|e| anyhow!("Failed to read dictionary: {e}"))?;
+
+ for (line_num, line) in dict_content.lines().enumerate() {
+ if line.is_empty()
+ || line.starts_with('#')
+ || line.chars().all(|c| c.is_ascii_digit())
+ {
+ continue;
+ }
+
+ let (entry_part, _comment_part) =
+ line.split_once('#').map_or((line, ""), |(e, c)| (e, c));
+
+ if let Some((lexeme, rest)) = entry_part.split_once('/') {
+ let (annotation, _whitespace) = match rest.split_once([' ', '\t']) {
+ Some((a, _)) => (a, &rest[a.len()..]),
+ None => (rest, ""),
+ };
+
+ let mut seen_flags = hashbrown::HashSet::new();
+
+ for flag in annotation.chars() {
+ if !seen_flags.insert(flag) {
+ eprintln!(
+ "Warning: Line {}: Duplicate annotation flag '{}' in entry: {}/{}",
+ line_num + 1,
+ flag,
+ lexeme,
+ annotation
+ );
+ // duplicate_flag_total += 1;
+ *duplicate_flags.entry(flag).or_insert(0) += 1;
+ }
+ if !annotation_flag_count.contains_key(&flag) {
+ eprintln!(
+ "Warning: Line {}: Unknown annotation flag '{}' in entry: {}/{}",
+ line_num + 1,
+ flag,
+ lexeme,
+ annotation
+ );
+ *unknown_flags.entry(flag).or_insert(0) += 1;
+ } else {
+ *annotation_flag_count.get_mut(&flag).unwrap() += 1;
+ }
+ }
+ }
+ }
+
+ for (flag, count) in annotation_flag_count {
+ if count == 0 {
+ eprintln!("Warning: Unused annotation flag '{}'", flag);
+ unused_flag_total += 1;
+ }
+ }
+
+ let duplicate_flag_total = duplicate_flags.values().sum::<usize>();
+ let unknown_flag_total = unknown_flags.values().sum::<usize>();
+
+ if duplicate_flag_total > 0 || unknown_flag_total > 0 || unused_flag_total > 0 {
+ eprintln!("\nAudit found issues:");
+ if duplicate_flag_total > 0 {
+ eprintln!(
+ " - {} duplicate flags found in {} entries",
+ duplicate_flags.len(),
+ duplicate_flag_total
+ );
+ }
+ if !unknown_flags.is_empty() {
+ let total_unknown = unknown_flags.values().sum::<usize>();
+ eprintln!(
+ " - {} unknown flags found in {} entries",
+ unknown_flags.len(),
+ total_unknown
+ );
+ }
+ if unused_flag_total > 0 {
+ eprintln!(" - {} unused flags found", unused_flag_total);
+ }
+ std::process::exit(1);
+ }
+
+ Ok(())
+ }
Args::Compounds => {
let mut compound_map: HashMap<String, Vec<String>> = HashMap::new();
diff --git a/harper-core/annotations.json b/harper-core/annotations.json
index a23031210..2d874bef0 100644
--- a/harper-core/annotations.json
+++ b/harper-core/annotations.json
@@ -942,6 +942,24 @@
"verb_form": "THIRD_PERSON_SINGULAR"
}
}
+ },
+ "♂": {
+ "#": "masculine property",
+ "metadata": {
+ "//": "not yet implemented"
+ }
+ },
+ "♀": {
+ "#": "feminine property",
+ "metadata": {
+ "//": "not yet implemented"
+ }
+ },
+ "ª": {
+ "#": "animate property",
+ "metadata": {
+ "//": "not yet implemented"
+ }
}
}
}
diff --git a/justfile b/justfile
index b6291429c..ff555167d 100644
--- a/justfile
+++ b/justfile
@@ -260,7 +260,7 @@ update-vscode-linters:
just format
# Run Rust formatting and linting.
-check-rust:
+check-rust: auditdictionary
#!/usr/bin/env bash
set -eo pipefail
@@ -659,3 +659,9 @@ suggestannotation input:
console.log(`None of the characters of "${input}" are available to use for new annotations, and none of them are OK to be moved to make way for new annotations.`);
}
}
+
+# Audit the curated dictionary for any issues.
+alias auditdict := auditdictionary
+
+auditdictionary DIR="harper-core":
+ cargo run --bin harper-cli -- audit-dictionary {{DIR}}