vibrato 0.5.2

Vibrato: viterbi-based accelerated tokenizer
Documentation
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
use std::num::NonZeroU32;
use std::ops::Range;

use bincode::{
    de::Decoder,
    enc::Encoder,
    error::{DecodeError, EncodeError},
    Decode, Encode,
};
use hashbrown::HashMap;
use regex::Regex;

#[derive(Debug, Decode, Encode)]
enum FeatureType {
    Index(usize),
    CharacterType,
}

#[derive(Debug, Decode, Encode)]
struct ParsedTemplate {
    raw_template: String,
    required_indices: Vec<usize>,
    captures: Vec<(Range<usize>, FeatureType)>,
}

pub struct FeatureExtractor {
    pub unigram_feature_ids: HashMap<String, NonZeroU32>,
    pub left_feature_ids: HashMap<String, NonZeroU32>,
    pub right_feature_ids: HashMap<String, NonZeroU32>,
    unigram_next_id: u32,
    left_next_id: u32,
    right_next_id: u32,
    unigram_templates: Vec<ParsedTemplate>,
    left_templates: Vec<ParsedTemplate>,
    right_templates: Vec<ParsedTemplate>,
}

impl FeatureExtractor {
    pub fn new<S>(unigram_templates: &[S], bigram_templates: &[(S, S)]) -> Self
    where
        S: ToString,
    {
        let unigram_feature_pattern = Regex::new(r"%((F|F\?)\[([0-9]+)\]|t)").unwrap();
        let left_feature_pattern = Regex::new(r"%(L|L\?)\[([0-9]+)\]").unwrap();
        let right_feature_pattern = Regex::new(r"%(R|R\?)\[([0-9]+)\]").unwrap();

        let mut unigram_parsed_templates = vec![];
        for template in unigram_templates {
            let raw_template = template.to_string();
            let mut required_indices = vec![];
            let mut captures = vec![];
            for m in unigram_feature_pattern.captures_iter(&raw_template) {
                let pattern = m.get(0).unwrap();
                if m.get(1).unwrap().as_str() == "t" {
                    captures.push((pattern.start()..pattern.end(), FeatureType::CharacterType));
                } else {
                    let idx: usize = m.get(3).unwrap().as_str().parse().unwrap();
                    match m.get(2).unwrap().as_str() {
                        "F" => {
                            captures
                                .push((pattern.start()..pattern.end(), FeatureType::Index(idx)));
                        }
                        "F?" => {
                            required_indices.push(idx);
                            captures
                                .push((pattern.start()..pattern.end(), FeatureType::Index(idx)));
                        }
                        _ => unreachable!(),
                    }
                }
            }
            unigram_parsed_templates.push(ParsedTemplate {
                raw_template,
                required_indices,
                captures,
            });
        }

        let mut left_parsed_templates = vec![];
        let mut right_parsed_templates = vec![];
        for (left_template, right_template) in bigram_templates {
            {
                let raw_template = left_template.to_string();
                let mut required_indices = vec![];
                let mut captures = vec![];
                for m in left_feature_pattern.captures_iter(&raw_template) {
                    let pattern = m.get(0).unwrap();
                    let idx: usize = m.get(2).unwrap().as_str().parse().unwrap();
                    match m.get(1).unwrap().as_str() {
                        "L" => {
                            captures
                                .push((pattern.start()..pattern.end(), FeatureType::Index(idx)));
                        }
                        "L?" => {
                            required_indices.push(idx);
                            captures
                                .push((pattern.start()..pattern.end(), FeatureType::Index(idx)));
                        }
                        _ => unreachable!(),
                    }
                }
                left_parsed_templates.push(ParsedTemplate {
                    raw_template,
                    required_indices,
                    captures,
                });
            }
            {
                let raw_template = right_template.to_string();
                let mut required_indices = vec![];
                let mut captures = vec![];
                for m in right_feature_pattern.captures_iter(&raw_template) {
                    let pattern = m.get(0).unwrap();
                    let idx: usize = m.get(2).unwrap().as_str().parse().unwrap();
                    match m.get(1).unwrap().as_str() {
                        "R" => {
                            captures
                                .push((pattern.start()..pattern.end(), FeatureType::Index(idx)));
                        }
                        "R?" => {
                            required_indices.push(idx);
                            captures
                                .push((pattern.start()..pattern.end(), FeatureType::Index(idx)));
                        }
                        _ => unreachable!(),
                    }
                }
                right_parsed_templates.push(ParsedTemplate {
                    raw_template,
                    required_indices,
                    captures,
                });
            }
        }

        Self {
            unigram_feature_ids: HashMap::new(),
            left_feature_ids: HashMap::new(),
            right_feature_ids: HashMap::new(),
            unigram_next_id: 1,
            left_next_id: 1,
            right_next_id: 1,
            unigram_templates: unigram_parsed_templates,
            left_templates: left_parsed_templates,
            right_templates: right_parsed_templates,
        }
    }

    /// Inserts feature patterns matched to the input templates in the hash map,
    /// while incrementally assigning new feature ids.
    /// Returns a sequence of ids of found features.
    /// Since the contents of `feature_ids` may be changed outside the function, the next ID to be
    /// given should be specified by `next_id`.
    fn extract_feature_ids<S>(
        features: &[S],
        templates: &[ParsedTemplate],
        feature_ids: &mut HashMap<String, NonZeroU32>,
        next_id: &mut u32,
        category_id: u32,
    ) -> Vec<Option<NonZeroU32>>
    where
        S: AsRef<str>,
    {
        let mut result = vec![];
        'a: for template in templates {
            for &required_idx in &template.required_indices {
                if features.get(required_idx).map_or("*", |f| f.as_ref()) == "*" {
                    result.push(None);
                    continue 'a;
                }
            }
            let mut feature_string = String::new();
            let mut start = 0;
            for (range, feature) in &template.captures {
                feature_string.push_str(&template.raw_template[start..range.start]);
                match feature {
                    FeatureType::Index(idx) => {
                        feature_string.push_str(features.get(*idx).map_or("*", |f| f.as_ref()));
                    }
                    FeatureType::CharacterType => {
                        feature_string.push_str(&category_id.to_string());
                    }
                }
                start = range.end;
            }
            feature_string.push_str(&template.raw_template[start..]);
            let new_id = NonZeroU32::new(*next_id).unwrap();
            let feature_id = *feature_ids.entry(feature_string).or_insert(new_id);
            if new_id == feature_id {
                *next_id += 1;
            }
            result.push(Some(feature_id));
        }
        result
    }

    pub fn extract_unigram_feature_ids<S>(
        &mut self,
        features: &[S],
        category_id: u32,
    ) -> Vec<NonZeroU32>
    where
        S: AsRef<str>,
    {
        Self::extract_feature_ids(
            features,
            &self.unigram_templates,
            &mut self.unigram_feature_ids,
            &mut self.unigram_next_id,
            category_id,
        )
        .into_iter()
        .flatten()
        .collect()
    }

    pub fn extract_left_feature_ids<S>(&mut self, features: &[S]) -> Vec<Option<NonZeroU32>>
    where
        S: AsRef<str>,
    {
        Self::extract_feature_ids(
            features,
            &self.left_templates,
            &mut self.left_feature_ids,
            &mut self.left_next_id,
            0,
        )
    }

    pub fn extract_right_feature_ids<S>(&mut self, features: &[S]) -> Vec<Option<NonZeroU32>>
    where
        S: AsRef<str>,
    {
        Self::extract_feature_ids(
            features,
            &self.right_templates,
            &mut self.right_feature_ids,
            &mut self.right_next_id,
            0,
        )
    }

    pub const fn left_feature_ids(&self) -> &HashMap<String, NonZeroU32> {
        &self.left_feature_ids
    }

    pub const fn right_feature_ids(&self) -> &HashMap<String, NonZeroU32> {
        &self.right_feature_ids
    }
}

impl<Context> Decode<Context> for FeatureExtractor {
    fn decode<D: Decoder>(decoder: &mut D) -> Result<Self, DecodeError> {
        let unigram_feature_ids: Vec<(String, NonZeroU32)> = Decode::decode(decoder)?;
        let left_feature_ids: Vec<(String, NonZeroU32)> = Decode::decode(decoder)?;
        let right_feature_ids: Vec<(String, NonZeroU32)> = Decode::decode(decoder)?;
        let unigram_next_id = Decode::decode(decoder)?;
        let left_next_id = Decode::decode(decoder)?;
        let right_next_id = Decode::decode(decoder)?;
        let unigram_templates = Decode::decode(decoder)?;
        let left_templates = Decode::decode(decoder)?;
        let right_templates = Decode::decode(decoder)?;
        Ok(Self {
            unigram_feature_ids: unigram_feature_ids.into_iter().collect(),
            left_feature_ids: left_feature_ids.into_iter().collect(),
            right_feature_ids: right_feature_ids.into_iter().collect(),
            unigram_next_id,
            left_next_id,
            right_next_id,
            unigram_templates,
            left_templates,
            right_templates,
        })
    }
}

impl Encode for FeatureExtractor {
    fn encode<E: Encoder>(&self, encoder: &mut E) -> Result<(), EncodeError> {
        let unigram_feature_ids: Vec<(String, NonZeroU32)> =
            self.unigram_feature_ids.clone().into_iter().collect();
        let left_feature_ids: Vec<(String, NonZeroU32)> =
            self.left_feature_ids.clone().into_iter().collect();
        let right_feature_ids: Vec<(String, NonZeroU32)> =
            self.right_feature_ids.clone().into_iter().collect();
        Encode::encode(&unigram_feature_ids, encoder)?;
        Encode::encode(&left_feature_ids, encoder)?;
        Encode::encode(&right_feature_ids, encoder)?;
        Encode::encode(&self.unigram_next_id, encoder)?;
        Encode::encode(&self.left_next_id, encoder)?;
        Encode::encode(&self.right_next_id, encoder)?;
        Encode::encode(&self.unigram_templates, encoder)?;
        Encode::encode(&self.left_templates, encoder)?;
        Encode::encode(&self.right_templates, encoder)?;
        Ok(())
    }
}

#[cfg(test)]
mod test {
    use super::*;

    use crate::test_utils::hashmap;

    fn prepare_extractor() -> FeatureExtractor {
        let unigram_templates = vec![
            "word:%F[0]",
            "word-pos:%F[0],%F[1]",
            "word-pron:%F[0],%F?[2]",
            "word-pos-pron:%F[0],%F[1],%F?[2]",
            "word-type:%F[0],%t",
        ];
        let bigram_templates = vec![
            ("pos:%L[1]", "pos:%R[1]"),
            ("pron:%L?[2]", "pron:%R?[2]"),
            ("pos-pron:%L[1],%L?[2]", "pos-pron:%R[1],%R?[2]"),
        ];

        FeatureExtractor::new(&unigram_templates, &bigram_templates)
    }

    #[test]
    fn test_unigram_feature_extraction() {
        let mut extractor = prepare_extractor();

        let feature_ids = extractor.extract_unigram_feature_ids(&["", "名詞", "ヒト"], 3);
        assert_eq!(
            vec![
                NonZeroU32::new(1).unwrap(),
                NonZeroU32::new(2).unwrap(),
                NonZeroU32::new(3).unwrap(),
                NonZeroU32::new(4).unwrap(),
                NonZeroU32::new(5).unwrap()
            ],
            feature_ids
        );

        let feature_ids = extractor.extract_unigram_feature_ids(&["", "接尾辞", "ジン"], 3);
        assert_eq!(
            vec![
                NonZeroU32::new(1).unwrap(),
                NonZeroU32::new(6).unwrap(),
                NonZeroU32::new(7).unwrap(),
                NonZeroU32::new(8).unwrap(),
                NonZeroU32::new(5).unwrap()
            ],
            feature_ids
        );

        assert_eq!(
            hashmap![
                "word:人".to_string() => NonZeroU32::new(1).unwrap(),
                "word-pos:人,名詞".to_string() => NonZeroU32::new(2).unwrap(),
                "word-pron:人,ヒト".to_string() => NonZeroU32::new(3).unwrap(),
                "word-pos-pron:人,名詞,ヒト".to_string() => NonZeroU32::new(4).unwrap(),
                "word-type:人,3".to_string() => NonZeroU32::new(5).unwrap(),
                "word-pos:人,接尾辞".to_string() => NonZeroU32::new(6).unwrap(),
                "word-pron:人,ジン".to_string() => NonZeroU32::new(7).unwrap(),
                "word-pos-pron:人,接尾辞,ジン".to_string() => NonZeroU32::new(8).unwrap(),
            ],
            extractor.unigram_feature_ids
        );
    }

    #[test]
    fn test_unigram_feature_extraction_undefined() {
        let mut extractor = prepare_extractor();

        let feature_ids = extractor.extract_unigram_feature_ids(&["", "補助記号", "*"], 4);
        assert_eq!(
            vec![
                NonZeroU32::new(1).unwrap(),
                NonZeroU32::new(2).unwrap(),
                NonZeroU32::new(3).unwrap()
            ],
            feature_ids
        );

        let feature_ids = extractor.extract_unigram_feature_ids(&["", "補助記号", "*"], 4);
        assert_eq!(
            vec![
                NonZeroU32::new(4).unwrap(),
                NonZeroU32::new(5).unwrap(),
                NonZeroU32::new(6).unwrap()
            ],
            feature_ids
        );

        assert_eq!(
            hashmap![
                "word:。".to_string() => NonZeroU32::new(1).unwrap(),
                "word-pos:。,補助記号".to_string() => NonZeroU32::new(2).unwrap(),
                "word-type:。,4".to_string() => NonZeroU32::new(3).unwrap(),
                "word:、".to_string() => NonZeroU32::new(4).unwrap(),
                "word-pos:、,補助記号".to_string() => NonZeroU32::new(5).unwrap(),
                "word-type:、,4".to_string() => NonZeroU32::new(6).unwrap(),
            ],
            extractor.unigram_feature_ids
        );
    }

    #[test]
    fn test_bigram_feature_extraction() {
        let mut extractor = prepare_extractor();

        let left_feature_ids = extractor.extract_left_feature_ids(&["火星", "名詞", "カセイ"]);
        let right_feature_ids = extractor.extract_right_feature_ids(&["", "接尾辞", "ジン"]);
        assert_eq!(
            vec![NonZeroU32::new(1), NonZeroU32::new(2), NonZeroU32::new(3)],
            left_feature_ids
        );
        assert_eq!(
            vec![NonZeroU32::new(1), NonZeroU32::new(2), NonZeroU32::new(3)],
            right_feature_ids
        );

        let left_feature_ids = extractor.extract_left_feature_ids(&["火星", "名詞", "カセイ"]);
        let right_feature_ids = extractor.extract_right_feature_ids(&["", "名詞", "ネコ"]);
        assert_eq!(
            vec![NonZeroU32::new(1), NonZeroU32::new(2), NonZeroU32::new(3)],
            left_feature_ids
        );
        assert_eq!(
            vec![NonZeroU32::new(4), NonZeroU32::new(5), NonZeroU32::new(6)],
            right_feature_ids
        );

        assert_eq!(
            hashmap![
                "pos:名詞".to_string() => NonZeroU32::new(1).unwrap(),
                "pron:カセイ".to_string() => NonZeroU32::new(2).unwrap(),
                "pos-pron:名詞,カセイ".to_string() => NonZeroU32::new(3).unwrap(),
            ],
            extractor.left_feature_ids
        );

        assert_eq!(
            hashmap![
                "pos:接尾辞".to_string() => NonZeroU32::new(1).unwrap(),
                "pron:ジン".to_string() => NonZeroU32::new(2).unwrap(),
                "pos-pron:接尾辞,ジン".to_string() => NonZeroU32::new(3).unwrap(),
                "pos:名詞".to_string() => NonZeroU32::new(4).unwrap(),
                "pron:ネコ".to_string() => NonZeroU32::new(5).unwrap(),
                "pos-pron:名詞,ネコ".to_string() => NonZeroU32::new(6).unwrap(),
            ],
            extractor.right_feature_ids
        );
    }

    #[test]
    fn test_bigram_feature_extraction_undefined() {
        let mut extractor = prepare_extractor();

        let left_feature_ids = extractor.extract_left_feature_ids(&["です", "助動詞", "デス"]);
        let right_feature_ids = extractor.extract_right_feature_ids(&["", "補助記号", "*"]);
        assert_eq!(
            vec![NonZeroU32::new(1), NonZeroU32::new(2), NonZeroU32::new(3)],
            left_feature_ids
        );
        assert_eq!(vec![NonZeroU32::new(1), None, None], right_feature_ids);

        let left_feature_ids = extractor.extract_left_feature_ids(&["", "補助記号", "*"]);
        let right_feature_ids = extractor.extract_right_feature_ids(&["", "名詞", "ネコ"]);
        assert_eq!(vec![NonZeroU32::new(4), None, None], left_feature_ids);
        assert_eq!(
            vec![NonZeroU32::new(2), NonZeroU32::new(3), NonZeroU32::new(4)],
            right_feature_ids
        );

        assert_eq!(
            hashmap![
                "pos:助動詞".to_string() => NonZeroU32::new(1).unwrap(),
                "pron:デス".to_string() => NonZeroU32::new(2).unwrap(),
                "pos-pron:助動詞,デス".to_string() => NonZeroU32::new(3).unwrap(),
                "pos:補助記号".to_string() => NonZeroU32::new(4).unwrap(),
            ],
            extractor.left_feature_ids
        );

        assert_eq!(
            hashmap![
                "pos:補助記号".to_string() => NonZeroU32::new(1).unwrap(),
                "pos:名詞".to_string() => NonZeroU32::new(2).unwrap(),
                "pron:ネコ".to_string() => NonZeroU32::new(3).unwrap(),
                "pos-pron:名詞,ネコ".to_string() => NonZeroU32::new(4).unwrap(),
            ],
            extractor.right_feature_ids
        );
    }

    #[test]
    fn test_fill_aster() {
        let mut extractor = prepare_extractor();

        extractor.extract_unigram_feature_ids(&[""], 4);

        assert_eq!(
            hashmap![
                "word:。".to_string() => NonZeroU32::new(1).unwrap(),
                "word-pos:。,*".to_string() => NonZeroU32::new(2).unwrap(),
                "word-type:。,4".to_string() => NonZeroU32::new(3).unwrap(),
            ],
            extractor.unigram_feature_ids
        );
    }
}