Skip to content

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

refactor: split nominal into nominal, noun, pronoun

hippietrail5f72b17099f5
diff --git a/harper-core/affixes.json b/harper-core/affixes.json
index ed037de66..720618301 100644
--- a/harper-core/affixes.json
+++ b/harper-core/affixes.json
@@ -285,12 +285,12 @@
}
],
"adds_metadata": {
- "nominal": {
+ "noun": {
"is_possessive": true
}
},
"gifts_metadata": {
- "nominal": {}
+ "noun": {}
}
},
"F": {
@@ -358,7 +358,7 @@
}
],
"adds_metadata": {
- "nominal": {
+ "noun": {
"is_plural": true
}
},
@@ -415,7 +415,8 @@
],
"adds_metadata": {
"verb": {},
- "nominal": null
+ "noun": null,
+ "adjective": null
},
"gifts_metadata": {}
},
@@ -511,7 +512,7 @@
"replacements": [],
"adds_metadata": {},
"gifts_metadata": {
- "nominal": {
+ "noun": {
"is_plural": false
}
}
@@ -523,7 +524,7 @@
"replacements": [],
"adds_metadata": {},
"gifts_metadata": {
- "nominal": {
+ "noun": {
"is_proper": true
}
}
@@ -599,7 +600,7 @@
"replacements": [],
"adds_metadata": {},
"gifts_metadata": {
- "nominal": {
+ "noun": {
"is_plural": true
}
}
diff --git a/harper-core/src/lib.rs b/harper-core/src/lib.rs
index 32d636102..a579d89a8 100644
--- a/harper-core/src/lib.rs
+++ b/harper-core/src/lib.rs
@@ -46,7 +46,7 @@ pub use token_kind::TokenKind;
pub use token_string_ext::TokenStringExt;
pub use vec_ext::VecExt;
pub use word_metadata::{
- AdverbData, ConjunctionData, NominalData, PronounData, Tense, VerbData, WordMetadata,
+ AdverbData, ConjunctionData, NounData, PronounData, Tense, VerbData, WordMetadata,
};
/// A utility function that removes overlapping lints in a vector,
diff --git a/harper-core/src/patterns/sequence_pattern.rs b/harper-core/src/patterns/sequence_pattern.rs
index 7cf1c37a3..7db8d1394 100644
--- a/harper-core/src/patterns/sequence_pattern.rs
+++ b/harper-core/src/patterns/sequence_pattern.rs
@@ -83,7 +83,7 @@ impl SequencePattern {
gen_then_from_is!(apostrophe);
gen_then_from_is!(hyphen);
gen_then_from_is!(determiner);
- gen_then_from_is!(proper_nominal);
+ gen_then_from_is!(proper_noun);
pub fn then_indefinite_article(self) -> Self {
self.then(IndefiniteArticle::default())
diff --git a/harper-core/src/patterns/singular_subject.rs b/harper-core/src/patterns/singular_subject.rs
index b0c4b3c31..4adfe1faa 100644
--- a/harper-core/src/patterns/singular_subject.rs
+++ b/harper-core/src/patterns/singular_subject.rs
@@ -11,7 +11,7 @@ impl Default for SingularSubject {
Self {
inner: SequencePattern::default()
.then_pronoun()
- .or(Box::new(SequencePattern::default().then_proper_nominal())),
+ .or(Box::new(SequencePattern::default().then_proper_noun())),
}
}
}
diff --git a/harper-core/src/spell/hunspell/mod.rs b/harper-core/src/spell/hunspell/mod.rs
index 819b0aa16..7c15ef155 100644
--- a/harper-core/src/spell/hunspell/mod.rs
+++ b/harper-core/src/spell/hunspell/mod.rs
@@ -131,12 +131,12 @@ mod tests {
}
],
"adds_metadata": {
- "nominal": {
+ "noun": {
"is_plural": true
}
},
"gifts_metadata": {
- "nominal": {}
+ "noun": {}
}
},
"M": {
diff --git a/harper-core/src/spell/mod.rs b/harper-core/src/spell/mod.rs
index e057bf2dc..9baf12850 100644
--- a/harper-core/src/spell/mod.rs
+++ b/harper-core/src/spell/mod.rs
@@ -62,7 +62,7 @@ fn order_suggestions(matches: Vec<FuzzyMatchResult>) -> Vec<&[char]> {
if let Some(nominal_index) = found
.iter()
.skip(3)
- .position(|i| i.metadata.is_proper_nominal())
+ .position(|i| i.metadata.is_proper_noun())
{
found.swap(2, nominal_index + 3);
}
diff --git a/harper-core/src/title_case.rs b/harper-core/src/title_case.rs
index 34852c30b..920ed3316 100644
--- a/harper-core/src/title_case.rs
+++ b/harper-core/src/title_case.rs
@@ -36,7 +36,7 @@ pub fn make_title_case(toks: &[Token], source: &[char], dict: &impl Dictionary)
while let Some((index, word)) = word_likes.next() {
if let Some(Some(metadata)) = word.kind.as_word() {
- if metadata.is_proper_nominal() {
+ if metadata.is_proper_noun() {
// Replace it with the dictionary entry verbatim.
let orig_text = word.span.get_content(source);
diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rs
index 9d78d4786..840550a66 100644
--- a/harper-core/src/token_kind.rs
+++ b/harper-core/src/token_kind.rs
@@ -1,7 +1,7 @@
use is_macro::Is;
use serde::{Deserialize, Serialize};
-use crate::{ConjunctionData, NominalData, Number, PronounData, Punctuation, Quote, WordMetadata};
+use crate::{ConjunctionData, NounData, Number, PronounData, Punctuation, Quote, WordMetadata};
#[derive(
Debug, Is, Clone, Copy, Serialize, Deserialize, Default, PartialOrd, Hash, Eq, PartialEq,
@@ -57,7 +57,26 @@ impl TokenKind {
matches!(
self,
TokenKind::Word(Some(WordMetadata {
- nominal: Some(NominalData {
+ noun: Some(NounData {
+ is_possessive: Some(true),
+ ..
+ }),
+ ..
+ })) | TokenKind::Word(Some(WordMetadata {
+ pronoun: Some(PronounData {
+ is_possessive: Some(true),
+ ..
+ }),
+ ..
+ }))
+ )
+ }
+
+ pub fn is_possessive_noun(&self) -> bool {
+ matches!(
+ self,
+ TokenKind::Word(Some(WordMetadata {
+ noun: Some(NounData {
is_possessive: Some(true),
..
}),
@@ -79,11 +98,11 @@ impl TokenKind {
)
}
- pub fn is_proper_nominal(&self) -> bool {
+ pub fn is_proper_noun(&self) -> bool {
matches!(
self,
TokenKind::Word(Some(WordMetadata {
- nominal: Some(NominalData {
+ noun: Some(NounData {
is_proper: Some(true),
..
}),
@@ -275,7 +294,15 @@ impl TokenKind {
return true;
};
- metadata.is_not_plural_nominal()
+ metadata.is_not_plural_noun() || metadata.is_not_plural_pronoun()
+ }
+
+ pub fn is_not_plural_noun(&self) -> bool {
+ let TokenKind::Word(Some(metadata)) = self else {
+ return true;
+ };
+
+ metadata.is_not_plural_noun()
}
pub fn is_not_plural_pronoun(&self) -> bool {
@@ -299,7 +326,7 @@ impl TokenKind {
return false;
};
- metadata.is_plural_nominal()
+ metadata.is_plural_noun() || metadata.is_plural_pronoun()
}
pub fn is_plural_pronoun(&self) -> bool {
@@ -310,12 +337,28 @@ impl TokenKind {
metadata.is_plural_pronoun()
}
+ pub fn is_plural_noun(&self) -> bool {
+ let TokenKind::Word(Some(metadata)) = self else {
+ return false;
+ };
+
+ metadata.is_plural_noun()
+ }
+
pub fn is_nominal(&self) -> bool {
let TokenKind::Word(Some(metadata)) = self else {
return false;
};
- metadata.is_nominal()
+ metadata.is_noun() || metadata.is_pronoun()
+ }
+
+ pub fn is_noun(&self) -> bool {
+ let TokenKind::Word(Some(metadata)) = self else {
+ return false;
+ };
+
+ metadata.is_noun()
}
pub fn is_pronoun(&self) -> bool {
diff --git a/harper-core/src/word_metadata.rs b/harper-core/src/word_metadata.rs
index ee26ee037..1c279e994 100644
--- a/harper-core/src/word_metadata.rs
+++ b/harper-core/src/word_metadata.rs
@@ -4,7 +4,7 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Serialize, Deserialize, PartialOrd, Hash)]
pub struct WordMetadata {
- pub nominal: Option<NominalData>,
+ pub noun: Option<NounData>,
pub pronoun: Option<PronounData>,
pub verb: Option<VerbData>,
pub adjective: Option<AdjectiveData>,
@@ -90,7 +90,7 @@ impl WordMetadata {
}
Self {
- nominal: merge!(self.nominal, other.nominal),
+ noun: merge!(self.noun, other.noun),
pronoun: merge!(self.pronoun, other.pronoun),
verb: merge!(self.verb, other.verb),
adjective: merge!(self.adjective, other.adjective),
@@ -104,7 +104,7 @@ impl WordMetadata {
}
generate_metadata_queries!(
- nominal has proper, plural, possessive.
+ noun has proper, plural, possessive.
pronoun has plural, possessive.
verb has linking.
conjunction has.
@@ -112,6 +112,79 @@ impl WordMetadata {
adverb has
);
+ /// Checks if the word is definitely a nominal.
+ pub fn is_nominal(&self) -> bool {
+ self.noun.is_some() || self.pronoun.is_some()
+ }
+
+ /// Checks if the word is definitely a nominal and more specifically is labeled as (a) plural.
+ pub fn is_plural_nominal(&self) -> bool {
+ matches!(
+ self.noun,
+ Some(NounData {
+ is_plural: Some(true),
+ ..
+ })
+ ) || matches!(
+ self.pronoun,
+ Some(PronounData {
+ is_plural: Some(true),
+ ..
+ })
+ )
+ }
+
+ /// Checks if the word is definitely a nominal and more specifically is labeled as (a) possessive.
+ pub fn is_possessive_nominal(&self) -> bool {
+ matches!(
+ self.noun,
+ Some(NounData {
+ is_possessive: Some(true),
+ ..
+ })
+ ) || matches!(
+ self.pronoun,
+ Some(PronounData {
+ is_possessive: Some(true),
+ ..
+ })
+ )
+ }
+
+ /// Checks if the word is definitely a nominal and more specifically is labeled as __not__ (a) plural.
+ pub fn is_not_plural_nominal(&self) -> bool {
+ matches!(
+ self.noun,
+ Some(NounData {
+ is_plural: Some(false),
+ ..
+ })
+ ) && matches!(
+ self.pronoun,
+ Some(PronounData {
+ is_plural: Some(false),
+ ..
+ })
+ )
+ }
+
+ /// Checks if the word is definitely a nominal and more specifically is labeled as __not__ (a) possessive.
+ pub fn is_not_possessive_nominal(&self) -> bool {
+ matches!(
+ self.noun,
+ Some(NounData {
+ is_possessive: Some(false),
+ ..
+ })
+ ) && matches!(
+ self.pronoun,
+ Some(PronounData {
+ is_possessive: Some(false),
+ ..
+ })
+ )
+ }
+
/// Checks whether a word is _definitely_ a swear.
pub fn is_swear(&self) -> bool {
matches!(self.swear, Some(true))
@@ -159,13 +232,13 @@ impl VerbData {
// TODO other noun properties may be worth adding:
// TODO count vs mass; abstract
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, PartialOrd, Eq, Hash, Default)]
-pub struct NominalData {
+pub struct NounData {
pub is_proper: Option<bool>,
pub is_plural: Option<bool>,
pub is_possessive: Option<bool>,
}
-impl NominalData {
+impl NounData {
/// Produce a copy of `self` with the known properties of `other` set.
pub fn or(&self, other: &Self) -> Self {
Self {
diff --git a/harper-typst/src/lib.rs b/harper-typst/src/lib.rs
index c0d7bbf42..2f3436830 100644
--- a/harper-typst/src/lib.rs
+++ b/harper-typst/src/lib.rs
@@ -39,7 +39,9 @@ mod tests {
use ordered_float::OrderedFloat;
use super::Typst;
- use harper_core::{Document, NominalData, Number, Punctuation, TokenKind, WordMetadata};
+ // use harper_core::{Document, NominalData, Number, Punctuation, TokenKind, WordMetadata};
+ use harper_core::{Document, NounData, Number, Punctuation, TokenKind, WordMetadata};
+ // use harper_core::{Document, Number, Punctuation, TokenKind, WordMetadata};
#[test]
fn number() {
@@ -298,7 +300,7 @@ mod tests {
token_kinds.as_slice(),
&[
TokenKind::Word(Some(WordMetadata {
- nominal: Some(NominalData {
+ noun: Some(NounData {
is_possessive: Some(true),
..
}),