Skip to content
ml-recall

Topic notes

NLP Fundamentals

The classic text pipeline before deep learning — tokens and types, normalization, stemming vs. lemmatization, regular expressions, edit distance, word statistics and tf-idf vectors.

4-min read AI-checked

Practise 37 questions Take a test

Key ideas

Formulas

Minimum edit distance is computed by dynamic programming over prefixes1:

D(i,j)=min⁡(D(i−1,j)+1,  D(i,j−1)+1,  D(i−1,j−1)+sub(ai,bj))D(i, j) = \min\big(D(i-1, j) + 1,\; D(i, j-1) + 1,\; D(i-1, j-1) + \mathrm{sub}(a_i, b_j)\big)

with sub=0\mathrm{sub} = 0 for equal letters. Levenshtein distance charges 1 for every insertion, deletion and substitution (intention → execution: 5); the textbook variant charges 2 for a substitution, the price of a deletion plus an insertion (intention → execution: 8)1.

Worked example

Corpus of N=4N = 4 documents: d1 "the cat sat on the mat", d2 "the dog sat", d3 "the cat ran", d4 "a dog barked". Weights for d1 with raw counts and log⁡10\log_{10}:

term count in d1 df idf =log⁡10(4/df)= \log_{10}(4/\mathrm{df}) tf-idf
the 2 3 0.125 0.250
cat, sat 1 2 0.301 0.301
on, mat 1 1 0.602 0.602

The words unique to d1 ("on", "mat") get the highest weight; "the" is damped by its idf.

In code

from sklearn.feature_extraction.text import TfidfVectorizer

docs = ["the cat sat on the mat", "the dog sat", "the cat ran", "a dog barked"]
vec = TfidfVectorizer()                  # smooth_idf=True, norm="l2" by default
X = vec.fit_transform(docs)
idf = dict(zip(vec.get_feature_names_out(), vec.idf_.round(3)))
print(idf["the"], idf["cat"], idf["mat"])  # 1.223 1.511 1.916
print("a" in idf)                          # False: default token_pattern needs 2+ characters
print(X[0].toarray().round(2))             # [[0. 0.36 0. 0.45 0.45 0. 0.36 0.58]]

With the smoothed idf, "the" (count 2) gets the largest weight in d1, unlike the textbook table above.

Common pitfalls

References

  1. Jurafsky & Martin, Speech and Language Processing, 3rd ed. draft (Aug 2026), Ch. 2 Words and Tokens — Sec. 2.1 Words, 2.6 Regular Expressions, 2.8.1 Sentence Segmentation, 2.9 Minimum Edit Distance
  2. Manning, Raghavan & Schütze, Introduction to Information Retrieval — Sec. 2.2.4 Stemming and lemmatization
  3. Manning, Raghavan & Schütze, Introduction to Information Retrieval — Sec. 5.1.2 Zipf's law: Modeling the distribution of terms
  4. Manning, Raghavan & Schütze, Introduction to Information Retrieval — Sec. 6.2.1 Inverse document frequency (Eq. 21)
  5. scikit-learn User Guide: Text feature extraction — Sec. 8.2.3 Text feature extraction (8.2.3.1 The Bag of Words representation, 8.2.3.4 Using stop words, 8.2.3.5 Tf–idf term weighting)
  6. Python documentation: re — Regular expression operations — Regular expression syntax, the `|` (alternation) operator
  7. Manning, Raghavan & Schütze, Introduction to Information Retrieval — Sec. 2.2.3 Normalization, Capitalization/case-folding
  8. Wikipedia: CYK algorithm — Introduction; Standard form; Probabilistic CYK; Parsing weighted context-free grammars