Skip to content

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

feat(core): added rule for oxymorons

Elijah Potterfd6163012559
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index 417e73f78..c96040b18 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -50001,4 +50001,5 @@ coexists/4
Minecraft/2M
IntelliSense/2M
Erdogan/2M
-Zig/2M
\ No newline at end of file
+Zig/2M
+oxymoronic/5
diff --git a/harper-core/src/linting/hedging.rs b/harper-core/src/linting/hedging.rs
index f67c82bc0..77287cd20 100644
--- a/harper-core/src/linting/hedging.rs
+++ b/harper-core/src/linting/hedging.rs
@@ -1,4 +1,4 @@
-use crate::linting::{Lint, LintKind, PatternLinter, Suggestion};
+use crate::linting::{Lint, LintKind, PatternLinter};
use crate::patterns::{EitherPattern, ExactPhrase, Pattern};
use crate::{Token, TokenStringExt};
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 31d3240b7..0532da45e 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -35,6 +35,7 @@ use super::multiple_sequential_pronouns::MultipleSequentialPronouns;
use super::nobody::Nobody;
use super::number_suffix_capitalization::NumberSuffixCapitalization;
use super::out_of_date::OutOfDate;
+use super::oxymorons::Oxymorons;
use super::pique_interest::PiqueInterest;
use super::plural_conjugate::PluralConjugate;
use super::possessive_your::PossessiveYour;
@@ -252,6 +253,7 @@ impl LintGroup {
insert_struct_rule!(LetsConfusion, true);
insert_struct_rule!(DespiteOf, true);
insert_struct_rule!(ChockFull, true);
+ insert_struct_rule!(Oxymorons, true);
insert_struct_rule!(Hedging, true);
insert_struct_rule!(ExpandTimeShorthands, true);
insert_struct_rule!(ModalOf, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 431dc2660..5a0395b6f 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -40,6 +40,7 @@ mod nobody;
mod number_suffix_capitalization;
mod out_of_date;
mod oxford_comma;
+mod oxymorons;
mod pattern_linter;
mod phrase_corrections;
mod pique_interest;
@@ -99,6 +100,7 @@ pub use nobody::Nobody;
pub use number_suffix_capitalization::NumberSuffixCapitalization;
pub use out_of_date::OutOfDate;
pub use oxford_comma::OxfordComma;
+pub use oxymorons::Oxymorons;
pub use pattern_linter::PatternLinter;
pub use pique_interest::PiqueInterest;
pub use plural_conjugate::PluralConjugate;
diff --git a/harper-core/src/linting/oxymorons.rs b/harper-core/src/linting/oxymorons.rs
new file mode 100644
index 000000000..c31bf4651
--- /dev/null
+++ b/harper-core/src/linting/oxymorons.rs
@@ -0,0 +1,142 @@
+use crate::linting::{Lint, LintKind, PatternLinter};
+use crate::patterns::{EitherPattern, ExactPhrase, Pattern};
+use crate::{Token, TokenStringExt};
+
+/// A linter that flags oxymoronic phrases.
+pub struct Oxymorons {
+ pattern: Box<dyn Pattern>,
+}
+
+impl Oxymorons {
+ pub fn new() -> Self {
+ // List of phrases that are considered oxymoronic.
+ let phrases = vec![
+ "amateur expert",
+ "increasingly less",
+ "advancing backwards?",
+ "alludes explicitly to",
+ "explicitly alludes to",
+ "totally obsolescent",
+ "completely obsolescent",
+ "generally always",
+ "usually always",
+ "build down",
+ "conspicuous absence",
+ "exact estimate",
+ "found missing",
+ "intense apathy",
+ "mandatory choice",
+ "nonworking mother",
+ "organized mess",
+ ];
+
+ // Build a vector of exact-match patterns for each oxymoron.
+ let patterns: Vec<Box<dyn Pattern>> = phrases
+ .into_iter()
+ .map(|s| Box::new(ExactPhrase::from_phrase(s)) as Box<dyn Pattern>)
+ .collect();
+
+ let pattern = Box::new(EitherPattern::new(patterns));
+ Self { pattern }
+ }
+}
+
+impl Default for Oxymorons {
+ fn default() -> Self {
+ Self::new()
+ }
+}
+
+impl PatternLinter for Oxymorons {
+ /// Returns the underlying pattern.
+ fn pattern(&self) -> &dyn Pattern {
+ self.pattern.as_ref()
+ }
+
+ fn match_to_lint(&self, matched_tokens: &[Token], source: &[char]) -> Option<Lint> {
+ let span = matched_tokens.span()?;
+ let matched_text: String = span.get_content(source).iter().collect();
+ Some(Lint {
+ span,
+ lint_kind: LintKind::Miscellaneous,
+ suggestions: Vec::new(),
+ message: format!("'{}' is an oxymoron.", matched_text),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &str {
+ "Flags oxymoronic phrases (e.g. 'amateur expert', 'increasingly less', etc.)."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::Oxymorons;
+ use crate::linting::tests::assert_lint_count;
+
+ #[test]
+ fn detects_amateur_expert() {
+ assert_lint_count("The amateur expert gave his opinion.", Oxymorons::new(), 1);
+ }
+
+ #[test]
+ fn detects_increasingly_less() {
+ assert_lint_count(
+ "The solution was increasingly less effective.",
+ Oxymorons::new(),
+ 1,
+ );
+ }
+
+ #[test]
+ fn detects_advancing_backwards() {
+ assert_lint_count("The project is advancing backwards?", Oxymorons::new(), 1);
+ }
+
+ #[test]
+ fn detects_alludes_explicitly_to() {
+ assert_lint_count(
+ "The report alludes explicitly to several issues.",
+ Oxymorons::new(),
+ 1,
+ );
+ }
+
+ #[test]
+ fn detects_explicitly_alludes_to() {
+ assert_lint_count(
+ "The report explicitly alludes to several issues.",
+ Oxymorons::new(),
+ 1,
+ );
+ }
+
+ #[test]
+ fn does_not_flag_clean_text() {
+ assert_lint_count("The expert provided clear advice.", Oxymorons::new(), 0);
+ }
+
+ #[test]
+ fn lowercase_match() {
+ assert_lint_count(
+ "the amateur expert is often unreliable.",
+ Oxymorons::new(),
+ 1,
+ );
+ }
+
+ #[test]
+ fn phrase_with_extra_whitespace() {
+ assert_lint_count("An organized mess was found.", Oxymorons::new(), 1);
+ }
+
+ #[test]
+ fn phrase_split_by_line_break() {
+ assert_lint_count(
+ "nonworking\nmother is not a term to be used.",
+ Oxymorons::new(),
+ 1,
+ );
+ }
+}