Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: VerbInsteadOfNoun to complement NounInsteadOfVerb (#1565)
Andrew Dunbar34d475665214
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dictindex 7c3d17520..ef27418b8 100644--- a/harper-core/dictionary.dict+++ b/harper-core/dictionary.dict@@ -26007,7 +26007,7 @@ gaslight/NVgSdGgasman/Ngasmen/9gasohol/Ng-gasoline/~NJg+gasoline/~Nmg # Wiktionary says it's also an adjective but other dictionaries don'tgasometer/NSgasp/VNgdGSgassed/JVediff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex dd3a85f6e..1e4482203 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -67,7 +67,6 @@ use super::no_match_for::NoMatchFor;use super::nobody::Nobody;use super::nominal_wants::NominalWants;use super::noun_countability::NounCountability;-use super::noun_instead_of_verb::NounInsteadOfVerb;use super::number_suffix_capitalization::NumberSuffixCapitalization;use super::of_course::OfCourse;use super::on_floor::OnFloor;@@ -445,7 +444,6 @@ impl LintGroup {insert_struct_rule!(NoOxfordComma, false);insert_expr_rule!(Nobody, true);insert_expr_rule!(NounCountability, true);- insert_expr_rule!(NounInsteadOfVerb, true);insert_struct_rule!(NumberSuffixCapitalization, true);insert_struct_rule!(OfCourse, true);insert_expr_rule!(OnFloor, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex 062addc67..e36132fc7 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -73,7 +73,7 @@ mod no_oxford_comma;mod nobody;mod nominal_wants;mod noun_countability;-mod noun_instead_of_verb;+mod noun_verb_confusion;mod number_suffix_capitalization;mod of_course;pub mod on_floor;@@ -186,7 +186,7 @@ pub use no_match_for::NoMatchFor;pub use no_oxford_comma::NoOxfordComma;pub use nobody::Nobody;pub use noun_countability::NounCountability;-pub use noun_instead_of_verb::NounInsteadOfVerb;+pub use noun_verb_confusion::NounVerbConfusion;pub use number_suffix_capitalization::NumberSuffixCapitalization;pub use of_course::OfCourse;pub use on_floor::OnFloor;diff --git a/harper-core/src/linting/noun_verb_confusion/mod.rs b/harper-core/src/linting/noun_verb_confusion/mod.rsnew file mode 100644index 000000000..f40045758--- /dev/null+++ b/harper-core/src/linting/noun_verb_confusion/mod.rs@@ -0,0 +1,27 @@+use super::merge_linters::merge_linters;++mod noun_instead_of_verb;+mod verb_instead_of_noun;++// Common noun-verb pairs that are often confused+// See also [`NounInsteadOfVerb``]+pub(crate) const NOUN_VERB_PAIRS: &[(&str, &str)] = &[+ ("advice", "advise"),+ ("belief", "believe"),+ ("breath", "breathe"),+ ("effect", "affect"), // "Effect" is also a verb meaning "to bring about". "Affect" is a noun in psychology.+ ("intent", "intend"),+ // ("proof", "prove"), // "Proof" is also a verb, a synonym of "proofread".+ // Add more pairs here as needed+];++pub use noun_instead_of_verb::NounInsteadOfVerb;+pub use verb_instead_of_noun::VerbInsteadOfNoun;++// Merge the two linters with a descriptive message+merge_linters! {+ NounVerbConfusion =>+ NounInsteadOfVerb,+ VerbInsteadOfNoun+ => "Handles common confusions between related nouns and verbs (e.g., 'advice/advise', 'breath/breathe')"+}diff --git a/harper-core/src/linting/noun_instead_of_verb.rs b/harper-core/src/linting/noun_verb_confusion/noun_instead_of_verb.rssimilarity index 95%rename from harper-core/src/linting/noun_instead_of_verb.rsrename to harper-core/src/linting/noun_verb_confusion/noun_instead_of_verb.rsindex d4a72bc19..e3f580d78 100644--- a/harper-core/src/linting/noun_instead_of_verb.rs+++ b/harper-core/src/linting/noun_verb_confusion/noun_instead_of_verb.rs@@ -1,20 +1,8 @@-use crate::expr::Expr;-use crate::expr::SequenceExpr;-use crate::expr::{FirstMatchOf, LongestMatchOf};+use crate::expr::{Expr, FirstMatchOf, LongestMatchOf, SequenceExpr};+use crate::linting::{ExprLinter, Lint, LintKind, Suggestion};use crate::{Lrc, Token, patterns::WordSet};-use super::{ExprLinter, Lint, LintKind, Suggestion};--/// Common noun-verb pairs that are often confused-const NOUN_VERB_PAIRS: &[(&str, &str)] = &[- ("advice", "advise"),- ("belief", "believe"),- ("breath", "breathe"),- ("effect", "affect"), // "Effect" is also a verb meaning "to bring about".- ("intent", "intend"),- // ("proof", "prove"), // "Proof" is also a verb, a synonym of "proofread".- // Add more pairs here as needed-];+use super::NOUN_VERB_PAIRS;/// Pronouns that can come before verbs but not nounsconst PRONOUNS: &[&str] = &["he", "I", "it", "she", "they", "we", "who", "you"];diff --git a/harper-core/src/linting/noun_verb_confusion/verb_instead_of_noun.rs b/harper-core/src/linting/noun_verb_confusion/verb_instead_of_noun.rsnew file mode 100644index 000000000..1304f6012--- /dev/null+++ b/harper-core/src/linting/noun_verb_confusion/verb_instead_of_noun.rs@@ -0,0 +1,189 @@+use crate::{+ Lrc, Token,+ expr::{Expr, SequenceExpr},+ linting::{ExprLinter, Lint, LintKind, Suggestion},+ patterns::WordSet,+};++use super::NOUN_VERB_PAIRS;++pub struct VerbInsteadOfNoun {+ expr: Box<dyn Expr>,+}++impl Default for VerbInsteadOfNoun {+ fn default() -> Self {+ let verbs = Lrc::new(WordSet::new(+ &NOUN_VERB_PAIRS+ .iter()+ .map(|&(_, verb)| verb)+ .collect::<Vec<_>>(),+ ));+ Self {+ expr: Box::new(+ SequenceExpr::default()+ .then_adjective()+ .then_whitespace()+ .then(verbs.clone()),+ ),+ }+ }+}++impl ExprLinter for VerbInsteadOfNoun {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {+ let adj_tok = &toks.first()?;+ let verb_tok = &toks.last()?;++ let adj_text = adj_tok.span.get_content_string(src);+ let verb_text = verb_tok.span.get_content_string(src);+ let verb_lower = verb_text.to_lowercase();++ let noun = NOUN_VERB_PAIRS+ .iter()+ .find(|(_, verb)| *verb == verb_lower)+ .map(|(noun, _)| noun)?;++ // Don't flag "so I better advise you", "you'd better believe this", "you'd best listen to me".+ if adj_text == "better" || adj_text == "best" {+ return None;+ }++ // "Sound" is both adjectve and noun. We want to flag the common "sound advise"+ // But not "sound affect", which is just as correct as "sound effect".+ if adj_text == "sound" && verb_text == "affect" {+ return None;+ }++ Some(Lint {+ span: verb_tok.span,+ lint_kind: LintKind::WordChoice,+ suggestions: vec![Suggestion::replace_with_match_case(+ noun.chars().collect(),+ verb_tok.span.get_content(src),+ )],+ message: format!("`{verb_text}` is a verb, the noun should be `{noun}`."),+ priority: 63,+ })+ }++ fn description(&self) -> &'static str {+ "Corrects verbs used instead of nouns when the two are related."+ }+}++#[cfg(test)]+mod tests {+ use super::VerbInsteadOfNoun;+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ #[test]+ fn corrects_good_advise() {+ assert_suggestion_result("Good advise", VerbInsteadOfNoun::default(), "Good advice");+ }++ #[test]+ fn corrects_bad_advise() {+ assert_suggestion_result(+ "I just wanted to bring attention to this because it stood out to me as potentially bad advise.",+ VerbInsteadOfNoun::default(),+ "I just wanted to bring attention to this because it stood out to me as potentially bad advice.",+ );+ }++ #[test]+ fn dont_flag_correct_better_advise() {+ assert_lint_count(+ "Hello! I am an engineer at Plexon and am conducting tests with Kilosort4 so we can better advise our clients.",+ VerbInsteadOfNoun::default(),+ 0,+ );+ }++ #[test]+ #[ignore = "'better advise' can be correct as above, or a mistake like here"]+ fn correct_better_advise() {+ assert_suggestion_result(+ "Maybe this will be a decent idea, .or anybody has better advise :)",+ VerbInsteadOfNoun::default(),+ "Maybe this will be a decent idea, .or anybody has better advice :)",+ );+ }++ #[test]+ fn dont_flag_correct_better_believe() {+ assert_lint_count(+ "You'd better believe this is bbedit-gist-maker.",+ VerbInsteadOfNoun::default(),+ 0,+ );+ }++ #[test]+ fn correct_great_affect() {+ assert_suggestion_result(+ "badges that they provide to users to allow them to promote their projects to great affect",+ VerbInsteadOfNoun::default(),+ "badges that they provide to users to allow them to promote their projects to great effect",+ );+ }++ #[test]+ fn correct_strong_believe() {+ assert_suggestion_result(+ "cause my strong believe is that we must give any user to describe whether a post is meant factual",+ VerbInsteadOfNoun::default(),+ "cause my strong belief is that we must give any user to describe whether a post is meant factual",+ );+ }++ #[test]+ fn dont_flag_best_affect() {+ assert_lint_count(+ "Using linear regression to predict and understand what factors best affect house price",+ VerbInsteadOfNoun::default(),+ 0,+ );+ }++ #[test]+ fn correct_deep_breathe() {+ assert_suggestion_result(+ "Take deep breathe and Do it again!",+ VerbInsteadOfNoun::default(),+ "Take deep breath and Do it again!",+ );+ }++ #[test]+ fn dont_flag_sound_affect() {+ assert_lint_count(+ "The goal of this study was to learn what properties of sound affect human focus the most.",+ VerbInsteadOfNoun::default(),+ 0,+ );+ }++ #[test]+ #[ignore = "`Affect` is both verb and noun, so we can't disambiguate without more context"]+ fn correct_sound_affect() {+ assert_suggestion_result(+ "Diesel Generator's animation returns to 'idle' state, but it's sound affect remains in the 'work' state.",+ VerbInsteadOfNoun::default(),+ "Diesel Generator's animation returns to 'idle' state, but it's sound effect remains in the 'work' state.",+ );+ }++ #[test]+ fn correct_bad_intend() {+ assert_suggestion_result(+ "What do you do if you only see slightly longer posts that may still be acceptable (and not bad intend from the poster)",+ VerbInsteadOfNoun::default(),+ "What do you do if you only see slightly longer posts that may still be acceptable (and not bad intent from the poster)",+ );+ }+}diff --git a/harper-core/tests/text/tagged/The Great Gatsby.md b/harper-core/tests/text/tagged/The Great Gatsby.mdindex f532c5496..368c8c3f0 100644--- a/harper-core/tests/text/tagged/The Great Gatsby.md+++ b/harper-core/tests/text/tagged/The Great Gatsby.md@@ -1977,7 +1977,7 @@> studies of him . ” His lips moved silently for a moment as he invented . “ ‘ George# NPl/V P ISg+ . . ISg/D$+ NPl/V+ V/J R C/P D/P+ NSg+ NSg/R NPr/ISg+ V/J+ . . Unlintable NPr+> B. Wilson at the Gasoline Pump , ’ or something like that . ”-# ? NPr+ NSg/P D+ NSg/J+ NSg/V . . NPr/C NSg/I/V/J NSg/V/J/C/P NSg/I/C/Ddem . .+# ? NPr+ NSg/P D+ NᴹSg+ NSg/V . . NPr/C NSg/I/V/J NSg/V/J/C/P NSg/I/C/Ddem . .>#> Catherine leaned close to me and whispered in my ear :@@ -5283,7 +5283,7 @@>#> “ Does the gasoline affect his nose ? ”-# . NPl/V D NSg/J NSg/V ISg/D$+ NSg/V+ . .+# . NPl/V D NᴹSg NSg/V ISg/D$+ NSg/V+ . .>#> “ I don’t think so , ” she said innocently . “ Why ? ”@@ -7747,7 +7747,7 @@> while in silence . Then as Doctor T. J. Eckleburg’s faded eyes came into sight# NSg/V/C/P NPr/J/P NSg/V+ . NSg/J/C NSg/R NSg/V+ ? ? ? J NPl/V+ NSg/V/P P NSg/V+> down the road , I remembered Gatsby’s caution about gasoline .-# NSg/V/J/P D+ NSg/J+ . ISg+ V/J NSg$ NSg/V+ J/P NSg/J+ .+# NSg/V/J/P D+ NSg/J+ . ISg+ V/J NSg$ NSg/V+ J/P NᴹSg+ .>#> “ We’ve got enough to get us to town , ” said Tom .