ICS 582Lecture 02
Words and tokens
From what counts as a word to the algorithms that carve text into units: types and tokens, Zipf and Heaps laws, morphology in English and Arabic, Unicode and UTF-8, byte-pair encoding, regular expressions, and minimum edit distance with dynamic programming.
- Parts
- 11
- Concepts
- 46
- Slides
- 94
- Reading
- 276 min
AOverview
This lecture answers a question that looks too simple to matter: what is a word, and how do you cut text into them? It starts with one picnic sentence that has 16 tokens under one policy and 18 under another, and it ends with a table that measures how far one string sits from another. Between the two lies the whole working surface of natural language processing: counting units, the two laws that govern their frequencies, the meaningful parts inside them, the bytes that carry any script, the algorithms that learn units from data, the patterns that find them, and the distance that compares them.
As a PhD student you need it in three places. Exam questions ask you to compute, not to recall: give N and|V| for a sentence under a stated policy, fit |V| = k N^beta to a corpus and read off a prediction, run four BPE merges by hand and state your tie rule, encode a code point into UTF-8 bytes, and fill an edit distance table cell by cell. Your research will depend on every one of these choices, because a vocabulary, a normalization scheme and a tokenizer are design decisions you will have to state and defend in any paper, and Arabic in particular makes them visible through templatic morphology, clitics and a rich orthography. And any system you build or review consumes tokens, so the cost, the coverage and the failure modes of the tokenizer are the first properties of the model you should be able to explain.
The path has five stops. You learn to count units and see that frequency and vocabulary follow two laws that make rare words unavoidable. You look inside those units for the meaningful parts that explain why a single lemma multiplies into many forms. You follow characters down to bytes so that no script is ever unrepresentable. You watch an algorithm learn its own units from data by merging the most frequent pair, and then see what a real tokenizer does with that algorithm in production. Finally you learn the pattern language that finds units and the distance that compares them, which is the dynamic programming pattern the rest of the course reuses.
Success looks like
- Compute the token and type counts N and |V| for a given string under a stated policy for case and punctuation, and explain why the same sentence gives two answers.
- State Zipf's law and Heaps' law, move between rank, frequency and vocabulary size with f(R) and |V| = k N^beta, and argue from them why OOV items are guaranteed.
- Segment a word into morphemes, label each as root or affix and free or bound, and tell inflection from derivation on a new example.
- Apply the selectivity and paradigm diagnostics to place a form as clitic or affix, and locate a language on the typology axes with an example.
- Distinguish code point, glyph and encoding, normalize a decomposed string, and encode any code point into UTF-8 bytes by hand.
- Run the BPE trainer and the encoder on a small corpus, list the vocabulary, state the tie rule, and explain why merge order decides the units of unseen text.
- Read and write a regular expression for a tokenization rule, and fill an edit distance table by hand to return a distance and an alignment under a stated cost convention.
How to study this lecture
- Choose your route. Read the full guide in one sitting for the big picture, or work through one part at a time when you want depth.
- Answer every recall prompt in your head, or on paper, before you reveal it. Counting, merging and encoding only stick if you do them by hand, and each is worth attempting cold first.
- Take each quiz and read the explanation even when you are right. The explanations carry the distinctions an examiner probes, such as inflection against derivation or NFC against NFKC.
- Use the simulators: count tokens and types, trace BPE merges, tokenize a real sentence, and fill the edit distance grid. Watching a decision go wrong is faster than reading that it can.
- Practise every computation under both conventions once, unit costs and cost 2 substitutions, so the exam convention never surprises you.
- Mark a concept as understood only when you could explain it to a classmate without looking. Unmarked concepts show you where to return.
- Come back after a few days and retry the recall prompts and quizzes cold. Spaced practice builds the long-term memory an exam needs.
Sources
- Speech and Language Processing, 3rd edition draft, chapter 2: Words and TokensBookStanford University (Jurafsky and Martin), release of 19 August 2026Free and current. The chapter behind this lecture: tokens and types, corpora and their variation, Unicode and UTF-8, byte-level and subword tokenization with the BPE trainer and encoder, and minimum edit distance(opens in a new tab)
- Neural Machine Translation of Rare Words with Subword UnitsPaperProceedings of ACL 2016, Berlin, pp. 1715-1725 (Sennrich, Haddow and Birch)The paper that brought byte pair encoding into NLP, introducing the merge trainer and encoder this lecture traces by hand and the end-of-word marker variant adopted by later toolkits(opens in a new tab)
BThe 11 parts
- 01What counts as a wordWhy a word is not a universal unit, the difference between tokens and types, how corpus vocabularies compare, and the preprocessing decisions every tokenizer must make.4 conceptsSlides 1-1024 min
- 02Zipf and Heaps: the shape of vocabularyTwo empirical laws that explain why a few words are everywhere, most words are rare, and vocabulary never stops growing, and why that pushes NLP toward subword units.4 conceptsSlides 11-1624 min
- 03Morphemes: the parts of wordsThe smallest meaning-bearing units, roots and affixes, inflection versus derivation, and how English concatenation differs from Arabic root-and-pattern morphology.3 conceptsSlides 17-2118 min
- 04Clitics and morphological typologyWhy some attached pieces are words in disguise, how to tell a clitic from an affix, and where languages sit between isolating and polysynthetic, with English and Arabic placed on the map.3 conceptsSlides 22-2818 min
- 05Corpora, Unicode and UTF-8What a corpus is and why it must be documented, how Unicode names every character with a code point, and how UTF-8 packs code points into bytes that any model can consume.4 conceptsSlides 29-3724 min
- 06Tokenization units and byte-pair encodingWords, characters or subwords as the unit of text, the tradeoffs among them, and the BPE algorithm that learns a subword vocabulary by merging frequent pairs, traced by hand on a toy corpus.5 conceptsSlides 38-4830 min
- 07Tokenizers in practice and sentence segmentationPre-tokenizers, byte-level BPE, multilingual fairness, SuperBPE, the design checklist for a real tokenizer, and the related problem of finding sentence boundaries.4 conceptsSlides 49-5524 min
- 08Regular expressions: the core syntaxFrom ELIZA's pattern rules to the building blocks of regular expressions: literals, bracket classes, ranges, negation, quantifiers, wildcards, anchors and the backslash aliases.4 conceptsSlides 56-6524 min
- 09Regular expressions in practiceEscaping, precedence and groups, substitutions with backreferences, lookarounds, the cheat sheets, the GPT-2 pre-tokenizer regex, and the engineering habits that keep patterns correct and fast.6 conceptsSlides 66-7836 min
- 9.1Escaping metacharacters and the precedence ladder
- 9.2Composing patterns, capture groups, substitution and lookahead
- 9.3The slide 72 practice set, solved
- 9.4The cheat sheet as reference tables
- 9.5The GPT-2 pre-tokenizer: a regex as the first pass of BPE
- 9.6Engineering regexes: error analysis, tests, Unicode, backtracking
- 10Minimum edit distance: alignments and searchMeasuring how far apart two strings are with insertions, deletions and substitutions, seeing the answer as an alignment, and recognizing the problem as a shortest path that dynamic programming solves.4 conceptsSlides 79-8624 min
- 11Filling the DP table and recovering the alignmentThe full algorithm, a cell by cell computation for intention to execution, backpointers and backtrace to read off the alignment, the time and space complexity, and the lecture in one page.5 conceptsSlides 87-9430 min
- 11.1One edit at a time: the path that the table will rebuild
- 11.2Filling D cell by cell: initialization, three candidates, one minimum
- 11.3Backpointers: reading the arrows back to the origin
- 11.4What the table costs: O(mn) time, and how little space you really need
- 11.5The lecture in seven lines, and where it came from