Skip to content

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

feat(core): create linter for discourse markers (#1470)

Elijah Potter7426b8eccbd8
diff --git a/harper-core/src/linting/discourse_markers.rs b/harper-core/src/linting/discourse_markers.rs
new file mode 100644
index 000000000..d24af68ca
--- /dev/null
+++ b/harper-core/src/linting/discourse_markers.rs
@@ -0,0 +1,294 @@
+use harper_brill::UPOS;
+
+use crate::expr::{Expr, FixedPhrase, LongestMatchOf, SequenceExpr};
+use crate::patterns::UPOSSet;
+use crate::{Document, Token, TokenStringExt};
+
+use super::{Lint, LintKind, Linter, Suggestion};
+
+pub struct DiscourseMarkers {
+ expr: SequenceExpr,
+}
+
+impl DiscourseMarkers {
+ pub fn new() -> Self {
+ let phrases = &[
+ "however",
+ "therefore",
+ "meanwhile",
+ "furthermore",
+ "nevertheless",
+ "consequently",
+ "thus",
+ "instead",
+ "moreover",
+ "alternatively",
+ "frankly",
+ "additionally",
+ "subsequently",
+ "accordingly",
+ "otherwise",
+ "incidentally",
+ "conversely",
+ "notwithstanding",
+ "hence",
+ "indeed",
+ "for example",
+ "on the other hand",
+ ];
+
+ let phrases_expr = LongestMatchOf::new(
+ phrases
+ .iter()
+ .map(|text: &&str| Box::new(FixedPhrase::from_phrase(text)) as Box<dyn Expr>)
+ .collect(),
+ );
+
+ Self {
+ expr: SequenceExpr::default()
+ .then(phrases_expr)
+ .t_ws()
+ .if_not_then_step_one(UPOSSet::new(&[UPOS::ADJ, UPOS::ADV, UPOS::ADP])),
+ }
+ }
+
+ fn lint_sentence(&self, sent: &[Token], source: &[char]) -> Option<Lint> {
+ let first_word_idx = sent.iter_word_indices().next()?;
+
+ if let Some(matched_phrase) = self.expr.run(first_word_idx, sent, source) {
+ Some(Lint {
+ span: sent[matched_phrase.start..matched_phrase.end - 2].span()?,
+ lint_kind: LintKind::Punctuation,
+ suggestions: vec![Suggestion::InsertAfter(vec![','])],
+ message: "Discourse markers at the beginning of a sentence should be followed by a comma.".into(),
+ priority: 31,
+ })
+ } else {
+ None
+ }
+ }
+}
+
+impl Default for DiscourseMarkers {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl Linter for DiscourseMarkers {
+ fn lint(&mut self, document: &Document) -> Vec<Lint> {
+ document
+ .iter_sentences()
+ .flat_map(|sent| self.lint_sentence(sent, document.get_source()))
+ .collect()
+ }
+
+ fn description(&self) -> &str {
+ "Flags sentences that begin with a discourse marker but omit the required following comma."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::tests::{assert_no_lints, assert_suggestion_result};
+
+ use super::DiscourseMarkers;
+
+ #[test]
+ fn corrects_frankly() {
+ assert_suggestion_result(
+ "Frankly I think he is wrong.",
+ DiscourseMarkers::default(),
+ "Frankly, I think he is wrong.",
+ );
+ }
+
+ #[test]
+ fn corrects_however() {
+ assert_suggestion_result(
+ "However I disagree with your conclusion.",
+ DiscourseMarkers::default(),
+ "However, I disagree with your conclusion.",
+ );
+ }
+
+ #[test]
+ fn corrects_therefore() {
+ assert_suggestion_result(
+ "Therefore we must act now.",
+ DiscourseMarkers::default(),
+ "Therefore, we must act now.",
+ );
+ }
+
+ #[test]
+ fn corrects_meanwhile() {
+ assert_suggestion_result(
+ "Meanwhile preparations continued in the background.",
+ DiscourseMarkers::default(),
+ "Meanwhile, preparations continued in the background.",
+ );
+ }
+
+ #[test]
+ fn corrects_furthermore() {
+ assert_suggestion_result(
+ "Furthermore this approach reduces complexity.",
+ DiscourseMarkers::default(),
+ "Furthermore, this approach reduces complexity.",
+ );
+ }
+
+ #[test]
+ fn corrects_nevertheless() {
+ assert_suggestion_result(
+ "Nevertheless we persevered despite the odds.",
+ DiscourseMarkers::default(),
+ "Nevertheless, we persevered despite the odds.",
+ );
+ }
+
+ #[test]
+ fn corrects_consequently() {
+ assert_suggestion_result(
+ "Consequently the system halted unexpectedly.",
+ DiscourseMarkers::default(),
+ "Consequently, the system halted unexpectedly.",
+ );
+ }
+
+ #[test]
+ fn corrects_thus() {
+ assert_suggestion_result(
+ "Thus we arrive at the final verdict.",
+ DiscourseMarkers::default(),
+ "Thus, we arrive at the final verdict.",
+ );
+ }
+
+ #[test]
+ fn allows_thus_far() {
+ assert_no_lints(
+ "Thus far there have been no problems.",
+ DiscourseMarkers::default(),
+ );
+ }
+
+ #[test]
+ fn corrects_instead() {
+ assert_suggestion_result(
+ "Instead he chose a different path.",
+ DiscourseMarkers::default(),
+ "Instead, he chose a different path.",
+ );
+ }
+
+ #[test]
+ fn corrects_moreover() {
+ assert_suggestion_result(
+ "Moreover this solution is more efficient.",
+ DiscourseMarkers::default(),
+ "Moreover, this solution is more efficient.",
+ );
+ }
+
+ #[test]
+ fn corrects_alternatively() {
+ assert_suggestion_result(
+ "Alternatively we could defer the decision.",
+ DiscourseMarkers::default(),
+ "Alternatively, we could defer the decision.",
+ );
+ }
+
+ #[test]
+ fn no_suggestion_if_comma_present() {
+ assert_no_lints(
+ "However, I disagree with your point.",
+ DiscourseMarkers::default(),
+ );
+ }
+
+ #[test]
+ fn no_lint_for_mid_sentence_marker() {
+ assert_no_lints(
+ "I said however I would consider it.",
+ DiscourseMarkers::default(),
+ );
+ }
+
+ #[test]
+ fn preserves_whitespace() {
+ assert_suggestion_result(
+ "However I disagree.",
+ DiscourseMarkers::default(),
+ "However, I disagree.",
+ );
+ }
+
+ #[test]
+ fn corrects_semicolon_case() {
+ assert_suggestion_result(
+ "However I disagree.",
+ DiscourseMarkers::default(),
+ "However, I disagree.",
+ );
+ }
+
+ #[test]
+ fn corrects_multiple_sentences() {
+ assert_suggestion_result(
+ "However I disagree. Therefore I propose an alternative.",
+ DiscourseMarkers::default(),
+ "However, I disagree. Therefore, I propose an alternative.",
+ );
+ }
+
+ #[test]
+ fn allows_single_word_sentence() {
+ assert_no_lints("Thus", DiscourseMarkers::default());
+ }
+
+ #[test]
+ fn corrects_for_example() {
+ assert_suggestion_result(
+ "For example I recommend updating the configuration.",
+ DiscourseMarkers::default(),
+ "For example, I recommend updating the configuration.",
+ );
+ }
+
+ #[test]
+ fn no_suggestion_if_comma_after_for_example() {
+ assert_no_lints(
+ "For example, I recommend updating the configuration.",
+ DiscourseMarkers::default(),
+ );
+ }
+
+ #[test]
+ fn preserves_whitespace_for_example() {
+ assert_suggestion_result(
+ "For example the outcome was unexpected.",
+ DiscourseMarkers::default(),
+ "For example, the outcome was unexpected.",
+ );
+ }
+
+ #[test]
+ fn corrects_on_the_other_hand() {
+ assert_suggestion_result(
+ "On the other hand we could delay the deployment.",
+ DiscourseMarkers::default(),
+ "On the other hand, we could delay the deployment.",
+ );
+ }
+
+ #[test]
+ fn no_lint_for_mid_sentence_on_the_other_hand() {
+ assert_no_lints(
+ "We might postpone, on the other hand this introduces risk.",
+ DiscourseMarkers::default(),
+ );
+ }
+}
diff --git a/harper-core/src/linting/hope_youre.rs b/harper-core/src/linting/hope_youre.rs
new file mode 100644
index 000000000..ec6241048
--- /dev/null
+++ b/harper-core/src/linting/hope_youre.rs
@@ -0,0 +1,167 @@
+use crate::{
+ Token,
+ expr::SequenceExpr,
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+ patterns::WordSet,
+};
+
+pub struct HopeYoure {
+ expr: Box<dyn crate::expr::Expr>,
+}
+
+impl Default for HopeYoure {
+ fn default() -> Self {
+ let loc = WordSet::new(&["here", "there"]);
+
+ let prep = SequenceExpr::default().t_ws().then_preposition();
+
+ let expr = SequenceExpr::aco("hope")
+ .t_ws()
+ .t_aco("your")
+ .t_ws()
+ .then_adjective()
+ .then_optional(prep)
+ .t_ws()
+ .then(loc);
+
+ Self {
+ expr: Box::new(expr),
+ }
+ }
+}
+
+impl ExprLinter for HopeYoure {
+ fn expr(&self) -> &dyn crate::expr::Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let your_tok = toks.get(2)?;
+ let span = your_tok.span;
+ let original = span.get_content(src);
+
+ Some(Lint {
+ span,
+ lint_kind: LintKind::WordChoice,
+ suggestions: vec![Suggestion::replace_with_match_case(
+ "you're".chars().collect(),
+ original,
+ )],
+ message: "Prefer `you're`—the contraction of “you are”—when expressing a hope about someone’s condition."
+ .into(),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Detects the misuse of possessive **your** after “hope” and an adjective \
+ (e.g., “I hope your well”) and advises using **you’re** to supply the \
+ missing verb “are.”"
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::HopeYoure;
+ use crate::linting::tests::{assert_lint_count, assert_suggestion_result};
+
+ #[test]
+ fn corrects_excited_here() {
+ assert_suggestion_result(
+ "I hope your excited here.",
+ HopeYoure::default(),
+ "I hope you're excited here.",
+ );
+ }
+
+ #[test]
+ fn corrects_safe_there() {
+ assert_suggestion_result(
+ "I hope your safe there.",
+ HopeYoure::default(),
+ "I hope you're safe there.",
+ );
+ }
+
+ #[test]
+ fn corrects_safe_over_there() {
+ assert_suggestion_result(
+ "I hope your safe over there.",
+ HopeYoure::default(),
+ "I hope you're safe over there.",
+ );
+ }
+
+ #[test]
+ fn corrects_fine_here() {
+ assert_suggestion_result(
+ "I hope your fine here.",
+ HopeYoure::default(),
+ "I hope you're fine here.",
+ );
+ }
+
+ #[test]
+ fn corrects_happy_there() {
+ assert_suggestion_result(
+ "I hope your happy there.",
+ HopeYoure::default(),
+ "I hope you're happy there.",
+ );
+ }
+
+ #[test]
+ fn corrects_healthy_out_here() {
+ assert_suggestion_result(
+ "I hope your healthy out here.",
+ HopeYoure::default(),
+ "I hope you're healthy out here.",
+ );
+ }
+
+ #[test]
+ fn corrects_strong_there() {
+ assert_suggestion_result(
+ "We hope your strong there.",
+ HopeYoure::default(),
+ "We hope you're strong there.",
+ );
+ }
+
+ #[test]
+ fn corrects_sorry_here() {
+ assert_suggestion_result(
+ "Hope your sorry here.",
+ HopeYoure::default(),
+ "Hope you're sorry here.",
+ );
+ }
+
+ #[test]
+ fn no_lint_with_contraction() {
+ assert_lint_count("I hope you're excited here.", HopeYoure::default(), 0);
+ }
+
+ #[test]
+ fn no_lint_without_adjective() {
+ assert_lint_count("I hope your trip went well.", HopeYoure::default(), 0);
+ }
+
+ #[test]
+ fn no_lint_with_following_clause() {
+ assert_lint_count(
+ "I hope your friends are well there.",
+ HopeYoure::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn no_lint_when_possessive_context() {
+ assert_lint_count(
+ "I hope your advice on punctuation is helpful.",
+ HopeYoure::default(),
+ 0,
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 1b4aa026f..c93d967eb 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -29,6 +29,7 @@ use super::compound_nouns::CompoundNouns;
use super::confident::Confident;
use super::correct_number_suffix::CorrectNumberSuffix;
use super::despite_of::DespiteOf;
+use super::discourse_markers::DiscourseMarkers;
use super::dot_initialisms::DotInitialisms;
use super::ellipsis_length::EllipsisLength;
use super::else_possessive::ElsePossessive;
@@ -381,6 +382,7 @@ impl LintGroup {
insert_pattern_rule!(BoringWords, false);
insert_struct_rule!(CapitalizePersonalPronouns, true);
insert_pattern_rule!(ChockFull, true);
+ insert_struct_rule!(DiscourseMarkers, true);
insert_pattern_rule!(WayTooAdjective, true);
insert_pattern_rule!(HavePronoun, true);
insert_pattern_rule!(PronounInflectionBe, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 91992c3a9..7fc606c99 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -23,6 +23,7 @@ mod correct_number_suffix;
mod currency_placement;
mod dashes;
mod despite_of;
+mod discourse_markers;
mod dot_initialisms;
mod ellipsis_length;
mod else_possessive;
@@ -36,6 +37,7 @@ mod have_pronoun;
mod hedging;
mod hereby;
mod hop_hope;
+mod hope_youre;
mod how_to;
mod hyphenate_number_day;
mod in_on_the_cards;
@@ -128,6 +130,7 @@ pub use correct_number_suffix::CorrectNumberSuffix;
pub use currency_placement::CurrencyPlacement;
pub use dashes::Dashes;
pub use despite_of::DespiteOf;
+pub use discourse_markers::DiscourseMarkers;
pub use dot_initialisms::DotInitialisms;
pub use ellipsis_length::EllipsisLength;
pub use everyday::Everyday;
@@ -240,6 +243,11 @@ pub mod tests {
use super::Linter;
use crate::{Document, FstDictionary, parsers::PlainEnglish};
+ #[track_caller]
+ pub fn assert_no_lints(text: &str, mut linter: impl Linter) {
+ assert_lint_count(text, linter, 0);
+ }
+
#[track_caller]
pub fn assert_lint_count(text: &str, mut linter: impl Linter, count: usize) {
let test = Document::new_markdown_default_curated(text);
diff --git a/harper-core/src/linting/phrase_corrections/mod.rs b/harper-core/src/linting/phrase_corrections/mod.rs
index 5b00a4200..a092c93c9 100644
--- a/harper-core/src/linting/phrase_corrections/mod.rs
+++ b/harper-core/src/linting/phrase_corrections/mod.rs
@@ -1357,7 +1357,20 @@ pub fn lint_group() -> LintGroup {
["thanks lot", "thanks alot"],
["thanks a lot"],
"Prefer the two-word expression `thanks a lot`.",
- "`Thanks a lot` is the fixed, widely accepted form, while variants like `thanks lot` or `thanks alot` are non-standard and can jar readers.")
+ "`Thanks a lot` is the fixed, widely accepted form, while variants like `thanks lot` or `thanks alot` are non-standard and can jar readers."
+ ),
+ "WroughtIron" => (
+ ["rod iron", "rot iron", "rod-iron", "rot-iron"],
+ ["wrought iron"],
+ "Prefer the standard term `wrought iron`.",
+ "`Wrought iron` is low-carbon, malleable iron used for decorative work; variants like `rod iron` or `rot iron` are phonetic misspellings that may confuse readers."
+ ),
+ "Beforehand" => (
+ ["before hand", "before-hand"],
+ ["beforehand"],
+ "Prefer the single-word adverb `beforehand`.",
+ "`Beforehand` functions as a fixed adverb meaning ‘in advance’; writing it as two words or with a hyphen is nonstandard and can jar readers."
+ ),
});
group.set_all_rules_to(Some(true));
diff --git a/harper-core/src/linting/phrase_corrections/tests.rs b/harper-core/src/linting/phrase_corrections/tests.rs
index 7c67ea148..3f8df570f 100644
--- a/harper-core/src/linting/phrase_corrections/tests.rs
+++ b/harper-core/src/linting/phrase_corrections/tests.rs
@@ -2054,3 +2054,49 @@ fn thanks_alot() {
fn thanks_a_lot_clean() {
assert_lint_count("thanks a lot", lint_group(), 0);
}
+
+#[test]
+fn corrects_rod_iron() {
+ assert_suggestion_result(
+ "The gate was crafted from rod iron.",
+ lint_group(),
+ "The gate was crafted from wrought iron.",
+ );
+}
+
+#[test]
+fn corrects_rot_iron() {
+ assert_suggestion_result(
+ "The artisan works in rot iron.",
+ lint_group(),
+ "The artisan works in wrought iron.",
+ );
+}
+
+#[test]
+fn allows_wrought_iron() {
+ assert_lint_count("She specialized in wrought iron artwork.", lint_group(), 0);
+}
+
+#[test]
+fn corrects_before_hand() {
+ assert_suggestion_result(
+ "Let me know before hand if you will attend.",
+ lint_group(),
+ "Let me know beforehand if you will attend.",
+ );
+}
+
+#[test]
+fn corrects_before_hand_hyphen() {
+ assert_suggestion_result(
+ "I prepared the documents before-hand.",
+ lint_group(),
+ "I prepared the documents beforehand.",
+ );
+}
+
+#[test]
+fn allows_beforehand() {
+ assert_lint_count("We finished the preparations beforehand.", lint_group(), 0);
+}
diff --git a/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml b/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml
index 946c211bf..c86533ef9 100644
--- a/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml
+++ b/harper-core/tests/text/linters/Alice's Adventures in Wonderland.snap.yml
@@ -1523,6 +1523,15 @@ Suggest:
+Lint: Punctuation (31 priority)
+Message: |
+ 1304 | I’m angry. Therefore I’m mad.”
+ | ^~~~~~~~~ Discourse markers at the beginning of a sentence should be followed by a comma.
+Suggest:
+ - Insert “,”
+
+
+
Lint: Readability (127 priority)
Message: |
1348 | like ears and the roof was thatched with fur. It was so large a house, that she
diff --git a/harper-core/tests/text/linters/Computer science.snap.yml b/harper-core/tests/text/linters/Computer science.snap.yml
index 30cd1a4f8..ea21c1ec7 100644
--- a/harper-core/tests/text/linters/Computer science.snap.yml
+++ b/harper-core/tests/text/linters/Computer science.snap.yml
@@ -972,6 +972,15 @@ Suggest:
+Lint: Punctuation (31 priority)
+Message: |
+ 360 | maintenance. For example software testing, systems engineering, technical debt
+ | ^~~~~~~~~~~ Discourse markers at the beginning of a sentence should be followed by a comma.
+Suggest:
+ - Insert “,”
+
+
+
Lint: Spelling (63 priority)
Message: |
371 | semiotics, electrical engineering, philosophy of mind, neurophysiology, and
@@ -1188,3 +1197,13 @@ Suggest:
+Lint: Punctuation (31 priority)
+Message: |
+ 490 | the data fields of the object with which they are associated. Thus
+ | ^~~~ Discourse markers at the beginning of a sentence should be followed by a comma.
+ 491 | object-oriented computer programs are made out of objects that interact with
+Suggest:
+ - Insert “,”
+
+
+
diff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.yml
index 7d99ab23d..987cdfae0 100644
--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml
+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml
@@ -1191,6 +1191,15 @@ Message: |
+Lint: Punctuation (31 priority)
+Message: |
+ 853 | milk all afternoon. Meanwhile Tom brought out a bottle of whiskey from a locked
+ | ^~~~~~~~~ Discourse markers at the beginning of a sentence should be followed by a comma.
+Suggest:
+ - Insert “,”
+
+
+
Lint: Readability (127 priority)
Message: |
860 | and I went out to buy some at the drugstore on the corner. When I came back they
@@ -2232,6 +2241,16 @@ Message: |
+Lint: Punctuation (31 priority)
+Message: |
+ 1824 | perspiration appeared on her upper lip. Nevertheless there was a vague
+ | ^~~~~~~~~~~~ Discourse markers at the beginning of a sentence should be followed by a comma.
+ 1825 | understanding that had to be tactfully broken off before I was free.
+Suggest:
+ - Insert “,”
+
+
+
Lint: Miscellaneous (31 priority)
Message: |
1827 | Every one suspects himself of at least one of the cardinal virtues, and this is
@@ -4402,6 +4421,15 @@ Suggest:
+Lint: Punctuation (31 priority)
+Message: |
+ 3111 | faintly true. Moreover he told it to me at a time of confusion, when I had
+ | ^~~~~~~~ Discourse markers at the beginning of a sentence should be followed by a comma.
+Suggest:
+ - Insert “,”
+
+
+
Lint: Readability (127 priority)
Message: |
3116 | It was a halt, too, in my association with his affairs. For several weeks I
@@ -5041,6 +5069,15 @@ Suggest:
+Lint: Punctuation (31 priority)
+Message: |
+ 3864 | “Nevertheless he’s an Oxford man.”
+ | ^~~~~~~~~~~~ Discourse markers at the beginning of a sentence should be followed by a comma.
+Suggest:
+ - Insert “,”
+
+
+
Lint: Spelling (63 priority)
Message: |
3874 | while in silence. Then as Doctor T. J. Eckleburg’s faded eyes came into sight
@@ -7464,6 +7501,15 @@ Suggest:
+Lint: Punctuation (31 priority)
+Message: |
+ 5721 | “Nevertheless you did throw me over,” said Jordan suddenly. ‘‘You threw me over
+ | ^~~~~~~~~~~~ Discourse markers at the beginning of a sentence should be followed by a comma.
+Suggest:
+ - Insert “,”
+
+
+
Lint: Readability (127 priority)
Message: |
5743 | One afternoon late in October I saw Tom Buchanan. He was walking ahead of me