Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat(core): add rule for pronouns followed by an inflection of `be` (#1442)
Elijah Potter12a08e7714e7
diff --git a/harper-core/src/expr/mod.rs b/harper-core/src/expr/mod.rsindex 1c1e7138a..c61c6a05d 100644--- a/harper-core/src/expr/mod.rs+++ b/harper-core/src/expr/mod.rs@@ -6,6 +6,7 @@ mod first_match_of;mod fixed_phrase;mod longest_match_of;mod mergeable_words;+mod optional;mod reflexive_pronoun;mod repeating;mod sequence_expr;@@ -27,6 +28,7 @@ pub use expr_map::ExprMap;pub use fixed_phrase::FixedPhrase;pub use longest_match_of::LongestMatchOf;pub use mergeable_words::MergeableWords;+pub use optional::Optional;pub use reflexive_pronoun::ReflexivePronoun;pub use repeating::Repeating;pub use sequence_expr::SequenceExpr;diff --git a/harper-core/src/expr/optional.rs b/harper-core/src/expr/optional.rsnew file mode 100644index 000000000..f76775c99--- /dev/null+++ b/harper-core/src/expr/optional.rs@@ -0,0 +1,30 @@+use crate::{Span, Token};++use super::Expr;++/// An optional expression.+/// Forces the optional expression to always return Some by transmuting `None` into+/// `Some(cursor..cursor)`.+pub struct Optional {+ inner: Box<dyn Expr>,+}++impl Optional {+ pub fn new(inner: impl Expr + 'static) -> Self {+ Self {+ inner: Box::new(inner),+ }+ }+}++impl Expr for Optional {+ fn run(&self, cursor: usize, tokens: &[Token], source: &[char]) -> Option<Span> {+ let res = self.inner.run(cursor, tokens, source);++ if res.is_none() {+ Some(Span::new_with_len(cursor, 0))+ } else {+ res+ }+ }+}diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rsindex fdc67861e..44ec7a915 100644--- a/harper-core/src/expr/sequence_expr.rs+++ b/harper-core/src/expr/sequence_expr.rs@@ -5,7 +5,7 @@ use crate::{patterns::{AnyPattern, IndefiniteArticle, WhitespacePattern, Word},};-use super::{Expr, Repeating, Step, UnlessStep};+use super::{Expr, Optional, Repeating, Step, UnlessStep};#[derive(Default)]pub struct SequenceExpr {@@ -52,7 +52,7 @@ impl Expr for SequenceExpr {let out = cur_expr.run(cursor, tokens, source)?;window.expand_to_include(out.start);- window.expand_to_include(out.end - 1);+ window.expand_to_include(out.end.checked_sub(1).unwrap_or(out.start));if out.start < cursor {cursor = out.start;@@ -66,8 +66,14 @@ impl Expr for SequenceExpr {}impl SequenceExpr {- pub fn then(mut self, window: impl Expr + 'static) -> Self {- self.exprs.push(Box::new(window));+ pub fn then(mut self, expr: impl Expr + 'static) -> Self {+ self.exprs.push(Box::new(expr));+ self+ }++ /// Pushes an expression that could move the cursor to the sequence, but does not require it.+ pub fn then_optional(mut self, expr: impl Expr + 'static) -> Self {+ self.exprs.push(Box::new(Optional::new(expr)));self}@@ -165,6 +171,12 @@ impl SequenceExpr {gen_then_from_is!(determiner);gen_then_from_is!(proper_noun);gen_then_from_is!(preposition);+ gen_then_from_is!(third_person_pronoun);+ gen_then_from_is!(third_person_singular_pronoun);+ gen_then_from_is!(third_person_plural_pronoun);+ gen_then_from_is!(first_person_singular_pronoun);+ gen_then_from_is!(first_person_plural_pronoun);+ gen_then_from_is!(second_person_pronoun);gen_then_from_is!(non_plural_nominal);}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 7b59eeefe..71211ceca 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -67,6 +67,7 @@ use super::phrasal_verb_as_compound_noun::PhrasalVerbAsCompoundNoun;use super::pique_interest::PiqueInterest;use super::possessive_your::PossessiveYour;use super::pronoun_contraction::PronounContraction;+use super::pronoun_inflection_be::PronounInflectionBe;use super::pronoun_knew::PronounKnew;use super::proper_noun_capitalization_linters;use super::repeated_words::RepeatedWords;@@ -374,6 +375,7 @@ impl LintGroup {insert_pattern_rule!(BoringWords, false);insert_struct_rule!(CapitalizePersonalPronouns, true);insert_pattern_rule!(ChockFull, true);+ insert_pattern_rule!(PronounInflectionBe, true);insert_struct_rule!(CommaFixes, true);insert_struct_rule!(CompoundNouns, true);insert_pattern_rule!(Confident, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex baa6adc87..f989c9364 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -74,6 +74,7 @@ mod phrase_corrections;mod pique_interest;mod possessive_your;mod pronoun_contraction;+mod pronoun_inflection_be;mod pronoun_knew;mod proper_noun_capitalization_linters;mod repeated_words;@@ -163,6 +164,7 @@ pub use phrasal_verb_as_compound_noun::PhrasalVerbAsCompoundNoun;pub use pique_interest::PiqueInterest;pub use possessive_your::PossessiveYour;pub use pronoun_contraction::PronounContraction;+pub use pronoun_inflection_be::PronounInflectionBe;pub use repeated_words::RepeatedWords;pub use save_to_safe::SaveToSafe;pub use sentence_capitalization::SentenceCapitalization;diff --git a/harper-core/src/linting/pronoun_inflection_be.rs b/harper-core/src/linting/pronoun_inflection_be.rsnew file mode 100644index 000000000..134f7f83f--- /dev/null+++ b/harper-core/src/linting/pronoun_inflection_be.rs@@ -0,0 +1,379 @@+use harper_brill::UPOS;++use crate::Lrc;+use crate::Token;+use crate::expr::All;+use crate::expr::AnchorStart;+use crate::expr::ExprMap;+use crate::expr::{Expr, SequenceExpr};+use crate::patterns::UPOSSet;++use super::Suggestion;+use super::{ExprLinter, Lint, LintKind};++pub struct PronounInflectionBe {+ expr: Box<dyn Expr>,+ map: Lrc<ExprMap<&'static str>>,+}++impl PronounInflectionBe {+ pub fn new() -> Self {+ let mod_term = Lrc::new(+ SequenceExpr::default()+ .t_ws()+ .then(UPOSSet::new(&[UPOS::ADJ, UPOS::ADV])),+ );++ let mut map = ExprMap::default();++ let are = SequenceExpr::default()+ .then_third_person_singular_pronoun()+ .then_optional(mod_term.clone())+ .t_ws()+ .t_aco("are");+ map.insert(are, "is");++ let arent = SequenceExpr::default()+ .then_third_person_singular_pronoun()+ .then_optional(mod_term.clone())+ .t_ws()+ .t_aco("aren't");+ map.insert(arent, "isn't");++ let is = SequenceExpr::default()+ .then_third_person_plural_pronoun()+ .then_optional(mod_term.clone())+ .t_ws()+ .t_aco("is");+ map.insert(is, "are");++ let isnt = SequenceExpr::default()+ .then_third_person_plural_pronoun()+ .then_optional(mod_term.clone())+ .t_ws()+ .t_aco("isn't");+ map.insert(isnt, "aren't");++ let was = SequenceExpr::default()+ .then(|tok: &Token, _: &[char]| tok.kind.is_first_person_plural_pronoun())+ .then_optional(mod_term.clone())+ .t_ws()+ .t_aco("was");+ map.insert(was, "were");++ // Special case for second and third-person+ let was_third = SequenceExpr::default()+ .then(AnchorStart)+ .then(|tok: &Token, _: &[char]| {+ tok.kind.is_third_person_plural_pronoun() || tok.kind.is_second_person_pronoun()+ })+ .then_optional(mod_term.clone())+ .t_ws()+ .t_aco("was");+ map.insert(was_third, "were");++ let were = SequenceExpr::default()+ .then(AnchorStart)+ .then(|tok: &Token, _: &[char]| {+ tok.kind.is_first_person_singular_pronoun()+ || tok.kind.is_third_person_singular_pronoun()+ })+ .then_optional(mod_term.clone())+ .t_ws()+ .t_aco("were");+ map.insert(were, "was");++ let map = Lrc::new(map);++ let mut all = All::default();+ all.add(map.clone());+ all.add(|tok: &Token, _: &[char]| tok.kind.is_upos(UPOS::PRON));++ Self {+ expr: Box::new(all),+ map,+ }+ }+}++impl Default for PronounInflectionBe {+ fn default() -> Self {+ Self::new()+ }+}++impl ExprLinter for PronounInflectionBe {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {+ let span = matched_tokens.last()?.span;++ // Determine the correct inflection of "be".+ let correct = self.map.lookup(0, matched_tokens, source)?;++ Some(Lint {+ span,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![Suggestion::replace_with_match_case_str(+ correct,+ span.get_content(source),+ )],+ message: "Make the verb agree with its subject.".to_owned(),+ priority: 30,+ })+ }+ fn description(&self) -> &str {+ "Checks subject–verb agreement for the verb `be`. Third-person singular \+ pronouns (`he`, `she`, `it`) require the singular form `is`, while the \+ plural pronoun `they` takes `are`. The linter flags mismatches such as \+ `He are` or `They is` and offers the correct concord."+ }+}++#[cfg(test)]+mod tests {+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};++ use super::PronounInflectionBe;++ #[test]+ fn corrects_he_are() {+ assert_suggestion_result(+ "He are my best friend.",+ PronounInflectionBe::default(),+ "He is my best friend.",+ );+ }++ #[test]+ fn corrects_she_are() {+ assert_suggestion_result(+ "She are my best friend.",+ PronounInflectionBe::default(),+ "She is my best friend.",+ );+ }++ #[test]+ fn corrects_they_is() {+ assert_suggestion_result(+ "They is my best friend.",+ PronounInflectionBe::default(),+ "They are my best friend.",+ );+ }++ #[test]+ fn allows_they_are() {+ assert_lint_count(+ "They are my best friend.",+ PronounInflectionBe::default(),+ 0,+ );+ }++ #[test]+ fn corrects_it_are() {+ assert_suggestion_result(+ "It are on the table.",+ PronounInflectionBe::default(),+ "It is on the table.",+ );+ }++ #[test]+ fn corrects_he_are_negation() {+ assert_suggestion_result(+ "He are not amused.",+ PronounInflectionBe::default(),+ "He is not amused.",+ );+ }++ #[test]+ fn corrects_she_are_progressive() {+ assert_suggestion_result(+ "She are going to win.",+ PronounInflectionBe::default(),+ "She is going to win.",+ );+ }++ #[test]+ fn corrects_they_is_negation() {+ assert_suggestion_result(+ "They is not ready.",+ PronounInflectionBe::default(),+ "They are not ready.",+ );+ }++ #[test]+ fn corrects_they_is_progressive() {+ assert_suggestion_result(+ "They is planning a trip.",+ PronounInflectionBe::default(),+ "They are planning a trip.",+ );+ }++ #[test]+ fn allows_he_is() {+ assert_lint_count("He is my best friend.", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn allows_she_is_lowercase() {+ assert_lint_count("she is excited to go.", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn allows_it_is() {+ assert_lint_count("It is what it is.", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn allows_they_are_negation() {+ assert_lint_count(+ "They are not interested.",+ PronounInflectionBe::default(),+ 0,+ );+ }++ #[test]+ fn allows_they_were() {+ assert_lint_count("They were already here.", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn allows_asdf_is() {+ assert_lint_count("asdf is not a word", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn no_subject() {+ assert_lint_count("is set", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn corrects_i_were() {+ assert_suggestion_result(+ "I were the best player on the field.",+ PronounInflectionBe::default(),+ "I was the best player on the field.",+ );+ }++ #[test]+ fn corrects_we_was() {+ assert_suggestion_result(+ "We was the best players on the field.",+ PronounInflectionBe::default(),+ "We were the best players on the field.",+ );+ }++ #[test]+ fn corrects_you_was() {+ assert_suggestion_result(+ "You was my best friend.",+ PronounInflectionBe::default(),+ "You were my best friend.",+ );+ }++ #[test]+ fn allows_you_were() {+ assert_lint_count(+ "You were my best friend.",+ PronounInflectionBe::default(),+ 0,+ );+ }++ #[test]+ fn corrects_he_were() {+ assert_suggestion_result(+ "He were late.",+ PronounInflectionBe::default(),+ "He was late.",+ );+ }++ #[test]+ fn corrects_they_was() {+ assert_suggestion_result(+ "They was on time.",+ PronounInflectionBe::default(),+ "They were on time.",+ );+ }++ #[test]+ fn allows_he_was() {+ assert_lint_count("He was here.", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn allows_we_were() {+ assert_lint_count("We were excited.", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn corrects_he_arent() {+ assert_suggestion_result(+ "He aren't ready.",+ PronounInflectionBe::default(),+ "He isn't ready.",+ );+ }++ #[test]+ fn corrects_they_isnt() {+ assert_suggestion_result(+ "They isn't coming.",+ PronounInflectionBe::default(),+ "They aren't coming.",+ );+ }++ #[test]+ fn allows_he_isnt() {+ assert_lint_count("He isn't ready.", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn allows_they_arent() {+ assert_lint_count("They aren't coming.", PronounInflectionBe::default(), 0);+ }++ #[test]+ fn corrects_she_really_are() {+ assert_suggestion_result(+ "She really are talented.",+ PronounInflectionBe::default(),+ "She really is talented.",+ );+ }++ #[test]+ fn corrects_they_often_is() {+ assert_suggestion_result(+ "They often is late.",+ PronounInflectionBe::default(),+ "They often are late.",+ );+ }++ #[test]+ fn corrects_because_he_are() {+ assert_suggestion_result(+ "because he are tired.",+ PronounInflectionBe::default(),+ "because he is tired.",+ );+ }+}diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rsindex 75df51d60..1b44f1306 100644--- a/harper-core/src/token_kind.rs+++ b/harper-core/src/token_kind.rs@@ -366,6 +366,62 @@ impl TokenKind {metadata.is_non_plural_pronoun()}+ pub fn is_second_person_pronoun(&self) -> bool {+ let TokenKind::Word(Some(metadata)) = self else {+ return true;+ };++ metadata.is_second_person_pronoun()+ }++ pub fn is_third_person_pronoun(&self) -> bool {+ let TokenKind::Word(Some(metadata)) = self else {+ return true;+ };++ metadata.is_third_person_pronoun()+ }++ pub fn is_first_person_singular_pronoun(&self) -> bool {+ let TokenKind::Word(Some(metadata)) = self else {+ return true;+ };++ metadata.is_first_person_singular_pronoun()+ }++ pub fn is_first_person_plural_pronoun(&self) -> bool {+ let TokenKind::Word(Some(metadata)) = self else {+ return true;+ };++ metadata.is_first_person_plural_pronoun()+ }++ pub fn is_third_person_singular_pronoun(&self) -> bool {+ let TokenKind::Word(Some(metadata)) = self else {+ return true;+ };++ metadata.is_third_person_singular_pronoun()+ }++ pub fn is_third_person_plural_pronoun(&self) -> bool {+ let TokenKind::Word(Some(metadata)) = self else {+ return true;+ };++ metadata.is_third_person_plural_pronoun()+ }++ pub fn is_object_pronoun(&self) -> bool {+ let TokenKind::Word(Some(metadata)) = self else {+ return true;+ };++ metadata.is_object_pronoun()+ }+pub fn is_common_word(&self) -> bool {let TokenKind::Word(Some(metadata)) = self else {return true;diff --git a/harper-core/src/word_metadata.rs b/harper-core/src/word_metadata.rsindex dd6f62b70..bf55f9053 100644--- a/harper-core/src/word_metadata.rs+++ b/harper-core/src/word_metadata.rs@@ -304,7 +304,7 @@ impl WordMetadata {generate_metadata_queries!(noun has proper, plural, possessive.- pronoun has personal, singular, plural, possessive, reflexive.+ pronoun has personal, singular, plural, possessive, reflexive, object.determiner has demonstrative, possessive.verb has linking, auxiliary.conjunction has.@@ -312,6 +312,70 @@ impl WordMetadata {adverb has);+ pub fn is_first_person_plural_pronoun(&self) -> bool {+ matches!(+ self.pronoun,+ Some(PronounData {+ person: Some(Person::First),+ is_plural: Some(true),+ ..+ })+ )+ }++ pub fn is_first_person_singular_pronoun(&self) -> bool {+ matches!(+ self.pronoun,+ Some(PronounData {+ person: Some(Person::First),+ is_singular: Some(true),+ ..+ })+ )+ }++ pub fn is_third_person_plural_pronoun(&self) -> bool {+ matches!(+ self.pronoun,+ Some(PronounData {+ person: Some(Person::Third),+ is_plural: Some(true),+ ..+ })+ )+ }++ pub fn is_third_person_singular_pronoun(&self) -> bool {+ matches!(+ self.pronoun,+ Some(PronounData {+ person: Some(Person::Third),+ is_singular: Some(true),+ ..+ })+ )+ }++ pub fn is_third_person_pronoun(&self) -> bool {+ matches!(+ self.pronoun,+ Some(PronounData {+ person: Some(Person::Third),+ ..+ })+ )+ }++ pub fn is_second_person_pronoun(&self) -> bool {+ matches!(+ self.pronoun,+ Some(PronounData {+ person: Some(Person::Second),+ ..+ })+ )+ }+pub fn is_verb_lemma(&self) -> bool {matches!(self.verb,diff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.ymlindex c6a8bce81..d1064cf44 100644--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml@@ -7103,6 +7103,16 @@ Suggest:+Lint: Miscellaneous (30 priority)+Message: |+ 5575 | “He come out to see me two years ago and bought me the house I live in now. Of+ 5576 | course we was broke up when he run off from home, but I see now there was a+ | ^~~ Make the verb agree with its subject.+Suggest:+ - Replace with: “were”+++Lint: Spelling (63 priority)Message: |5582 | pocket a ragged old copy of a book called “Hopalong Cassidy.”diff --git a/harper-core/tests/text/linters/this and that.snap.yml b/harper-core/tests/text/linters/this and that.snap.ymlindex e69de29bb..7b061862b 100644--- a/harper-core/tests/text/linters/this and that.snap.yml+++ b/harper-core/tests/text/linters/this and that.snap.yml@@ -0,0 +1,9 @@+Lint: Miscellaneous (30 priority)+Message: |+ 2 | As such, disambiguating them is necessary.+ | ^~ Make the verb agree with its subject.+Suggest:+ - Replace with: “are”+++