TechCoder.io / AI & Machine Learning

NLP Essentials

How computers understand text. Learn about Tokenization (BPE, WordPiece), Stemming, Lemmatization, TF-IDF, Named Entity Recognition, and modern NLP preprocessing pipelines.

By TechCoder TeamLast updated: 2026-07-23
In a Nutshell

How computers understand text. Learn about Tokenization (BPE, WordPiece), Stemming, Lemmatization, TF-IDF, Named Entity Recognition, and modern NLP preprocessing pipelines. This hands-on tutorial focuses on practical implementation of nlp essentials concepts.

NLP Essentials

Natural Language Processing (NLP) is the field of AI focused on the interaction between computers and human language. Before we can feed text into a model, we must turn it into numbers — and doing this well is where great NLP begins.

1. Tokenization ✂️

Tokenization is the process of breaking text into smaller chunks called tokens. Tokens can be words, characters, or sub-words depending on the algorithm.

Word-Level Tokenization (Simple but Limited)

  • Sentence: "I love AI."
  • Word Tokens: ["I", "love", "AI", "."]
  • Problem: Vocabulary explosion — "run", "running", "ran" are 3 separate tokens. Unknown words become <UNK>.

Subword Tokenization — The Modern Standard

Modern LLMs use subword algorithms that balance vocabulary size with coverage of rare words.

BPE (Byte Pair Encoding) — Used by GPT, LLaMA

BPE starts with individual characters and iteratively merges the most frequent adjacent pairs:

PYTHON PLAYGROUND
⏳ Loading editor…

WordPiece — Used by BERT

Similar to BPE but merges based on likelihood rather than frequency. Uses ## prefix for continuation tokens.

  • "playing"["play", "##ing"]
  • "unbelievably"["un", "##believ", "##ably"]

Tokenizer Comparison

AlgorithmUsed ByMerge StrategyVocabulary Size
BPEGPT-2/3/4, LLaMA, MistralMost frequent pair~50K tokens
WordPieceBERT, DistilBERTMaximum likelihood~30K tokens
UnigramT5, mBART, ALBERTProbabilistic pruning~32K tokens
Byte-level BPEGPT-4, RoBERTaByte-level pairs~50K tokens, no UNK

📚 Official Resources

2. Cleaning Text 🧹

Raw text is messy. We often perform these steps:

  • Lowercasing: "AI" → "ai"
  • Removing Punctuation: "Hello!" → "Hello"
  • Stop Word Removal: Removing common words like "the", "is", "and" that don't carry much meaning.

[!NOTE] For modern LLMs, you often skip these steps — the model learns from raw text. Classical NLP tasks (TF-IDF, BM25, keyword search) benefit most from preprocessing.

3. Stemming vs. Lemmatization 🌱

We want to treat "running", "runs", and "run" as the same word.

  • Stemming: Chops off the end of words. Fast but crude.
    • runningrun
    • betterbetter (fails to map to good)
  • Lemmatization: Uses a dictionary to find the root form (lemma).
    • bettergood
    • wasbe

4. Named Entity Recognition (NER) 🏷️

NER identifies and classifies named entities in text — people, places, organizations, dates, and more. It's one of the most practically useful NLP tasks.

PYTHON PLAYGROUND
⏳ Loading editor…

📚 NER Tools

5. Part-of-Speech (POS) Tagging 🏷️

POS tagging identifies the grammatical role of each word: Noun, Verb, Adjective, Adverb, etc.

  • "The quick (ADJ) brown (ADJ) fox (NOUN) jumps (VERB) over the lazy (ADJ) dog (NOUN)."

POS tags are used in:

  • Information extraction (find all noun phrases)
  • Grammar checking
  • Named entity disambiguation

6. Bag of Words (BoW) 👜

A simple way to represent text. We count how many times each word appears. It ignores grammar and word order but works surprisingly well for classification tasks.

7. TF-IDF 📊

Term Frequency - Inverse Document Frequency. It highlights words that are important to a specific document but rare across the entire dataset.

  • TF: How often a word appears in this document.
  • IDF: How rare the word is across all documents.
  • Combined: TF-IDF(t, d) = TF(t, d) × log(N / df(t))

Where N = total documents, df(t) = documents containing term t.

Interactive Challenge: Build a Tokenizer

Let's write a function to clean and tokenize text — and compare word vs. subword tokenization.

PYTHON PLAYGROUND
⏳ Loading editor…

Quiz

Quiz

Question 1 of 3

What is Tokenization?

Translating text
Breaking text into smaller chunks (words, subwords, or characters)
Removing stop words

Key Takeaways

BPE Tokenization is the standard for modern LLMs — it handles unknown words via subword splits.
Lemmatization produces real dictionary words; stemming is faster but cruder.
NER extracts structured information (people, places, organizations) from unstructured text.
TF-IDF helps find important keywords — still used in search engines and BM25 retrieval.

What's Next?

Counting words is useful, but it doesn't capture meaning. "King" and "Queen" are just different strings to a computer. How can we teach it that they are related?

Next Chapter: Word Embeddings.