Skip to content

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

refactor(core): use patterns for `CompoundNouns`

Elijah Potter01c0fbdb8e56
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index f49c3c8ee..69c906717 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -11396,6 +11396,7 @@ afoul/
afraid/5U
afresh/
aft/15RZ
+after/+
afterbirth/1M
afterbirths/1
afterburner/1MS
diff --git a/harper-core/src/linting/compound_nouns.rs b/harper-core/src/linting/compound_nouns.rs
index 4ec73425e..a5d4c812e 100644
--- a/harper-core/src/linting/compound_nouns.rs
+++ b/harper-core/src/linting/compound_nouns.rs
@@ -1,115 +1,78 @@
-use std::sync::Arc;
-
-use itertools::Itertools;
-
use crate::{
- CharString, CharStringExt, Dictionary, Document, FstDictionary, Span, TokenKind, TokenStringExt,
+ patterns::{All, SplitCompoundWord},
+ CharString, CharStringExt, Dictionary, Document, FstDictionary, Span, TokenKind,
+ TokenStringExt, WordMetadata,
};
use super::{Lint, LintKind, Linter, Suggestion};
-pub struct CompoundNouns {
- dict: Arc<FstDictionary>,
-}
+use crate::{
+ patterns::{Pattern, SequencePattern, WordPatternGroup},
+ Lrc, Token,
+};
-impl CompoundNouns {
- pub fn new() -> Self {
- Self {
- dict: FstDictionary::curated(),
- }
- }
+use super::PatternLinter;
- /// Create a lint for two attempted lint matches.
- /// Since this function does not have access to tokens, you must set and modify the span
- /// yourself.
- fn attempt_lint_match(
- &self,
- word_a: &[char],
- word_b: &[char],
- buffer: &mut CharString,
- ) -> Option<Lint> {
- buffer.clear();
- buffer.extend_from_slice(word_a);
- buffer.extend_from_slice(word_b);
-
- if self.dict.contains_word(word_a)
- && self.dict.contains_word(word_b)
- && self.dict.get_word_metadata(buffer).is_noun()
- {
- Some(Lint {
- span: Span::default(),
- lint_kind: LintKind::Spelling,
- suggestions: vec![Suggestion::ReplaceWith(buffer.to_vec())],
- message: format!(
- "Did you mean the closed compound noun “{}”?",
- buffer.to_string()
- ),
- priority: 63,
- })
- } else {
- None
- }
- }
+pub struct CompoundNouns {
+ pattern: Box<dyn Pattern>,
+ split_pattern: Lrc<SplitCompoundWord>,
}
impl Default for CompoundNouns {
fn default() -> Self {
- Self::new()
- }
-}
+ let exceptions_pattern = SequencePattern::default()
+ .then(Box::new(|tok: &Token, _: &[char]| {
+ let Some(meta) = tok.kind.as_word() else {
+ return false;
+ };
-impl Linter for CompoundNouns {
- fn lint(&mut self, document: &Document) -> Vec<Lint> {
- let mut lints = Vec::new();
-
- // A persistent buffer for misc operations.
- let mut buffer = CharString::new();
-
- for (a, w, b) in document.tokens().tuple_windows() {
- if !a.kind.is_word() || !w.kind.is_whitespace() || !b.kind.is_word() {
- continue;
- }
-
- let a_meta = a.kind.expect_word();
- let b_meta = b.kind.expect_word();
- let mut a_chars: CharString = document.get_span_content(a.span).into();
- let mut b_chars: CharString = document.get_span_content(b.span).into();
-
- if a_chars.len() <= 1
- || b_chars.len() <= 1
- || matches!(b_chars.as_slice(), &['i', 's'])
- || a_meta.article
- || b_meta.article
- || (a_meta.is_verb() && b_meta.is_adverb())
- {
- continue;
- }
-
- let span = [a, w, b].span().unwrap();
-
- macro_rules! attempt {
- () => {
- if let Some(mut lint) = self.attempt_lint_match(&a_chars, &b_chars, &mut buffer)
- {
- lint.span = span;
- lints.push(lint);
- continue;
- }
+ tok.span.len() > 1 && !meta.article && !meta.preposition
+ }))
+ .then_whitespace()
+ .then(Box::new(|tok: &Token, _: &[char]| {
+ let Some(meta) = tok.kind.as_word() else {
+ return false;
};
- }
-
- attempt!();
- a_chars[0] = a_chars[0].to_ascii_uppercase();
- attempt!();
- b_chars[0] = b_chars[0].to_ascii_uppercase();
- attempt!();
- a_chars[0] = a_chars[0].to_ascii_lowercase();
- attempt!();
- b_chars[0] = b_chars[0].to_ascii_lowercase();
- attempt!();
+
+ tok.span.len() > 1 && !meta.article && !meta.is_adverb() && !meta.preposition
+ }));
+
+ let split_pattern = Lrc::new(SplitCompoundWord::new(|meta| meta.is_noun()));
+
+ let mut pattern = All::default();
+ pattern.add(Box::new(split_pattern.clone()));
+ pattern.add(Box::new(exceptions_pattern));
+
+ Self {
+ pattern: Box::new(pattern),
+ split_pattern,
}
+ }
+}
- lints
+impl PatternLinter for CompoundNouns {
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {
+ let span = matched_tokens.span().unwrap();
+ // If the pattern matched, this will not return `None`.
+ let word = self
+ .split_pattern
+ .get_merged_word(matched_tokens[0], matched_tokens[2], source)
+ .unwrap();
+
+ Lint {
+ span,
+ lint_kind: LintKind::Spelling,
+ suggestions: vec![Suggestion::ReplaceWith(word.to_vec())],
+ message: format!(
+ "Did you mean the closed compound noun “{}”?",
+ word.to_string()
+ ),
+ priority: 63,
+ }
}
fn description(&self) -> &str {
@@ -132,9 +95,6 @@ mod tests {
#[test]
fn web_cam() {
let test_sentence = "The web cam captured a stunning image.";
- let expected = "The web cam captured a stunning image."; // see note below
- // In many contexts, "web cam" is acceptable if meant as a phrase.
- // However, if your dictionary marks "webcam" as the noun, then:
let expected = "The webcam captured a stunning image.";
assert_suggestion_result(test_sentence, CompoundNouns::default(), expected);
}
diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rs
index 2a0b867c5..870d0e88d 100644
--- a/harper-core/src/patterns/mod.rs
+++ b/harper-core/src/patterns/mod.rs
@@ -22,6 +22,7 @@ mod noun_phrase;
mod repeating_pattern;
mod sequence_pattern;
mod similar_to_phrase;
+mod split_compound_word;
mod token_kind_pattern_group;
mod whitespace_pattern;
mod within_edit_distance;
@@ -42,6 +43,7 @@ pub use noun_phrase::NounPhrase;
pub use repeating_pattern::RepeatingPattern;
pub use sequence_pattern::SequencePattern;
pub use similar_to_phrase::SimilarToPhrase;
+pub use split_compound_word::SplitCompoundWord;
pub use token_kind_pattern_group::TokenKindPatternGroup;
pub use whitespace_pattern::WhitespacePattern;
pub use word_pattern_group::WordPatternGroup;
diff --git a/harper-core/src/patterns/split_compound_word.rs b/harper-core/src/patterns/split_compound_word.rs
new file mode 100644
index 000000000..9cb53d9cb
--- /dev/null
+++ b/harper-core/src/patterns/split_compound_word.rs
@@ -0,0 +1,88 @@
+use std::sync::Arc;
+
+use crate::{CharString, Dictionary, FstDictionary, Token, WordMetadata};
+
+use super::{Pattern, SequencePattern};
+
+/// A [`Pattern`] that looks for valid words accidentally split by whitespace.
+///
+/// Note that matches of this pattern are not guaranteed to be valid if merged.
+pub struct SplitCompoundWord {
+ inner: SequencePattern,
+ dict: Arc<FstDictionary>,
+ predicate: Box<dyn Fn(WordMetadata) -> bool + Send + Sync>,
+}
+
+impl SplitCompoundWord {
+ /// Create a new instance of the linter which will only look for compound words that fit the
+ /// provided predicate.
+ pub fn new(predicate: impl Fn(WordMetadata) -> bool + Send + Sync + 'static) -> Self {
+ Self {
+ inner: SequencePattern::default()
+ .then_any_word()
+ .then_whitespace()
+ .then_any_word(),
+ dict: FstDictionary::curated(),
+ predicate: Box::new(predicate),
+ }
+ }
+
+ /// The merged word from the dictionary that this pattern would match on if it was split.
+ pub fn get_merged_word(
+ &self,
+ word_a: Token,
+ word_b: Token,
+ source: &[char],
+ ) -> Option<CharString> {
+ let mut a_chars: CharString = word_a.span.get_content(source).into();
+ let mut b_chars: CharString = word_b.span.get_content(source).into();
+
+ let mut buffer = CharString::new();
+
+ macro_rules! attempt {
+ () => {
+ buffer.clear();
+ buffer.extend_from_slice(&a_chars);
+ buffer.extend_from_slice(&b_chars);
+
+ if self.dict.contains_word(&buffer) {
+ let metadata = self.dict.get_word_metadata(&buffer);
+ if (self.predicate)(metadata) {
+ return Some(buffer);
+ }
+ }
+ };
+ }
+
+ attempt!();
+ a_chars[0] = a_chars[0].to_ascii_uppercase();
+ attempt!();
+ b_chars[0] = b_chars[0].to_ascii_uppercase();
+ attempt!();
+ a_chars[0] = a_chars[0].to_ascii_lowercase();
+ attempt!();
+ b_chars[0] = b_chars[0].to_ascii_lowercase();
+ attempt!();
+
+ None
+ }
+}
+
+impl Pattern for SplitCompoundWord {
+ fn matches(&self, tokens: &[Token], source: &[char]) -> usize {
+ let inner_match = self.inner.matches(tokens, source);
+
+ if inner_match != 3 {
+ return 0;
+ }
+
+ let a = tokens[0];
+ let b = tokens[2];
+
+ if self.get_merged_word(a, b, source).is_some() {
+ return inner_match;
+ }
+
+ 0
+ }
+}