Skip to content

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

feat: created linter for offensive language

Elijah Potter8189f740d87e
diff --git a/harper-core/affixes.json b/harper-core/affixes.json
index efab51053..fc87f8b94 100644
--- a/harper-core/affixes.json
+++ b/harper-core/affixes.json
@@ -525,6 +525,17 @@
"gifts_metadata": {
"adjective": {}
}
+ },
+ "6": {
+ "suffix": true,
+ "cross_product": true,
+ "replacements": [],
+ "adds_metadata": {
+ "swear": true
+ },
+ "gifts_metadata": {
+ "swear": true
+ }
}
}
}
diff --git a/harper-core/dictionary.dict b/harper-core/dictionary.dict
index 3de2b6a4c..841a644f4 100644
--- a/harper-core/dictionary.dict
+++ b/harper-core/dictionary.dict
@@ -12705,7 +12705,7 @@ aspiration/M1
aspirator/SM1
aspire/GDS4
aspirin/MS1
-ass/MS15
+ass/MS156
assail/GBDS4
assailable/U5
assailant/SM15
@@ -12736,7 +12736,7 @@ assessor/MS1
asset/MS1
asseverate/DSGN4
asseveration/M1
-asshole/MS!1
+asshole/MS!16
assiduity/M1
assiduous/PY5
assiduousness/M1
@@ -17202,7 +17202,7 @@ cockney/SM152
cockpit/SM1
cockroach/MS1
cockscomb/SM1
-cocksucker/MS!1
+cocksucker/MS!16
cocksure/5
cocktail/MS154
cocky/RTP514
@@ -19072,7 +19072,7 @@ cumulus/M1
cuneiform/M51
cunnilingus/M1
cunning/MRYT51
-cunt/MS!145
+cunt/MS!1456
cup/SM14
cupboard/SM14
cupcake/MS14
@@ -20159,7 +20159,7 @@ dichotomous/5
dichotomy/SM1
dicier/5
diciest/5
-dick/MRXZS14
+dick/MRXZS146
dicker/DG41
dickey/SM15
dickhead/S1
@@ -23103,7 +23103,7 @@ farrow/SMDG145
farseeing/5
farsighted/P5
farsightedness/M1
-fart/MDGS41
+fart/MDGS416
farther/54
farthermost/5
farthest/5
@@ -24534,9 +24534,9 @@ fryer/SM1
ft/1
ftp/ZGS5124
fuchsia/MS15
-fuck/SMGDRZ!41
-fucker/M!1
-fuckhead/S!1
+fuck/SMGDRZ!416
+fucker/M!16
+fuckhead/S!16
fuddle/DSMG41
fudge/DSMG14
fuehrer/MS1
@@ -27132,7 +27132,7 @@ horsemen/1
horseplay/M14
horsepower/M1
horseradish/MS1
-horseshit/!1
+horseshit/!16
horseshoe/DSM14
horseshoeing/4
horsetail/SM1
@@ -32731,7 +32731,7 @@ moth/M14
mothball/GMDS14
mother/MDYSG14
motherboard/SM1
-motherfucker/MS!1
+motherfucker/MS!16
motherfucking/!5
motherhood/M1
motherland/MS1
@@ -36620,7 +36620,7 @@ piroshki/M1
pirouette/DSMG14
piscatorial/5
pismire/SM1
-piss/ZGMDRS14
+piss/ZGMDRS146
pissoir/S1
pistachio/SM1
piste/S1
@@ -41661,13 +41661,13 @@ shirtsleeve/SM1
shirttail/SM1
shirtwaist/MS1
shirty/5
-shit/SM!154
-shitfaced/!5
-shithead/S!1
-shitload/!1
-shitted/!4
-shitting/!145
-shitty/RT!5
+shit/SM!1546
+shitfaced/!56
+shithead/S!16
+shitload/!16
+shitted/!46
+shitting/!1456
+shitty/RT!56
shiv/ZMRS14
shiver/MDG41
shivery/5
@@ -46774,7 +46774,7 @@ turboprop/SM1
turbot/SM1
turbulence/M1
turbulent/Y5
-turd/MS14
+turd/MS146
turducken/SM1
tureen/SM1
turf/MDSG14
@@ -46831,7 +46831,7 @@ twain/M514
twang/SMDG14
twangy/RT5
twas/
-twat/S14
+twat/S146
tweak/SMDG41
twee/51
tweed/SM1
@@ -48830,7 +48830,7 @@ whop/S41
whopped/4
whopper/SM1
whopping/541
-whore/SMG14
+whore/SMG146
whorehouse/MS1
whoreish/5
whorish/5
diff --git a/harper-core/src/linting/avoid_curses.rs b/harper-core/src/linting/avoid_curses.rs
new file mode 100644
index 000000000..ee2e961e4
--- /dev/null
+++ b/harper-core/src/linting/avoid_curses.rs
@@ -0,0 +1,32 @@
+use super::{Lint, LintKind, Linter};
+use crate::{Document, TokenStringExt};
+
+#[derive(Debug, Default)]
+pub struct AvoidCurses;
+
+impl Linter for AvoidCurses {
+ fn lint(&mut self, document: &Document) -> Vec<Lint> {
+ document
+ .iter_words()
+ .filter(|t| t.kind.is_swear())
+ .map(|t| Lint {
+ span: t.span,
+ lint_kind: LintKind::Miscellaneous,
+ suggestions: vec![],
+ message: "Try to avoid offensive language.".to_string(),
+ priority: 63
+ })
+ .collect()
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::AvoidCurses;
+ use crate::linting::tests::assert_lint_count;
+
+ #[test]
+ fn detects_shit() {
+ assert_lint_count("He ate shit when he fell off the bike.", AvoidCurses, 1);
+ }
+}
diff --git a/harper-core/src/linting/lint_group.rs b/harper-core/src/linting/lint_group.rs
index cc7bdcdb1..d07669a4e 100644
--- a/harper-core/src/linting/lint_group.rs
+++ b/harper-core/src/linting/lint_group.rs
@@ -2,6 +2,7 @@ use paste::paste;
use serde::{Deserialize, Serialize};
use super::an_a::AnA;
+use super::avoid_curses::AvoidCurses;
use super::correct_number_suffix::CorrectNumberSuffix;
use super::linking_verbs::LinkingVerbs;
use super::long_sentences::LongSentences;
@@ -107,7 +108,8 @@ create_lint_group_config!(
CorrectNumberSuffix => true,
NumberSuffixCapitalization => true,
MultipleSequentialPronouns => true,
- LinkingVerbs => false
+ LinkingVerbs => false,
+ AvoidCurses => true
);
impl<T: Dictionary + Default> Default for LintGroup<T> {
diff --git a/harper-core/src/linting/mod.rs b/harper-core/src/linting/mod.rs
index 65f09639b..49d88ecab 100644
--- a/harper-core/src/linting/mod.rs
+++ b/harper-core/src/linting/mod.rs
@@ -1,4 +1,5 @@
mod an_a;
+mod avoid_curses;
mod correct_number_suffix;
mod linking_verbs;
mod lint;
diff --git a/harper-core/src/token.rs b/harper-core/src/token.rs
index 835deead7..030686ef1 100644
--- a/harper-core/src/token.rs
+++ b/harper-core/src/token.rs
@@ -73,6 +73,16 @@ impl TokenKind {
matches!(self, TokenKind::Punctuation(Punctuation::Pipe))
}
+ pub fn is_swear(&self) -> bool {
+ matches!(
+ self,
+ TokenKind::Word(WordMetadata {
+ swear: Some(true),
+ ..
+ })
+ )
+ }
+
/// Checks that `self` is the same enum variant as `other`, regardless of
/// whether the inner metadata is also equal.
pub fn matches_variant_of(&self, other: &Self) -> bool {
diff --git a/harper-core/src/word_metadata.rs b/harper-core/src/word_metadata.rs
index 8891dc8bd..68e38a711 100644
--- a/harper-core/src/word_metadata.rs
+++ b/harper-core/src/word_metadata.rs
@@ -3,10 +3,11 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Hash)]
pub struct WordMetadata {
- noun: Option<NounData>,
- verb: Option<VerbData>,
- adjective: Option<AdjectiveData>,
- adverb: Option<AdverbData>
+ pub noun: Option<NounData>,
+ pub verb: Option<VerbData>,
+ pub adjective: Option<AdjectiveData>,
+ pub adverb: Option<AdverbData>,
+ pub swear: Option<bool>
}
impl WordMetadata {
@@ -16,7 +17,8 @@ impl WordMetadata {
noun: self.noun.or(other.noun),
verb: self.verb.or(other.verb),
adjective: self.adjective.or(other.adjective),
- adverb: self.adverb.or(other.adverb)
+ adverb: self.adverb.or(other.adverb),
+ swear: self.swear.or(other.swear)
}
}
@@ -76,6 +78,11 @@ impl WordMetadata {
)
}
+ /// Checks whether a word is _definitely_ a swear.
+ pub fn is_swear(&self) -> bool {
+ matches!(self.swear, Some(true))
+ }
+
/// Same thing as [`Self::or`], except in-place rather than a copy.
pub fn append(&mut self, other: &Self) -> &mut Self {
*self = self.or(other);
diff --git a/harper-ls/README.md b/harper-ls/README.md
index 5764a1c3e..cafd096cc 100644
--- a/harper-ls/README.md
+++ b/harper-ls/README.md
@@ -75,7 +75,8 @@ lspconfig.harper_ls.setup {
correct_number_suffix = true,
number_suffix_capitalization = true,
multiple_sequential_pronouns = true,
- linking_verbs = false
+ linking_verbs = false,
+ avoid_curses = true,
}
}
},
diff --git a/packages/vscode-plugin/package.json b/packages/vscode-plugin/package.json
index 6603ccacf..0e3972c2e 100644
--- a/packages/vscode-plugin/package.json
+++ b/packages/vscode-plugin/package.json
@@ -137,6 +137,12 @@
"default": true,
"description": "Detect improper use of linking verbs."
},
+ "harper-ls.linters.avoid_curses": {
+ "scope": "resource",
+ "type": "boolean",
+ "default": true,
+ "description": "Catch use of curse/swear words."
+ },
"harper-ls.diagnosticSeverity": {
"scope": "resource",
"type": "string",