Skip to content

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

fix(#253): exclude homophones from repetition lint

Grant Lemons890c1ff0cd7f
diff --git a/harper-core/src/document.rs b/harper-core/src/document.rs
index 9ec2b3284..c20b765ce 100644
--- a/harper-core/src/document.rs
+++ b/harper-core/src/document.rs
@@ -502,6 +502,7 @@ impl TokenStringExt for Document {
create_fns_on_doc!(sentence_terminator);
create_fns_on_doc!(chunk_terminator);
create_fns_on_doc!(punctuation);
+ create_fns_on_doc!(homophone);
fn first_sentence_word(&self) -> Option<Token> {
self.tokens.first_sentence_word()
diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rs
index 60c7c0410..f2b1e2f11 100644
--- a/harper-core/src/linting/repeated_words.rs
+++ b/harper-core/src/linting/repeated_words.rs
@@ -16,7 +16,7 @@ impl Linter for RepeatedWords {
let word_a = document.get_span_content(tok_a.span);
let word_b = document.get_span_content(tok_b.span);
- if word_a.to_lower() == word_b.to_lower() {
+ if !tok_a.kind.is_homophone() && word_a.to_lower() == word_b.to_lower() {
let intervening_tokens = &chunk[idx_a + 1..*idx_b];
if intervening_tokens.iter().any(|t| !t.kind.is_whitespace()) {
@@ -49,4 +49,10 @@ mod tests {
fn catches_basic() {
assert_lint_count("I wanted the the banana.", RepeatedWords::default(), 1)
}
+
+ #[test]
+ fn does_not_lint_homophones() {
+ assert_lint_count("To address address problems.", RepeatedWords::default(), 0);
+ assert_lint_count("To record record profits.", RepeatedWords::default(), 0);
+ }
}
diff --git a/harper-core/src/token.rs b/harper-core/src/token.rs
index c4316ba23..9df4b1704 100644
--- a/harper-core/src/token.rs
+++ b/harper-core/src/token.rs
@@ -323,6 +323,14 @@ impl TokenKind {
metadata.is_noun()
}
+ pub fn is_homophone(&self) -> bool {
+ let TokenKind::Word(metadata) = self else {
+ return false;
+ };
+
+ metadata.is_homophone()
+ }
+
pub fn is_comma(&self) -> bool {
matches!(self, TokenKind::Punctuation(Punctuation::Comma))
}
@@ -397,6 +405,7 @@ pub trait TokenStringExt {
create_decl_for!(sentence_terminator);
create_decl_for!(chunk_terminator);
create_decl_for!(punctuation);
+ create_decl_for!(homophone);
fn iter_linking_verb_indices(&self) -> impl Iterator<Item = usize> + '_;
fn iter_linking_verbs(&self) -> impl Iterator<Item = Token> + '_;
@@ -429,6 +438,7 @@ impl TokenStringExt for [Token] {
create_fns_for!(unlintable);
create_fns_for!(sentence_terminator);
create_fns_for!(chunk_terminator);
+ create_fns_for!(homophone);
fn first_non_whitespace(&self) -> Option<Token> {
self.iter().find(|t| !t.kind.is_whitespace()).copied()
diff --git a/harper-core/src/word_metadata.rs b/harper-core/src/word_metadata.rs
index ad34cafda..b57abcb44 100644
--- a/harper-core/src/word_metadata.rs
+++ b/harper-core/src/word_metadata.rs
@@ -113,6 +113,41 @@ impl WordMetadata {
)
}
+ /// Checks if the word is a homophone
+ /// This is based off of metadata, so it is not guaranteed to be correct
+ pub fn is_homophone(&self) -> bool {
+ [
+ matches!(
+ self.noun,
+ Some(NounData {
+ is_proper: None,
+ is_plural: None,
+ is_possessive: None,
+ is_pronoun: None
+ })
+ ),
+ matches!(
+ self.verb,
+ Some(VerbData {
+ is_linking: None,
+ tense: None
+ })
+ ),
+ self.is_conjunction(),
+ self.is_adjective(),
+ self.is_adverb(),
+ self.is_possessive_noun(),
+ self.is_plural_noun(),
+ self.is_proper_noun(),
+ self.is_pronoun(),
+ self.is_linking_verb(),
+ ]
+ .iter()
+ .map(|b| *b as u8)
+ .sum::<u8>()
+ > 1
+ }
+
/// Checks whether a word is _definitely_ a swear.
pub fn is_swear(&self) -> bool {
matches!(self.swear, Some(true))