Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
refactor: Rename `ACO` to `Word` (#1149)
Michael Schmidtf37fcd2cdef6
diff --git a/harper-core/src/linting/confident.rs b/harper-core/src/linting/confident.rsindex c4cb8aff2..7150131a0 100644--- a/harper-core/src/linting/confident.rs+++ b/harper-core/src/linting/confident.rs@@ -1,7 +1,6 @@use crate::{Token,- char_string::char_string,- patterns::{AnyCapitalization, OwnedPatternExt, Pattern, SequencePattern},+ patterns::{OwnedPatternExt, Pattern, SequencePattern, Word},};use super::{Lint, LintKind, PatternLinter, Suggestion};@@ -15,7 +14,7 @@ impl Default for Confident {let pattern = SequencePattern::default().then((|tok: &Token, _source: &[char]| tok.kind.is_verb() || tok.kind.is_determiner())- .or(Box::new(AnyCapitalization::new(char_string!("very")))),+ .or(Box::new(Word::new("very"))),).then_whitespace().t_aco("confidant");diff --git a/harper-core/src/linting/for_noun.rs b/harper-core/src/linting/for_noun.rsindex dbfb4eb12..6e17f710e 100644--- a/harper-core/src/linting/for_noun.rs+++ b/harper-core/src/linting/for_noun.rs@@ -1,6 +1,6 @@use crate::{Token,- patterns::{AnyCapitalization, NominalPhrase, OwnedPatternExt, Pattern, SequencePattern},+ patterns::{NominalPhrase, OwnedPatternExt, Pattern, SequencePattern, Word},};use super::{Lint, LintKind, PatternLinter, Suggestion};@@ -13,7 +13,7 @@ impl Default for ForNoun {fn default() -> Self {let pattern = SequencePattern::aco("fro").then_whitespace()- .then(NominalPhrase.or(Box::new(AnyCapitalization::of("sure"))));+ .then(NominalPhrase.or(Box::new(Word::new("sure"))));Self {pattern: Box::new(pattern),diff --git a/harper-core/src/linting/the_my.rs b/harper-core/src/linting/the_my.rsindex 698dd453a..f1253e746 100644--- a/harper-core/src/linting/the_my.rs+++ b/harper-core/src/linting/the_my.rs@@ -1,7 +1,7 @@use super::{Lint, LintKind, PatternLinter, Suggestion};use crate::{CharStringExt, Token, TokenStringExt,- patterns::{AnyCapitalization, EitherPattern, Pattern, SequencePattern, WordSet},+ patterns::{EitherPattern, Pattern, SequencePattern, Word, WordSet},};pub struct TheMy {@@ -10,7 +10,7 @@ pub struct TheMy {impl Default for TheMy {fn default() -> Self {- let the = AnyCapitalization::of("the");+ let the = Word::new("the");let any_possessive = WordSet::new(&["my", "your", "his", "her", "its", "our", "their"]);let the_poss = SequencePattern::default()diff --git a/harper-core/src/linting/then_than.rs b/harper-core/src/linting/then_than.rsindex 4bf9f4fb2..5130b0b4f 100644--- a/harper-core/src/linting/then_than.rs+++ b/harper-core/src/linting/then_than.rs@@ -2,8 +2,7 @@ use super::{Lint, LintKind, PatternLinter};use crate::Token;use crate::linting::Suggestion;use crate::patterns::{- All, AnyCapitalization, EitherPattern, Invert, OwnedPatternExt, Pattern, SequencePattern,- WordSet,+ All, EitherPattern, Invert, OwnedPatternExt, Pattern, SequencePattern, Word, WordSet,};#[doc = "Corrects the misuse of `then` to `than`."]@@ -19,7 +18,7 @@ impl ThenThan {// Comparative form of adjectiveBox::new(SequencePattern::default()- .then(AnyCapitalization::of("other").or(Box::new(+ .then(Word::new("other").or(Box::new(|tok: &Token, source: &[char]| {is_comparative_adjective(tok, source)},@@ -27,7 +26,7 @@ impl ThenThan {.then_whitespace().then_any_capitalization_of("then").then_whitespace()- .then(Invert::new(AnyCapitalization::of("that"))),+ .then(Invert::new(Word::new("that"))),),// Positive form of adjective following "more" or "less"Box::new(@@ -38,7 +37,7 @@ impl ThenThan {.then_whitespace().then_any_capitalization_of("then").then_whitespace()- .then(Invert::new(AnyCapitalization::of("that"))),+ .then(Invert::new(Word::new("that"))),),])),// Exceptions to the rule.diff --git a/harper-core/src/linting/widely_accepted.rs b/harper-core/src/linting/widely_accepted.rsindex 0d7554107..2bb36df61 100644--- a/harper-core/src/linting/widely_accepted.rs+++ b/harper-core/src/linting/widely_accepted.rs@@ -1,7 +1,7 @@use crate::{Token,linting::{Lint, LintKind, PatternLinter, Suggestion},- patterns::{AnyCapitalization, Pattern, SequencePattern, WordSet},+ patterns::{Pattern, SequencePattern, Word, WordSet},};pub struct WidelyAccepted {@@ -11,7 +11,7 @@ pub struct WidelyAccepted {impl Default for WidelyAccepted {fn default() -> Self {let pattern = SequencePattern::default()- .then(AnyCapitalization::new("wide".chars().collect()))+ .then(Word::new("wide")).then_whitespace().then(WordSet::new(&["accepted", "acceptable", "used"]));diff --git a/harper-core/src/patterns/any_capitalization.rs b/harper-core/src/patterns/any_capitalization.rsdeleted file mode 100644index 67cecab25..000000000--- a/harper-core/src/patterns/any_capitalization.rs+++ /dev/null@@ -1,46 +0,0 @@-use std::num::NonZeroUsize;--use crate::{CharString, Token};--use super::Pattern;--/// A [`Pattern`] that matches any capitalization of a provided word.-#[derive(Clone)]-pub struct AnyCapitalization {- word: CharString,-}--impl AnyCapitalization {- pub fn new(word: CharString) -> Self {- Self { word }- }-- pub fn of(word: &str) -> Self {- let chars = word.chars().collect();-- Self::new(chars)- }-}--impl Pattern for AnyCapitalization {- fn matches(&self, tokens: &[Token], source: &[char]) -> Option<NonZeroUsize> {- let tok = tokens.first()?;-- if !tok.kind.is_word() {- return None;- }-- if tok.span.len() != self.word.len() {- return None;- }-- let tok_chars = tok.span.get_content(source);-- let partial_match = tok_chars- .iter()- .zip(&self.word)- .all(|(a, b)| a.eq_ignore_ascii_case(b));-- NonZeroUsize::new(if partial_match { 1 } else { 0 })- }-}diff --git a/harper-core/src/patterns/exact_phrase.rs b/harper-core/src/patterns/exact_phrase.rsindex 9eb543cd2..eb40061c9 100644--- a/harper-core/src/patterns/exact_phrase.rs+++ b/harper-core/src/patterns/exact_phrase.rs@@ -2,7 +2,7 @@ use std::num::NonZeroUsize;use crate::{Document, Token, TokenKind};-use super::{AnyCapitalization, Pattern, SequencePattern};+use super::{Pattern, SequencePattern, Word};pub struct ExactPhrase {inner: SequencePattern,@@ -20,7 +20,7 @@ impl ExactPhrase {for token in doc.fat_tokens() {match token.kind {TokenKind::Word(_word_metadata) => {- phrase = phrase.then(AnyCapitalization::new(token.content.as_slice().into()));+ phrase = phrase.then(Word::from_chars(token.content.as_slice()));}TokenKind::Space(_) => {phrase = phrase.then_whitespace();diff --git a/harper-core/src/patterns/mod.rs b/harper-core/src/patterns/mod.rsindex bc75e251e..3f5de927b 100644--- a/harper-core/src/patterns/mod.rs+++ b/harper-core/src/patterns/mod.rs@@ -10,7 +10,6 @@ use std::{collections::VecDeque, num::NonZeroUsize};use crate::{Document, Span, Token, VecExt};mod all;-mod any_capitalization;mod any_pattern;mod either_pattern;mod exact_phrase;@@ -27,11 +26,11 @@ mod similar_to_phrase;mod split_compound_word;mod whitespace_pattern;mod within_edit_distance;+mod word;mod word_pattern_group;mod word_set;pub use all::All;-pub use any_capitalization::AnyCapitalization;pub use any_pattern::AnyPattern;use blanket::blanket;pub use either_pattern::EitherPattern;@@ -48,6 +47,7 @@ pub use sequence_pattern::SequencePattern;pub use similar_to_phrase::SimilarToPhrase;pub use split_compound_word::SplitCompoundWord;pub use whitespace_pattern::WhitespacePattern;+pub use word::Word;pub use word_pattern_group::WordPatternGroup;pub use word_set::WordSet;diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rsindex abfd2817d..959fc45e1 100644--- a/harper-core/src/patterns/sequence_pattern.rs+++ b/harper-core/src/patterns/sequence_pattern.rs@@ -3,7 +3,7 @@ use std::num::NonZeroUsize;use paste::paste;use super::whitespace_pattern::WhitespacePattern;-use super::{AnyCapitalization, AnyPattern, IndefiniteArticle, Pattern, RepeatingPattern};+use super::{AnyPattern, IndefiniteArticle, Pattern, RepeatingPattern, Word};use crate::{Token, TokenKind};/// A pattern that checks that a sequence of other patterns match.@@ -94,25 +94,7 @@ impl SequencePattern {}pub fn then_exact_word(mut self, word: &'static str) -> Self {- self.token_patterns- .push(Box::new(|tok: &Token, source: &[char]| {- if !tok.kind.is_word() {- return false;- }-- let tok_chars = tok.span.get_content(source);-- let mut w_char_count = 0;- for (i, w_char) in word.chars().enumerate() {- w_char_count += 1;-- if tok_chars.get(i).cloned() != Some(w_char) {- return false;- }- }-- w_char_count == tok_chars.len()- }));+ self.token_patterns.push(Box::new(Word::new_exact(word)));self}@@ -132,8 +114,7 @@ impl SequencePattern {/// Match examples of `word` that have any capitalization.pub fn then_any_capitalization_of(mut self, word: &'static str) -> Self {- self.token_patterns- .push(Box::new(AnyCapitalization::of(word)));+ self.token_patterns.push(Box::new(Word::new(word)));self}diff --git a/harper-core/src/patterns/similar_to_phrase.rs b/harper-core/src/patterns/similar_to_phrase.rsindex 10fdd1c31..1577a4388 100644--- a/harper-core/src/patterns/similar_to_phrase.rs+++ b/harper-core/src/patterns/similar_to_phrase.rs@@ -2,9 +2,7 @@ use std::num::NonZeroUsize;use crate::{Document, Token, TokenKind};-use super::{- AnyCapitalization, Pattern, SequencePattern, within_edit_distance::WithinEditDistance,-};+use super::{Pattern, SequencePattern, Word, within_edit_distance::WithinEditDistance};pub struct SimilarToPhrase {phrase: SequencePattern,@@ -37,7 +35,7 @@ impl SimilarToPhrase {for token in document.fat_tokens() {match token.kind {TokenKind::Word(_word_metadata) => {- phrase = phrase.then(AnyCapitalization::new(token.content.as_slice().into()));+ phrase = phrase.then(Word::from_chars(token.content.as_slice()));fuzzy_phrase = fuzzy_phrase.then(WithinEditDistance::new(token.content.into(), max_edit_dist));}diff --git a/harper-core/src/patterns/word.rs b/harper-core/src/patterns/word.rsnew file mode 100644index 000000000..53a098e77--- /dev/null+++ b/harper-core/src/patterns/word.rs@@ -0,0 +1,98 @@+use std::num::NonZeroUsize;++use super::Pattern;++use crate::{CharString, Token};++/// Matches a predefined word.+///+/// Note that any capitalization of the contained words will result in a match.+#[derive(Clone)]+pub struct Word {+ word: CharString,+ exact: bool,+}++impl Word {+ /// Matches the provided word, ignoring case.+ pub fn new(word: &'static str) -> Self {+ Self {+ word: word.chars().collect(),+ exact: false,+ }+ }+ /// Matches the provided word, ignoring case.+ pub fn from_chars(word: &[char]) -> Self {+ Self {+ word: word.iter().copied().collect(),+ exact: false,+ }+ }++ /// Matches the provided word, case-sensitive.+ pub fn new_exact(word: &'static str) -> Self {+ Self {+ word: word.chars().collect(),+ exact: true,+ }+ }+}++impl Pattern for Word {+ fn matches(&self, tokens: &[Token], source: &[char]) -> Option<NonZeroUsize> {+ let tok = tokens.first()?;+ if !tok.kind.is_word() {+ return None;+ }+ if tok.span.len() != self.word.len() {+ return None;+ }++ let chars = tok.span.get_content(source);+ let eq = if self.exact {+ chars == self.word.as_slice()+ } else {+ chars+ .iter()+ .zip(&self.word)+ .all(|(a, b)| a.eq_ignore_ascii_case(b))+ };++ NonZeroUsize::new(if eq { 1 } else { 0 })+ }+}++#[cfg(test)]+mod tests {+ use crate::{Document, Span, patterns::DocPattern};++ use super::Word;++ #[test]+ fn fruit() {+ let doc = Document::new_markdown_default_curated("I ate a banana and an apple today.");++ assert_eq!(+ Word::new("banana").find_all_matches_in_doc(&doc),+ vec![Span::new(6, 7)]+ );+ assert_eq!(+ Word::new_exact("banana").find_all_matches_in_doc(&doc),+ vec![Span::new(6, 7)]+ );+ }++ #[test]+ fn fruit_whack_capitalization() {+ let doc = Document::new_markdown_default_curated("I Ate A bAnaNa And aN apPlE today.");++ assert_eq!(+ Word::new("banana").find_all_matches_in_doc(&doc),+ vec![Span::new(6, 7)]+ );+ assert_eq!(+ Word::new_exact("banana").find_all_matches_in_doc(&doc),+ vec![]+ );+ }+}diff --git a/harper-core/src/patterns/word_set.rs b/harper-core/src/patterns/word_set.rsindex cbb892004..e60961b80 100644--- a/harper-core/src/patterns/word_set.rs+++ b/harper-core/src/patterns/word_set.rs@@ -5,10 +5,10 @@ use smallvec::SmallVec;use crate::{CharString, Token};-// A [`Pattern`] that matches against any of a set of provided words.-// For small sets of short words, it doesn't allocate.-//-// Note that any capitalization of the contained words will result in a match.+/// A [`Pattern`] that matches against any of a set of provided words.+/// For small sets of short words, it doesn't allocate.+///+/// Note that any capitalization of the contained words will result in a match.#[derive(Debug, Default, Clone)]pub struct WordSet {words: SmallVec<[CharString; 4]>,