1use harper_brill::UPOS;
2use is_macro::Is;
3use serde::{Deserialize, Serialize};
4
5use crate::{Number, Punctuation, Quote, TokenKind::Word, WordMetadata};
6
7macro_rules! delegate_to_metadata {
10 ($($method:ident),* $(,)?) => {
11 $(
12 #[doc = concat!(
13 "Delegates to [`WordMetadata::",
14 stringify!($method),
15 "`] when this token is a word.\n\n",
16 "Returns `false` if the token is not a word."
17 )]
18 pub fn $method(&self) -> bool {
19 let Word(Some(metadata)) = self else {
20 return false;
21 };
22 metadata.$method()
23 }
24 )*
25 };
26}
27
28#[derive(Debug, Is, Clone, Serialize, Deserialize, Default, PartialOrd, Hash, Eq, PartialEq)]
33#[serde(tag = "kind", content = "value")]
34pub enum TokenKind {
35 Word(Option<WordMetadata>),
37 Punctuation(Punctuation),
38 Decade,
39 Number(Number),
40 Space(usize),
42 Newline(usize),
44 EmailAddress,
45 Url,
46 Hostname,
47 #[default]
50 Unlintable,
51 ParagraphBreak,
52 Regexish,
53}
54
55impl TokenKind {
56 delegate_to_metadata! {
58 is_nominal,
60 is_noun,
61 is_pronoun,
62 is_proper_noun,
63 is_singular_nominal,
64 is_plural_nominal,
65 is_possessive_nominal,
66 is_non_plural_nominal,
67 is_singular_noun,
68 is_plural_noun,
69 is_non_plural_noun,
70 is_countable_noun,
71 is_non_countable_noun,
72 is_mass_noun,
73 is_mass_noun_only,
74 is_non_mass_noun,
75 is_singular_pronoun,
76 is_plural_pronoun,
77 is_non_plural_pronoun,
78 is_reflexive_pronoun,
79 is_first_person_singular_pronoun,
80 is_first_person_plural_pronoun,
81 is_second_person_pronoun,
82 is_third_person_pronoun,
83 is_third_person_singular_pronoun,
84 is_third_person_plural_pronoun,
85 is_object_pronoun,
86 is_possessive_noun,
87 is_possessive_pronoun,
88
89 is_verb,
91 is_auxiliary_verb,
92 is_linking_verb,
93 is_verb_lemma,
94 is_verb_past_form,
95 is_verb_progressive_form,
96 is_verb_third_person_singular_present_form,
97
98 is_adjective,
100 is_comparative_adjective,
101 is_superlative_adjective,
102 is_positive_adjective,
103
104 is_adverb,
106
107 is_determiner,
109 is_demonstrative_determiner,
110 is_possessive_determiner,
111 is_quantifier,
112 is_non_quantifier_determiner,
113
114 is_conjunction,
116
117 is_swear,
119 is_likely_homograph,
120
121 is_lowercase,
123 is_titlecase,
124 is_allcaps,
125 is_lower_camel,
126 is_upper_camel
127 }
128
129 pub fn is_preposition(&self) -> bool {
131 let Word(Some(metadata)) = self else {
132 return false;
133 };
134 metadata.preposition
135 }
136
137 pub fn is_common_word(&self) -> bool {
138 let Word(Some(metadata)) = self else {
139 return true;
140 };
141 metadata.common
142 }
143
144 pub fn is_oov(&self) -> bool {
149 matches!(self, TokenKind::Word(None))
150 }
151
152 pub fn is_open_square(&self) -> bool {
155 matches!(self, TokenKind::Punctuation(Punctuation::OpenSquare))
156 }
157
158 pub fn is_close_square(&self) -> bool {
159 matches!(self, TokenKind::Punctuation(Punctuation::CloseSquare))
160 }
161
162 pub fn is_open_round(&self) -> bool {
163 matches!(self, TokenKind::Punctuation(Punctuation::OpenRound))
164 }
165
166 pub fn is_close_round(&self) -> bool {
167 matches!(self, TokenKind::Punctuation(Punctuation::CloseRound))
168 }
169
170 pub fn is_pipe(&self) -> bool {
171 matches!(self, TokenKind::Punctuation(Punctuation::Pipe))
172 }
173
174 pub fn is_currency(&self) -> bool {
175 matches!(self, TokenKind::Punctuation(Punctuation::Currency(..)))
176 }
177
178 pub fn is_ellipsis(&self) -> bool {
179 matches!(self, TokenKind::Punctuation(Punctuation::Ellipsis))
180 }
181
182 pub fn is_hyphen(&self) -> bool {
183 matches!(self, TokenKind::Punctuation(Punctuation::Hyphen))
184 }
185
186 pub fn is_quote(&self) -> bool {
187 matches!(self, TokenKind::Punctuation(Punctuation::Quote(_)))
188 }
189
190 pub fn is_apostrophe(&self) -> bool {
191 matches!(self, TokenKind::Punctuation(Punctuation::Apostrophe))
192 }
193
194 pub fn is_period(&self) -> bool {
195 matches!(self, TokenKind::Punctuation(Punctuation::Period))
196 }
197
198 pub fn is_at(&self) -> bool {
199 matches!(self, TokenKind::Punctuation(Punctuation::At))
200 }
201
202 pub fn is_comma(&self) -> bool {
203 matches!(self, TokenKind::Punctuation(Punctuation::Comma))
204 }
205
206 pub fn is_semicolon(&self) -> bool {
207 matches!(self, TokenKind::Punctuation(Punctuation::Semicolon))
208 }
209
210 pub fn is_ampersand(&self) -> bool {
211 matches!(self, TokenKind::Punctuation(Punctuation::Ampersand))
212 }
213
214 pub fn is_word_like(&self) -> bool {
219 matches!(
220 self,
221 TokenKind::Word(..)
222 | TokenKind::EmailAddress
223 | TokenKind::Hostname
224 | TokenKind::Decade
225 | TokenKind::Number(..)
226 )
227 }
228
229 pub(crate) fn is_chunk_terminator(&self) -> bool {
230 if self.is_sentence_terminator() {
231 return true;
232 }
233
234 match self {
235 TokenKind::Punctuation(punct) => {
236 matches!(
237 punct,
238 Punctuation::Comma | Punctuation::Quote { .. } | Punctuation::Colon
239 )
240 }
241 _ => false,
242 }
243 }
244
245 pub(crate) fn is_sentence_terminator(&self) -> bool {
246 match self {
247 TokenKind::Punctuation(punct) => [
248 Punctuation::Period,
249 Punctuation::Bang,
250 Punctuation::Question,
251 ]
252 .contains(punct),
253 TokenKind::ParagraphBreak => true,
254 _ => false,
255 }
256 }
257
258 pub fn is_case_separator(&self) -> bool {
262 matches!(self, TokenKind::Punctuation(Punctuation::Underscore))
263 || matches!(self, TokenKind::Punctuation(Punctuation::Hyphen))
264 }
265
266 pub fn is_whitespace(&self) -> bool {
268 matches!(self, TokenKind::Space(_) | TokenKind::Newline(_))
269 }
270
271 pub fn is_upos(&self, upos: UPOS) -> bool {
272 let Some(Some(meta)) = self.as_word() else {
273 return false;
274 };
275
276 meta.pos_tag == Some(upos)
277 }
278
279 pub fn matches_variant_of(&self, other: &Self) -> bool {
284 self.with_default_data() == other.with_default_data()
285 }
286
287 pub fn with_default_data(&self) -> Self {
291 match self {
292 TokenKind::Word(_) => TokenKind::Word(Default::default()),
293 TokenKind::Punctuation(_) => TokenKind::Punctuation(Default::default()),
294 TokenKind::Number(..) => TokenKind::Number(Default::default()),
295 TokenKind::Space(_) => TokenKind::Space(Default::default()),
296 TokenKind::Newline(_) => TokenKind::Newline(Default::default()),
297 _ => self.clone(),
298 }
299 }
300
301 pub fn blank_word() -> Self {
303 Self::Word(None)
304 }
305
306 pub fn as_mut_quote(&mut self) -> Option<&mut Quote> {
309 self.as_mut_punctuation()?.as_mut_quote()
310 }
311
312 pub fn as_quote(&self) -> Option<&Quote> {
313 self.as_punctuation()?.as_quote()
314 }
315}
316
317#[cfg(test)]
318mod tests {
319 use crate::Document;
320
321 #[test]
322 fn car_is_singular_noun() {
323 let doc = Document::new_plain_english_curated("car");
324 let tk = &doc.tokens().next().unwrap().kind;
325 assert!(tk.is_singular_noun());
326 }
327
328 #[test]
329 fn traffic_is_mass_noun_only() {
330 let doc = Document::new_plain_english_curated("traffic");
331 let tk = &doc.tokens().next().unwrap().kind;
332 assert!(tk.is_mass_noun_only());
333 }
334
335 #[test]
336 fn equipment_is_mass_noun() {
337 let doc = Document::new_plain_english_curated("equipment");
338 let tk = &doc.tokens().next().unwrap().kind;
339 assert!(tk.is_mass_noun());
340 }
341
342 #[test]
343 fn equipment_is_non_countable_noun() {
344 let doc = Document::new_plain_english_curated("equipment");
345 let tk = &doc.tokens().next().unwrap().kind;
346 assert!(tk.is_non_countable_noun());
347 }
348
349 #[test]
350 fn equipment_isnt_countable_noun() {
351 let doc = Document::new_plain_english_curated("equipment");
352 let tk = &doc.tokens().next().unwrap().kind;
353 assert!(!tk.is_countable_noun());
354 }
355}