Skip to content

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

Let me laugh (#3365)

Jason Adams36af6c6b442f
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index 353a82aa7..613c48acd 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -1,4 +1,4 @@
-54700
+54701
# Start of original dictionary import
# combined with dialect spelling dictionary import.
@@ -27753,6 +27753,7 @@ gyve/NgSVGd
h/~NJGnXZvz # removed `>` 'her' is not comparative of 'h'
h'm/
ha/~
+hah/~
habeas corpus/N
haberdasher/NSg
haberdashery/NSg
diff --git a/harper-core/src/linting/informal_laughter.rs b/harper-core/src/linting/informal_laughter.rs
new file mode 100644
index 000000000..9a4301f4c
--- /dev/null
+++ b/harper-core/src/linting/informal_laughter.rs
@@ -0,0 +1,50 @@
+pub fn is_informal_laughter(chars: &[char]) -> bool {
+ if chars.len() < 2 {
+ return false;
+ }
+
+ chars.iter().enumerate().all(|(idx, c)| {
+ if idx % 2 == 0 {
+ matches!(c, 'h' | 'H')
+ } else {
+ matches!(c, 'a' | 'A')
+ }
+ })
+}
+
+#[cfg(test)]
+mod tests {
+ use super::is_informal_laughter;
+
+ fn matches(word: &str) -> bool {
+ is_informal_laughter(&word.chars().collect::<Vec<_>>())
+ }
+
+ #[test]
+ fn matches_alternating_laughter() {
+ for word in ["ha", "hah", "haha", "hahah", "hahaha", "hahahah"] {
+ assert!(matches(word), "{word} should match");
+ }
+ }
+
+ #[test]
+ fn matches_laughter_case_insensitively() {
+ for word in ["Hah", "Hahahah", "HAHAHA", "HaHaH"] {
+ assert!(matches(word), "{word} should match");
+ }
+ }
+
+ #[test]
+ fn does_not_match_malformed_hah_chains() {
+ for word in ["hahhah", "haah", "hahh", "hha"] {
+ assert!(!matches(word), "{word} should not match");
+ }
+ }
+
+ #[test]
+ fn does_not_match_out_of_scope_interjections() {
+ for word in ["ah", "Ah", "ahah", "hehe"] {
+ assert!(!matches(word), "{word} should be ignored by this matcher");
+ }
+ }
+}
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 10fde2cec..09c2386a9 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -103,6 +103,7 @@ mod in_favour_of_doing;
mod in_on_the_cards;
mod in_time_from_now;
mod inflected_verb_after_to;
+mod informal_laughter;
mod initialism_linter;
mod initialisms;
mod interested_in;
diff --git a/harper-core/src/linting/repeated_words.rs b/harper-core/src/linting/repeated_words.rs
index 050c4ef8f..9d89a9765 100644
--- a/harper-core/src/linting/repeated_words.rs
+++ b/harper-core/src/linting/repeated_words.rs
@@ -1,6 +1,7 @@
use super::{Lint, LintKind, Linter, Suggestion};
use crate::TokenStringExt;
use crate::char_string::char_string;
+use crate::linting::informal_laughter::is_informal_laughter;
use crate::{CharString, CharStringExt, Document, Span};
#[derive(Debug, Clone)]
@@ -42,6 +43,10 @@ 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 is_informal_laughter(word_a) && is_informal_laughter(word_b) {
+ continue;
+ }
+
let prev_tok = document.get_token_offset(idx_a, -1);
let next_tok = document.get_token_offset(*idx_b, 1);
@@ -175,4 +180,11 @@ mod tests {
fn dont_flag_hyphenated_either_side() {
assert_lint_count("foo-foo foo bar bar-bar", RepeatedWords::default(), 0);
}
+
+ #[test]
+ fn dont_flag_repeated_laughter() {
+ for source in ["ha ha", "Ha ha", "hah hah", "ha ha ha"] {
+ assert_lint_count(source, RepeatedWords::default(), 0);
+ }
+ }
}
diff --git a/harper-core/src/linting/spell_check.rs b/harper-core/src/linting/spell_check.rs
index 715cc9a9b..ad272c2d8 100644
--- a/harper-core/src/linting/spell_check.rs
+++ b/harper-core/src/linting/spell_check.rs
@@ -3,8 +3,8 @@ use std::num::NonZero;
use lru::LruCache;
use smallvec::ToSmallVec;
-use super::Suggestion;
use super::{Lint, LintKind, Linter};
+use super::{Suggestion, informal_laughter::is_informal_laughter};
use crate::document::Document;
use crate::spell::{Dictionary, suggest_correct_spelling};
use crate::{CharString, CharStringExt, Dialect, TokenStringExt};
@@ -73,6 +73,10 @@ impl<T: Dictionary> Linter for SpellCheck<T> {
for word in document.iter_words() {
let word_chars = document.get_span_content(&word.span);
+ if is_informal_laughter(word_chars) {
+ continue;
+ }
+
if let Some(metadata) = word.kind.as_word().unwrap()
&& metadata.dialects.is_dialect_enabled(self.dialect)
&& (self.dictionary.contains_exact_word(word_chars)
@@ -1007,4 +1011,14 @@ mod tests {
"children's",
);
}
+
+ #[test]
+ fn allows_informal_laughter() {
+ for source in ["hahah", "hahaha", "hahahah", "Hahahah", "HAHAHA"] {
+ assert_no_lints(
+ source,
+ SpellCheck::new(FstDictionary::curated(), Dialect::American),
+ );
+ }
+ }
}
diff --git a/harper-core/src/linting/split_words.rs b/harper-core/src/linting/split_words.rs
index dd1b4d389..98d73ba62 100644
--- a/harper-core/src/linting/split_words.rs
+++ b/harper-core/src/linting/split_words.rs
@@ -6,6 +6,7 @@ use crate::expr::Expr;
use crate::linting::{
ExprLinter, LintKind, Suggestion,
expr_linter::{Chunk, at_start_of_sentence, preceded_by_word},
+ informal_laughter::is_informal_laughter,
};
use crate::spell::{Dictionary, FstDictionary, TrieDictionary};
use crate::{Lint, Token};
@@ -55,6 +56,9 @@ impl ExprLinter for SplitWords {
}
let chars = &word.get_ch(source);
+ if is_informal_laughter(chars) {
+ return None;
+ }
// Get all possible prefix candidates from trie and extract valid split positions
let candidates = self.dict.find_words_with_common_prefix(chars);
@@ -354,4 +358,11 @@ mod tests {
"`thankyou` should probably be written as `thank you`.",
);
}
+
+ #[test]
+ fn allows_informal_laughter() {
+ for source in ["hah", "haha", "hahah", "hahaha", "Hahahah"] {
+ assert_no_lints(source, SplitWords::default());
+ }
+ }
}