Module 2 cheat sheet: NLP Fundamentals

Turn messy text into clean tokens and numbers: tokenization, preprocessing, regex, counts and embeddings.

All modules📖 Glossary

Key ideas

  • Modern models use subword tokens: common words whole, rare words in pieces. S4 →
  • English ≈ 4 characters per token; many other scripts need more tokens. S4 →
  • Stemming chops endings; lemmatization finds the dictionary form. S5 →
  • Removing stopwords can delete “not” and flip the meaning. S5 →
  • Redact PII with regex before text reaches an LLM. S6 →
  • Embeddings put words used in similar contexts close together. S8 →

Tokenizer algorithms

AlgorithmIdeaUsed by
BPEmerge the most frequent pairGPT, Llama 3
WordPiecelikelihood-based merges, ## piecesBERT
Unigramprune a big vocabularyT5, ALBERT
SentencePieceBPE/Unigram on raw text, ▁ for spacesT5, Llama 2, Gemma

Regex essentials

PatternMeaning
\d \w \sdigit · word character · space
[A-Z]{5}exactly 5 capital letters
+ * ?one or more · zero or more · optional
\bword boundary
( ) and |capture group · or
re.findall · re.search · re.sub

From words to numbers

MethodCapturesLimit
One-hotwhich wordno similarity, huge
Bag of Wordsword countsorder lost
N-gramsshort phrasesmany features
TF-IDFimportant, rare wordsstill no meaning
Word2Vec / GloVemeaning from contextone vector per word
TF-IDF = TF × log(N / DF)  ·  cosine = a·b / (|a||b|)

Code: TF-IDF in four lines

from sklearn.feature_extraction.text import TfidfVectorizer
docs = ["UPI payment failed", "card payment failed again", "refund for failed UPI payment"]
tfidf = TfidfVectorizer()
X = tfidf.fit_transform(docs)        # rows = documents, columns = words

Common mistakes

  • Stemming or removing stopwords before sending text to an LLM.
  • Estimating cost from words instead of tokens.
  • Regex without \b, causing partial matches.
  • Expecting Word2Vec to tell “river bank” from “bank account”.

Created by Ajeetkumar · EasyLearnSpace