Foundations of NLP & Text Preprocessing
What natural language is, why it is hard to process, and how to turn raw bytes into the tokens a model can read — tokenisation, normalisation, stemming vs lemmatisation, and the regular expressions that still do the surgical work.
01 · Definition
What is Natural Language Processing?
Natural language is compositional, ambiguous, and unbounded. Every preprocessing decision you make is a trade-off between throwing information away and keeping the input tractable.
NLP is the computational analysis, interpretation, and production of natural language in written or spoken form. Strip away the marketing and every NLP system is a function: it takes one piece of language as input and returns another piece of language — or a label, a number, a score, a tree. The differences between systems are which input, which output, and how the mapping is learned.
Three vocabularies show up across the syllabus — keep them straight from day one:
- NLU — Natural Language Understanding: text → labels, structure, vectors. Classification, NER, parsing, sentiment.
- NLG — Natural Language Generation: structure or text → text. Summarisation, translation, dialogue.
- NLP — the umbrella that contains both, plus the engineering around them.
Why this field exists
Virtually all of human knowledge lives in text — books, papers, emails, web pages, medical records, legal contracts. Before NLP, every one of those documents required a human reader. NLP is the long project of giving computers the same access.
Identify the NLP task
Pick an example I/O pair, then click the task you think it is. Look at the shape of the output — a label, a span, another sentence — that alone usually pins the task.
Name the task and a core difficulty
Given a system that maps a 1000-word document to a one-paragraph headline, name the NLP task and one difficulty. Graders want summarisation and the extractive vs abstractive distinction (any headline shorter than the source title is almost always abstractive), plus at least one of information selection, faithfulness / hallucination, or length control. Trap: calling it “compression”, or confusing it with translation because both shorten text.
02 · Properties
What makes human language special?
Animal communication is largely signalling: one signal, one meaning, no novelty. Human language is fundamentally different. Four properties matter for the exam.
1 · Compositionality
Meaning of the whole is a function of the parts. Combine 100 nouns, 100 verbs and 100 objects and you get distinct sentences. No finite signal dictionary can capture that.
2 · Displacement
Language can refer to things not here, now, or even real: Caesar, next Tuesday, the square root of two, a unicorn in your fridge. Animal signals are anchored to the immediate situation.
3 · Productivity
A speaker produces — and a listener understands — sentences neither has heard before. “The purple bicycle sneezed eloquently” is grammatical, odd, and instantly parseable.
4 · Ambiguity
Almost every sentence has more than one reading at some level — phonetic, lexical, syntactic, semantic, pragmatic. Humans use context to pick one. The biggest engineering problem in NLP.
The dolphin contrast
Dolphins have an estimated ~125 distinct whistles. Each signals something — danger, food, identity — but you cannot combine two whistles to invent a new meaning. Human language is the opposite: a small set of rules generating unbounded sentences.
Spot the ambiguity
Pick a sentence, then choose the ambiguity type you think dominates. Lexical = a word has two meanings; syntactic = the structure parses two ways; semantic = fixed parse, unclear meaning; pragmatic = meaning depends on context.
“I saw her duck.”
Three distinguishing properties, one example each
Pick from compositionality, displacement, productivity, ambiguity. Each example must be one concrete sentence, not a description. Traps: “vocabulary size” is not a property (dolphins have vocabulary too); “grammar” alone is too vague — graders want compositional grammar.
03 · Difficulty
Why processing language is hard
Six recurring difficulties show up in every NLP system, in roughly increasing order of severity:
- Ambiguity at every level. Phonetic (“ice cream” / “I scream”), lexical (“bank”), syntactic (“I saw her duck”), semantic (“every man loves a woman”), pragmatic (“can you pass the salt?”).
- Variation. The same meaning expressed a thousand ways: buy / purchase / pick up / get.
- Sparsity. Words follow a Zipfian distribution — about half of all word types in a corpus appear exactly once. A model that must see a word to learn it has a problem.
- Productivity. Test data contains words and structures the training data never saw.
- Context dependence. Meaning depends on speaker, listener, time, place, prior discourse, world knowledge.
- Encoding the discrete. Text is a sequence of categorical symbols, but most modern ML runs on continuous vectors. Bridging the two (Ch. 4–6) is most of the course.
The classic confusion
Ambiguity is one input, many meanings. Variation is the mirror — one meaning, many inputs. The exam asks about both and expects the right term.
Zipf’s law — the sparsity problem, visualised
Plot frequency vs rank. A handful of words dominate; a long tail of types appears once or twice. That tail is why models need smoothing and sub-word units.
29 tokens · 12 types · 5 hapax (1×) · 42% of types appear once
Lexical vs syntactic ambiguity for a parser
Give one lexical and one syntactic example sentence, plus the mechanism: lexical ambiguity is resolved by word-sense disambiguation (classification over senses); syntactic ambiguity by parse selection (choosing one tree out of many).
04 · The pipeline
The text preprocessing pipeline
Before any model sees your text, it goes through a sequence of irreversible decisions. Every step throws information away in exchange for a more tractable input. The canonical order:
- Decode. Bytes → Unicode characters. Pick UTF-8 unless you have a reason not to.
- Normalise. Lower-case, strip diacritics, NFC vs NFKC, collapse whitespace, strip HTML.
- Tokenise. Character stream → list of tokens.
- Filter. Optionally drop stopwords, punctuation, very rare or very frequent tokens.
- Morphology. Optionally stem or lemmatise.
- Vectorise. Tokens → vectors (BoW, embeddings, subword IDs).
Preprocessing pipeline simulator
Toggle each step and watch the token list change. The toolbar order matches the order the pipeline runs — dropping stopwords before lower-casing would miss “The”.
Pipeline: 23 → 15 tokens kept · 65% retention
Every step is destructive
You cannot recover “Milano’s” from “milano”, or “isn’t” from “isn t” / “is not”. Preprocessing decisions are permanent with respect to the downstream model.
Stopwords and case for three tasks
For (a) sentiment, (b) author identification, (c) information retrieval — decide whether to remove stopwords and lower-case:
- Sentiment — keep stopwords (“not”, “no”, “never” flip sentiment); lower-casing is fine.
- Author ID — keep stopwords (a function-word histogram is the strongest authorship signal); do not lower-case (capitalisation is a stylistic fingerprint).
- IR — remove stopwords (they bloat the index, rarely change the topic); lower-case is standard.
05 · Tokenization
Tokenization — three regimes
Tokenisation converts a character stream into a list of discrete units. Every model in this course operates on tokens, never raw text. Three families dominate.
Word tokens
Split on whitespace and punctuation. Easy, interpretable. But a huge vocabulary (English Wikipedia
≈ 1.3 M types) and zero handling of the unseen — tokenize("antidisestablishmentarianism") → [UNK].
Character tokens
Split on every Unicode code-point. Tiny vocabulary (≈ 300), never an unknown token, but sequences are 4–6× longer and the model must learn what a “word” is from scratch.
Sub-word tokens
BPE, WordPiece, SentencePiece — the modern compromise every transformer uses. Vocabulary ≈ 30–50 k. Frequent words stay whole; rare words decompose into pieces.
Tokenizer playground — word vs char vs sub-word
Try a sentence with rare or compound words. At the word level “antidisestablishmentarianism” is one token; at the character level it is 31. The sub-word BPE finds a sensible middle by greedily merging the most frequent character pairs.
8 tokens · 8 unique · avg 9.8 chars/token
[UNK]: frequent words stay whole, rare words decompose into known pieces.Formal Byte-Pair Encoding (BPE) — the merge algorithm
BPE is a greedy, frequency-based vocabulary builder:
- Initialise the vocabulary with every character that appears in the corpus.
- Represent every word as a sequence of those characters, ending in a boundary symbol.
- At step , find the adjacent pair with the highest co-occurrence count across the corpus.
- Add the merged symbol to and rewrite every as .
- Repeat until reaches the target size (typically 30–50 k).
Common words become single tokens; rare words decompose into their most-merged pieces. At inference the same merges run in learned order, so tokenisation is deterministic.
Three reasons transformers use sub-words
- No
[UNK]— any unseen string still decomposes into known sub-words. - Fixed, small vocabulary — keeps the embedding matrix and softmax tractable (30 k rows, not 1 M).
- Morphological sharing — play, playing, played share the play piece, so generalisation comes free.
- (Bonus) Language-agnostic — the same algorithm trains on any script.
06 · Normalisation
Normalisation: stemming vs lemmatisation
Both reduce surface forms to a base form. The difference is whether they care about what the word means.
- Stemming chops suffixes with rule-based heuristics. Fast, language-specific (Porter, Snowball, Lancaster), and frequently produces non-words:
"studies" → "studi","running" → "run","better" → "better"(irregular — rules can’t help). - Lemmatisation looks up the canonical dictionary form (the lemma) via morphological analysis. It needs POS to be accurate:
"studies" → "study","better" → "good". Slower, but the output is always a real word.
Stem vs lemma, side by side
Type words (space-separated). Try better, flies, organization — stemmers handle regular suffixes but break on irregulars and homographs, where the dictionary-based lemmatiser still gets the real base form.
| Surface | Stem (heuristic) | Lemma (dictionary) |
|---|---|---|
| studies | studi | study |
| studying | study | study |
| studied | studi | study |
| better | better | good |
| cats | cat | cat |
| running | runn | run |
| flies | fli | fly |
| organization | organ | organization |
When to choose which
- Stemming — large-scale IR where speed dominates; non-words are fine because the query goes through the same stemmer.
- Lemmatisation — anything read by a human, or that needs morphological correctness.
- Trap. “Stemming is more accurate.” It isn’t — it is faster and cruder.
07 · Tool
Regular expressions — the workhorse
Before neural networks, before BoW, there were regular expressions — still the right tool for surgical text matching. Exam regex problems are almost always: write a pattern, read a pattern, identify a bug, or compute what a pattern matches. The building blocks to know cold:
.any character except newline ·\d \w \sdigit, word-char, whitespace (uppercase = negation)[abc] [^abc] [a-z]character classes ·? * + {m,n}quantifiers^ $anchors ·(…)capture ·(?:…)non-capturing ·\1back-reference(?=…) (?!…)look-ahead ·|alternation
Regex tester
The preset matches a capitalised word. Try \b\d{4}\b for 4-digit years, or \b[A-Z][A-Z]+(?:-[0-9])?\b for tech acronyms like BERT, GPT-4.
6 matches
Italian postal codes (5 digits)
Answer: \b\d{5}\b. Trap: forgetting the word boundaries — without \b the pattern matches the
first five digits of any longer number. [0-9]{5} is equivalent unless Unicode digits matter.
Worked example Worked exam problem — tokens, types, and TTR
Take “The cats sat on a mat; the cat ran.” Apply: lowercase → drop punctuation → remove stop words {the, a, on} → stem plurals (drop a trailing “s”). How many tokens and distinct types remain, and what is the type–token ratio (TTR)?
- Lowercase + drop punctuation →
the cats sat on a mat the cat ran(9 tokens). - Remove stop words {the, a, on} →
cats sat mat cat ran(5 tokens). - Stem trailing “s” →
cat sat mat cat ran. - Tokens = 5; distinct types = {cat, sat, mat, ran} = 4.
- TTR = types / tokens = 4 / 5 = 0.8.
Answer: 5 tokens, 4 types, TTR = 0.8. “cat” appears twice (once from stemming “cats”), so types < tokens.
08 · Self-check
Three questions before you move on
A system maps "Caesar crossed the Rubicon in 49 BC" to [Caesar]PER [Rubicon]LOC [49 BC]DATE. What NLP task is this?
You're building a sentiment classifier. A colleague suggests removing stopwords to reduce noise. What's the strongest objection?
Which is the strongest reason modern transformers use sub-word tokenisation?
09 · Recap
One-screen summary
Chapter 01 — load-bearing ideas
- Human language is compositional — finite rules generate unbounded sentences. That is why no signal-dictionary approach works.
- Ambiguity is one input → many meanings; variation is one meaning → many inputs. Use the right term.
- Every preprocessing step is destructive. Choose what to throw away by the downstream task, not by reflex.
- Tokenisation has three regimes — word (huge vocab,
[UNK]), character (tiny vocab, long sequences), sub-word/BPE (the modern compromise). - BPE merge rule: .
- Stemming vs lemmatisation: crude rule-based suffix stripping vs dictionary + morphology. Stemming is faster, not more accurate.
- Zipf’s law — about half of all types occur once (hapax), which is why we need smoothing (Ch. 4) and sub-words.
- Regular expressions remain the workhorse for span-level extraction.
10 · Exam · past papers
Past-paper questions
A curated bank of real Polimi past-paper items for this chapter. Click an option for instant feedback; “Reveal all” exposes every answer.
Q143Why are Natural Language Processing (NLP) techniques important?
Q144Which of the following tasks would NOT usually be considered a Natural Language Processing task?
Q145Sentences in natural language can often be ambiguous (e.g. "I made her duck"), because:
Q146The sentence "Colourless green ideas sleep furiously." is an example of a phrase that:
Q117Which of the following is NOT a text pre-processing step in an NLP application?
Q120What is the difference between stemming and lemmatization?
Q123What are ASCII and UTF-8, and what is the difference between them?
Q121What effect does Byte-Pair Encoding (BPE) achieve in Transformer models?
Q149Which one of the following is NOT a valid description of a Regular Expression component?
Q147Which regular expression would match a student email of the form "first.lastname@mail.polimi.it"?
Q148Which string would the regex [A-Z]{6}\d{2}[A-Z]\d{2}[A-Z]\d{3}[A-Z] match?
Q127Which of the following would NOT be considered a limitation of regular-expression based text extraction?