7 episodes · follows chapter 2 of the textbook
Words and Tokens
What is a "word", and what does a model actually read? This module starts from counting (types, instances, Heaps' and Zipf's power laws), descends to the bytes underneath text (Unicode, UTF-8, normalization), treats regular expressions as the finite machines they are, and then builds the tokenizer every modern LLM uses: byte-pair encoding, learned by greedy compression and applied by a merge table. It closes with minimum edit distance as dynamic programming and the noisy-channel spelling corrector, the course's first Bayesian decoder.
Objectives
Language
- Distinguish word types from word instances; explain why vocabulary never stops growing
- Explain why whitespace tokenization fails for Chinese, Japanese and Thai, and why NFC vs NFD changes token ids
- Explain how BPE learns subwords, why pretokenization exists, and how tokenization causes 'strawberry' and arithmetic failures and cross-language cost inequity
- Correct spelling with a noisy-channel model and diagnose its errors
Math & data structures
- Power laws and log-log linearization; least-squares line fitting in closed form and by gradient descent
- Code points, UTF-8 bit layout and validity rules; canonical vs compatibility equivalence
- Regular expressions as NFAs (Thompson construction), set simulation vs backtracking, subset construction and DFA blowup
- Hash maps, inverted indexes and lazy-deletion heaps for incremental BPE
- Dynamic programming with backpointers: Wagner-Fischer edit distance, alignment, O(nm) time, two-row space
- Bayes' rule in log space: prior x likelihood, add-alpha smoothing of confusion counts
Code
- Heaps/Zipf measurement and fitting with a logged gradient-descent run
- UTF-8 encoder/validating decoder; grapheme and normalization tools
- Thompson NFA from scratch; Unix word-count pipeline replicated exactly
- Character-level and byte-level BPE (naive + heap learners) with merge-table inspection; Rust port
- Edit distance with backtrace (Python + C), reused by Automatic Speech Recognition for WER
- Noisy-channel spelling corrector with a learned error model, cross-validated
Episodes
- Words, Types, and Heaps' Law31 min · 34 cues
Types vs instances, corpora, Heaps/Herdan law |V|=kN^β, Zipf, power laws and log-log plots, morphology/morphemes.
- Unicode for Programmers27 min · 28 cues
Code points, UTF-8 bytes, grapheme clusters, normalization NFC/NFD/NFKC, why bytes matter for tokenizers.
- Regular Expressions Are Machines (and Unix Is a Tokenizer)34 min · 30 cues
Regex as finite automata (Kleene, Thompson), NFA/DFA, Unix tools pipelines (tr, sort, uniq), rule-based tokenization and its limits.
- BPE I: Learning a Vocabulary by Compression30 min · 30 cues
Gage 1994 → Sennrich 2016; learner algorithm; pair-count hash maps; heap acceleration; pretokenization.
- BPE II: Segmenting, Bytes, and Fairness28 min · 29 cues
Segmenter (apply merges), byte-level BPE, tiktoken/minbpe dissection, token inequity, 'strawberry' and arithmetic failures.
- Minimum Edit Distance as Dynamic Programming26 min · 29 cues
Levenshtein/Wagner–Fischer, DP table + backpointers, alignment, costs, complexity, link to git diff and WER later.
- The Noisy Channel: Spelling Correction (Appendix D)30 min · 30 cues
Noisy-channel model, Bayes in action, candidate generation, error models, language-model prior; Shannon's channel.
Exercises
Labs
- Words, Types, and Heaps' Law browser C02.01 · 50 min
- Unicode for Programmers browser C02.02 · 45 min
- Regular Expressions Are Machines (and Unix Is a Tokenizer) browser C02.03 · 60 min
- BPE I: Learning a Vocabulary by Compression browser C02.04 · 75 min
- BPE II: Segmenting, Bytes, and Fairness browser C02.05 · 60 min
- Minimum Edit Distance as Dynamic Programming browser C02.06 · 55 min
- The Noisy Channel: Spelling Correction (Appendix D) browser C02.07 · 60 min
Library contributions
Files in nlpcore this module teaches you to write.
nlpcore/tokenizers/bpe.pynlpcore/tokenizers/byte_bpe.pynlpcore/tokenizers/regex_pretok.pynlpcore/text/edit_distance.pynlpcore/text/regex_nfa.pynlpcore/text/unicode_tools.pynlpcore/text/spelling.py
Sources
- Eric Brill, Robert C. Moore, An Improved Error Model for Noisy Channel Spelling Correction, ACL 2000
- Kenneth Ward Church, Unix for Poets (tutorial notes)
- Cloudflare, Details of the Cloudflare outage on July 2, 2019 (WAF regex backtracking) · link
- Graham, Knuth, Patashnik, Concrete Mathematics, 2nd ed., ch. 9 (asymptotics) and ch. 2 (sums)
- Russ Cox, Regular Expression Matching Can Be Simple And Fast (but is slow in Java, Perl, PHP, Python, Ruby, ...) · link
- Zhang, Lipton, Li, Smola, Dive into Deep Learning, sec. 3.1 (linear regression, squared loss, closed form, minibatch SGD) · link
- Fred J. Damerau, A technique for computer detection and correction of spelling errors, Communications of the ACM 7(3), 1964
- Philip Gage, A New Algorithm for Data Compression, The C Users Journal 12(2):23-38, February 1994
- Go standard library, src/testdata/Isaac.Newton-Opticks.txt, used by compress/flate, compress/bzip2, internal/zstd and index/suffixarray tests
- Gustav Herdan, Type-Token Mathematics (1960); H. S. Heaps, Information Retrieval: Computational and Theoretical Aspects (1978)
- D. S. Hirschberg, A linear space algorithm for computing maximal common subsequences, CACM 1975
- Mark D. Kernighan, Kenneth W. Church, William A. Gale, A Spelling Correction Program Based on a Noisy Channel Model, COLING 1990 · link
- S. C. Kleene, Representation of Events in Nerve Nets and Finite Automata, RAND (research memorandum, 1951; in Automata Studies, Princeton 1956) · link
- Taku Kudo, John Richardson, SentencePiece (2018); Taku Kudo, Subword Regularization (2018, unigram LM tokenizer)
- Jeremy Kun, A Programmer's Introduction to Mathematics (culture notes: notation as API, definitions as type signatures)
- Sander Land, Max Bartolo, Fishing for Magikarp: Automatically Detecting Under-trained Tokens in Large Language Models (EMNLP 2024) · arXiv:2405.05417
- V. I. Levenshtein, Binary codes capable of correcting deletions, insertions, and reversals, Doklady Akademii Nauk SSSR 163(4), 1965 (English: Soviet Physics Doklady 10(8), 1966) · link
- Jon Bentley, Programming Pearls column 'A Literate Program' with Don Knuth's program and Doug McIlroy's review and six-line shell pipeline, CACM 1986
- Eugene W. Myers, An O(ND) Difference Algorithm and Its Variations, Algorithmica 1986 (basis of diff/git diff)
- Peter Norvig, How to Write a Spelling Corrector (essay) · link
- Aleksandar Petrov, Emanuele La Malfa, Philip H.S. Torr, Adel Bibi, Language Model Tokenizers Introduce Unfairness Between Languages (NeurIPS 2023) · arXiv:2305.15425
- Rob Pike, UTF-8 history (email, 2003), archived at https://www.cl.cam.ac.uk/~mgk25/ucs/utf-8-history.txt · link
- Radford et al., Language Models are Unsupervised Multitask Learners (GPT-2 report), OpenAI 2019 (byte-level BPE, regex pretokenizer)
- Rico Sennrich, Barry Haddow, Alexandra Birch, Neural Machine Translation of Rare Words with Subword Units (ACL 2016) · arXiv:1508.07909
- Claude E. Shannon, A Mathematical Theory of Communication, Bell System Technical Journal 27, 1948
- Aaditya K. Singh, DJ Strouse, Tokenization counts: the impact of tokenization on arithmetic in frontier LLMs (2024) · arXiv:2402.14903
- Jurafsky & Martin, SLP3, Appendix D: Spelling Correction and the Noisy Channel (online appendix) · link
- Jurafsky & Martin, SLP3 draft (Aug 2026), Ch. 1 Introduction (tokens; Einstein's German title, 27 tokens) · link
- Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft (Aug 19, 2026), Ch. 2 Words and Tokens · link · §2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9, Historical Notes
- Ken Thompson, Programming Techniques: Regular expression search algorithm, Communications of the ACM 11(6):419-422, 1968
- Unicode Standard Annex #15, Unicode Normalization Forms (NFC, NFD, NFKC, NFKD) · link
- Unicode Standard Annex #29, Unicode Text Segmentation (grapheme cluster rules GB1-GB999) · link
- Robert A. Wagner, Michael J. Fischer, The String-to-String Correction Problem, Journal of the ACM 21(1):168-173, 1974 · link
- George Kingsley Zipf, The Psycho-Biology of Language (1935); Human Behavior and the Principle of Least Effort (1949)