vaporetto 0.2.0

Vaporetto: a pointwise prediction based 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
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
use anyhow::{anyhow, Result};

/// Character type.
#[derive(Debug, Clone, Copy, Hash, PartialEq, Eq)]
#[repr(u8)]
pub enum CharacterType {
    /// Digit character. (e.g. 0, 1, 2, ...)
    Digit = b'D',

    /// Roman character. (e.g. A, B, C, ...)
    Roman = b'R',

    /// Japanese Hiragana character. (e.g. あ, い, う, ...)
    Hiragana = b'H',

    /// Japanese Katakana character. (e.g. ア, イ, ウ, ...)
    Katakana = b'T',

    /// Kanji (a.k.a. Hanzi or Hanja) character. (e.g. 漢, 字, ...)
    Kanji = b'K',

    /// Other character.
    Other = b'O',
}

impl CharacterType {
    /// Gets a character type of a given character.
    ///
    /// # Arguments
    ///
    /// * `c` - A character.
    ///
    /// # Returns
    ///
    /// A character type.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::CharacterType;
    ///
    /// let t = CharacterType::get_type('A');
    /// assert_eq!(CharacterType::Roman, t);
    /// ```
    pub const fn get_type(c: char) -> Self {
        match c as u32 {
            0x30..=0x39 | 0xFF10..=0xFF19 => Self::Digit,
            0x41..=0x5A | 0x61..=0x7A | 0xFF21..=0xFF3A | 0xFF41..=0xFF5A => Self::Roman,
            0x3040..=0x3096 => Self::Hiragana,
            0x30A0..=0x30FA | 0x30FC..=0x30FF | 0xFF66..=0xFF9F => Self::Katakana,
            0x3400..=0x4DBF          // CJK Unified Ideographs Extension A
                | 0x4E00..=0x9FFF    // CJK Unified Ideographs
                | 0xF900..=0xFAFF    // CJK Compatibility Ideographs
                | 0x20000..=0x2A6DF  // CJK Unified Ideographs Extension B
                | 0x2A700..=0x2B73F  // CJK Unified Ideographs Extension C
                | 0x2B740..=0x2B81F  // CJK Unified Ideographs Extension D
                | 0x2B820..=0x2CEAF  // CJK Unified Ideographs Extension E
                | 0x2F800..=0x2FA1F  // CJK Compatibility Ideographs Supplement
                => Self::Kanji,
            _ => Self::Other,
        }
    }
}

/// Boundary type.
#[derive(Debug, PartialEq, Clone, Copy)]
#[repr(u8)]
pub enum BoundaryType {
    /// Inner of a word.
    NotWordBoundary = 0,

    /// Word boundary.
    WordBoundary = 1,

    /// Unknown. (Not annotated.)
    Unknown = 2,
}

/// Sentence with boundary annotations.
#[derive(Debug, PartialEq, Clone)]
pub struct Sentence {
    pub(crate) text: String,
    pub(crate) str_to_char_pos: Vec<usize>,
    pub(crate) char_to_str_pos: Vec<usize>,
    pub(crate) char_type: Vec<u8>,
    pub(crate) boundaries: Vec<BoundaryType>,
    pub(crate) boundary_scores: Option<Vec<f64>>,
}

impl Sentence {
    fn common_info(chars: &[char]) -> (Vec<usize>, Vec<usize>, Vec<u8>) {
        let mut char_to_str_pos = Vec::with_capacity(chars.len() + 1);
        let mut char_type = Vec::with_capacity(chars.len());
        let mut pos = 0;
        char_to_str_pos.push(0);
        for &c in chars {
            pos += c.len_utf8();
            char_to_str_pos.push(pos);
            char_type.push(CharacterType::get_type(c) as u8)
        }
        let mut str_to_char_pos = vec![0; char_to_str_pos.last().unwrap_or(&0) + 1];
        for (i, &j) in char_to_str_pos.iter().enumerate() {
            // j < str_to_char_pos.len()
            unsafe {
                *str_to_char_pos.get_unchecked_mut(j) = i;
            }
        }
        (char_to_str_pos, str_to_char_pos, char_type)
    }

    /// Creates a new [`Sentence`] from a given string.
    ///
    /// # Arguments
    ///
    /// * `text` - A raw string without any annotation.
    ///
    /// # Returns
    ///
    /// A new [`Sentence`].
    ///
    /// # Errors
    ///
    /// If the given `text` is empty, an error variant will be returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::Sentence;
    ///
    /// let s = Sentence::from_raw("How are you?");
    /// assert!(s.is_ok());
    ///
    /// let s = Sentence::from_raw("");
    /// assert!(s.is_err());
    /// ```
    pub fn from_raw<S>(text: S) -> Result<Self>
    where
        S: Into<String>,
    {
        let text = text.into();

        if text.is_empty() {
            return Err(anyhow!("`text` is empty"));
        }

        let chars: Vec<char> = text.chars().collect();
        let boundaries = vec![BoundaryType::Unknown; chars.len() - 1];

        let (char_to_str_pos, str_to_char_pos, char_type) = Self::common_info(&chars);

        Ok(Self {
            text,
            str_to_char_pos,
            char_to_str_pos,
            char_type,
            boundaries,
            boundary_scores: None,
        })
    }

    /// Gets a string without any annotation.
    ///
    /// # Returns
    ///
    /// A reference to the string.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::Sentence;
    ///
    /// let s = Sentence::from_raw("How are you?").unwrap();
    /// assert_eq!("How are you?", s.to_raw_string());
    /// ```
    pub fn to_raw_string(&self) -> &str {
        &self.text
    }

    /// Creates a new [`Sentence`] from a tokenized string.
    ///
    /// # Arguments
    ///
    /// * `tokenized_text` - A tokenized string containing whitespaces for word boundaries.
    ///
    /// # Returns
    ///
    /// A new [`Sentence`].
    ///
    /// # Errors
    ///
    /// This function will return an error variant when:
    ///
    /// * `tokenized_text` is empty.
    /// * `tokenized_text` starts/ends with a whitespace.
    /// * `tokenized_text` contains consecutive whitespaces.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::Sentence;
    ///
    /// let s = Sentence::from_tokenized("How are you?");
    /// assert!(s.is_ok());
    ///
    /// let s = Sentence::from_tokenized("How  are you?");
    /// assert!(s.is_err());
    /// ```
    pub fn from_tokenized<S>(tokenized_text: S) -> Result<Self>
    where
        S: AsRef<str>,
    {
        let tokenized_text = tokenized_text.as_ref();

        if tokenized_text.is_empty() {
            return Err(anyhow!("`tokenized_text` is empty"));
        }

        let tokenized_chars: Vec<char> = tokenized_text.chars().collect();
        let mut chars = Vec::with_capacity(tokenized_chars.len());
        let mut boundaries = Vec::with_capacity(tokenized_chars.len() - 1);

        let mut prev_boundary = false;
        let mut escape = false;
        for c in tokenized_chars {
            match (escape, c) {
                (false, '\\') => {
                    escape = true;
                }
                (false, ' ') => {
                    if chars.is_empty() {
                        return Err(anyhow!("`tokenized_text` starts with a whitespace"));
                    } else if prev_boundary {
                        return Err(anyhow!("`tokenized_text` contains consecutive whitespaces"));
                    }
                    prev_boundary = true;
                }
                (_, _) => {
                    if !chars.is_empty() {
                        boundaries.push(if prev_boundary {
                            BoundaryType::WordBoundary
                        } else {
                            BoundaryType::NotWordBoundary
                        });
                    }
                    prev_boundary = false;
                    escape = false;
                    chars.push(c);
                }
            };
        }
        if prev_boundary {
            return Err(anyhow!("`tokenized_text` ends with a whitespace"));
        }

        let (char_to_str_pos, str_to_char_pos, char_type) = Self::common_info(&chars);
        Ok(Self {
            text: chars.iter().collect(),
            char_to_str_pos,
            str_to_char_pos,
            char_type,
            boundaries,
            boundary_scores: None,
        })
    }

    /// Generates a string with whitespaces for word boundaries.
    ///
    /// # Returns
    ///
    /// A newly allocated string containing whitespaces for word boundaries.
    ///
    /// # Errors
    ///
    /// If the sentence contains unknown boundary, an error variant will be returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::Sentence;
    ///
    /// let s = Sentence::from_tokenized("How are you?").unwrap();
    /// assert_eq!("How are you?", s.to_tokenized_string().unwrap());
    /// ```
    pub fn to_tokenized_string(&self) -> Result<String> {
        let chars: Vec<char> = self.text.chars().collect();
        let mut result = String::with_capacity(self.text.len() + chars.len() - 1);
        match chars[0] {
            '\\' | '/' | '&' | ' ' => result.push('\\'),
            _ => (),
        }
        result.push(chars[0]);
        for (&c, b) in chars[1..].iter().zip(&self.boundaries) {
            match b {
                BoundaryType::WordBoundary => {
                    result.push(' ');
                }
                BoundaryType::NotWordBoundary => (),
                BoundaryType::Unknown => {
                    return Err(anyhow!("sentence contains an unknown boundary"));
                }
            }
            match c {
                '\\' | '/' | '&' | ' ' => result.push('\\'),
                _ => (),
            }
            result.push(c);
        }
        Ok(result)
    }

    /// Generates a vector of words.
    ///
    /// # Returns
    ///
    /// A newly allocated vector of words.
    ///
    /// # Errors
    ///
    /// If the sentence contains unknown boundaries, an error variant will be returned.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::Sentence;
    ///
    /// let s = Sentence::from_tokenized("How are you ?").unwrap();
    /// assert_eq!(vec![
    ///     "How",
    ///     "are",
    ///     "you",
    ///     "?",
    /// ], s.to_tokenized_vec().unwrap());
    /// ```
    pub fn to_tokenized_vec(&self) -> Result<Vec<&str>> {
        let mut result = vec![];
        let mut start = 0;
        for (i, b) in self.boundaries.iter().enumerate() {
            match b {
                BoundaryType::WordBoundary => {
                    let end = unsafe { *self.char_to_str_pos.get_unchecked(i + 1) };
                    let word = unsafe { self.text.get_unchecked(start..end) };
                    result.push(word);
                    start = end;
                }
                BoundaryType::NotWordBoundary => (),
                BoundaryType::Unknown => {
                    return Err(anyhow!("sentence contains an unknown boundary"));
                }
            }
        }
        let word = unsafe { self.text.get_unchecked(start..) };
        result.push(word);
        Ok(result)
    }

    /// Creates a new [`Sentence`] from a string with partial annotations.
    ///
    /// # Arguments
    ///
    /// * `labeled_text` - A string with partial annotations.
    ///
    /// # Returns
    ///
    /// A new [`Sentence`].
    ///
    /// # Errors
    ///
    /// This function will return an error variant when:
    ///
    /// * `labeled_text` is empty.
    /// * The length of `lsbeled_text` is even numbers.
    /// * `labeled_text` contains invalid boundary characters.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::Sentence;
    ///
    /// let s = Sentence::from_partial_annotation("g-o-o-d|i-d e-a");
    /// assert!(s.is_ok());
    ///
    /// let s = Sentence::from_partial_annotation("b-a-d/i-d-e-a");
    /// assert!(s.is_err());
    /// ```
    pub fn from_partial_annotation<S>(labeled_text: S) -> Result<Self>
    where
        S: AsRef<str>,
    {
        let labeled_text = labeled_text.as_ref();

        if labeled_text.is_empty() {
            return Err(anyhow!("`labeled_text` is empty"));
        }

        let labeled_chars: Vec<char> = labeled_text.chars().collect();
        if labeled_chars.len() & 0x01 == 0 {
            return Err(anyhow!(
                "invalid length for `labeled_text`: {}",
                labeled_chars.len()
            ));
        }
        let mut chars = Vec::with_capacity(labeled_chars.len() / 2 + 1);
        let mut boundaries = Vec::with_capacity(labeled_chars.len() / 2);

        for c in labeled_chars.iter().skip(1).step_by(2) {
            boundaries.push(match c {
                ' ' => BoundaryType::Unknown,
                '|' => BoundaryType::WordBoundary,
                '-' => BoundaryType::NotWordBoundary,
                _ => return Err(anyhow!("invalid boundary character: '{}'", c)),
            });
        }
        for c in labeled_chars.into_iter().step_by(2) {
            chars.push(c);
        }

        let (char_to_str_pos, str_to_char_pos, char_type) = Self::common_info(&chars);
        Ok(Self {
            text: chars.iter().collect(),
            char_to_str_pos,
            str_to_char_pos,
            char_type,
            boundaries,
            boundary_scores: None,
        })
    }

    /// Generates a string with partial annotations.
    ///
    /// # Returns
    ///
    /// A newly allocated string with partial annotations.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::Sentence;
    ///
    /// let s = Sentence::from_tokenized("How are you ?").unwrap();
    /// assert_eq!("H-o-w|a-r-e|y-o-u|?", &s.to_partial_annotation_string());
    /// ```
    pub fn to_partial_annotation_string(&self) -> String {
        let chars: Vec<char> = self.text.chars().collect();
        let mut result = String::with_capacity(self.text.len() + chars.len() - 1);
        result.push(chars[0]);
        for (&c, b) in chars[1..].iter().zip(&self.boundaries) {
            result.push(match b {
                BoundaryType::WordBoundary => '|',
                BoundaryType::NotWordBoundary => '-',
                BoundaryType::Unknown => ' ',
            });
            result.push(c);
        }
        result
    }

    /// Gets a reference to the boundary information.
    ///
    /// # Returns
    ///
    /// A reference to the boundary information.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::{BoundaryType, Sentence};
    ///
    /// let s = Sentence::from_partial_annotation("a|b-c d").unwrap();
    /// assert_eq!(&[
    ///     BoundaryType::WordBoundary,
    ///     BoundaryType::NotWordBoundary,
    ///     BoundaryType::Unknown,
    /// ], s.boundaries());
    /// ```
    pub fn boundaries(&self) -> &[BoundaryType] {
        &self.boundaries
    }

    /// Gets a mutable reference to the boundary information.
    ///
    /// # Returns
    ///
    /// A mutable reference to the boundary information.
    pub fn boundaries_mut(&mut self) -> &mut [BoundaryType] {
        &mut self.boundaries
    }

    /// Gets a reference to the character type information.
    ///
    /// # Returns
    ///
    /// A reference to the character type information.
    ///
    /// # Examples
    ///
    /// ```
    /// use vaporetto::Sentence;
    ///
    /// let s = Sentence::from_raw("A1あエ漢?").unwrap();
    /// assert_eq!(&[b'R', b'D', b'H', b'T', b'K', b'O',], s.char_types());
    /// ```
    pub fn char_types(&self) -> &[u8] {
        &self.char_type
    }

    /// Gets a reference to the boundary score information.
    ///
    /// # Returns
    ///
    /// If the predictor inserted, the boundary score information is returned. Otherwise, None.
    pub fn boundary_scores(&self) -> Option<&[f64]> {
        self.boundary_scores.as_deref()
    }

    /// Gets a character position in the code point unit.
    ///
    /// # Returns
    ///
    /// A position in the code point unit.
    ///
    /// # Errors
    ///
    /// `index` must be a valid position.
    pub fn get_char_pos(&self, index: usize) -> Result<usize> {
        if index == 0 {
            Ok(0)
        } else {
            match self.str_to_char_pos.get(index) {
                Some(index) if *index != 0 => Ok(*index),
                _ => Err(anyhow!("invalid index")),
            }
        }
    }

    #[cfg(feature = "train")]
    pub(crate) fn char_substring(&self, start: usize, end: usize) -> &str {
        let begin = self.char_to_str_pos[start];
        let end = self.char_to_str_pos[end];
        &self.text.as_str()[begin..end]
    }

    #[cfg(feature = "train")]
    pub(crate) fn type_substring(&self, start: usize, end: usize) -> &[u8] {
        &self.char_type[start..end]
    }
}

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

    #[test]
    fn test_sentence_from_raw_empty() {
        let s = Sentence::from_raw("");

        assert!(s.is_err());
        assert_eq!("`text` is empty", &s.err().unwrap().to_string());
    }

    #[test]
    fn test_sentence_from_raw_one() {
        let s = Sentence::from_raw("");

        let expected = Sentence {
            text: "".to_string(),
            str_to_char_pos: vec![0, 0, 0, 1],
            char_to_str_pos: vec![0, 3],
            char_type: ct2u8vec![Hiragana],
            boundaries: vec![],
            boundary_scores: None,
        };
        assert_eq!(expected, s.unwrap());
    }

    #[test]
    fn test_sentence_from_raw() {
        let s = Sentence::from_raw("Rustで良いプログラミング体験を!");

        let expected = Sentence {
            text: "Rustで良いプログラミング体験を!".to_string(),
            str_to_char_pos: vec![
                0, 1, 2, 3, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 11, 0,
                0, 12, 0, 0, 13, 0, 0, 14, 0, 0, 15, 0, 0, 16, 0, 0, 17, 0, 0, 18,
            ],
            char_to_str_pos: vec![
                0, 1, 2, 3, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
            ],
            char_type: ct2u8vec![
                Roman, Roman, Roman, Roman, Hiragana, Kanji, Hiragana, Katakana, Katakana,
                Katakana, Katakana, Katakana, Katakana, Katakana, Kanji, Kanji, Hiragana, Other,
            ],
            boundaries: vec![Unknown; 17],
            boundary_scores: None,
        };
        assert_eq!(expected, s.unwrap());
    }

    #[test]
    fn test_sentence_to_raw() {
        let s = Sentence::from_raw("Rustで良いプログラミング体験を!");

        assert_eq!(
            "Rustで良いプログラミング体験を!",
            s.unwrap().to_raw_string()
        );
    }

    #[test]
    fn test_sentence_from_tokenized_empty() {
        let s = Sentence::from_tokenized("");

        assert!(s.is_err());
        assert_eq!("`tokenized_text` is empty", &s.err().unwrap().to_string());
    }

    #[test]
    fn test_sentence_from_tokenized_start_with_space() {
        let s = Sentence::from_tokenized(" Rust で 良い プログラミング 体験 を !");

        assert!(s.is_err());
        assert_eq!(
            "`tokenized_text` starts with a whitespace",
            &s.err().unwrap().to_string()
        );
    }

    #[test]
    fn test_sentence_from_tokenized_end_with_space() {
        let s = Sentence::from_tokenized("Rust で 良い プログラミング 体験 を ! ");

        assert!(s.is_err());
        assert_eq!(
            "`tokenized_text` ends with a whitespace",
            &s.err().unwrap().to_string()
        );
    }

    #[test]
    fn test_sentence_from_tokenized_two_spaces() {
        let s = Sentence::from_tokenized("Rust で 良い  プログラミング 体験 を !");

        assert!(s.is_err());
        assert_eq!(
            "`tokenized_text` contains consecutive whitespaces",
            &s.err().unwrap().to_string()
        );
    }

    #[test]
    fn test_sentence_from_tokenized_one() {
        let s = Sentence::from_tokenized("");

        let expected = Sentence {
            text: "".to_string(),
            str_to_char_pos: vec![0, 0, 0, 1],
            char_to_str_pos: vec![0, 3],
            char_type: ct2u8vec![Hiragana],
            boundaries: vec![],
            boundary_scores: None,
        };
        assert_eq!(expected, s.unwrap());
    }

    #[test]
    fn test_sentence_from_tokenized() {
        let s = Sentence::from_tokenized("Rust で 良い プログラミング 体験 を !");

        let expected = Sentence {
            text: "Rustで良いプログラミング体験を!".to_string(),
            str_to_char_pos: vec![
                0, 1, 2, 3, 4, 0, 0, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9, 0, 0, 10, 0, 0, 11, 0,
                0, 12, 0, 0, 13, 0, 0, 14, 0, 0, 15, 0, 0, 16, 0, 0, 17, 0, 0, 18,
            ],
            char_to_str_pos: vec![
                0, 1, 2, 3, 4, 7, 10, 13, 16, 19, 22, 25, 28, 31, 34, 37, 40, 43, 46,
            ],
            char_type: ct2u8vec![
                Roman, Roman, Roman, Roman, Hiragana, Kanji, Hiragana, Katakana, Katakana,
                Katakana, Katakana, Katakana, Katakana, Katakana, Kanji, Kanji, Hiragana, Other,
            ],
            boundaries: vec![
                NotWordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                WordBoundary,
                WordBoundary,
                NotWordBoundary,
                WordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                WordBoundary,
                NotWordBoundary,
                WordBoundary,
                WordBoundary,
            ],
            boundary_scores: None,
        };
        assert_eq!(expected, s.unwrap());
    }

    #[test]
    fn test_sentence_from_tokenized_with_escape_whitespace() {
        let s = Sentence::from_tokenized("火星 猫 の 生態 ( M \\  et\\ al. )");

        let expected = Sentence {
            text: "火星猫の生態(M et al.)".to_string(),
            str_to_char_pos: vec![
                0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6, 7, 8, 9, 10, 11, 12, 13,
                14, 15, 16,
            ],
            char_to_str_pos: vec![
                0, 3, 6, 9, 12, 15, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28,
            ],
            char_type: ct2u8vec![
                Kanji, Kanji, Kanji, Hiragana, Kanji, Kanji, Other, Roman, Other, Roman, Roman,
                Other, Roman, Roman, Other, Other,
            ],
            boundaries: vec![
                NotWordBoundary,
                WordBoundary,
                WordBoundary,
                WordBoundary,
                NotWordBoundary,
                WordBoundary,
                WordBoundary,
                WordBoundary,
                WordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                NotWordBoundary,
                WordBoundary,
            ],
            boundary_scores: None,
        };
        assert_eq!(expected, s.unwrap());
    }

    #[test]
    fn test_sentence_from_tokenized_with_escape_backslash() {
        let s = Sentence::from_tokenized("改行 に \\\\n を 用い る");

        let expected = Sentence {
            text: "改行に\\nを用いる".to_string(),
            str_to_char_pos: vec![
                0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 4, 5, 0, 0, 6, 0, 0, 7, 0, 0, 8, 0, 0, 9,
            ],
            char_to_str_pos: vec![0, 3, 6, 9, 10, 11, 14, 17, 20, 23],
            char_type: ct2u8vec![
                Kanji, Kanji, Hiragana, Other, Roman, Hiragana, Kanji, Hiragana, Hiragana,
            ],
            boundaries: vec![
                NotWordBoundary,
                WordBoundary,
                WordBoundary,
                NotWordBoundary,
                WordBoundary,
                WordBoundary,
                NotWordBoundary,
                WordBoundary,
            ],
            boundary_scores: None,
        };
        assert_eq!(expected, s.unwrap());
    }

    #[test]
    fn test_sentence_to_tokenized_string_unknown() {
        let s = Sentence::from_partial_annotation("火-星 猫|の|生-態");
        let result = s.unwrap().to_tokenized_string();

        assert!(result.is_err());
        assert_eq!(
            "sentence contains an unknown boundary",
            result.err().unwrap().to_string()
        );
    }

    #[test]
    fn test_sentence_to_tokenized_string() {
        let s = Sentence::from_tokenized("Rust で 良い プログラミング 体験 を !");

        assert_eq!(
            "Rust で 良い プログラミング 体験 を !",
            s.unwrap().to_tokenized_string().unwrap()
        );
    }

    #[test]
    fn test_sentence_to_tokenized_string_escape() {
        let s = Sentence::from_partial_annotation("火-星-猫|の| |生-態|\\-n");

        assert_eq!(
            "火星猫 の \\  生態 \\\\n",
            s.unwrap().to_tokenized_string().unwrap()
        );
    }

    #[test]
    fn test_sentence_to_tokenized_vec_unknown() {
        let s = Sentence::from_partial_annotation("火-星 猫|の|生-態").unwrap();
        let result = s.to_tokenized_vec();

        assert!(result.is_err());
        assert_eq!(
            "sentence contains an unknown boundary",
            result.err().unwrap().to_string()
        );
    }

    #[test]
    fn test_sentence_to_tokenized_vec() {
        let s = Sentence::from_tokenized("Rust で 良い プログラミング 体験 を !").unwrap();

        assert_eq!(
            vec!["Rust", "", "良い", "プログラミング", "体験", "", ""],
            s.to_tokenized_vec().unwrap()
        );
    }

    #[test]
    fn test_sentence_from_partial_annotation_empty() {
        let s = Sentence::from_partial_annotation("");

        assert!(s.is_err());
        assert_eq!("`labeled_text` is empty", &s.err().unwrap().to_string());
    }

    #[test]
    fn test_sentence_from_partial_annotation_invalid_length() {
        let s = Sentence::from_partial_annotation("火-星 猫|の|生-態 ");

        assert!(s.is_err());
        assert_eq!(
            "invalid length for `labeled_text`: 12",
            &s.err().unwrap().to_string()
        );
    }

    #[test]
    fn test_sentence_from_partial_annotation_invalid_boundary_character() {
        let s = Sentence::from_partial_annotation("火-星?猫|の|生-態");

        assert!(s.is_err());
        assert_eq!(
            "invalid boundary character: '?'",
            &s.err().unwrap().to_string()
        );
    }

    #[test]
    fn test_sentence_from_partial_annotation_one() {
        let s = Sentence::from_partial_annotation("火-星 猫|の|生-態");

        let expected = Sentence {
            text: "火星猫の生態".to_string(),
            str_to_char_pos: vec![0, 0, 0, 1, 0, 0, 2, 0, 0, 3, 0, 0, 4, 0, 0, 5, 0, 0, 6],
            char_to_str_pos: vec![0, 3, 6, 9, 12, 15, 18],
            char_type: ct2u8vec![Kanji, Kanji, Kanji, Hiragana, Kanji, Kanji],
            boundaries: vec![
                NotWordBoundary,
                Unknown,
                WordBoundary,
                WordBoundary,
                NotWordBoundary,
            ],
            boundary_scores: None,
        };
        assert_eq!(expected, s.unwrap());
    }

    #[test]
    fn test_sentence_to_partial_annotation_string() {
        let s = Sentence::from_partial_annotation("火-星 猫|の|生-態");

        assert_eq!(
            "火-星 猫|の|生-態",
            s.unwrap().to_partial_annotation_string()
        );
    }
}