Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: remove filler words "uh" and "um" (#1626)
Andrew Dunbarf6455241b56c
diff --git a/harper-core/src/linting/filler_words.rs b/harper-core/src/linting/filler_words.rsnew file mode 100644index 000000000..5d702bcb5--- /dev/null+++ b/harper-core/src/linting/filler_words.rs@@ -0,0 +1,75 @@+use crate::{+ Lrc, Token, TokenStringExt,+ expr::{Expr, SequenceExpr},+ linting::{ExprLinter, Lint, LintKind, Suggestion},+ patterns::WordSet,+};++pub struct FillerWords {+ expr: Box<dyn Expr>,+}++impl Default for FillerWords {+ // A filler is unlikely to be completely on its own, so check for and remove with whitespace either before or after.+ fn default() -> Self {+ let filler_words = Lrc::new(WordSet::new(&["uh", "um"]));++ let pattern = SequenceExpr::default().then_any_of(vec![+ Box::new(+ SequenceExpr::default()+ .then(filler_words.clone())+ .then_whitespace(),+ ),+ Box::new(SequenceExpr::default().then_whitespace().then(filler_words)),+ ]);++ Self {+ expr: Box::new(pattern),+ }+ }+}++impl ExprLinter for FillerWords {+ fn expr(&self) -> &dyn Expr {+ self.expr.as_ref()+ }++ fn match_to_lint(&self, toks: &[Token], _src: &[char]) -> Option<Lint> {+ // A filler is unlikely to be completely on its own, so check for and remove with whitespace either before or after.+ Some(Lint {+ span: toks.span()?,+ lint_kind: LintKind::Miscellaneous,+ suggestions: vec![Suggestion::Remove],+ message: "Remove this unnecessary filler word.".to_string(),+ priority: 31,+ })+ }++ fn description(&self) -> &str {+ "Removes filler words."+ }+}++#[cfg(test)]+mod tests {+ use super::FillerWords;+ use crate::linting::tests::assert_suggestion_result;++ #[test]+ fn remove_uh() {+ assert_suggestion_result(+ "Let's remove all the uh filler words.",+ FillerWords::default(),+ "Let's remove all the filler words.",+ );+ }++ #[test]+ fn remove_um_st_start() {+ assert_suggestion_result(+ "Um but I'll just add some context for this.",+ FillerWords::default(),+ "but I'll just add some context for this.",+ );+ }+}diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rsindex 334725884..4f7080521 100644--- a/harper-core/src/linting/lint_group.rs+++ b/harper-core/src/linting/lint_group.rs@@ -39,6 +39,7 @@ use super::everyday::Everyday;use super::expand_time_shorthands::ExpandTimeShorthands;use super::expr_linter::run_on_chunk;use super::few_units_of_time_ago::FewUnitsOfTimeAgo;+use super::filler_words::FillerWords;use super::first_aid_kit::FirstAidKit;use super::for_noun::ForNoun;use super::have_pronoun::HavePronoun;@@ -386,6 +387,7 @@ impl LintGroup {out.merge_from(&mut initialisms::lint_group());// Add all the more complex rules to the group.+ // On *nix you can maintain sort order with `sort -t'(' -k2`insert_expr_rule!(APart, true);insert_expr_rule!(AdjectiveDoubleDegree, true);insert_struct_rule!(AdjectiveOfA, true);@@ -400,13 +402,6 @@ impl LintGroup {insert_expr_rule!(BoringWords, false);insert_struct_rule!(CapitalizePersonalPronouns, true);insert_expr_rule!(ChockFull, true);- insert_struct_rule!(ItsPossessive, true);- insert_expr_rule!(DoubleModal, true);- insert_expr_rule!(MissingPreposition, true);- insert_struct_rule!(DiscourseMarkers, true);- insert_expr_rule!(WayTooAdjective, true);- insert_expr_rule!(HavePronoun, true);- insert_expr_rule!(PronounInflectionBe, true);insert_struct_rule!(CommaFixes, true);insert_struct_rule!(CompoundNouns, true);insert_expr_rule!(Confident, true);@@ -414,21 +409,25 @@ impl LintGroup {insert_struct_rule!(CurrencyPlacement, true);insert_expr_rule!(Dashes, true);insert_expr_rule!(DespiteOf, true);+ insert_struct_rule!(DiscourseMarkers, true);insert_expr_rule!(DotInitialisms, true);+ insert_expr_rule!(DoubleModal, true);insert_struct_rule!(EllipsisLength, true);insert_struct_rule!(ElsePossessive, true);insert_struct_rule!(Everyday, true);insert_expr_rule!(ExpandTimeShorthands, true);insert_expr_rule!(FewUnitsOfTimeAgo, true);+ insert_expr_rule!(FillerWords, true);insert_struct_rule!(FirstAidKit, true);insert_struct_rule!(ForNoun, true);+ insert_expr_rule!(HavePronoun, true);insert_expr_rule!(Hedging, true);insert_expr_rule!(Hereby, true);- insert_expr_rule!(OpenCompounds, true);insert_struct_rule!(HopHope, true);insert_struct_rule!(HowTo, true);insert_expr_rule!(HyphenateNumberDay, true);insert_struct_rule!(ItsContraction, true);+ insert_struct_rule!(ItsPossessive, true);insert_expr_rule!(LeftRightHand, true);insert_expr_rule!(LessWorse, true);insert_struct_rule!(LetsConfusion, true);@@ -436,20 +435,22 @@ impl LintGroup {insert_struct_rule!(LongSentences, true);insert_expr_rule!(LookingForwardTo, true);insert_struct_rule!(MergeWords, true);+ insert_expr_rule!(MissingPreposition, true);insert_expr_rule!(ModalOf, true);insert_expr_rule!(Months, true);insert_expr_rule!(MostNumber, true);insert_expr_rule!(MultipleSequentialPronouns, true);- insert_expr_rule!(NoMatchFor, true);insert_struct_rule!(NailOnTheHead, true);- insert_struct_rule!(NominalWants, true);+ insert_expr_rule!(NoMatchFor, true);insert_struct_rule!(NoOxfordComma, false);insert_expr_rule!(Nobody, true);+ insert_struct_rule!(NominalWants, true);insert_expr_rule!(NounCountability, true);insert_struct_rule!(NumberSuffixCapitalization, true);insert_struct_rule!(OfCourse, true);insert_expr_rule!(OnFloor, true);insert_expr_rule!(OneAndTheSame, true);+ insert_expr_rule!(OpenCompounds, true);insert_expr_rule!(OpenTheLight, true);insert_expr_rule!(OutOfDate, true);insert_struct_rule!(OxfordComma, true);@@ -458,13 +459,14 @@ impl LintGroup {insert_expr_rule!(PiqueInterest, true);insert_expr_rule!(PossessiveYour, true);insert_struct_rule!(PronounContraction, true);+ insert_expr_rule!(PronounInflectionBe, true);insert_struct_rule!(PronounKnew, true);insert_expr_rule!(RedundantAdditiveAdverbs, true);insert_struct_rule!(RepeatedWords, true);insert_struct_rule!(SaveToSafe, true);insert_expr_rule!(SemicolonApostrophe, true);- insert_expr_rule!(SinceDuration, true);insert_expr_rule!(ShootOneselfInTheFoot, true);+ insert_expr_rule!(SinceDuration, true);insert_expr_rule!(SomewhatSomething, true);insert_struct_rule!(Spaces, true);insert_struct_rule!(SpelledNumbers, false);@@ -479,6 +481,7 @@ impl LintGroup {insert_expr_rule!(UseGenitive, false);insert_expr_rule!(VeryUnique, true);insert_expr_rule!(WasAloud, true);+ insert_expr_rule!(WayTooAdjective, true);insert_expr_rule!(Whereas, true);insert_expr_rule!(WidelyAccepted, true);insert_struct_rule!(WidelyAccepted, true);diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rsindex ca2c8b65f..adbb0c5e1 100644--- a/harper-core/src/linting/mod.rs+++ b/harper-core/src/linting/mod.rs@@ -33,6 +33,7 @@ mod everyday;mod expand_time_shorthands;mod expr_linter;mod few_units_of_time_ago;+mod filler_words;mod first_aid_kit;mod for_noun;mod have_pronoun;@@ -154,6 +155,7 @@ pub use everyday::Everyday;pub use expand_time_shorthands::ExpandTimeShorthands;pub use expr_linter::ExprLinter;pub use few_units_of_time_ago::FewUnitsOfTimeAgo;+pub use filler_words::FillerWords;pub use for_noun::ForNoun;pub use have_pronoun::HavePronoun;pub use hedging::Hedging;diff --git a/harper-core/src/linting/pronoun_contraction/should_contract.rs b/harper-core/src/linting/pronoun_contraction/should_contract.rsindex 41c6cd26a..4f62af383 100644--- a/harper-core/src/linting/pronoun_contraction/should_contract.rs+++ b/harper-core/src/linting/pronoun_contraction/should_contract.rs@@ -69,8 +69,6 @@ impl ExprLinter for ShouldContract {#[cfg(test)]mod tests {- use std::default;-use super::ShouldContract;use crate::linting::tests::{assert_lint_count, assert_no_lints, assert_suggestion_result};