use anyhow::Result;
use torsh_text::tokenization::{BPETokenizer, Tokenizer, WhitespaceTokenizer};
use torsh_text::vocab::{SpecialTokens, Vocabulary};
fn main() -> Result<()> {
println!("=== Basic Tokenization Examples ===\n");
println!("1. Whitespace Tokenization:");
let tokenizer = WhitespaceTokenizer::new();
let text = "Hello world! This is a test.";
let tokens = tokenizer.tokenize(text)?;
println!("Input: {}", text);
println!("Tokens: {:?}\n", tokens);
println!("2. BPE Tokenization:");
let training_texts = vec![
"hello world".to_string(),
"world hello".to_string(),
"hello there".to_string(),
"there world".to_string(),
"hello hello world world".to_string(),
];
let bpe_tokenizer = BPETokenizer::from_texts(&training_texts, 50)?;
let test_text = "hello there world";
let bpe_tokens = bpe_tokenizer.tokenize(test_text)?;
let token_ids = bpe_tokenizer.encode(test_text)?;
println!("Input: {}", test_text);
println!("BPE Tokens: {:?}", bpe_tokens);
println!("Token IDs: {:?}", token_ids);
let decoded = bpe_tokenizer.decode(&token_ids)?;
println!("Decoded: {}\n", decoded);
println!("3. Vocabulary Management:");
let mut vocab = Vocabulary::new(Some(SpecialTokens::default()));
let words = vec!["hello", "world", "test", "example"];
for word in &words {
vocab.add_token(word);
}
println!("Vocabulary size: {}", vocab.len());
for word in &words {
if let Some(id) = vocab.get_token_id(word) {
println!("{} -> {}", word, id);
}
}
Ok(())
}