Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): make possessive nouns imply compound nouns
Elijah Potter207b6fd59fa5
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 69c906717..46ce8ac0c 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -48479,7 +48479,7 @@ waxiness/1Mwaxwing/1SMwaxwork/1SMwaxy/51RPT-way/145SM~+way/15SM~waybill/14SMwayfarer/1MSwayfaring/514SMdiff --git a/harper-core/src/linting/compound_nouns/general_compound_nouns.rs b/harper-core/src/linting/compound_nouns/general_compound_nouns.rsnew file mode 100644index 000000000..02b6ca2f7--- /dev/null+++ b/harper-core/src/linting/compound_nouns/general_compound_nouns.rs@@ -0,0 +1,79 @@+use crate::{+ linting::PatternLinter,+ patterns::{All, SplitCompoundWord},+ CharStringExt, TokenStringExt,+};++use super::{Lint, LintKind, Suggestion};++use crate::{+ patterns::{Pattern, SequencePattern},+ Lrc, Token,+};++pub struct GeneralCompoundNouns {+ pattern: Box<dyn Pattern>,+ split_pattern: Lrc<SplitCompoundWord>,+}++impl Default for GeneralCompoundNouns {+ fn default() -> Self {+ let exceptions_pattern = SequencePattern::default()+ .then(Box::new(|tok: &Token, _: &[char]| {+ let Some(meta) = tok.kind.as_word() else {+ return false;+ };++ 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;+ };++ 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,+ }+ }+}++impl PatternLinter for GeneralCompoundNouns {+ 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 {+ "Detects compound nouns split by a space and suggests merging them when both parts form a valid noun. Has checks to avoid erroneous cases."+ }+}diff --git a/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs b/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rsnew file mode 100644index 000000000..989118990--- /dev/null+++ b/harper-core/src/linting/compound_nouns/implied_ownership_compound_nouns.rs@@ -0,0 +1,62 @@+use crate::{+ linting::PatternLinter, patterns::SplitCompoundWord, CharStringExt, Lrc, TokenStringExt,+};++use super::{Lint, LintKind, Suggestion};++use crate::{+ patterns::{Pattern, SequencePattern},+ Token,+};++/// Looks for closed compound nouns which can be condensed due to their position after a+/// possessive noun (which implies ownership).+pub struct ImpliedOwnershipCompoundNouns {+ pattern: Box<dyn Pattern>,+ split_pattern: Lrc<SplitCompoundWord>,+}++impl Default for ImpliedOwnershipCompoundNouns {+ fn default() -> Self {+ let split_pattern = Lrc::new(SplitCompoundWord::new(|meta| meta.is_noun()));+ let pattern = SequencePattern::default()+ .then_possessive_noun()+ .then_whitespace()+ .then(Box::new(split_pattern.clone()));++ Self {+ pattern: Box::new(pattern),+ split_pattern,+ }+ }+}++impl PatternLinter for ImpliedOwnershipCompoundNouns {+ fn pattern(&self) -> &dyn Pattern {+ self.pattern.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Lint {+ let span = matched_tokens[2..].span().unwrap();+ // If the pattern matched, this will not return `None`.+ let word = self+ .split_pattern+ .get_merged_word(matched_tokens[2], matched_tokens[4], source)+ .unwrap();++ Lint {+ span,+ lint_kind: LintKind::Spelling,+ suggestions: vec![Suggestion::ReplaceWith(word.to_vec())],+ message: format!(+ "The possessive noun implies ownership of the closed compound noun “{}”.",+ word.to_string()+ ),+ priority: 63,+ }+ }++ fn description(&self) -> &str {+ "Detects split compound nouns following a possessive noun and suggests merging them."+ }+}diff --git a/harper-core/src/linting/compound_nouns.rs b/harper-core/src/linting/compound_nouns/mod.rssimilarity index 79%rename from harper-core/src/linting/compound_nouns.rsrename to harper-core/src/linting/compound_nouns/mod.rsindex 4ad0442a0..465a948a9 100644--- a/harper-core/src/linting/compound_nouns.rs+++ b/harper-core/src/linting/compound_nouns/mod.rs@@ -1,89 +1,17 @@-use crate::{- patterns::{All, SplitCompoundWord},- CharString, CharStringExt, Dictionary, Document, FstDictionary, Span, TokenKind,- TokenStringExt, WordMetadata,-};+mod general_compound_nouns;+mod implied_ownership_compound_nouns;-use super::{Lint, LintKind, Linter, Suggestion};+use super::{merge_linters::merge_linters, Lint, LintKind, Suggestion};-use crate::{- patterns::{Pattern, SequencePattern, WordPatternGroup},- Lrc, Token,-};+use general_compound_nouns::GeneralCompoundNouns;+use implied_ownership_compound_nouns::ImpliedOwnershipCompoundNouns;-use super::PatternLinter;--pub struct CompoundNouns {- pattern: Box<dyn Pattern>,- split_pattern: Lrc<SplitCompoundWord>,-}--impl Default for CompoundNouns {- fn default() -> Self {- let exceptions_pattern = SequencePattern::default()- .then(Box::new(|tok: &Token, _: &[char]| {- let Some(meta) = tok.kind.as_word() else {- return false;- };-- 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;- };-- 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,- }- }-}--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 {- "Accidentally inserting a space inside a word is common. This rule looks for valid compound nouns that are split by whitespace and whose components are also valid words."- }-}+merge_linters!(CompoundNouns => GeneralCompoundNouns, ImpliedOwnershipCompoundNouns => "Detects compound nouns split by a space and suggests merging them when both parts form a valid noun." );#[cfg(test)]mod tests {use super::CompoundNouns;- use crate::linting::tests::assert_suggestion_result;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};#[test]fn lap_top() {@@ -329,4 +257,9 @@ mod tests {let expected = "The detective collected a fingerprint as evidence.";assert_suggestion_result(test_sentence, CompoundNouns::default(), expected);}++ #[test]+ fn got_is_not_possessive() {+ assert_lint_count("I got here by car...", CompoundNouns::default(), 0);+ }}diff --git a/harper-core/src/linting/merge_linters.rs b/harper-core/src/linting/merge_linters.rsindex 57c980790..c34a47e79 100644--- a/harper-core/src/linting/merge_linters.rs+++ b/harper-core/src/linting/merge_linters.rs@@ -4,7 +4,7 @@ macro_rules! merge_linters {mod merge_rule_hidden {use paste::paste;- use crate::{Document, linting::{Lint, Linter}};+ use crate::{Document, linting::{Lint, Linter}, remove_overlaps};$(use super::$linter;@@ -26,6 +26,8 @@ macro_rules! merge_linters {lints.extend(self.[< $linter:snake >].lint(document));)*+ remove_overlaps(&mut lints);+lints}diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rsindex 39aa4a98f..e8843651e 100644--- a/harper-core/src/patterns/sequence_pattern.rs+++ b/harper-core/src/patterns/sequence_pattern.rs@@ -67,6 +67,7 @@ macro_rules! gen_then_from_is {impl SequencePattern {gen_then_from_is!(noun);+ gen_then_from_is!(possessive_noun);gen_then_from_is!(plural_noun);gen_then_from_is!(verb);gen_then_from_is!(linking_verb);diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rsindex 0e5a09597..90863dded 100644--- a/harper-core/src/token_kind.rs+++ b/harper-core/src/token_kind.rs@@ -51,6 +51,19 @@ impl TokenKind {)}+ pub fn is_possessive_noun(&self) -> bool {+ matches!(+ self,+ TokenKind::Word(WordMetadata {+ noun: Some(NounData {+ is_possessive: Some(true),+ ..+ }),+ ..+ })+ )+ }+pub fn is_pronoun(&self) -> bool {matches!(self,