1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
//! The error type of this crate.
use std::fmt;
use std::io;
/// The result type of this crate.
pub type Result<T> = std::result::Result<T, Error>;
/// An error of this crate.
#[derive(Debug)]
#[non_exhaustive]
pub enum Error {
/// The input is not in the lattice format.
Format {
/// The number of the line, from 1.
line: usize,
/// What is wrong with the line.
message: String,
},
/// A sentence is not one the lattice format can hold: its bunsetsu do not
/// cover its morphemes, a head or a position is out of range, or a
/// string holds a tab or a line break.
Structure(String),
/// Reading or writing failed.
Io(io::Error),
/// The chunker was given something it cannot use: a template or a list
/// of columns that cannot be read, weights that are not finite, or
/// training data that cannot be trained on.
Chunker(String),
/// Bytes are not a model file this crate reads: another magic number or
/// version, bytes that end early or go on after the end, or content
/// that is not what the format says.
Model(String),
/// A model was used with another dictionary than the one it was trained
/// for: the name of the dictionary differs, or a morpheme has fewer
/// feature columns than the schema of the model reads.
Dictionary(String),
/// No model is embedded in the build for the dictionary asked for: no
/// feature embeds one for it, the build is without the feature, or it
/// is a documentation build, which embeds an empty file.
Embedded(String),
/// The parser was given something it cannot use: a feature set that
/// cannot be read, or weights that are not finite.
Parser(String),
/// A schema was given something it cannot hold: rules of
/// normalization that are not a table.
Schema(String),
/// A model of CaboCha cannot be converted: a file is not a text model
/// of CaboCha, or it is a model this crate has no schema for.
#[cfg(feature = "train")]
Cabocha(String),
/// The input is not a CoNLL-U file the corpus conversion can read.
#[cfg(feature = "train")]
Conllu {
/// The number of the line, from 1.
line: usize,
/// What is wrong with the line.
message: String,
},
/// The tokenizer given to the corpus conversion failed.
#[cfg(feature = "train")]
Tokenizer(Box<dyn std::error::Error + Send + Sync>),
/// The files given to the evaluation cannot be compared: they do not
/// hold the same number of sentences.
#[cfg(feature = "train")]
Evaluation(String),
}
impl Error {
/// Makes a [`Error::Format`].
///
/// # Arguments
///
/// * `line` - The number of the line, from 1.
/// * `message` - What is wrong with the line.
///
/// # Returns
///
/// The error.
pub(crate) fn format(line: usize, message: impl Into<String>) -> Self {
Error::Format {
line,
message: message.into(),
}
}
/// Makes a [`Error::Conllu`].
///
/// # Arguments
///
/// * `line` - The number of the line, from 1.
/// * `message` - What is wrong with the line.
///
/// # Returns
///
/// The error.
#[cfg(feature = "train")]
pub(crate) fn conllu(line: usize, message: impl Into<String>) -> Self {
Error::Conllu {
line,
message: message.into(),
}
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Error::Format { line, message } => write!(f, "line {line}: {message}"),
Error::Structure(message) => write!(f, "invalid sentence: {message}"),
Error::Io(error) => write!(f, "{error}"),
Error::Chunker(message) => write!(f, "{message}"),
Error::Model(message) => write!(f, "{message}"),
Error::Dictionary(message) => write!(f, "{message}"),
Error::Embedded(message) => write!(f, "{message}"),
Error::Parser(message) => write!(f, "{message}"),
Error::Schema(message) => write!(f, "{message}"),
#[cfg(feature = "train")]
Error::Cabocha(message) => write!(f, "{message}"),
#[cfg(feature = "train")]
Error::Conllu { line, message } => write!(f, "line {line}: {message}"),
#[cfg(feature = "train")]
Error::Tokenizer(error) => write!(f, "tokenizer: {error}"),
#[cfg(feature = "train")]
Error::Evaluation(message) => write!(f, "{message}"),
}
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Error::Io(error) => Some(error),
#[cfg(feature = "train")]
Error::Tokenizer(error) => Some(error.as_ref()),
_ => None,
}
}
}
impl From<io::Error> for Error {
fn from(error: io::Error) -> Self {
Error::Io(error)
}
}