Skip to content

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

feat(core): condense multi-token latin words and phrases

Elijah Potter0559558fd18a
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index aa29a4e70..b2ed30c1f 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -49773,3 +49773,6 @@ a8c/SM
a11n/1
a12s/9
intergenerational
+etc.
+vs.
+et al.
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index c3fb7afd3..3ecb6678b 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -5,7 +5,9 @@ use std::fmt::Display;
use paste::paste;
use crate::parsers::{Markdown, MarkdownOptions, Parser, PlainEnglish};
-use crate::patterns::{PatternExt, RepeatingPattern, SequencePattern};
+use crate::patterns::{
+ DocPattern, EitherPattern, Pattern, PatternExt, RepeatingPattern, SequencePattern, WordSet,
+};
use crate::punctuation::Punctuation;
use crate::vec_ext::VecExt;
use crate::{Dictionary, FatToken, FstDictionary, Lrc, Token, TokenKind, TokenStringExt};
@@ -111,6 +113,7 @@ impl Document {
self.condense_contractions();
self.condense_dotted_initialisms();
self.condense_number_suffixes();
+ self.condense_latin();
self.match_quotes();
for token in self.tokens.iter_mut() {
@@ -326,6 +329,44 @@ impl Document {
self.tokens.remove_indices(remove_these);
}
+ thread_local! {
+ static LATIN_PATTERN: Lrc<EitherPattern> = Document::uncached_latin_pattern();
+ }
+
+ fn uncached_latin_pattern() -> Lrc<EitherPattern> {
+ Lrc::new(EitherPattern::new(vec![
+ Box::new(
+ SequencePattern::default()
+ .then_word_set(WordSet::all(&["etc", "vs"]))
+ .then_period(),
+ ),
+ Box::new(
+ SequencePattern::aco("et")
+ .then_whitespace()
+ .t_aco("al")
+ .then_period(),
+ ),
+ ]))
+ }
+
+ /// Assumes that the first matched token is the canonical one to be condensed into.
+ fn condense_pattern(&mut self, pattern: &impl Pattern) {
+ let matches = pattern.find_all_matches_in_doc(self);
+
+ let mut remove_indices = VecDeque::with_capacity(matches.len());
+
+ for m in matches {
+ remove_indices.extend(m.start + 1..m.end);
+ self.tokens[m.start].span.end = self.tokens[m.end - 1].span.end;
+ }
+
+ self.tokens.remove_indices(remove_indices);
+ }
+
+ fn condense_latin(&mut self) {
+ self.condense_pattern(&Self::LATIN_PATTERN.with(|v| v.clone()))
+ }
+
/// Searches for multiple sequential newline tokens and condenses them down
/// into one.
fn condense_newlines(&mut self) {
diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rs
index ce2d042d4..a5f00a89b 100644
--- a/harper-core/src/patterns/mod.rs
+++ b/harper-core/src/patterns/mod.rs
@@ -127,7 +127,7 @@ where
}
}
-trait DocPattern {
+pub trait DocPattern {
fn find_all_matches_in_doc(&self, document: &Document) -> Vec<Span>;
}
diff --git a/harper-core/tests/run_tests.rs b/harper-core/tests/run_tests.rs
index 39f1e511f..81ec966e7 100644
--- a/harper-core/tests/run_tests.rs
+++ b/harper-core/tests/run_tests.rs
@@ -48,6 +48,8 @@ create_test!(amazon_hostname.md, 0);
create_test!(issue_159.md, 1);
create_test!(issue_358.md, 0);
create_test!(issue_195.md, 0);
+create_test!(issue_118.md, 0);
+create_test!(lots_of_latin.md, 0);
// Make sure it doesn't panic
create_test!(lukas_homework.md, 3);
diff --git a/harper-core/tests/test_sources/issue_118.md b/harper-core/tests/test_sources/issue_118.md
new file mode 100644
index 000000000..3f27836fc
--- /dev/null
+++ b/harper-core/tests/test_sources/issue_118.md
@@ -0,0 +1 @@
+This is another sentence that says item 1, item 2, etc. in the middle of the sentence.
diff --git a/harper-core/tests/test_sources/lots_of_latin.md b/harper-core/tests/test_sources/lots_of_latin.md
new file mode 100644
index 000000000..6dc2f8991
--- /dev/null
+++ b/harper-core/tests/test_sources/lots_of_latin.md
@@ -0,0 +1,3 @@
+We had some issues with correctly parsing certain Latin terms.
+
+It caused issues with phrases like, "it was Mike Tyson vs. Weird Al!" and "Mike Tyson et al. wrote this paper," etc., especially for scientific papers.