avila-tokenizers 0.1.0

The most complete tokenizer library in Rust - BPE, WordPiece, Unigram, with native support for GPT, BERT, Llama, Claude
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
use std::collections::HashMap;
use crate::error::{Result, TokenizerError};

/// Efficient vocabulary lookup using HashMap
#[derive(Debug, Clone)]
pub struct VocabHashMap {
    /// Token to ID mapping
    token_to_id: HashMap<String, u32>,
    /// ID to token mapping (for decoding)
    id_to_token: HashMap<u32, String>,
    /// Special tokens
    special_tokens: HashMap<String, u32>,
    /// Unknown token
    unk_token: Option<String>,
    /// Unknown token ID
    unk_id: Option<u32>,
}

impl VocabHashMap {
    /// Create a new empty vocabulary
    pub fn new() -> Self {
        Self {
            token_to_id: HashMap::new(),
            id_to_token: HashMap::new(),
            special_tokens: HashMap::new(),
            unk_token: None,
            unk_id: None,
        }
    }

    /// Create vocabulary with capacity
    pub fn with_capacity(capacity: usize) -> Self {
        Self {
            token_to_id: HashMap::with_capacity(capacity),
            id_to_token: HashMap::with_capacity(capacity),
            special_tokens: HashMap::new(),
            unk_token: None,
            unk_id: None,
        }
    }

    /// Create from HashMap
    pub fn from_hashmap(vocab: HashMap<String, u32>) -> Self {
        let id_to_token: HashMap<u32, String> = vocab
            .iter()
            .map(|(k, &v)| (v, k.clone()))
            .collect();

        Self {
            token_to_id: vocab,
            id_to_token,
            special_tokens: HashMap::new(),
            unk_token: None,
            unk_id: None,
        }
    }

    /// Insert a token into the vocabulary
    pub fn insert(&mut self, token: String, id: u32) {
        self.id_to_token.insert(id, token.clone());
        self.token_to_id.insert(token, id);
    }

    /// Insert a special token
    pub fn insert_special(&mut self, token: String, id: u32) {
        self.insert(token.clone(), id);
        self.special_tokens.insert(token, id);
    }

    /// Set unknown token
    pub fn set_unk_token(&mut self, token: String, id: u32) {
        self.insert(token.clone(), id);
        self.unk_token = Some(token);
        self.unk_id = Some(id);
    }

    /// Get token ID
    pub fn get_id(&self, token: &str) -> Option<u32> {
        self.token_to_id.get(token).copied()
    }

    /// Get token ID or unknown token ID
    pub fn get_id_or_unk(&self, token: &str) -> u32 {
        self.get_id(token)
            .or(self.unk_id)
            .unwrap_or(0)
    }

    /// Get token by ID
    pub fn get_token(&self, id: u32) -> Option<&str> {
        self.id_to_token.get(&id).map(|s| s.as_str())
    }

    /// Get token by ID or unknown token
    pub fn get_token_or_unk(&self, id: u32) -> Result<&str> {
        self.get_token(id)
            .or(self.unk_token.as_deref())
            .ok_or(TokenizerError::InvalidTokenId(id))
    }

    /// Check if token exists
    pub fn contains_token(&self, token: &str) -> bool {
        self.token_to_id.contains_key(token)
    }

    /// Check if ID exists
    pub fn contains_id(&self, id: u32) -> bool {
        self.id_to_token.contains_key(&id)
    }

    /// Check if token is special
    pub fn is_special_token(&self, token: &str) -> bool {
        self.special_tokens.contains_key(token)
    }

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

    /// Check if vocabulary is empty
    pub fn is_empty(&self) -> bool {
        self.token_to_id.is_empty()
    }

    /// Get all tokens
    pub fn tokens(&self) -> Vec<&str> {
        self.token_to_id.keys().map(|s| s.as_str()).collect()
    }

    /// Get all IDs
    pub fn ids(&self) -> Vec<u32> {
        self.id_to_token.keys().copied().collect()
    }

    /// Get token-to-ID map reference
    pub fn token_to_id_map(&self) -> &HashMap<String, u32> {
        &self.token_to_id
    }

    /// Get ID-to-token map reference
    pub fn id_to_token_map(&self) -> &HashMap<u32, String> {
        &self.id_to_token
    }

    /// Get special tokens
    pub fn special_tokens(&self) -> &HashMap<String, u32> {
        &self.special_tokens
    }

    /// Get unknown token
    pub fn unk_token(&self) -> Option<&str> {
        self.unk_token.as_deref()
    }

    /// Get unknown token ID
    pub fn unk_id(&self) -> Option<u32> {
        self.unk_id
    }

    /// Convert tokens to IDs
    pub fn convert_tokens_to_ids(&self, tokens: &[String]) -> Vec<u32> {
        tokens.iter().map(|t| self.get_id_or_unk(t)).collect()
    }

    /// Convert IDs to tokens
    pub fn convert_ids_to_tokens(&self, ids: &[u32]) -> Result<Vec<String>> {
        ids.iter()
            .map(|&id| {
                self.get_token(id)
                    .map(|s| s.to_string())
                    .ok_or(TokenizerError::InvalidTokenId(id))
            })
            .collect()
    }

    /// Encode text to IDs (simple whitespace split)
    pub fn encode_simple(&self, text: &str) -> Vec<u32> {
        text.split_whitespace()
            .map(|token| self.get_id_or_unk(token))
            .collect()
    }

    /// Decode IDs to text
    pub fn decode(&self, ids: &[u32]) -> Result<String> {
        let tokens = self.convert_ids_to_tokens(ids)?;
        Ok(tokens.join(" "))
    }

    /// Decode IDs to text, skipping special tokens
    pub fn decode_skip_special(&self, ids: &[u32]) -> Result<String> {
        let tokens: Result<Vec<String>> = ids.iter()
            .filter_map(|&id| {
                self.get_token(id).and_then(|token| {
                    if self.is_special_token(token) {
                        None
                    } else {
                        Some(Ok(token.to_string()))
                    }
                })
            })
            .collect();

        Ok(tokens?.join(" "))
    }

    /// Merge another vocabulary into this one
    pub fn merge(&mut self, other: &VocabHashMap) {
        for (token, &id) in &other.token_to_id {
            if !self.contains_token(token) {
                self.insert(token.clone(), id);
            }
        }
    }

    /// Extend vocabulary with new tokens
    pub fn extend(&mut self, tokens: Vec<String>) {
        let start_id = self.len() as u32;
        for (i, token) in tokens.into_iter().enumerate() {
            if !self.contains_token(&token) {
                self.insert(token, start_id + i as u32);
            }
        }
    }

    /// Clear the vocabulary
    pub fn clear(&mut self) {
        self.token_to_id.clear();
        self.id_to_token.clear();
        self.special_tokens.clear();
        self.unk_token = None;
        self.unk_id = None;
    }

    /// Get vocabulary statistics
    pub fn stats(&self) -> VocabStats {
        VocabStats {
            total_tokens: self.len(),
            special_tokens: self.special_tokens.len(),
            has_unk_token: self.unk_token.is_some(),
            max_id: self.id_to_token.keys().max().copied(),
        }
    }
}

impl Default for VocabHashMap {
    fn default() -> Self {
        Self::new()
    }
}

/// Vocabulary statistics
#[derive(Debug, Clone)]
pub struct VocabStats {
    pub total_tokens: usize,
    pub special_tokens: usize,
    pub has_unk_token: bool,
    pub max_id: Option<u32>,
}

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

    #[test]
    fn test_vocab_insert_and_get() {
        let mut vocab = VocabHashMap::new();

        vocab.insert("hello".to_string(), 1);
        vocab.insert("world".to_string(), 2);

        assert_eq!(vocab.get_id("hello"), Some(1));
        assert_eq!(vocab.get_id("world"), Some(2));
        assert_eq!(vocab.get_token(1), Some("hello"));
        assert_eq!(vocab.get_token(2), Some("world"));
        assert_eq!(vocab.len(), 2);
    }

    #[test]
    fn test_vocab_special_tokens() {
        let mut vocab = VocabHashMap::new();

        vocab.insert_special("[PAD]".to_string(), 0);
        vocab.insert_special("[UNK]".to_string(), 1);
        vocab.insert("hello".to_string(), 2);

        assert!(vocab.is_special_token("[PAD]"));
        assert!(vocab.is_special_token("[UNK]"));
        assert!(!vocab.is_special_token("hello"));
        assert_eq!(vocab.special_tokens().len(), 2);
    }

    #[test]
    fn test_vocab_unk_token() {
        let mut vocab = VocabHashMap::new();

        vocab.set_unk_token("[UNK]".to_string(), 0);
        vocab.insert("hello".to_string(), 1);

        assert_eq!(vocab.get_id_or_unk("hello"), 1);
        assert_eq!(vocab.get_id_or_unk("unknown"), 0);
        assert_eq!(vocab.unk_token(), Some("[UNK]"));
        assert_eq!(vocab.unk_id(), Some(0));
    }

    #[test]
    fn test_vocab_convert_tokens() {
        let mut vocab = VocabHashMap::new();

        vocab.set_unk_token("[UNK]".to_string(), 0);
        vocab.insert("hello".to_string(), 1);
        vocab.insert("world".to_string(), 2);

        let tokens = vec!["hello".to_string(), "world".to_string(), "unknown".to_string()];
        let ids = vocab.convert_tokens_to_ids(&tokens);

        assert_eq!(ids, vec![1, 2, 0]); // unknown -> [UNK] (0)
    }

    #[test]
    fn test_vocab_convert_ids() {
        let mut vocab = VocabHashMap::new();

        vocab.insert("hello".to_string(), 1);
        vocab.insert("world".to_string(), 2);

        let ids = vec![1, 2];
        let tokens = vocab.convert_ids_to_tokens(&ids).unwrap();

        assert_eq!(tokens, vec!["hello", "world"]);
    }

    #[test]
    fn test_vocab_decode() {
        let mut vocab = VocabHashMap::new();

        vocab.insert("hello".to_string(), 1);
        vocab.insert("world".to_string(), 2);

        let ids = vec![1, 2];
        let text = vocab.decode(&ids).unwrap();

        assert_eq!(text, "hello world");
    }

    #[test]
    fn test_vocab_decode_skip_special() {
        let mut vocab = VocabHashMap::new();

        vocab.insert_special("[CLS]".to_string(), 0);
        vocab.insert("hello".to_string(), 1);
        vocab.insert("world".to_string(), 2);
        vocab.insert_special("[SEP]".to_string(), 3);

        let ids = vec![0, 1, 2, 3];
        let text = vocab.decode_skip_special(&ids).unwrap();

        assert_eq!(text, "hello world");
    }

    #[test]
    fn test_vocab_extend() {
        let mut vocab = VocabHashMap::new();

        vocab.insert("hello".to_string(), 0);

        vocab.extend(vec!["world".to_string(), "test".to_string()]);

        assert_eq!(vocab.len(), 3);
        assert!(vocab.contains_token("world"));
        assert!(vocab.contains_token("test"));
    }

    #[test]
    fn test_vocab_from_hashmap() {
        let mut map = HashMap::new();
        map.insert("hello".to_string(), 1);
        map.insert("world".to_string(), 2);

        let vocab = VocabHashMap::from_hashmap(map);

        assert_eq!(vocab.len(), 2);
        assert_eq!(vocab.get_id("hello"), Some(1));
        assert_eq!(vocab.get_token(2), Some("world"));
    }

    #[test]
    fn test_vocab_stats() {
        let mut vocab = VocabHashMap::new();

        vocab.set_unk_token("[UNK]".to_string(), 0);
        vocab.insert_special("[PAD]".to_string(), 1);
        vocab.insert("hello".to_string(), 2);
        vocab.insert("world".to_string(), 3);

        let stats = vocab.stats();

        assert_eq!(stats.total_tokens, 4);
        assert_eq!(stats.special_tokens, 1); // Only [PAD], [UNK] is set separately
        assert!(stats.has_unk_token);
        assert_eq!(stats.max_id, Some(3));
    }
}