Skip to content

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

feat: unpossible etc. → impossible (#3081)

Andrew Dunbar34cb7340bdcb
diff --git a/harper-core/default_config.json b/harper-core/default_config.json
index 051498382..67d5442b6 100644
--- a/harper-core/default_config.json
+++ b/harper-core/default_config.json
@@ -3937,6 +3937,13 @@
"state": true,
"label": "Would Never Have"
}
+ },
+ {
+ "Bool": {
+ "name": "WrongNegative",
+ "state": true,
+ "label": "Wrong Negative"
+ }
}
]
}
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index de8151d1e..a51eecde0 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -18607,7 +18607,7 @@ commotion/~NwSg
communal/~JY
commune/~NSgVdGXn
communicability/Ng
-communicable/Ji
+communicable/JiU
communicably/Ry
communicant/NgSJ
communicate/~VGdSnvX
@@ -29958,6 +29958,7 @@ inanimate/~JYpNV
inanimateness/Ng
inanity/NSg
inappropriate/~JY
+inarguably/R
inarticulate/JYN
inasmuch/
inaudible/J
diff --git a/harper-core/src/linting/lint_group/mod.rs b/harper-core/src/linting/lint_group/mod.rs
index 38e379a32..069bfde4e 100644
--- a/harper-core/src/linting/lint_group/mod.rs
+++ b/harper-core/src/linting/lint_group/mod.rs
@@ -306,6 +306,7 @@ use super::wordpress_dotcom::WordPressDotcom;
use super::worth_to_do::WorthToDo;
use super::would_never_have::WouldNeverHave;
use super::wrong_apostrophe::WrongApostrophe;
+use super::wrong_negative::WrongNegative;
// Modules that create multiple linters each
use super::be_adjective_confusions;
@@ -889,6 +890,7 @@ impl LintGroup {
insert_struct_rule!(WordPressDotcom);
insert_expr_rule_with_dict!(WorthToDo);
insert_expr_rule!(WouldNeverHave);
+ insert_expr_rule_with_dict!(WrongNegative);
// Uses Sentence rather than Chunk
out.add("AspireTo", AspireTo::default());
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index a6a8e22fe..0f38a998b 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -324,6 +324,7 @@ mod wordpress_dotcom;
mod worth_to_do;
mod would_never_have;
mod wrong_apostrophe;
+mod wrong_negative;
pub use expr_linter::{Chunk, ExprLinter, Sentence};
pub use initialism_linter::InitialismLinter;
diff --git a/harper-core/src/linting/wrong_negative.rs b/harper-core/src/linting/wrong_negative.rs
new file mode 100644
index 000000000..995870de1
--- /dev/null
+++ b/harper-core/src/linting/wrong_negative.rs
@@ -0,0 +1,125 @@
+use crate::{
+ CharStringExt, Lint, Token,
+ expr::Expr,
+ linting::{ExprLinter, LintKind, Suggestion, expr_linter::Chunk},
+ spell::Dictionary,
+};
+
+const NEGATIVE_PREFIXES: &[&str] = &["anti", "de", "dis", "dys", "il", "im", "in", "non", "un"];
+
+fn looks_negative_but_oov(token: &Token, source: &[char]) -> bool {
+ token.kind.is_oov()
+ && token
+ .get_ch(source)
+ .starts_with_any_ignore_ascii_case_str(NEGATIVE_PREFIXES)
+}
+
+pub struct WrongNegative<D: Dictionary + 'static> {
+ pattern: fn(&Token, &[char]) -> bool,
+ dict: D,
+}
+
+impl<D: Dictionary + 'static> WrongNegative<D> {
+ pub fn new(dict: D) -> Self {
+ Self {
+ pattern: looks_negative_but_oov,
+ dict,
+ }
+ }
+}
+
+impl<D: Dictionary + 'static> ExprLinter for WrongNegative<D> {
+ type Unit = Chunk;
+
+ fn match_to_lint(&self, toks: &[Token], src: &[char]) -> Option<Lint> {
+ if toks.len() != 1 {
+ return None;
+ }
+ let span = toks[0].span;
+ let word = toks[0].get_ch(src);
+
+ let new_negs: Vec<Vec<char>> = NEGATIVE_PREFIXES
+ .iter()
+ .filter(|&prefix| word.starts_with_ignore_ascii_case_str(prefix))
+ .filter_map(|&prefix| {
+ let rest = &word[prefix.len()..];
+ (!rest.is_empty()).then_some((prefix, rest))
+ })
+ .flat_map(|(prefix, rest)| {
+ NEGATIVE_PREFIXES
+ .iter()
+ .filter(move |&&new_prefix| new_prefix != prefix)
+ .filter_map(move |&new_prefix| {
+ let new_neg: Vec<char> =
+ new_prefix.chars().chain(rest.iter().copied()).collect();
+
+ self.dict.contains_word(&new_neg).then_some(new_neg)
+ })
+ })
+ .collect();
+
+ let suggestions: Vec<Suggestion> = new_negs
+ .into_iter()
+ .map(|value| Suggestion::replace_with_match_case(value, span.get_content(src)))
+ .collect();
+
+ if suggestions.is_empty() {
+ return None;
+ }
+
+ Some(Lint {
+ span,
+ lint_kind: LintKind::WordChoice,
+ message: if suggestions.len() == 1 {
+ "Could this be the negative word you intended?"
+ } else {
+ "Could one of these be the negative word you intended?"
+ }
+ .to_string(),
+ suggestions,
+ ..Default::default()
+ })
+ }
+
+ fn expr(&self) -> &dyn Expr {
+ &self.pattern
+ }
+
+ fn description(&self) -> &str {
+ "If an unknown word looks like it might be a negative word, suggests correct words that are in the dictionary."
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use crate::{linting::tests::assert_suggestion_result, spell::FstDictionary};
+
+ use super::WrongNegative;
+
+ #[test]
+ fn fix_insecured() {
+ assert_suggestion_result(
+ "Problem with insecured chart repositories",
+ WrongNegative::new(FstDictionary::curated()),
+ "Problem with unsecured chart repositories",
+ );
+ }
+
+ #[test]
+ fn fix_unsecure() {
+ assert_suggestion_result(
+ "Our one and only host is showing up as unsecure, but connected.",
+ WrongNegative::new(FstDictionary::curated()),
+ "Our one and only host is showing up as insecure, but connected.",
+ );
+ }
+
+ #[test]
+ fn fix_unpossible() {
+ assert_suggestion_result(
+ "This System makes a individual design unpossible.",
+ WrongNegative::new(FstDictionary::curated()),
+ "This System makes a individual design impossible.",
+ );
+ }
+}
diff --git a/harper-core/tests/text/linters/The Great Gatsby.snap.yml b/harper-core/tests/text/linters/The Great Gatsby.snap.yml
index 1647fc2c2..9153837dd 100644
--- a/harper-core/tests/text/linters/The Great Gatsby.snap.yml
+++ b/harper-core/tests/text/linters/The Great Gatsby.snap.yml
@@ -4537,26 +4537,6 @@ Suggest:
-Lint: Spelling (63 priority)
-Message: |
- 3349 | But the rest offended her—and inarguably, because it wasn’t a gesture but an
- | ^~~~~~~~~~ Did you mean to spell `inarguably` this way?
-Suggest:
- - Replace with: “inarguable”
- - Replace with: “unarguably”
- - Replace with: “arguably”
-
-
-
-Lint: Typo (31 priority)
-Message: |
- 3349 | But the rest offended her—and inarguably, because it wasn’t a gesture but an
- | ^~~~~~~~~~ `inarguably` should probably be written as `in arguably`.
-Suggest:
- - Replace with: “in arguably”
-
-
-
Lint: Readability (127 priority)
Message: |
3350 | emotion. She was appalled by West Egg, this unprecedented “place” that Broadway
@@ -4649,18 +4629,6 @@ Suggest:
-Lint: Spelling (63 priority)
-Message: |
- 3490 | them than a wisp of startled air. But they made no sound, and what I had almost
- 3491 | remembered was uncommunicable forever.
- | ^~~~~~~~~~~~~~ Did you mean to spell `uncommunicable` this way?
-Suggest:
- - Replace with: “incommunicable”
- - Replace with: “communicable”
- - Replace with: “noncommunicable”
-
-
-
Lint: Spelling (63 priority)
Message: |
3496 | house failed to go on one Saturday night—and, as obscurely as it had begun, his
diff --git a/harper-core/tests/text/tagged/The Great Gatsby.md b/harper-core/tests/text/tagged/The Great Gatsby.md
index cb7a686e9..5696df583 100644
--- a/harper-core/tests/text/tagged/The Great Gatsby.md
+++ b/harper-core/tests/text/tagged/The Great Gatsby.md
@@ -6695,7 +6695,7 @@
>
#
> But the rest offended her — and inarguably , because it wasn’t a gesture but an
-# NSg/C/P D+ NSg/VB+ VP/J ISg/D$+ . VB/C ? . C/P NPr/ISg+ VPt D/P NSg/VB+ NSg/C/P D/P
+# NSg/C/P D+ NSg/VB+ VP/J ISg/D$+ . VB/C R . C/P NPr/ISg+ VPt D/P NSg/VB+ NSg/C/P D/P
> emotion . She was appalled by West Egg , this unprecedented “ place ” that Broadway
# N🅪Sg+ . ISg+ VLPt VP/J P NPr/VB/J+ N🅪Sg/VB+ . I/Ddem J . N🅪Sg/VB . I/C/Ddem NPr/J+
> had begotten upon a Long Island fishing village — appalled by its raw vigor that
@@ -6979,7 +6979,7 @@
> them than a wisp of startled air . But they made no sound , and what I had almost
# NSg/IPl+ C/P D/P NSg/VB P VP/J N🅪Sg/VB+ . NSg/C/P IPl+ VP NSg/Dq/P+ N🅪Sg/VB/J+ . VB/C NSg/I+ ISg/#r+ VP R
> remembered was uncommunicable forever .
-# VP/J VLPt ? NSg/J .
+# VP/J VLPt J NSg/J .
>
#
> CHAPTER VII