sticker_encoders/
lib.rs

1//! Label encoders.
2
3use std::error::Error;
4
5use conllu::graph::Sentence;
6
7pub mod categorical;
8
9pub mod deprel;
10
11pub mod layer;
12
13pub mod lemma;
14
15/// An encoding with its probability.
16#[derive(Debug)]
17pub struct EncodingProb<E> {
18    encoding: E,
19    prob: f32,
20}
21
22impl<E> EncodingProb<E>
23where
24    E: ToOwned,
25{
26    /// Create an encoding with its probability.
27    ///
28    /// This constructor takes an owned encoding.
29    pub fn new(encoding: E, prob: f32) -> Self {
30        EncodingProb { encoding, prob }
31    }
32
33    /// Get the encoding.
34    pub fn encoding(&self) -> &E {
35        &self.encoding
36    }
37
38    /// Get the probability of the encoding.
39    pub fn prob(&self) -> f32 {
40        self.prob
41    }
42}
43
44impl<E> From<EncodingProb<E>> for (String, f32)
45where
46    E: Clone + ToString,
47{
48    fn from(prob: EncodingProb<E>) -> Self {
49        (prob.encoding().to_string(), prob.prob())
50    }
51}
52
53/// Trait for sentence decoders.
54///
55/// A sentence decoder adds a representation to each token in a
56/// sentence, such as a part-of-speech tag or a topological field.
57pub trait SentenceDecoder {
58    type Encoding: ToOwned;
59
60    /// The decoding error type.
61    type Error: Error;
62
63    fn decode<S>(&self, labels: &[S], sentence: &mut Sentence) -> Result<(), Self::Error>
64    where
65        S: AsRef<[EncodingProb<Self::Encoding>]>;
66}
67
68/// Trait for sentence encoders.
69///
70/// A sentence encoder extracts a representation of each token in a
71/// sentence, such as a part-of-speech tag or a topological field.
72pub trait SentenceEncoder {
73    type Encoding;
74
75    /// The encoding error type.
76    type Error: Error;
77
78    /// Encode the given sentence.
79    fn encode(&self, sentence: &Sentence) -> Result<Vec<Self::Encoding>, Self::Error>;
80}