Skip to content

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

feat: .is_oov() method on TokenKind (#1551)

Andrew Dunbarc39008960255
diff --git a/harper-core/src/linting/phrasal_verb_as_compound_noun.rs b/harper-core/src/linting/phrasal_verb_as_compound_noun.rs
index a13debd09..4bb1a3089 100644
--- a/harper-core/src/linting/phrasal_verb_as_compound_noun.rs
+++ b/harper-core/src/linting/phrasal_verb_as_compound_noun.rs
@@ -2,7 +2,7 @@ use std::sync::Arc;
use super::{Lint, LintKind, Linter, Suggestion};
use crate::spell::{Dictionary, FstDictionary};
-use crate::{CharStringExt, Document, Span, TokenKind, TokenStringExt};
+use crate::{CharStringExt, Document, Span, TokenStringExt};
/// Detect phrasal verbs written as compound nouns.
pub struct PhrasalVerbAsCompoundNoun {
@@ -161,7 +161,7 @@ impl Linter for PhrasalVerbAsCompoundNoun {
}
// If the previous word is OOV, those are most commonly nouns
- if let TokenKind::Word(None) = &prev_tok.kind {
+ if prev_tok.kind.is_oov() {
continue;
}
}
diff --git a/harper-core/src/token_kind.rs b/harper-core/src/token_kind.rs
index 0096de34c..d8af9cb55 100644
--- a/harper-core/src/token_kind.rs
+++ b/harper-core/src/token_kind.rs
@@ -106,7 +106,11 @@ impl TokenKind {
is_possessive_determiner,
// Conjunction methods
- is_conjunction
+ is_conjunction,
+
+ // Generic word methods
+ is_swear,
+ is_likely_homograph
}
// Word metadata delegation methods not generated by macro
@@ -117,13 +121,6 @@ impl TokenKind {
metadata.preposition
}
- pub fn is_swear(&self) -> bool {
- let Word(Some(metadata)) = self else {
- return false;
- };
- metadata.is_swear()
- }
-
pub fn is_common_word(&self) -> bool {
let Word(Some(metadata)) = self else {
return true;
@@ -131,11 +128,12 @@ impl TokenKind {
metadata.common
}
- pub fn is_likely_homograph(&self) -> bool {
- let Word(Some(metadata)) = self else {
- return false;
- };
- metadata.is_likely_homograph()
+ /// Checks whether a word token is out-of-vocabulary (not found in the dictionary).
+ ///
+ /// Returns `true` if the token is a word that was not found in the dictionary,
+ /// `false` if the token is a word found in the dictionary or is not a word token.
+ pub fn is_oov(&self) -> bool {
+ matches!(self, TokenKind::Word(None))
}
// Punctuation and symbol is-methods