File size: 9,136 Bytes
2bbfbb7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
//! Text tokenization for TTS
//!
//! Uses SentencePiece BPE tokenization for converting text to tokens

use crate::{Error, Result};
use std::collections::HashMap;
use std::path::Path;

/// Tokenizer configuration
#[derive(Debug, Clone)]
pub struct TokenizerConfig {
    /// Path to BPE model
    pub model_path: String,
    /// Vocabulary size
    pub vocab_size: usize,
    /// Start of text token ID
    pub bos_id: i64,
    /// End of text token ID
    pub eos_id: i64,
    /// Unknown token ID
    pub unk_id: i64,
    /// Padding token ID
    pub pad_id: i64,
}

impl Default for TokenizerConfig {
    fn default() -> Self {
        Self {
            model_path: "models/bpe.model".to_string(),
            vocab_size: 6681,
            bos_id: 1,
            eos_id: 2,
            unk_id: 0,
            pad_id: 3,
        }
    }
}

/// Text tokenizer using BPE (Byte Pair Encoding)
#[derive(Debug)]
pub struct TextTokenizer {
    /// Configuration
    config: TokenizerConfig,
    /// Token to ID mapping
    token_to_id: HashMap<String, i64>,
    /// ID to token mapping
    id_to_token: HashMap<i64, String>,
    /// Character-level fallback vocabulary
    char_vocab: HashMap<char, i64>,
}

impl TextTokenizer {
    /// Create new tokenizer with default vocabulary
    pub fn new(config: TokenizerConfig) -> Result<Self> {
        let mut token_to_id = HashMap::new();
        let mut id_to_token = HashMap::new();
        let mut char_vocab = HashMap::new();

        // Add special tokens
        token_to_id.insert("<unk>".to_string(), config.unk_id);
        token_to_id.insert("<s>".to_string(), config.bos_id);
        token_to_id.insert("</s>".to_string(), config.eos_id);
        token_to_id.insert("<pad>".to_string(), config.pad_id);

        id_to_token.insert(config.unk_id, "<unk>".to_string());
        id_to_token.insert(config.bos_id, "<s>".to_string());
        id_to_token.insert(config.eos_id, "</s>".to_string());
        id_to_token.insert(config.pad_id, "<pad>".to_string());

        // Add basic ASCII characters
        let mut next_id = 4i64;
        for c in ' '..='~' {
            char_vocab.insert(c, next_id);
            token_to_id.insert(c.to_string(), next_id);
            id_to_token.insert(next_id, c.to_string());
            next_id += 1;
        }

        // Add Chinese character range (simplified approach)
        // In production, this would load from the actual BPE model
        for code_point in 0x4E00u32..=0x9FFF {
            if let Some(c) = char::from_u32(code_point) {
                char_vocab.insert(c, next_id);
                token_to_id.insert(c.to_string(), next_id);
                id_to_token.insert(next_id, c.to_string());
                next_id += 1;

                if next_id >= config.vocab_size as i64 {
                    break;
                }
            }
        }

        Ok(Self {
            config,
            token_to_id,
            id_to_token,
            char_vocab,
        })
    }

    /// Load tokenizer from model file
    pub fn load<P: AsRef<Path>>(path: P) -> Result<Self> {
        let path = path.as_ref();
        if !path.exists() {
            return Err(Error::FileNotFound(path.display().to_string()));
        }

        // In production, this would load the actual SentencePiece model
        // For now, create a character-level tokenizer
        let config = TokenizerConfig {
            model_path: path.display().to_string(),
            ..Default::default()
        };

        Self::new(config)
    }

    /// Encode text to token IDs
    pub fn encode(&self, text: &str) -> Result<Vec<i64>> {
        let mut tokens = Vec::new();

        // Add BOS token
        tokens.push(self.config.bos_id);

        // Tokenize character by character (simplified)
        // In production, this would use BPE merging
        for ch in text.chars() {
            if let Some(&id) = self.char_vocab.get(&ch) {
                tokens.push(id);
            } else if let Some(&id) = self.token_to_id.get(&ch.to_string()) {
                tokens.push(id);
            } else {
                // Unknown token
                tokens.push(self.config.unk_id);
            }
        }

        // Add EOS token
        tokens.push(self.config.eos_id);

        Ok(tokens)
    }

    /// Encode text without special tokens
    pub fn encode_without_special(&self, text: &str) -> Result<Vec<i64>> {
        let mut tokens = Vec::new();

        for ch in text.chars() {
            if let Some(&id) = self.char_vocab.get(&ch) {
                tokens.push(id);
            } else if let Some(&id) = self.token_to_id.get(&ch.to_string()) {
                tokens.push(id);
            } else {
                tokens.push(self.config.unk_id);
            }
        }

        Ok(tokens)
    }

    /// Decode token IDs to text
    pub fn decode(&self, tokens: &[i64]) -> Result<String> {
        let mut text = String::new();

        for &token_id in tokens {
            // Skip special tokens
            if token_id == self.config.bos_id
                || token_id == self.config.eos_id
                || token_id == self.config.pad_id
            {
                continue;
            }

            if let Some(token) = self.id_to_token.get(&token_id) {
                text.push_str(token);
            } else {
                // Unknown token placeholder
                text.push('?');
            }
        }

        Ok(text)
    }

    /// Get vocabulary size
    pub fn vocab_size(&self) -> usize {
        self.config.vocab_size
    }

    /// Get BOS token ID
    pub fn bos_id(&self) -> i64 {
        self.config.bos_id
    }

    /// Get EOS token ID
    pub fn eos_id(&self) -> i64 {
        self.config.eos_id
    }

    /// Get UNK token ID
    pub fn unk_id(&self) -> i64 {
        self.config.unk_id
    }

    /// Get PAD token ID
    pub fn pad_id(&self) -> i64 {
        self.config.pad_id
    }

    /// Pad sequences to same length
    pub fn pad_sequences(&self, sequences: &[Vec<i64>], max_len: Option<usize>) -> Vec<Vec<i64>> {
        let max_length = max_len.unwrap_or_else(|| sequences.iter().map(|s| s.len()).max().unwrap_or(0));

        sequences
            .iter()
            .map(|seq| {
                let mut padded = seq.clone();
                while padded.len() < max_length {
                    padded.push(self.config.pad_id);
                }
                padded.truncate(max_length);
                padded
            })
            .collect()
    }

    /// Create attention mask (1 for real tokens, 0 for padding)
    pub fn create_attention_mask(&self, tokens: &[i64]) -> Vec<i64> {
        tokens
            .iter()
            .map(|&t| if t == self.config.pad_id { 0 } else { 1 })
            .collect()
    }

    /// Batch encode multiple texts
    pub fn batch_encode(&self, texts: &[&str]) -> Result<Vec<Vec<i64>>> {
        texts.iter().map(|text| self.encode(text)).collect()
    }

    /// Batch encode and pad
    pub fn batch_encode_padded(
        &self,
        texts: &[&str],
        max_len: Option<usize>,
    ) -> Result<Vec<Vec<i64>>> {
        let encoded: Vec<Vec<i64>> = self.batch_encode(texts)?;
        Ok(self.pad_sequences(&encoded, max_len))
    }
}

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

    #[test]
    fn test_tokenizer_creation() {
        let config = TokenizerConfig::default();
        let tokenizer = TextTokenizer::new(config).unwrap();
        assert!(tokenizer.vocab_size() > 0);
    }

    #[test]
    fn test_encode_decode() {
        let config = TokenizerConfig::default();
        let tokenizer = TextTokenizer::new(config).unwrap();

        let text = "Hello world";
        let tokens = tokenizer.encode(text).unwrap();

        // Should start with BOS and end with EOS
        assert_eq!(tokens[0], tokenizer.bos_id());
        assert_eq!(*tokens.last().unwrap(), tokenizer.eos_id());

        let decoded = tokenizer.decode(&tokens).unwrap();
        assert_eq!(decoded, text);
    }

    #[test]
    fn test_encode_chinese() {
        let config = TokenizerConfig::default();
        let tokenizer = TextTokenizer::new(config).unwrap();

        let text = "你好";
        let tokens = tokenizer.encode(text).unwrap();

        // Should have BOS + 2 chars + EOS = 4 tokens
        assert_eq!(tokens.len(), 4);
    }

    #[test]
    fn test_pad_sequences() {
        let config = TokenizerConfig::default();
        let tokenizer = TextTokenizer::new(config).unwrap();

        let seq1 = vec![1, 2, 3];
        let seq2 = vec![1, 2, 3, 4, 5];

        let padded = tokenizer.pad_sequences(&[seq1, seq2], None);

        assert_eq!(padded[0].len(), 5);
        assert_eq!(padded[1].len(), 5);
        assert_eq!(padded[0][3], tokenizer.pad_id());
    }

    #[test]
    fn test_attention_mask() {
        let config = TokenizerConfig::default();
        let tokenizer = TextTokenizer::new(config).unwrap();

        let tokens = vec![1, 2, tokenizer.pad_id(), tokenizer.pad_id()];
        let mask = tokenizer.create_attention_mask(&tokens);

        assert_eq!(mask, vec![1, 1, 0, 0]);
    }
}