harper_core/patterns/
upos_set.rs

1use harper_brill::UPOS;
2use serde::{Deserialize, Serialize};
3use smallvec::{SmallVec, ToSmallVec};
4
5use crate::Token;
6
7use super::Pattern;
8
9#[derive(Debug, Serialize, Deserialize, Clone)]
10pub struct UPOSSet {
11    allowed_tags: SmallVec<[UPOS; 10]>,
12}
13
14impl UPOSSet {
15    pub fn new(allowed: &[UPOS]) -> Self {
16        Self {
17            allowed_tags: allowed.to_smallvec(),
18        }
19    }
20}
21
22impl Pattern for UPOSSet {
23    fn matches(&self, tokens: &[Token], _source: &[char]) -> Option<usize> {
24        tokens.first()?.kind.as_word()?.as_ref().and_then(|w| {
25            if self.allowed_tags.contains(&(w.pos_tag?)) {
26                Some(1)
27            } else {
28                None
29            }
30        })
31    }
32}