Skip to content

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

feat(core): ignore prepositions as initiators

Elijah Potter4c83096972c8
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index 2d523e632..97d78eff5 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -12354,7 +12354,7 @@ appetizing/514Y
applaud/14ZGDRS
applauder/1M
applause/1M
-apple/14MS
+apple/1MS
applejack/1M
applesauce/1M
applet/1MS
@@ -39101,7 +39101,7 @@ recursion/1S
recuse/4DSG
recyclable/51SM
recycling/14M
-red/514PSM
+red/51PSM
redact/4SDG
redaction/1M
redactor/1SM
@@ -44602,7 +44602,7 @@ survive/4DSGB
survivor/1SM
susceptibility/1SM
susceptible/51I
-sushi/14M
+sushi/1M
suspect/415SMDG
suspected/54U
suspend/4SDRZG
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index fc5a88640..53a2625f0 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -125,6 +125,7 @@ impl Document {
self.condense_ellipsis();
self.condense_latin();
self.match_quotes();
+ self.articles_imply_nouns();
for token in self.tokens.iter_mut() {
if let TokenKind::Word(meta) = &mut token.kind {
@@ -135,6 +136,32 @@ impl Document {
}
}
+ fn uncached_article_pattern() -> Lrc<SequencePattern> {
+ Lrc::new(
+ SequencePattern::default()
+ .then_article()
+ .then_whitespace()
+ .then(|t: &Token, _source: &[char]| t.kind.is_adjective() && t.kind.is_noun())
+ .then_whitespace()
+ .then_noun(),
+ )
+ }
+
+ thread_local! {static ARTICLE_PATTERN: Lrc<SequencePattern> = Document::uncached_article_pattern()}
+
+ /// When a word that is either an adjective or a noun is sandwiched between an article and a noun,
+ /// it definitely is not a noun.
+ fn articles_imply_nouns(&mut self) {
+ let pattern = Self::ELLIPSIS_PATTERN.with(|v| v.clone());
+
+ for m in pattern.find_all_matches_in_doc(self) {
+ if let TokenKind::Word(Some(metadata)) = &mut self.tokens[m.start + 2].kind {
+ metadata.noun = None;
+ metadata.verb = None;
+ }
+ }
+ }
+
/// Convert all sets of newlines greater than 2 to paragraph breaks.
fn newlines_to_breaks(&mut self) {
for token in &mut self.tokens {
diff --git a/harper-core/src/linting/oxford_comma.rs b/harper-core/src/linting/oxford_comma.rs
index ccf2868fc..578483fa4 100644
--- a/harper-core/src/linting/oxford_comma.rs
+++ b/harper-core/src/linting/oxford_comma.rs
@@ -11,23 +11,20 @@ pub struct OxfordComma {
impl OxfordComma {
pub fn new() -> Self {
- Self {
- pattern: {
- let this = {
- let this = SequencePattern::default().then_one_or_more(Box::new(
- SequencePattern::default()
- .then(NounPhrase)
- .then_comma()
- .then_whitespace(),
- ));
- this.then(NounPhrase)
- }
- .then_whitespace()
- .then(WordSet::new(&["and", "or", "nor"]))
- .then_whitespace();
- this.then(NounPhrase)
- },
- }
+ let pattern = SequencePattern::default()
+ .then_one_or_more(Box::new(
+ SequencePattern::default()
+ .then(NounPhrase)
+ .then_comma()
+ .then_whitespace(),
+ ))
+ .then(NounPhrase)
+ .then_whitespace()
+ .then(WordSet::new(&["and", "or", "nor"]))
+ .then_whitespace()
+ .then(NounPhrase);
+
+ Self { pattern }
}
fn match_to_lint(&self, matched_toks: &[Token], _source: &[char]) -> Option<Lint> {
@@ -57,6 +54,19 @@ impl Linter for OxfordComma {
for sentence in document.iter_sentences() {
let mut tok_cursor = 0;
+ if let Some(first) = sentence
+ .first()
+ .and_then(|t| t.kind.as_word().cloned())
+ .flatten()
+ {
+ if first.preposition {
+ tok_cursor = sentence
+ .iter()
+ .position(|t| t.kind.is_comma())
+ .unwrap_or(sentence.iter().len())
+ }
+ }
+
loop {
if tok_cursor >= sentence.len() {
break;
@@ -94,11 +104,6 @@ mod tests {
use super::OxfordComma;
- #[test]
- fn fruits() {
- assert_lint_count("An apple, a banana and a pear", OxfordComma::default(), 1);
- }
-
#[test]
fn people() {
assert_suggestion_result(
@@ -170,4 +175,13 @@ mod tests {
"I like carrots, kale, nor broccoli.",
);
}
+
+ #[test]
+ fn allow_non_list_transportation() {
+ assert_lint_count(
+ "In transportation, autonomous vehicles and smart traffic management systems promise to reduce accidents and optimize travel routes.",
+ OxfordComma::default(),
+ 0,
+ );
+ }
}
diff --git a/harper-core/src/linting/phrase_corrections.rs b/harper-core/src/linting/phrase_corrections.rs
index 830a95442..f7c090b23 100644
--- a/harper-core/src/linting/phrase_corrections.rs
+++ b/harper-core/src/linting/phrase_corrections.rs
@@ -239,7 +239,7 @@ pub fn lint_group() -> LintGroup {
"int he",
["in the"],
"Did you mean 'in the'?",
- "Detects and corrects a spacing error where 'in the' is mistakenly written as 'int he'. Proper spacing is essential for readability and grammatical correctness in common phrases."
+ "Detects and corrects a spacing error where 'in the' is mistakenly written as `int he`. Proper spacing is essential for readability and grammatical correctness in common phrases."
)
});
diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rs
index b43a7684c..3e976fee4 100644
--- a/harper-core/src/patterns/sequence_pattern.rs
+++ b/harper-core/src/patterns/sequence_pattern.rs
@@ -84,6 +84,7 @@ impl SequencePattern {
gen_then_from_is!(hyphen);
gen_then_from_is!(article);
gen_then_from_is!(proper_noun);
+ gen_then_from_is!(preposition);
pub fn then_indefinite_article(self) -> Self {
self.then(IndefiniteArticle::default())