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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
use std::fmt;
use std::ops;
use std::slice;
use std::vec;
use features::Features;
#[derive(Clone, Debug,PartialEq)]
pub struct Sentence {
tokens: Vec<Token>,
}
impl Sentence {
pub fn new(tokens: Vec<Token>) -> Sentence {
Sentence { tokens: tokens }
}
pub fn as_tokens(&self) -> &[Token] {
&self.tokens
}
pub fn iter(&self) -> slice::Iter<Token> {
self.tokens.iter()
}
pub fn iter_mut(&mut self) -> slice::IterMut<Token> {
self.tokens.iter_mut()
}
}
impl fmt::Display for Sentence {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let last_id = self.tokens.len() - 1;
for (id, token) in self.iter().enumerate() {
if id == last_id {
write!(f, "{}\t{}", id + 1, token)?
} else {
write!(f, "{}\t{}\n", id + 1, token)?
}
}
Ok(())
}
}
impl ops::Index<usize> for Sentence {
type Output = Token;
fn index(&self, index: usize) -> &Token {
&self.tokens[index]
}
}
impl ops::IndexMut<usize> for Sentence {
fn index_mut(&mut self, index: usize) -> &mut Token {
&mut self.tokens[index]
}
}
impl IntoIterator for Sentence {
type Item = Token;
type IntoIter = vec::IntoIter<Token>;
fn into_iter(self) -> vec::IntoIter<Token> {
self.tokens.into_iter()
}
}
impl<'a> IntoIterator for &'a Sentence {
type Item = &'a Token;
type IntoIter = slice::Iter<'a, Token>;
fn into_iter(self) -> slice::Iter<'a, Token> {
self.iter()
}
}
impl<'a> IntoIterator for &'a mut Sentence {
type Item = &'a mut Token;
type IntoIter = slice::IterMut<'a, Token>;
fn into_iter(mut self) -> slice::IterMut<'a, Token> {
self.iter_mut()
}
}
pub struct TokenBuilder {
token: Token,
}
impl TokenBuilder {
pub fn new() -> TokenBuilder {
Default::default()
}
pub fn form<S>(mut self, form: S) -> TokenBuilder
where S: Into<String>
{
self.token.set_form(Some(form));
self
}
pub fn lemma<S>(mut self, lemma: S) -> TokenBuilder
where S: Into<String>
{
self.token.set_lemma(Some(lemma));
self
}
pub fn cpos<S>(mut self, cpos: S) -> TokenBuilder
where S: Into<String>
{
self.token.set_cpos(Some(cpos));
self
}
pub fn pos<S>(mut self, pos: S) -> TokenBuilder
where S: Into<String>
{
self.token.set_pos(Some(pos));
self
}
pub fn features(mut self, features: Features) -> TokenBuilder {
self.token.set_features(Some(features));
self
}
pub fn head(mut self, head: usize) -> TokenBuilder {
self.token.set_head(Some(head));
self
}
pub fn head_rel<S>(mut self, head_rel: S) -> TokenBuilder
where S: Into<String>
{
self.token.set_head_rel(Some(head_rel));
self
}
pub fn p_head(mut self, p_head: usize) -> TokenBuilder {
self.token.set_p_head(Some(p_head));
self
}
pub fn p_head_rel<S>(mut self, p_head_rel: S) -> TokenBuilder
where S: Into<String>
{
self.token.set_p_head_rel(Some(p_head_rel));
self
}
pub fn token(self) -> Token {
self.token
}
}
impl Default for TokenBuilder {
fn default() -> Self {
TokenBuilder { token: Default::default() }
}
}
#[derive(Clone,Debug,PartialEq)]
pub struct Token {
form: Option<String>,
lemma: Option<String>,
cpos: Option<String>,
pos: Option<String>,
features: Option<Features>,
head: Option<usize>,
head_rel: Option<String>,
p_head: Option<usize>,
p_head_rel: Option<String>,
}
impl Token {
pub fn new() -> Token {
Default::default()
}
pub fn form(&self) -> Option<&str> {
self.form.as_ref().map(String::as_ref)
}
pub fn lemma(&self) -> Option<&str> {
self.lemma.as_ref().map(String::as_ref)
}
pub fn cpos(&self) -> Option<&str> {
self.cpos.as_ref().map(String::as_ref)
}
pub fn pos(&self) -> Option<&str> {
self.pos.as_ref().map(String::as_ref)
}
pub fn features(&self) -> Option<&Features> {
self.features.as_ref()
}
pub fn head(&self) -> Option<usize> {
self.head
}
pub fn head_rel(&self) -> Option<&str> {
self.head_rel.as_ref().map(String::as_ref)
}
pub fn p_head(&self) -> Option<usize> {
self.p_head
}
pub fn p_head_rel(&self) -> Option<&str> {
self.p_head_rel.as_ref().map(String::as_ref)
}
pub fn set_form<S>(&mut self, form: Option<S>)
where S: Into<String>
{
self.form = form.map(|i| i.into())
}
pub fn set_lemma<S>(&mut self, lemma: Option<S>)
where S: Into<String>
{
self.lemma = lemma.map(|i| i.into())
}
pub fn set_cpos<S>(&mut self, cpos: Option<S>)
where S: Into<String>
{
self.cpos = cpos.map(|i| i.into())
}
pub fn set_pos<S>(&mut self, pos: Option<S>)
where S: Into<String>
{
self.pos = pos.map(|i| i.into())
}
pub fn set_features(&mut self, features: Option<Features>) {
self.features = features
}
pub fn set_head(&mut self, head: Option<usize>) {
self.head = head
}
pub fn set_head_rel<S>(&mut self, head_rel: Option<S>)
where S: Into<String>
{
self.head_rel = head_rel.map(|i| i.into())
}
pub fn set_p_head(&mut self, p_head: Option<usize>) {
self.p_head = p_head
}
pub fn set_p_head_rel<S>(&mut self, p_head_rel: Option<S>)
where S: Into<String>
{
self.p_head_rel = p_head_rel.map(|i| i.into())
}
}
impl Default for Token {
fn default() -> Self {
Token {
form: None,
lemma: None,
cpos: None,
pos: None,
features: None,
head: None,
head_rel: None,
p_head: None,
p_head_rel: None,
}
}
}
pub const EMPTY_TOKEN: &'static str = "_";
impl fmt::Display for Token {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let head_str = self.head.as_ref().map(|n| n.to_string());
let p_head_str = self.p_head.as_ref().map(|n| n.to_string());
write!(f,
"{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}\t{}",
self.form.as_ref().map(|s| s.as_ref()).unwrap_or(EMPTY_TOKEN),
self.lemma.as_ref().map(|s| s.as_ref()).unwrap_or(EMPTY_TOKEN),
self.cpos.as_ref().map(|s| s.as_ref()).unwrap_or(EMPTY_TOKEN),
self.pos.as_ref().map(|s| s.as_ref()).unwrap_or(EMPTY_TOKEN),
self.features.as_ref().map(|s| s.as_str()).unwrap_or(EMPTY_TOKEN),
head_str.as_ref().map(|s| s.as_ref()).unwrap_or(EMPTY_TOKEN),
self.head_rel.as_ref().map(|s| s.as_ref()).unwrap_or(EMPTY_TOKEN),
self.p_head.clone().map(|n| n.to_string()).unwrap_or(EMPTY_TOKEN.to_string()),
p_head_str.as_ref().map(|s| s.as_ref()).unwrap_or(EMPTY_TOKEN))
}
}