Skip to main content

harper_pos_utils/
upos.rs

1use std::fmt::Display;
2
3use is_macro::Is;
4use serde::{Deserialize, Serialize};
5use strum_macros::{AsRefStr, EnumIter, EnumString};
6
7/// Represents the universal parts of speech as outlined by [universaldependencies.org](https://universaldependencies.org/u/pos/index.html).
8#[derive(
9    EnumString,
10    Debug,
11    Default,
12    Hash,
13    Eq,
14    PartialEq,
15    Clone,
16    Copy,
17    EnumIter,
18    AsRefStr,
19    Serialize,
20    Deserialize,
21    PartialOrd,
22    Ord,
23    Is,
24)]
25pub enum UPOS {
26    /// Adjective
27    ADJ,
28    /// Adposition
29    ADP,
30    /// Adverb
31    ADV,
32    /// Auxiliary
33    AUX,
34    /// Coordinating conjunction
35    CCONJ,
36    /// Determiner
37    DET,
38    /// Interjection
39    INTJ,
40    /// Noun
41    #[default]
42    NOUN,
43    /// Numeral
44    NUM,
45    /// Particle
46    PART,
47    /// Pronoun
48    PRON,
49    /// Proper noun
50    PROPN,
51    /// Punctuation
52    PUNCT,
53    /// Subordinating conjunction
54    SCONJ,
55    /// Symbol
56    SYM,
57    /// Verb
58    VERB,
59}
60
61impl UPOS {
62    pub fn from_conllu(other: rs_conllu::UPOS) -> Option<Self> {
63        Some(match other {
64            rs_conllu::UPOS::ADJ => UPOS::ADJ,
65            rs_conllu::UPOS::ADP => UPOS::ADP,
66            rs_conllu::UPOS::ADV => UPOS::ADV,
67            rs_conllu::UPOS::AUX => UPOS::AUX,
68            rs_conllu::UPOS::CCONJ => UPOS::CCONJ,
69            rs_conllu::UPOS::DET => UPOS::DET,
70            rs_conllu::UPOS::INTJ => UPOS::INTJ,
71            rs_conllu::UPOS::NOUN => UPOS::NOUN,
72            rs_conllu::UPOS::NUM => UPOS::NUM,
73            rs_conllu::UPOS::PART => UPOS::PART,
74            rs_conllu::UPOS::PRON => UPOS::PRON,
75            rs_conllu::UPOS::PROPN => UPOS::PROPN,
76            rs_conllu::UPOS::PUNCT => UPOS::PUNCT,
77            rs_conllu::UPOS::SCONJ => UPOS::SCONJ,
78            rs_conllu::UPOS::SYM => UPOS::SYM,
79            rs_conllu::UPOS::VERB => UPOS::VERB,
80            rs_conllu::UPOS::X => return None,
81        })
82    }
83
84    pub fn is_nominal(&self) -> bool {
85        matches!(self, Self::NOUN | Self::PROPN | Self::PRON)
86    }
87}
88
89impl Display for UPOS {
90    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
91        let desc = match self {
92            UPOS::ADJ => "Adjective",
93            UPOS::ADP => "Adposition",
94            UPOS::ADV => "Adverb",
95            UPOS::AUX => "Auxiliary",
96            UPOS::CCONJ => "Coordinating conjunction",
97            UPOS::DET => "Determiner",
98            UPOS::INTJ => "Interjection",
99            UPOS::NOUN => "Noun",
100            UPOS::NUM => "Numeral",
101            UPOS::PART => "Particle",
102            UPOS::PRON => "Pronoun",
103            UPOS::PROPN => "Proper noun",
104            UPOS::PUNCT => "Punctuation",
105            UPOS::SCONJ => "Subordinating conjunction",
106            UPOS::SYM => "Symbol",
107            UPOS::VERB => "Verb",
108        };
109        write!(f, "{desc}")
110    }
111}