Skip to content

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

fix(core): detect acronyms in `AnA` (#2072)

Steve Kurch6dd676ef0d23
diff --git a/harper-core/src/char_ext.rs b/harper-core/src/char_ext.rs
index 83879f94b..35b84bf65 100644
--- a/harper-core/src/char_ext.rs
+++ b/harper-core/src/char_ext.rs
@@ -9,6 +9,10 @@ pub trait CharExt {
fn is_english_lingual(&self) -> bool;
fn is_emoji(&self) -> bool;
fn is_punctuation(&self) -> bool;
+ /// Whether the character is an (English) vowel.
+ ///
+ /// Checks whether the character is in the set (A, E, I, O, U); case-insensitive.
+ fn is_vowel(&self) -> bool;
}
impl CharExt for char {
@@ -77,6 +81,10 @@ impl CharExt for char {
fn is_punctuation(&self) -> bool {
Punctuation::from_char(*self).is_some()
}
+
+ fn is_vowel(&self) -> bool {
+ matches!(self.to_ascii_lowercase(), 'a' | 'e' | 'i' | 'o' | 'u')
+ }
}
#[cfg(test)]
diff --git a/harper-core/src/linting/an_a.rs b/harper-core/src/linting/an_a.rs
index 87d6787cd..44656e0ec 100644
--- a/harper-core/src/linting/an_a.rs
+++ b/harper-core/src/linting/an_a.rs
@@ -2,6 +2,7 @@ use std::borrow::Cow;
use itertools::Itertools;
+use crate::char_ext::CharExt;
use crate::linting::{Lint, LintKind, Linter, Suggestion};
use crate::{Document, TokenStringExt};
@@ -98,7 +99,7 @@ fn to_lower_word(word: &[char]) -> Cow<'_, [char]> {
fn starts_with_vowel(word: &[char]) -> bool {
let is_likely_initialism = word.iter().all(|c| !c.is_alphabetic() || c.is_uppercase());
- if is_likely_initialism && !word.is_empty() {
+ if is_likely_initialism && !word.is_empty() && !is_likely_acronym(word) {
return matches!(
word[0],
'A' | 'E' | 'F' | 'H' | 'I' | 'L' | 'M' | 'N' | 'O' | 'R' | 'S' | 'X'
@@ -191,6 +192,17 @@ fn starts_with_vowel(word: &[char]) -> bool {
)
}
+fn is_likely_acronym(word: &[char]) -> bool {
+ // If the first two letters are not consonants, the initialism might be an acronym.
+ // (Like MAC, NASA, LAN, etc.)
+ word.get(..2).is_some_and(|first_chars| {
+ first_chars
+ .iter()
+ .fold(0, |acc, char| acc + !char.is_vowel() as u8)
+ < 2
+ })
+}
+
#[cfg(test)]
mod tests {
use super::AnA;
@@ -289,4 +301,15 @@ mod tests {
fn disallow_a_mp_and_a_mp3() {
assert_lint_count("a MP and a MP3?", AnA, 2);
}
+
+ #[test]
+ fn recognize_acronyms() {
+ assert_lint_count("using a MAC address", AnA, 0);
+ assert_lint_count("a NASA spacecraft", AnA, 0);
+ assert_lint_count("a NAT", AnA, 0);
+ assert_lint_count("a REST API", AnA, 0);
+ assert_lint_count("a LIBERO", AnA, 0);
+ assert_lint_count("a README", AnA, 0);
+ assert_lint_count("a LAN", AnA, 0);
+ }
}