2. Text Processing

Tokenization

Study tokenization strategies for different languages, subword methods, and effects on downstream tasks and model performance.

Tokenization

Hey students! đź‘‹ Welcome to one of the most fundamental concepts in Natural Language Processing - tokenization! In this lesson, you'll discover how computers break down human language into digestible pieces, explore different strategies used across various languages, and understand how these choices dramatically impact the performance of AI language models. By the end of this lesson, you'll understand why tokenization is often called the "first domino" in the NLP pipeline and how it sets the stage for everything that follows.

What is Tokenization and Why Does It Matter?

Imagine trying to teach someone a new language by showing them an entire novel at once - overwhelming, right? 📚 That's exactly the challenge computers face when processing human language. Tokenization is the process of breaking down raw text into smaller, manageable units called "tokens" that language models can understand and process effectively.

Think of tokenization like cutting a pizza into slices - you need to decide how big each slice should be and where to make the cuts. In NLP, these "slices" are tokens, and they can be individual characters, words, parts of words, or even larger chunks of text. The way you slice your text pizza directly affects how well your AI model will digest and understand the language!

Research shows that tokenization significantly influences language model performance, with studies indicating that poor tokenization choices can reduce model accuracy by up to 15-20% on downstream tasks. This makes tokenization a critical preprocessing step that deserves careful consideration.

Traditional Word-Level Tokenization: The Starting Point

The most intuitive approach to tokenization is splitting text by spaces and punctuation - essentially treating each word as a separate token. For example, the sentence "The cat sat on the mat" would become six tokens: ["The", "cat", "sat", "on", "the", "mat"].

Word-level tokenization works reasonably well for languages like English, where words are clearly separated by spaces. However, this approach faces several challenges:

The Out-of-Vocabulary Problem: Imagine your model encounters the word "supercalifragilisticexpialidocious" during testing, but it wasn't in the training data. The model would treat this as an unknown token, losing valuable information. Real-world vocabularies can contain millions of unique words, making it impossible to include every possible word in your model's vocabulary.

Morphologically Rich Languages: Languages like Turkish, Finnish, or German can create new words by combining existing ones. The German word "Donaudampfschifffahrtsgesellschaftskapitän" (Danube steamship company captain) would be treated as a single, likely unknown token, despite being composed of meaningful parts.

Memory Inefficiency: Storing large vocabularies requires significant memory. GPT-2, for instance, uses a vocabulary of 50,257 tokens, while some word-level approaches might require vocabularies exceeding 100,000 tokens.

Subword Tokenization: The Game Changer

Subword tokenization emerged as a solution to the limitations of word-level approaches. Instead of treating entire words as indivisible units, subword methods break words into smaller, meaningful components. This approach offers the best of both worlds: it can handle unknown words by breaking them into known subparts while maintaining semantic meaning.

Byte Pair Encoding (BPE): Building Vocabulary from Patterns

Byte Pair Encoding, originally developed for data compression, has become one of the most popular subword tokenization methods. BPE works by iteratively finding the most frequent pair of characters or character sequences in your training data and merging them into a single token.

Here's how BPE works in practice: Starting with individual characters, BPE identifies the most common adjacent pairs. If "th" appears frequently, it becomes a single token. Then "the" might become common enough to merge into one token, and so on. This process continues until you reach your desired vocabulary size.

For example, the word "tokenization" might be split as: ["token", "ization"] or ["tok", "en", "ization"] depending on what patterns BPE learned from the training data. This allows the model to handle new words like "detokenization" by recognizing the familiar parts "de", "token", and "ization".

Research indicates that BPE typically achieves vocabulary sizes between 10,000-50,000 tokens while maintaining good coverage of natural language patterns. Models using BPE have shown improved performance on machine translation tasks, with BLEU scores increasing by 2-3 points compared to word-level tokenization.

WordPiece: Google's Approach to Subword Tokenization

WordPiece, developed by Google and used in models like BERT, takes a slightly different approach than BPE. While BPE focuses on frequency, WordPiece considers the likelihood improvement when merging tokens. It asks: "If I merge these two tokens, how much will it improve my language model's ability to predict text?"

WordPiece uses a special prefix "##" to indicate subword tokens that don't start a word. For example, "playing" might be tokenized as ["play", "##ing"]. This helps the model understand word boundaries and relationships between subwords.

Studies show that WordPiece often produces more linguistically meaningful splits compared to BPE, particularly for morphologically complex words. In multilingual settings, WordPiece has demonstrated superior performance on tasks requiring understanding of word formation patterns.

SentencePiece: Language-Agnostic Tokenization

SentencePiece, developed by Google, represents a significant advancement in tokenization technology. Unlike BPE and WordPiece, which assume pre-tokenized input (words separated by spaces), SentencePiece treats the input as a raw stream of characters, making it truly language-agnostic.

This approach is particularly valuable for languages without clear word boundaries, such as Chinese, Japanese, or Thai. SentencePiece can handle these languages without requiring language-specific preprocessing steps.

SentencePiece also implements both BPE and unigram language model algorithms, giving practitioners flexibility in choosing the most appropriate method for their specific use case. Research has shown that SentencePiece's non-greedy tokenization approach can improve downstream task performance by 1-2% compared to traditional greedy methods.

Tokenization Strategies for Different Languages

Different languages present unique challenges for tokenization, and understanding these differences is crucial for building effective multilingual NLP systems.

English and Similar Languages: Space-separated languages work well with subword methods. BPE and WordPiece typically achieve good results with vocabulary sizes around 30,000-50,000 tokens.

Chinese, Japanese, and Korean: These languages don't use spaces between words, making word boundary detection challenging. Character-level tokenization was traditionally used, but modern approaches like SentencePiece have shown superior results by learning meaningful character combinations.

Arabic and Hebrew: Right-to-left languages with complex morphology benefit from subword approaches that can handle prefixes, suffixes, and root variations. Special preprocessing to handle diacritics and normalization is often necessary.

Agglutinative Languages (Turkish, Finnish, Hungarian): These languages create words by combining multiple morphemes. Subword tokenization excels here, as it can break down complex words into their constituent meaningful parts.

Research comparing tokenization strategies across languages shows that subword methods consistently outperform word-level approaches, with improvements ranging from 5-15% on various NLP tasks depending on the language's morphological complexity.

Impact on Downstream Tasks and Model Performance

The choice of tokenization strategy has profound effects on how well your model performs on specific tasks. Let's explore these impacts:

Machine Translation: Subword tokenization has revolutionized machine translation by enabling models to handle rare words and maintain semantic relationships across languages. Studies show that BPE-based systems achieve 15-20% better BLEU scores compared to word-level systems on low-resource language pairs.

Text Classification: The granularity of tokenization affects how models capture semantic features. Research indicates that moderate subword vocabulary sizes (20,000-40,000 tokens) often provide the best balance between semantic preservation and computational efficiency for classification tasks.

Question Answering: Fine-grained tokenization can help models better understand word relationships and morphological variations, leading to improved exact match scores in reading comprehension tasks.

Named Entity Recognition: Subword tokenization can both help and hinder NER performance. While it handles unknown names better, it can also split entity names across multiple tokens, requiring careful handling during prediction and evaluation.

Recent studies analyzing tokenization effects across multiple tasks found that optimal vocabulary sizes vary by task: machine translation benefits from larger vocabularies (40,000-60,000), while text classification often performs best with smaller ones (15,000-30,000).

Conclusion

Tokenization serves as the critical bridge between human language and machine understanding in NLP systems. We've explored how traditional word-level approaches gave way to sophisticated subword methods like BPE, WordPiece, and SentencePiece, each offering unique advantages for different scenarios. The choice of tokenization strategy significantly impacts model performance across various tasks and languages, with subword approaches consistently demonstrating superior handling of vocabulary diversity and morphological complexity. As you continue your NLP journey, remember that tokenization isn't just a preprocessing step - it's a fundamental design choice that shapes how your models understand and process language.

Study Notes

• Tokenization Definition: Process of breaking text into smaller units (tokens) that models can process effectively

• Word-Level Limitations: Out-of-vocabulary problems, memory inefficiency, poor handling of morphologically rich languages

• Byte Pair Encoding (BPE): Iteratively merges most frequent character pairs; vocabulary sizes typically 10,000-50,000 tokens

• WordPiece: Merges tokens based on likelihood improvement; uses "##" prefix for subword tokens; used in BERT

• SentencePiece: Language-agnostic approach; treats input as raw character stream; handles languages without word boundaries

• Performance Impact: Subword methods improve machine translation BLEU scores by 15-20% over word-level approaches

• Optimal Vocabulary Sizes: Machine translation (40,000-60,000), text classification (15,000-30,000), general NLP (20,000-40,000)

• Language-Specific Considerations: Space-separated languages favor BPE/WordPiece; character-based languages benefit from SentencePiece

• Downstream Task Effects: Tokenization choice affects model accuracy by 5-20% depending on task complexity and language morphology

Practice Quiz

5 questions to test your understanding

Tokenization — Natural Language Processing | A-Warded