Skip to content

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

feat: did went→did go (#2744)

Andrew Dunbarf4e0e0b646b7
diff --git a/harper-core/src/expr/duration_expr.rs b/harper-core/src/expr/duration_expr.rs
index b829c9d46..da4a74d9e 100644
--- a/harper-core/src/expr/duration_expr.rs
+++ b/harper-core/src/expr/duration_expr.rs
@@ -17,14 +17,13 @@ impl Expr for DurationExpr {
"months", "year", "years",
]);
- let expr = SequenceExpr::default()
- .then_longest_of(vec![
- Box::new(SpelledNumberExpr),
- Box::new(SequenceExpr::default().then_number()),
- Box::new(IndefiniteArticle::default()),
- ])
- .then_whitespace()
- .then(units);
+ let expr = SequenceExpr::longest_of(vec![
+ Box::new(SpelledNumberExpr),
+ Box::new(SequenceExpr::default().then_number()),
+ Box::new(IndefiniteArticle::default()),
+ ])
+ .then_whitespace()
+ .then(units);
expr.run(cursor, tokens, source)
}
diff --git a/harper-core/src/expr/sequence_expr.rs b/harper-core/src/expr/sequence_expr.rs
index 8467f9660..b39bf43c1 100644
--- a/harper-core/src/expr/sequence_expr.rs
+++ b/harper-core/src/expr/sequence_expr.rs
@@ -133,6 +133,11 @@ impl SequenceExpr {
Self::default().then_any_of(exprs)
}
+ /// Match the longest of multiple expressions.
+ pub fn longest_of(exprs: Vec<Box<dyn Expr>>) -> Self {
+ Self::default().then_longest_of(exprs)
+ }
+
/// Will be accepted unless the condition matches.
pub fn unless(condition: impl Expr + 'static) -> Self {
Self::default().then_unless(condition)
diff --git a/harper-core/src/irregular_verbs.rs b/harper-core/src/irregular_verbs.rs
index 1839b96a7..42f792fc5 100644
--- a/harper-core/src/irregular_verbs.rs
+++ b/harper-core/src/irregular_verbs.rs
@@ -63,6 +63,13 @@ impl IrregularVerbs {
.find(|(_, pt, _)| pt.eq_ignore_ascii_case(preterite))
.map(|(_, _, pp)| pp.as_str())
}
+
+ pub fn get_lemma_for_preterite(&self, preterite: &str) -> Option<&str> {
+ self.verbs
+ .iter()
+ .find(|(_, pt, _)| pt.eq_ignore_ascii_case(preterite))
+ .map(|(lemma, _, _)| lemma.as_str())
+ }
}
impl Default for IrregularVerbs {
diff --git a/harper-core/src/linting/change_tack.rs b/harper-core/src/linting/change_tack.rs
index 2ea40a801..c89d79f60 100644
--- a/harper-core/src/linting/change_tack.rs
+++ b/harper-core/src/linting/change_tack.rs
@@ -18,18 +18,17 @@ impl Default for ChangeTack {
Self {
expr: Box::new(FirstMatchOf::new(vec![
Box::new(
- SequenceExpr::default()
- .then_longest_of(vec![
- Box::new(SequenceExpr::word_set(verb_forms).then_optional(
- SequenceExpr::default().t_ws().then_any_of(vec![
- Box::new(SequenceExpr::default().then_possessive_determiner()),
- Box::new(Word::new("it's")),
- ]),
- )),
- Box::new(SequenceExpr::word_set(noun_forms).t_ws().t_aco("of")),
- ])
- .t_ws()
- .then_word_set(eggcorns),
+ SequenceExpr::longest_of(vec![
+ Box::new(SequenceExpr::word_set(verb_forms).then_optional(
+ SequenceExpr::default().t_ws().then_any_of(vec![
+ Box::new(SequenceExpr::default().then_possessive_determiner()),
+ Box::new(Word::new("it's")),
+ ]),
+ )),
+ Box::new(SequenceExpr::word_set(noun_forms).t_ws().t_aco("of")),
+ ])
+ .t_ws()
+ .then_word_set(eggcorns),
),
Box::new(SequenceExpr::aco("different").t_ws().t_aco("tact")),
])),
diff --git a/harper-core/src/linting/did_past.rs b/harper-core/src/linting/did_past.rs
new file mode 100644
index 000000000..18e3505bd
--- /dev/null
+++ b/harper-core/src/linting/did_past.rs
@@ -0,0 +1,314 @@
+use crate::{
+ CharStringExt, Lint, Token,
+ char_ext::CharExt,
+ expr::{Expr, FixedPhrase, SequenceExpr},
+ irregular_verbs::IrregularVerbs,
+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
+ patterns::WordSet,
+ spell::Dictionary,
+};
+
+pub struct DidPast<D> {
+ expr: Box<dyn Expr>,
+ dict: D,
+}
+
+impl<D> DidPast<D>
+where
+ D: Dictionary,
+{
+ pub fn new(dict: D) -> Self {
+ Self {
+ expr: Box::new(
+ SequenceExpr::longest_of(vec![
+ Box::new(WordSet::new(&["did", "didn't", "didnt"])),
+ Box::new(FixedPhrase::from_phrase("did not")),
+ ])
+ .then_optional(SequenceExpr::default().t_ws().then_subject_pronoun())
+ .t_ws()
+ // Note that 'simple past forms' may apply only to irregular verbs
+ // Note and that 'past forms' applies to regular verbs where preterite and participle share a form
+ .then_kind_where(|k| {
+ (k.is_verb_simple_past_form() || k.is_verb_past_form()) && !k.is_verb_lemma()
+ }),
+ ),
+ dict,
+ }
+ }
+
+ fn keep_suggestion_if_lemma(&self, suggs: &mut Vec<Vec<char>>, candidate: &[char]) {
+ if self
+ .dict
+ .get_word_metadata(candidate)
+ .is_some_and(|md| md.is_verb_lemma())
+ {
+ suggs.push(candidate.to_vec());
+ }
+ }
+}
+
+impl<D> ExprLinter for DidPast<D>
+where
+ D: Dictionary,
+{
+ type Unit = Chunk;
+
+ fn description(&self) -> &str {
+ "Corrects past forms of verbs to their base form, when used together with \"did\"."
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let vspan = toks.last()?.span;
+ let vchars = vspan.get_content(src);
+ let vstr = vspan.get_content_string(src);
+
+ let mut suggs = vec![];
+
+ // Chop -d/-ed off regular verbs
+
+ if vchars.ends_with_ignore_ascii_case_chars(&['d']) {
+ let without_d = &vchars[..vchars.len() - 1];
+
+ if without_d.ends_with_ignore_ascii_case_chars(&['e']) {
+ let without_ed = &without_d[..without_d.len() - 1];
+
+ self.keep_suggestion_if_lemma(&mut suggs, without_ed);
+
+ // If the stem without -ed now ends in -i, try changing that to -y to find the lemma
+ if without_ed.ends_with_ignore_ascii_case_chars(&['i']) {
+ let mut with_final_y = without_ed[..without_ed.len() - 1].to_vec();
+ with_final_y.push('y');
+ self.keep_suggestion_if_lemma(&mut suggs, &with_final_y);
+ }
+
+ // If the stem without -ed ends in a doubled consonant, try with just a single one
+ if without_ed.last().is_some_and(|c| !c.is_vowel()) {
+ let without_doubled_consonant = without_ed[..without_ed.len() - 1].to_vec();
+ self.keep_suggestion_if_lemma(&mut suggs, &without_doubled_consonant);
+ }
+ }
+ self.keep_suggestion_if_lemma(&mut suggs, without_d);
+ }
+
+ // Look up irregular verbs
+
+ if let Some(lemma) = IrregularVerbs::curated().get_lemma_for_preterite(&vstr) {
+ suggs.push(lemma.chars().collect());
+ }
+
+ if !suggs.is_empty() {
+ Some(Lint {
+ span: vspan,
+ lint_kind: LintKind::Redundancy,
+ suggestions: suggs
+ .into_iter()
+ .map(|s| Suggestion::replace_with_match_case(s, vchars))
+ .collect(),
+ message: "Use the base form of the verb with \"did\".".to_string(),
+ ..Default::default()
+ })
+ } else {
+ None
+ }
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::DidPast;
+ use crate::{
+ linting::tests::{assert_no_lints, assert_suggestion_result},
+ spell::FstDictionary,
+ };
+
+ // Test basic 'true positive' regular verb cases
+
+ // Regular verb where past is lemma+ed
+
+ #[test]
+ fn ed_did_forked() {
+ assert_suggestion_result(
+ "Did they forked the repo?",
+ DidPast::new(FstDictionary::curated()),
+ "Did they fork the repo?",
+ );
+ }
+
+ // Regular verb where past is lemma+d
+
+ #[test]
+ fn d_did_used() {
+ assert_suggestion_result(
+ "It didn't used a macro.",
+ DidPast::new(FstDictionary::curated()),
+ "It didn't use a macro.",
+ );
+ }
+
+ // Regular verb where past is lemma -y +ied
+
+ #[test]
+ fn y_did_fried() {
+ assert_suggestion_result(
+ "I hope that didn't fried any chips!",
+ DidPast::new(FstDictionary::curated()),
+ "I hope that didn't fry any chips!",
+ );
+ }
+
+ // Regular verb where past doubled the final consonant
+
+ #[test]
+ fn doubed_consonant_logged() {
+ assert_suggestion_result(
+ "There was a segfault but it did logged the error.",
+ DidPast::new(FstDictionary::curated()),
+ "There was a segfault but it did log the error.",
+ );
+ }
+
+ // Test basic 'true positive' irregular verb cases
+
+ #[test]
+ fn did_past() {
+ assert_suggestion_result("Did went", DidPast::new(FstDictionary::curated()), "Did go");
+ }
+
+ #[test]
+ fn did_past_with_apostrophe() {
+ assert_suggestion_result(
+ "Didn't saw",
+ DidPast::new(FstDictionary::curated()),
+ "Didn't see",
+ );
+ }
+
+ #[test]
+ fn didnt_past_no_apostrophe() {
+ assert_suggestion_result(
+ "Didnt had",
+ DidPast::new(FstDictionary::curated()),
+ "Didnt have",
+ );
+ }
+
+ #[test]
+ fn did_i_heard() {
+ assert_suggestion_result(
+ "Did I heard",
+ DidPast::new(FstDictionary::curated()),
+ "Did I hear",
+ );
+ }
+
+ #[test]
+ fn did_i_heard_with_apostrophe() {
+ assert_suggestion_result(
+ "Didn't we heard",
+ DidPast::new(FstDictionary::curated()),
+ "Didn't we hear",
+ );
+ }
+
+ #[test]
+ fn didnt_i_forgot_no_apostrophe() {
+ assert_suggestion_result(
+ "Didnt he forgot",
+ DidPast::new(FstDictionary::curated()),
+ "Didnt he forget",
+ );
+ }
+
+ // Test basic 'true negative' cases - verb is valid as both lemma and simple past
+
+ #[test]
+ fn ignore_lemma_same_as_past_tense() {
+ assert_no_lints("Did read", DidPast::new(FstDictionary::curated()));
+ }
+
+ // Real-world examples
+
+ #[test]
+ fn fix_did_you_cmae() {
+ assert_suggestion_result(
+ "How did you came to this",
+ DidPast::new(FstDictionary::curated()),
+ "How did you come to this",
+ );
+ }
+
+ #[test]
+ fn fix_did_you_wrote() {
+ assert_suggestion_result(
+ "I'm very interested in the script, if you did wrote it.",
+ DidPast::new(FstDictionary::curated()),
+ "I'm very interested in the script, if you did write it.",
+ );
+ }
+
+ #[test]
+ fn fix_didnt_had() {
+ assert_suggestion_result(
+ "and i DO know that i didnt had any Terracota",
+ DidPast::new(FstDictionary::curated()),
+ "and i DO know that i didnt have any Terracota",
+ );
+ }
+
+ #[test]
+ fn did_you_went() {
+ assert_suggestion_result(
+ "Did you went out of memory maybe?",
+ DidPast::new(FstDictionary::curated()),
+ "Did you go out of memory maybe?",
+ );
+ }
+
+ #[test]
+ fn fix_did_needed() {
+ assert_suggestion_result(
+ "since our CI was broken this did needed to be done",
+ DidPast::new(FstDictionary::curated()),
+ "since our CI was broken this did need to be done",
+ );
+ }
+
+ #[test]
+ fn fix_did_thought() {
+ assert_suggestion_result(
+ "I did thought of adding it as a tooltip on hover",
+ DidPast::new(FstDictionary::curated()),
+ "I did think of adding it as a tooltip on hover",
+ );
+ }
+
+ #[test]
+ fn fix_did_wanted() {
+ assert_suggestion_result(
+ "I did wanted catch all errors in my previous example.",
+ DidPast::new(FstDictionary::curated()),
+ "I did want catch all errors in my previous example.",
+ );
+ }
+
+ #[test]
+ fn fix_did_not_changed() {
+ assert_suggestion_result(
+ "freeing space and reboot frequently did not changed anything",
+ DidPast::new(FstDictionary::curated()),
+ "freeing space and reboot frequently did not change anything",
+ );
+ }
+
+ #[test]
+ fn ignore_did_you_read() {
+ assert_no_lints(
+ "Did You Read the Instructions?",
+ DidPast::new(FstDictionary::curated()),
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 2f9daf33a..ec83ca40c 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -52,6 +52,7 @@ use super::currency_placement::CurrencyPlacement;
use super::day_and_age::DayAndAge;
use super::despite_it_is::DespiteItIs;
use super::despite_of::DespiteOf;
+use super::did_past::DidPast;
use super::didnt::Didnt;
use super::discourse_markers::DiscourseMarkers;
use super::disjoint_prefixes::DisjointPrefixes;
@@ -683,6 +684,9 @@ impl LintGroup {
out.add("WorthToDo", WorthToDo::new(dictionary.clone()));
out.config.set_rule_enabled("WorthToDo", true);
+ out.add_chunk_expr_linter("DidPast", DidPast::new(dictionary.clone()));
+ out.config.set_rule_enabled("DidPast", true);
+
out
}
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 98674dc66..4e463d199 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -47,6 +47,7 @@ mod day_and_age;
mod despite_it_is;
mod despite_of;
mod determiner_without_noun;
+mod did_past;
mod didnt;
mod discourse_markers;
mod disjoint_prefixes;