Computers cannot understand human language directly; they operate purely on numbers and matrix transformations. Natural Language Processing (NLP) is the branch of AI that enables machines to ingest, parse, understand, and generate human languages. The foundational bridge converting continuous unstructured human text into discrete numeric tokens is Tokenization.
Historically, classical NLP utilized rule-based linguistic pipelines:
studi). Fast, but frequently outputs non-words.good).Word-level tokenization suffers from a fatal dilemma: if your vocabulary contains 50,000 words, any rare word, misspelling, slang, or technical jargon (e.g., "bioinformatics", "neurodegenerative") is mapped to an unknown token: <UNK>. Conversely, character-level tokenization eliminates OOV words but produces sequence lengths 5x longer, blowing up the ( O(N^2) ) memory cost of Transformers!
Modern foundation models solve this through Subword Tokenization. Common words remain intact as single tokens, while rare words are automatically decomposed into meaningful morphological prefixes, roots, and suffixes (e.g., "unbreakable" becomes ["un", "break", "able"]):
| Algorithm | Key Mechanics & Splitting Rule | Used In Modern AI |
|---|---|---|
| Byte Pair Encoding (BPE) (Sennrich 2016) | Starts with individual characters. Iteratively counts the most frequent adjacent pair of symbols across the corpus and merges them into a new token until vocabulary reaches target size ( V ). | GPT-2, GPT-3.5, GPT-4, Llama 2/3, Mistral (uses Byte-level BPE on raw UTF-8 bytes). |
| WordPiece (Schuster & Nakajima) | Similar to BPE, but selects the pair to merge that maximizes the likelihood of the language model training data. Uses ## to denote subword continuation. |
BERT, DistilBERT, Electra |
| Unigram / SentencePiece (Kudo 2018) | Starts with an enormous vocabulary and iteratively prunes the tokens that contribute least to overall corpus likelihood. Treats whitespace as an explicit symbol (_), language-agnostic. |
T5, ALBERT, Llama (early), Gemma |
Modern LLMs (like OpenAI's tiktoken) operate directly on raw UTF-8 bytes rather than Unicode characters. Because the byte space is strictly bounded between 0 and 255, any text in any human language, programming language, emoji, or mathematical symbol can be encoded without ever encountering an Out-Of-Vocabulary (<UNK>) error!
The following script demonstrates how modern LLM tokenizers break sentences into discrete token IDs:
import tiktoken
# 1. Load the GPT-4o / GPT-4 tokenizer (o200k_base / cl100k_base)
tokenizer = tiktoken.get_encoding("cl100k_base")
text = "Artificial Intelligence is transforming bioinformatics & quantum computing!"
# 2. Encode text into discrete token IDs
token_ids = tokenizer.encode(text)
print(f"Original Text: '{text}'")
print(f"Total Tokens : {len(token_ids)}")
print(f"Token IDs : {token_ids}
")
# 3. Decode each token ID back to its subword piece
print("Subword Token Breakdown:")
for token_id in token_ids:
piece = tokenizer.decode([token_id])
print(f" Token ID {token_id:6d} -> Subword: '{piece}'")
Broaden your foundational and advanced AI knowledge across our curriculum:
Transforming discrete tokens into rich geometric vector embeddings.
How token IDs and positional embeddings enter the transformer stack.
Token budgets, context window constraints, and BPE tokenizers.
Exam definitions for stemming, lemmatization, stop words, and N-grams.