syntaxdot_encoders/
lib.rs

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