trustformers-tokenizers 0.1.1

Tokenizers for TrustformeRS
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
use crate::vocab::Vocab;
#[cfg(feature = "mecab")]
use mecab::Tagger;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
#[cfg(feature = "mecab")]
use std::sync::{Arc, Mutex};
#[cfg(feature = "mecab")]
use trustformers_core::errors::ErrorKind;
use trustformers_core::errors::{Result, TrustformersError};
use trustformers_core::traits::{TokenizedInput, Tokenizer};

/// Configuration for Japanese tokenization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct JapaneseTokenizerConfig {
    /// Whether to use MeCab for morphological analysis
    pub use_mecab: bool,
    /// MeCab configuration string
    pub mecab_config: Option<String>,
    /// Tokenization mode: "word", "morpheme", or "character"
    pub mode: JapaneseMode,
    /// Whether to keep punctuation as separate tokens
    pub keep_punctuation: bool,
    /// Whether to normalize katakana to hiragana
    pub normalize_katakana: bool,
    /// Whether to split compound words
    pub split_compounds: bool,
    /// Whether to include part-of-speech tags
    pub include_pos: bool,
    /// Maximum word length for character mode
    pub max_word_length: usize,
    /// Unknown token
    pub unk_token: String,
    /// Padding token
    pub pad_token: String,
    /// Special tokens
    pub special_tokens: Vec<String>,
}

/// Tokenization mode for Japanese text
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum JapaneseMode {
    /// Word-level tokenization
    Word,
    /// Morpheme-level tokenization with MeCab
    Morpheme,
    /// Character-level tokenization
    Character,
}

impl Default for JapaneseTokenizerConfig {
    fn default() -> Self {
        Self {
            use_mecab: cfg!(feature = "mecab"),
            mecab_config: None,
            mode: JapaneseMode::Morpheme,
            keep_punctuation: true,
            normalize_katakana: false,
            split_compounds: false,
            include_pos: false,
            max_word_length: 10,
            unk_token: "[UNK]".to_string(),
            pad_token: "[PAD]".to_string(),
            special_tokens: vec![
                "[PAD]".to_string(),
                "[UNK]".to_string(),
                "[CLS]".to_string(),
                "[SEP]".to_string(),
                "[MASK]".to_string(),
            ],
        }
    }
}

#[cfg(feature = "mecab")]
/// Thread-safe wrapper for MeCab Tagger
struct ThreadSafeTagger {
    tagger: Arc<Mutex<Tagger>>,
}

#[cfg(feature = "mecab")]
// MeCab's Tagger is not inherently thread-safe, but we protect it with a Mutex
// This is safe because:
// 1. We never access the raw pointers directly
// 2. All access is protected by the Mutex
// 3. We don't share the Tagger between threads without synchronization
unsafe impl Send for ThreadSafeTagger {}
#[cfg(feature = "mecab")]
unsafe impl Sync for ThreadSafeTagger {}

#[cfg(feature = "mecab")]
impl ThreadSafeTagger {
    #[allow(clippy::arc_with_non_send_sync)]
    fn new(config: &str) -> Result<Self> {
        let tagger = Tagger::new(config);
        Ok(Self {
            tagger: Arc::new(Mutex::new(tagger)),
        })
    }

    fn parse_to_node(&self, text: &str) -> Result<Vec<(String, String)>> {
        let mut tagger = self.tagger.lock().expect("lock should not be poisoned");
        let node = tagger.parse_to_node(text);
        let mut result = Vec::new();

        let mut current = node;
        loop {
            let surface = current.surface.clone();
            let feature = current.feature.clone();

            if !surface.is_empty() && feature != "BOS/EOS" {
                result.push((surface, feature));
            }

            if let Some(next) = current.next() {
                current = next;
            } else {
                break;
            }
        }

        Ok(result)
    }
}

#[cfg(feature = "mecab")]
impl std::fmt::Debug for ThreadSafeTagger {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "ThreadSafeTagger")
    }
}

/// Japanese tokenizer with MeCab integration
#[derive(Debug)]
pub struct JapaneseTokenizer {
    config: JapaneseTokenizerConfig,
    vocab: Vocab,
    #[cfg(feature = "mecab")]
    mecab: Option<ThreadSafeTagger>,
    /// Built-in dictionary of common Japanese words
    word_dict: HashSet<String>,
    /// Whether normalizer is enabled
    use_normalizer: bool,
}

impl JapaneseTokenizer {
    /// Create a new Japanese tokenizer
    pub fn new(config: JapaneseTokenizerConfig, vocab: Vocab) -> Result<Self> {
        #[cfg(feature = "mecab")]
        let mecab = if config.use_mecab {
            let mecab_config = config.mecab_config.as_deref().unwrap_or("");
            Some(ThreadSafeTagger::new(mecab_config)?)
        } else {
            None
        };

        let mut tokenizer = Self {
            config,
            vocab,
            #[cfg(feature = "mecab")]
            mecab,
            word_dict: HashSet::new(),
            use_normalizer: false,
        };

        tokenizer.init_builtin_dict();
        Ok(tokenizer)
    }

    /// Enable basic text normalization
    pub fn with_normalization(mut self) -> Self {
        self.use_normalizer = true;
        self
    }

    /// Initialize built-in Japanese word dictionary
    fn init_builtin_dict(&mut self) {
        // Common Japanese words
        let common_words = vec![
            "日本",
            "東京",
            "大阪",
            "名古屋",
            "福岡",
            "札幌",
            "",
            "",
            "",
            "",
            "彼女",
            "あなた",
            "",
            "今日",
            "明日",
            "昨日",
            "",
            "",
            "",
            "",
            "する",
            "した",
            "します",
            "できる",
            "ある",
            "いる",
            "なる",
            "とても",
            "少し",
            "たくさん",
            "全部",
            "一部",
            "何か",
            "誰か",
            "学校",
            "会社",
            "",
            "",
            "病院",
            "",
            "空港",
            "先生",
            "学生",
            "医者",
            "警察",
            "運転手",
            "料理人",
            "技術者",
            "好き",
            "嫌い",
            "",
            "恨み",
            "嬉しい",
            "悲しい",
            "怒り",
            "恐怖",
            "もの",
            "こと",
            "場所",
            "時間",
            "問題",
            "方法",
            "機会",
            "経験",
            "知識",
            "技術",
            "能力",
            "水準",
            "",
            "",
            "価格",
        ];

        for word in common_words {
            self.word_dict.insert(word.to_string());
        }
    }

    /// Check if a character is Hiragana
    pub fn is_hiragana(ch: char) -> bool {
        let code = ch as u32;
        (0x3040..=0x309F).contains(&code)
    }

    /// Check if a character is Katakana
    pub fn is_katakana(ch: char) -> bool {
        let code = ch as u32;
        (0x30A0..=0x30FF).contains(&code)
    }

    /// Check if a character is Kanji
    pub fn is_kanji(ch: char) -> bool {
        let code = ch as u32;
        // CJK Unified Ideographs
        (0x4E00..=0x9FFF).contains(&code) ||
        // CJK Extension A
        (0x3400..=0x4DBF).contains(&code) ||
        // CJK Extension B
        (0x20000..=0x2A6DF).contains(&code)
    }

    /// Check if a character is Japanese
    pub fn is_japanese_char(ch: char) -> bool {
        Self::is_hiragana(ch) || Self::is_katakana(ch) || Self::is_kanji(ch)
    }

    /// Check if a character is Japanese punctuation
    pub fn is_japanese_punctuation(ch: char) -> bool {
        matches!(
            ch,
            '' | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
                | ''
        )
    }

    /// Convert Katakana to Hiragana
    pub fn katakana_to_hiragana(text: &str) -> String {
        text.chars()
            .map(|ch| {
                if Self::is_katakana(ch) {
                    // Convert Katakana to Hiragana by subtracting 0x60
                    char::from_u32(ch as u32 - 0x60).unwrap_or(ch)
                } else {
                    ch
                }
            })
            .collect()
    }

    /// Preprocess text by normalizing and handling special cases
    fn preprocess_text(&self, text: &str) -> String {
        let mut processed = if self.use_normalizer {
            // Basic normalization: full-width to half-width and whitespace normalization
            text.chars()
                .map(|c| {
                    // Convert full-width ASCII to half-width
                    if (0xFF01..=0xFF5E).contains(&(c as u32)) {
                        char::from_u32(c as u32 - 0xFEE0).unwrap_or(c)
                    } else {
                        c
                    }
                })
                .filter(|c| !c.is_whitespace() || *c == ' ')
                .collect()
        } else {
            text.to_string()
        };

        // Normalize katakana to hiragana if requested
        if self.config.normalize_katakana {
            processed = Self::katakana_to_hiragana(&processed);
        }

        processed
    }

    /// Tokenize using MeCab morphological analyzer
    #[cfg(feature = "mecab")]
    fn tokenize_with_mecab(&self, text: &str) -> Result<Vec<String>> {
        let mecab = self.mecab.as_ref().ok_or_else(|| {
            TrustformersError::new(ErrorKind::TokenizationError {
                reason: "MeCab not initialized".to_string(),
            })
        })?;

        let mut tokens = Vec::new();
        let node_data = mecab.parse_to_node(text)?;

        for (surface, feature) in node_data {
            let token = if self.config.include_pos {
                format!("{}#{}", surface, feature.split(',').next().unwrap_or(""))
            } else {
                surface
            };
            tokens.push(token);
        }

        Ok(tokens)
    }

    /// Tokenize using simple character-based approach
    fn tokenize_characters(&self, text: &str) -> Vec<String> {
        let chars: Vec<char> = text.chars().collect();
        let mut tokens = Vec::new();
        let mut i = 0;

        while i < chars.len() {
            let ch = chars[i];

            // Handle punctuation
            if Self::is_japanese_punctuation(ch) || ch.is_ascii_punctuation() {
                if self.config.keep_punctuation {
                    tokens.push(ch.to_string());
                }
                i += 1;
                continue;
            }

            // Handle special characters
            if ch.is_whitespace() {
                i += 1;
                continue;
            }

            // Regular character
            tokens.push(ch.to_string());
            i += 1;
        }

        tokens
    }

    /// Tokenize using word-based approach
    fn tokenize_words(&self, text: &str) -> Vec<String> {
        let mut tokens = Vec::new();
        let mut current_word = String::new();
        let mut current_type = None;

        for ch in text.chars() {
            let char_type = if Self::is_hiragana(ch) {
                "hiragana"
            } else if Self::is_katakana(ch) {
                "katakana"
            } else if Self::is_kanji(ch) {
                "kanji"
            } else if ch.is_ascii_alphanumeric() {
                "ascii"
            } else if Self::is_japanese_punctuation(ch) || ch.is_ascii_punctuation() {
                "punct"
            } else {
                "other"
            };

            if char_type == "punct" {
                if !current_word.is_empty() {
                    tokens.push(current_word.clone());
                    current_word.clear();
                }
                if self.config.keep_punctuation {
                    tokens.push(ch.to_string());
                }
                current_type = None;
            } else if char_type == "other" || ch.is_whitespace() {
                if !current_word.is_empty() {
                    tokens.push(current_word.clone());
                    current_word.clear();
                }
                current_type = None;
            } else {
                // Check if we need to break the word
                if let Some(prev_type) = current_type {
                    if prev_type != char_type
                        || (char_type == "kanji"
                            && current_word.len() >= self.config.max_word_length)
                    {
                        tokens.push(current_word.clone());
                        current_word.clear();
                    }
                }
                current_word.push(ch);
                current_type = Some(char_type);
            }
        }

        if !current_word.is_empty() {
            tokens.push(current_word);
        }

        tokens
    }

    /// Tokenize text based on the configured mode
    pub fn tokenize_text(&self, text: &str) -> Result<Vec<String>> {
        let processed_text = self.preprocess_text(text);

        match self.config.mode {
            #[cfg(feature = "mecab")]
            JapaneseMode::Morpheme if self.config.use_mecab => {
                self.tokenize_with_mecab(&processed_text)
            },
            JapaneseMode::Word => Ok(self.tokenize_words(&processed_text)),
            JapaneseMode::Character => Ok(self.tokenize_characters(&processed_text)),
            JapaneseMode::Morpheme => {
                // Fallback to word mode if MeCab is not available
                Ok(self.tokenize_words(&processed_text))
            },
        }
    }

    /// Add word to dictionary
    pub fn add_word(&mut self, word: String) {
        self.word_dict.insert(word);
    }

    /// Remove word from dictionary
    pub fn remove_word(&mut self, word: &str) -> bool {
        self.word_dict.remove(word)
    }

    /// Load dictionary from text file
    pub fn load_dictionary(&mut self, words: Vec<String>) {
        for word in words {
            self.word_dict.insert(word);
        }
    }

    /// Get dictionary size
    pub fn dictionary_size(&self) -> usize {
        self.word_dict.len()
    }

    /// Check if word is in dictionary
    pub fn contains_word(&self, word: &str) -> bool {
        self.word_dict.contains(word)
    }

    /// Process tokens to handle special cases
    fn process_tokens(&self, tokens: Vec<String>) -> Vec<String> {
        let mut processed = Vec::new();

        for token in tokens {
            // Check if it's a special token
            if self.config.special_tokens.contains(&token) {
                processed.push(token);
                continue;
            }

            // Skip empty tokens
            if token.is_empty() {
                continue;
            }

            processed.push(token);
        }

        processed
    }
}

impl Tokenizer for JapaneseTokenizer {
    fn encode(&self, text: &str) -> Result<TokenizedInput> {
        let tokens = self.tokenize_text(text)?;
        let processed_tokens = self.process_tokens(tokens);

        let mut token_ids = Vec::new();
        let mut attention_mask = Vec::new();

        for token in &processed_tokens {
            if let Some(id) = self.vocab.get_id(token) {
                token_ids.push(id);
                attention_mask.push(1);
            } else {
                // Use UNK token
                if let Some(unk_id) = self.vocab.get_id(&self.config.unk_token) {
                    token_ids.push(unk_id);
                    attention_mask.push(1);
                } else {
                    return Err(TrustformersError::other(
                        "UNK token not found in vocabulary".to_string(),
                    ));
                }
            }
        }

        Ok(TokenizedInput {
            input_ids: token_ids,
            attention_mask,
            token_type_ids: None,
            special_tokens_mask: None,
            offset_mapping: None,
            overflowing_tokens: None,
        })
    }

    fn decode(&self, token_ids: &[u32]) -> Result<String> {
        let mut result = String::new();

        for &token_id in token_ids {
            if let Some(token) = self.vocab.get_token(token_id) {
                // Skip padding tokens
                if *token == self.config.pad_token {
                    continue;
                }

                // Handle POS tags if included
                if self.config.include_pos && token.contains('#') {
                    let parts: Vec<&str> = token.split('#').collect();
                    if !parts.is_empty() {
                        result.push_str(parts[0]);
                    }
                } else {
                    result.push_str(&token);
                }
            } else {
                result.push_str(&self.config.unk_token);
            }
        }

        Ok(result)
    }

    fn encode_pair(&self, text_a: &str, text_b: &str) -> Result<TokenizedInput> {
        let tokens_a = self.tokenize_text(text_a)?;
        let tokens_b = self.tokenize_text(text_b)?;

        let processed_a = self.process_tokens(tokens_a);
        let processed_b = self.process_tokens(tokens_b);

        let mut all_tokens = processed_a;
        all_tokens.push("[SEP]".to_string());
        all_tokens.extend(processed_b);

        let mut token_ids = Vec::new();
        let mut attention_mask = Vec::new();
        let mut token_type_ids = Vec::new();

        let sep_pos = all_tokens.iter().position(|t| t == "[SEP]").unwrap_or(0);

        for (i, token) in all_tokens.iter().enumerate() {
            if let Some(id) = self.vocab.get_id(token) {
                token_ids.push(id);
                attention_mask.push(1);
                token_type_ids.push(if i <= sep_pos { 0 } else { 1 });
            } else if let Some(unk_id) = self.vocab.get_id(&self.config.unk_token) {
                token_ids.push(unk_id);
                attention_mask.push(1);
                token_type_ids.push(if i <= sep_pos { 0 } else { 1 });
            } else {
                return Err(TrustformersError::other(
                    "UNK token not found in vocabulary".to_string(),
                ));
            }
        }

        Ok(TokenizedInput {
            input_ids: token_ids,
            attention_mask,
            token_type_ids: Some(token_type_ids),
            special_tokens_mask: None,
            offset_mapping: None,
            overflowing_tokens: None,
        })
    }

    fn vocab_size(&self) -> usize {
        self.vocab.size()
    }

    fn get_vocab(&self) -> HashMap<String, u32> {
        self.vocab.get_vocab().clone()
    }

    fn token_to_id(&self, token: &str) -> Option<u32> {
        self.vocab.get_id(token)
    }

    fn id_to_token(&self, id: u32) -> Option<String> {
        self.vocab.get_token(id)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::vocab::Vocab;
    use std::collections::HashMap;

    fn create_test_vocab() -> Vocab {
        let mut token_to_id = HashMap::new();

        let tokens = [
            "[PAD]",
            "[UNK]",
            "[CLS]",
            "[SEP]",
            "[MASK]",
            "",
            "",
            "",
            "",
            "",
            "です",
            "ます",
            "こんにちは",
            "ありがとう",
            "すみません",
            "はい",
            "いいえ",
            "今日",
            "明日",
            "昨日",
            "時間",
            "場所",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
            "",
        ];

        for (i, token) in tokens.iter().enumerate() {
            token_to_id.insert(token.to_string(), i as u32);
        }

        Vocab::from_map(token_to_id)
    }

    #[test]
    fn test_character_type_detection() {
        assert!(JapaneseTokenizer::is_hiragana(''));
        assert!(JapaneseTokenizer::is_hiragana(
            "ひらがな".chars().next().expect("Operation failed in test")
        ));
        assert!(JapaneseTokenizer::is_katakana(''));
        assert!(JapaneseTokenizer::is_katakana(
            "カタカナ".chars().next().expect("Operation failed in test")
        ));
        assert!(JapaneseTokenizer::is_kanji(''));
        assert!(JapaneseTokenizer::is_kanji(''));
        assert!(!JapaneseTokenizer::is_japanese_char('a'));
        assert!(!JapaneseTokenizer::is_japanese_char('1'));
    }

    #[test]
    fn test_japanese_punctuation_detection() {
        assert!(JapaneseTokenizer::is_japanese_punctuation(''));
        assert!(JapaneseTokenizer::is_japanese_punctuation(''));
        assert!(JapaneseTokenizer::is_japanese_punctuation(''));
        assert!(JapaneseTokenizer::is_japanese_punctuation(''));
        assert!(!JapaneseTokenizer::is_japanese_punctuation('.'));
        assert!(!JapaneseTokenizer::is_japanese_punctuation(','));
    }

    #[test]
    fn test_katakana_to_hiragana() {
        let result = JapaneseTokenizer::katakana_to_hiragana("カタカナ");
        assert_eq!(result, "かたかな");

        let mixed = JapaneseTokenizer::katakana_to_hiragana("こんにちはカタカナ");
        assert_eq!(mixed, "こんにちはかたかな");
    }

    #[test]
    fn test_character_tokenization() {
        let config = JapaneseTokenizerConfig {
            mode: JapaneseMode::Character,
            use_mecab: false,
            ..Default::default()
        };
        let vocab = create_test_vocab();
        let tokenizer = JapaneseTokenizer::new(config, vocab).expect("Construction failed");

        let result = tokenizer.tokenize_text("こんにちは").expect("Operation failed in test");
        assert_eq!(result.len(), 5);
        assert_eq!(result, vec!["", "", "", "", ""]);
    }

    #[test]
    fn test_word_tokenization() {
        let config = JapaneseTokenizerConfig {
            mode: JapaneseMode::Word,
            use_mecab: false,
            ..Default::default()
        };
        let vocab = create_test_vocab();
        let tokenizer = JapaneseTokenizer::new(config, vocab).expect("Construction failed");

        let result = tokenizer.tokenize_text("こんにちは世界").expect("Operation failed in test");
        assert!(result.len() > 0);
        // Should separate hiragana from kanji
        assert!(result.iter().any(|t| t.chars().all(JapaneseTokenizer::is_hiragana)));
        assert!(result.iter().any(|t| t.chars().all(JapaneseTokenizer::is_kanji)));
    }

    #[test]
    fn test_tokenization_encode_decode() {
        let config = JapaneseTokenizerConfig {
            mode: JapaneseMode::Character,
            use_mecab: false,
            ..Default::default()
        };
        let vocab = create_test_vocab();
        let tokenizer = JapaneseTokenizer::new(config, vocab).expect("Construction failed");

        let result = tokenizer.encode("日本語").expect("Encoding failed");
        assert!(!result.input_ids.is_empty());
        assert_eq!(result.input_ids.len(), result.attention_mask.len());

        let decoded = tokenizer.decode(&result.input_ids).expect("Decoding failed");
        assert_eq!(decoded, "日本語");
    }

    #[test]
    fn test_pair_encoding() {
        let config = JapaneseTokenizerConfig {
            mode: JapaneseMode::Character,
            use_mecab: false,
            ..Default::default()
        };
        let vocab = create_test_vocab();
        let tokenizer = JapaneseTokenizer::new(config, vocab).expect("Construction failed");

        let result = tokenizer.encode_pair("こんにちは", "世界").expect("Operation failed in test");
        assert!(!result.input_ids.is_empty());
        assert!(result.token_type_ids.is_some());

        let token_type_ids = result.token_type_ids.expect("Operation failed in test");
        assert!(token_type_ids.contains(&0)); // First sequence
        assert!(token_type_ids.contains(&1)); // Second sequence
    }

    #[test]
    fn test_dictionary_management() {
        let config = JapaneseTokenizerConfig::default();
        let vocab = create_test_vocab();
        let mut tokenizer = JapaneseTokenizer::new(config, vocab).expect("Construction failed");

        let initial_size = tokenizer.dictionary_size();

        tokenizer.add_word("テスト".to_string());
        assert_eq!(tokenizer.dictionary_size(), initial_size + 1);
        assert!(tokenizer.contains_word("テスト"));

        assert!(tokenizer.remove_word("テスト"));
        assert_eq!(tokenizer.dictionary_size(), initial_size);
        assert!(!tokenizer.contains_word("テスト"));
    }

    #[test]
    fn test_normalization() {
        let config = JapaneseTokenizerConfig {
            normalize_katakana: true,
            mode: JapaneseMode::Character,
            use_mecab: false,
            ..Default::default()
        };
        let vocab = create_test_vocab();
        let tokenizer = JapaneseTokenizer::new(config, vocab).expect("Construction failed");

        let result = tokenizer.tokenize_text("カタカナ").expect("Operation failed in test");
        // Should be normalized to hiragana
        assert!(result.iter().all(|t| t.chars().all(JapaneseTokenizer::is_hiragana)));
    }
}