Skip to content

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

feat(core): `CautionaryTale`

Elijah Potterb20710ba617d
diff --git a/harper-core/src/linting/cautionary_tale.rs b/harper-core/src/linting/cautionary_tale.rs
new file mode 100644
index 000000000..8fc977af2
--- /dev/null
+++ b/harper-core/src/linting/cautionary_tale.rs
@@ -0,0 +1,175 @@
+use crate::{
+ Token,
+ expr::{Expr, SequenceExpr},
+ linting::{ExprLinter, Lint, LintKind, Suggestion},
+ patterns::WordSet,
+};
+
+/// Corrects the homophone confusion between "tale" (story) and "tail" (appendage)
+/// in common phrases like "cautionary tale" and "inspirational tale".
+pub struct CautionaryTale {
+ expr: Box<dyn Expr>,
+}
+
+impl Default for CautionaryTale {
+ fn default() -> Self {
+ let adjectives = WordSet::new(&["cautionary", "inspirational"]);
+
+ let pattern = SequenceExpr::default()
+ .then(adjectives)
+ .t_ws()
+ .t_aco("tail");
+
+ Self {
+ expr: Box::new(pattern),
+ }
+ }
+}
+
+impl ExprLinter for CautionaryTale {
+ fn expr(&self) -> &dyn Expr {
+ self.expr.as_ref()
+ }
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ let tail_span = toks.last()?.span;
+ let tail_text = tail_span.get_content(src);
+
+ Some(Lint {
+ span: tail_span,
+ lint_kind: LintKind::Miscellaneous,
+ suggestions: vec![Suggestion::replace_with_match_case(
+ ['t', 'a', 'l', 'e'].to_vec(),
+ tail_text,
+ )],
+ message: "Did you mean `tale` (story)?".to_owned(),
+ priority: 31,
+ })
+ }
+
+ fn description(&self) -> &'static str {
+ "Corrects confusion between `tale` (story) and `tail` (appendage) in common phrases."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::linting::{CautionaryTale, tests::{assert_lint_count, assert_suggestion_result}};
+
+ #[test]
+ fn catches_cautionary_tail() {
+ assert_suggestion_result(
+ "It serves as a cautionary tail.",
+ CautionaryTale::default(),
+ "It serves as a cautionary tale.",
+ );
+ }
+
+ #[test]
+ fn catches_inspirational_tail() {
+ assert_suggestion_result(
+ "Her journey is an inspirational tail of perseverance.",
+ CautionaryTale::default(),
+ "Her journey is an inspirational tale of perseverance.",
+ );
+ }
+
+ #[test]
+ fn catches_capitalized_cautionary_tail() {
+ assert_suggestion_result(
+ "The article discusses a Cautionary Tail about privacy.",
+ CautionaryTale::default(),
+ "The article discusses a Cautionary Tale about privacy.",
+ );
+ }
+
+ #[test]
+ fn catches_uppercase_cautionary_tail() {
+ assert_suggestion_result(
+ "THE STORY IS A CAUTIONARY TAIL.",
+ CautionaryTale::default(),
+ "THE STORY IS A CAUTIONARY TALE.",
+ );
+ }
+
+ #[test]
+ fn catches_mixed_case() {
+ assert_suggestion_result(
+ "This serves as an inspirational Tail for all.",
+ CautionaryTale::default(),
+ "This serves as an inspirational Tale for all.",
+ );
+ }
+
+ #[test]
+ fn allows_actual_tail() {
+ assert_lint_count(
+ "The dog wagged its tail happily.",
+ CautionaryTale::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn allows_different_adjective_with_tail() {
+ assert_lint_count(
+ "The cat has a long tail.",
+ CautionaryTale::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn allows_correct_tale() {
+ assert_lint_count(
+ "It serves as a cautionary tale.",
+ CautionaryTale::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn allows_inspirational_tale() {
+ assert_lint_count(
+ "Her story is an inspirational tale.",
+ CautionaryTale::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn catches_in_longer_text() {
+ assert_suggestion_result(
+ "The movie presents a cautionary tail about the dangers of AI. It's really scary.",
+ CautionaryTale::default(),
+ "The movie presents a cautionary tale about the dangers of AI. It's really scary.",
+ );
+ }
+
+ #[test]
+ fn catches_multiple_occurrences() {
+ assert_lint_count(
+ "This cautionary tail is also an inspirational tail about overcoming adversity.",
+ CautionaryTale::default(),
+ 2,
+ );
+ }
+
+ #[test]
+ fn allows_tail_in_different_context() {
+ assert_lint_count(
+ "The inspirational speaker told the tale of a dog's tail.",
+ CautionaryTale::default(),
+ 0,
+ );
+ }
+
+ #[test]
+ fn catches_at_start_of_sentence() {
+ assert_suggestion_result(
+ "Cautionary tail: don't trust strangers.",
+ CautionaryTale::default(),
+ "Cautionary tale: don't trust strangers.",
+ );
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index 4db54468c..6a122bbb1 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -28,6 +28,7 @@ use super::best_of_all_time::BestOfAllTime;
use super::boring_words::BoringWords;
use super::cant::Cant;
use super::capitalize_personal_pronouns::CapitalizePersonalPronouns;
+use super::cautionary_tale::CautionaryTale;
use super::chock_full::ChockFull;
use super::comma_fixes::CommaFixes;
use super::compound_nouns::CompoundNouns;
@@ -447,6 +448,7 @@ impl LintGroup {
insert_expr_rule!(BoringWords, false);
insert_expr_rule!(Cant, true);
insert_struct_rule!(CapitalizePersonalPronouns, true);
+ insert_expr_rule!(CautionaryTale, true);
insert_expr_rule!(ChockFull, true);
insert_struct_rule!(CommaFixes, true);
insert_struct_rule!(CompoundNouns, true);
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index d893f46b6..887936e88 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -19,6 +19,7 @@ mod best_of_all_time;
mod boring_words;
mod cant;
mod capitalize_personal_pronouns;
+mod cautionary_tale;
mod chock_full;
mod closed_compounds;
mod comma_fixes;
@@ -182,6 +183,7 @@ pub use best_of_all_time::BestOfAllTime;
pub use boring_words::BoringWords;
pub use cant::Cant;
pub use capitalize_personal_pronouns::CapitalizePersonalPronouns;
+pub use cautionary_tale::CautionaryTale;
pub use chock_full::ChockFull;
pub use comma_fixes::CommaFixes;
pub use compound_nouns::CompoundNouns;