33af60bb2ed3aceafc751e3f3797e297d054c864 — DaniĆ«l de Kok 4 months ago 51316a1
Improve encoder error handling
R sticker/src/encoder/deprel/errors.rs => sticker/src/encoder/deprel/error.rs +7 -7
@@ 1,19 1,19 @@
 use failure::Fail;
 
 /// Encoder errors.
-#[derive(Clone, Copy, Debug, Eq, Fail, PartialEq)]
+#[derive(Clone, Debug, Eq, Fail, PartialEq)]
 pub enum EncodeError {
     /// The token does not have a head.
-    #[fail(display = "missing head")]
-    MissingHead,
+    #[fail(display = "missing head: '{}'", form)]
+    MissingHead { form: String },
 
     /// The token does not have a part-of-speech.
-    #[fail(display = "missing part-of-speech tag")]
-    MissingPOS,
+    #[fail(display = "missing part-of-speech tag: '{}'", form)]
+    MissingPOS { form: String },
 
     /// The token does not have a dependency relation.
-    #[fail(display = "missing dependency relation")]
-    MissingRelation,
+    #[fail(display = "missing dependency relation: '{}'", form)]
+    MissingRelation { form: String },
 }
 
 /// Decoder errors.

M sticker/src/encoder/deprel/mod.rs => sticker/src/encoder/deprel/mod.rs +2 -2
@@ 2,8 2,8 @@
 
 use serde_derive::{Deserialize, Serialize};
 
-mod errors;
-pub use self::errors::*;
+mod error;
+pub use self::error::*;
 
 mod post_processing;
 pub(crate) use self::post_processing::*;

M sticker/src/encoder/deprel/relative_pos.rs => sticker/src/encoder/deprel/relative_pos.rs +13 -7
@@ 142,20 142,26 @@ impl SentenceEncoder for RelativePOSEncoder {
 
         let mut encoded = Vec::with_capacity(sentence.len());
         for idx in 0..sentence.len() {
-            let token = &sentence[idx];
-            if token.is_root() {
-                continue;
-            }
+            let form = match &sentence[idx] {
+                Node::Root => continue,
+                Node::Token(token) => token.form(),
+            };
 
             let triple = sentence
                 .dep_graph()
                 .head(idx)
-                .ok_or(EncodeError::MissingHead)?;
-            let relation = triple.relation().ok_or(EncodeError::MissingRelation)?;
+                .ok_or(EncodeError::MissingHead {
+                    form: form.to_owned(),
+                })?;
+            let relation = triple.relation().ok_or(EncodeError::MissingRelation {
+                form: form.to_owned(),
+            })?;
 
             let head_pos = match &sentence[triple.head()] {
                 Node::Root => "ROOT",
-                Node::Token(head_token) => head_token.pos().ok_or(EncodeError::MissingPOS)?,
+                Node::Token(head_token) => head_token.pos().ok_or(EncodeError::MissingPOS {
+                    form: form.to_owned(),
+                })?,
             };
 
             let position = Self::relative_dependent_position(

M sticker/src/encoder/deprel/relative_position.rs => sticker/src/encoder/deprel/relative_position.rs +11 -7
@@ 1,4 1,4 @@
-use conllx::graph::{DepTriple, Sentence};
+use conllx::graph::{DepTriple, Node, Sentence};
 use failure::Error;
 use serde_derive::{Deserialize, Serialize};
 


@@ 57,16 57,20 @@ impl SentenceEncoder for RelativePositionEncoder {
     fn encode(&mut self, sentence: &Sentence) -> Result<Vec<Self::Encoding>, Error> {
         let mut encoded = Vec::with_capacity(sentence.len());
         for idx in 0..sentence.len() {
-            let token = &sentence[idx];
-            if token.is_root() {
-                continue;
-            }
+            let form = match &sentence[idx] {
+                Node::Root => continue,
+                Node::Token(token) => token.form(),
+            };
 
             let triple = sentence
                 .dep_graph()
                 .head(idx)
-                .ok_or(EncodeError::MissingHead)?;
-            let relation = triple.relation().ok_or(EncodeError::MissingRelation)?;
+                .ok_or(EncodeError::MissingHead {
+                    form: form.to_owned(),
+                })?;
+            let relation = triple.relation().ok_or(EncodeError::MissingRelation {
+                form: form.to_owned(),
+            })?;
 
             encoded.push(DependencyEncoding {
                 label: relation.to_owned(),

A sticker/src/encoder/layer/error.rs => sticker/src/encoder/layer/error.rs +8 -0
@@ 0,0 1,8 @@
+use failure::Fail;
+
+#[derive(Clone, Debug, Eq, Fail, PartialEq)]
+pub enum EncodeError {
+    /// The token does not have a label.
+    #[fail(display = "token without a label: '{}'", form)]
+    MissingLabel { form: String },
+}

M sticker/src/encoder/layer/mod.rs => sticker/src/encoder/layer/mod.rs +7 -2
@@ 1,11 1,14 @@
 //! CoNLL-X layer encoder.
 
 use conllx::graph::{Node, Sentence};
-use failure::{format_err, Error};
+use failure::Error;
 
 use super::{EncodingProb, SentenceDecoder, SentenceEncoder};
 use crate::{Layer, LayerValue};
 
+mod error;
+use self::error::*;
+
 /// Encode sentences using a CoNLL-X layer.
 #[derive(Clone, Debug, Eq, PartialEq)]
 pub struct LayerEncoder {


@@ 55,7 58,9 @@ impl SentenceEncoder for LayerEncoder {
         for token in sentence.iter().filter_map(Node::token) {
             let label = token
                 .value(&self.layer)
-                .ok_or_else(|| format_err!("Token without a label: {}", token.form()))?;
+                .ok_or_else(|| EncodeError::MissingLabel {
+                    form: token.form().to_owned(),
+                })?;
             encoding.push(label.to_owned());
         }