Sub topic 2
Subword tokenisation: BPE and WordPiece
Subword tokenisation: BPE and WordPiece
Word-level tokenisers fail on words they have never seen. Subword tokenisers avoid this by splitting rare words into smaller known pieces, so any word can be built from a fixed vocabulary.
Byte pair encoding (BPE) starts with single characters. It counts adjacent pairs across a training corpus, merges the most frequent pair into one new symbol, and repeats until the vocabulary reaches a set size. An unseen word such as "lowest" can then be written as known pieces like "low" and "est".
WordPiece, used in BERT, follows the same pattern but chooses each merge by how much it raises the likelihood of the training data. Continuation pieces carry a "##" prefix, so "playing" may become "play" and "##ing".
Word forms: inflection, derivation, stems and lemmas
Inflection changes a word's grammatical form without changing its core meaning or part of speech: "cat" to "cats", "walk" to "walked". Derivation builds a new word, often with a new part of speech: "happy" to "happiness".
A stem is the piece left after removing affixes. It need not be a real word. A lemma is the dictionary form of a word, such as "run" for "running" used as a verb.
Stemming by suffix stripping
A stemmer removes suffixes using fixed rules and no dictionary. It is fast, but it makes errors. Stripping rules can produce non-words, such as "poni" from "ponies". Stems still help search, as long as every form of a word maps to the same stem.
The Porter and Snowball stemmers in practice
The Porter stemmer, published in 1980, applies ordered rule steps to strip suffixes. Snowball is a framework for writing stemmers. Its English stemmer, often called Porter2, refines the original Porter rules. Both are available in NLTK.
from nltk.stem import PorterStemmer, SnowballStemmer
porter = PorterStemmer()
snowball = SnowballStemmer("english")
for word in ["caresses", "ponies", "running", "studies"]:
print(word, porter.stem(word), snowball.stem(word))> Note: Stemming can merge unrelated words. Porter maps "universal", "university" and "universe" to the same stem, "univers". Check stemmer output on your own data before you rely on it.
Overstemming groups words that should stay apart, as with "univers" above. Understemming leaves related forms apart, such as "run" and "ran" under many stemmers.
Exercise
Exercise
Using the Porter stemmer, "universal", "university" and "universe" all produce "univers". What type of stemming error is this, and why does it matter for search?
Show answer
This is overstemming. Unrelated words share one stem, so a search for "university" also matches documents about "universal" and "universe", which lowers precision.