Module 9

Natural Language Processing

Natural language processing — from tokenization through transformers, BERT, GPT, and building real NLP systems.

20 lessonsAI & MLHarinIT Academy
Module 9 · Lesson 9.1

Text Processing

Module 9.1 – Text Processing

Learning Objectives

By the end of this chapter, you will be able to

  • Understand what text processing is.
  • Explain why text processing is necessary in NLP.
  • Identify common problems in raw text data.
  • Learn the steps involved in text preprocessing.
  • Understand how processed text improves AI and Machine Learning models.

1. Introduction

Imagine you ask a computer

"I absolutely LOVE Machine Learning!!! 😊"

To a human, the meaning is obvious. We know that

  • The sentence expresses a positive opinion.
  • "LOVE" emphasizes strong emotion.
  • The emoji reinforces positivity.

However, a computer doesn't naturally understand language. It only processes numbers. Before a machine learning model can analyze this sentence, the text must be cleaned and transformed into a structured form.

This preparation is called Text Processing (or Text Preprocessing).

Definition

Text Processing is the process of cleaning, transforming, and preparing raw text data so it can be analyzed by computers and machine learning models.

2. Why is Text Processing Important?

Real-world text is often messy.

Consider this example

Hey!!! Visit https://example.com 😊

I LOVE Machine Learning!!!

This contains

  • Uppercase letters
  • Punctuation
  • A URL
  • An emoji
  • Extra whitespace

Most machine learning algorithms cannot directly use this data.

After text processing, it may become

love machine learning

This cleaner version is much easier for models to analyze.

3. What Problems Exist in Raw Text?

Raw text may include

ProblemExample
Uppercase and lowercase"Apple" vs "apple"
Punctuation"Hello!!!"
Extra spaces"AI Engineer"
URLshttps://example.com
HTML tags<p>Hello</p>
Emojis😊 😂 ❤️
Numbers12345
Stop wordsthe, is, am, are
Misspellings"Machin Learning"
Different word formsrun, running, ran

These inconsistencies can confuse machine learning models if not handled properly.

4. Goals of Text Processing

The main goals are

  • Remove unnecessary information.
  • Standardize the text.
  • Reduce noise.
  • Improve model accuracy.

Convert text into a format suitable for machine learning.

5. Text Processing Pipeline

A typical NLP pipeline looks like this

Raw Text

Lowercasing

Remove URLs

Remove HTML Tags

Remove Punctuation

Tokenization

Stop Word Removal

Stemming / Lemmatization

Convert Text to Numbers

(TF-IDF / Word2Vec / BERT Embeddings)

Machine Learning Model

Each step prepares the text for the next stage.

6. Common Text Processing Steps

Step 1: Lowercasing

Convert all text to lowercase so words are treated consistently.

Example

Before

Machine Learning

After

  • machine learning
  • Without this step, "Machine" and "machine" might be treated as different words.
  • Step 2: Remove Punctuation

Before

Hello!!!

After

  • Hello
  • Punctuation often doesn't contribute to the meaning for many NLP tasks.
  • Step 3: Remove Numbers (Optional)

Before

Laptop costs 50000

After

Laptop costs

Whether to remove numbers depends on the application. For example, prices may be important in financial analysis.

Step 4: Remove URLs

Before

Visit https://openai.com

After

  • Visit
  • URLs usually don't add value to sentiment analysis or text classification.
  • Step 5: Remove HTML Tags

Before

<p>Hello World</p>

After

  • Hello World
  • Useful when processing data scraped from websites.
  • Step 6: Remove Emojis (Task Dependent)

Before

I love AI 😊

Possible outcome

I love AI

However, for sentiment analysis, emojis may contain valuable emotional information, so they are sometimes converted into words (e.g., 😊 → "smile") instead of removed.

Step 7: Tokenization

Break the sentence into individual words or tokens.

Sentence

I love AI

Tokens

\["I", "love", "AI"\]

Tokenization is covered in detail in the next chapter.

Step 8: Remove Stop Words

Before

I am learning Machine Learning

After

learning Machine Learning

Stop words (e.g., the, is, am, are) are very common words that may not carry much meaning for certain tasks.

Step 9: Stemming / Lemmatization

Before

  • running
  • runs
  • ran

After

  • run
  • This reduces different forms of a word to a common base form.
  • Step 10: Convert Text to Numbers
  • Machine learning models work with numbers, not words.

Examples of conversion techniques

  • Bag of Words (BoW)
  • TF-IDF
  • Word2Vec
  • GloVe
  • FastText
  • BERT Embeddings

These methods will be covered in later chapters.

7. Real-World Example

Suppose you have the following product review

  • I absolutely LOVE this phone!!! 😊😊
  • Original Text
  • I absolutely LOVE this phone!!! 😊😊
  • After Lowercasing
  • i absolutely love this phone!!! 😊😊
  • After Removing Punctuation
  • i absolutely love this phone 😊😊
  • After Tokenization
\["i", "absolutely", "love", "this", "phone"\]

After Stop Word Removal

\["absolutely", "love", "phone"\]

After Lemmatization

\["absolutely", "love", "phone"\]

This cleaned representation is much easier for an NLP model to use.

8. Applications of Text Processing

Text processing is used in

  • Spam email detection
  • Sentiment analysis
  • Machine translation
  • Search engines
  • Chatbots
  • Voice assistants
  • Question answering
  • News categorization
  • Resume screening
  • Healthcare text analysis

9. Common Mistakes

  • Removing too much information (e.g., deleting numbers that are important).
  • Removing emojis in tasks where sentiment matters.
  • Using stemming when word meaning must be preserved.
  • Applying different preprocessing steps to training and prediction data.

10. Best Practices

  • Understand the problem before choosing preprocessing steps.
  • Keep preprocessing consistent across training and inference.
  • Preserve information that is relevant to your task.
  • Test how each preprocessing step affects model performance.

11. Key Takeaways

  • Text Processing is the foundation of every NLP system.
  • It transforms messy, unstructured text into clean, structured data.
  • Different NLP tasks require different preprocessing strategies.
  • Proper text processing improves model accuracy and efficiency.

Modern language models like BERT and GPT still rely on well-designed preprocessing pipelines, even though they perform much of the language understanding internally.

What's Next?

In Chapter 9.2 – Tokenization, you'll learn how text is broken into meaningful units (tokens), why tokenization is essential for NLP, and how modern tokenizers used by BERT and GPT differ from traditional word-based approaches.

Module 9 · Lesson 9.2

Tokenization

Chapter 9.2 – Tokenization

Learning Objectives

After completing this chapter, you will be able to

  • Understand what tokenization is.
  • Learn why tokenization is the first step in NLP.
  • Differentiate between words, tokens, and vocabulary.
  • Explore different types of tokenization.
  • Understand how GPT and BERT tokenize text.
  • Implement tokenization using Python.

1. Introduction

Imagine you have the sentence

"I love Artificial Intelligence."

To humans, this is a complete sentence.

  • To a computer, it is simply a sequence of characters.
  • Before a computer can understand this sentence, it must break it into smaller meaningful units.
  • This process is called Tokenization.
  • What is Tokenization?

Definition

Tokenization is the process of splitting text into smaller units called tokens.

A token can be

  • A word
  • A sentence
  • A character
  • A subword
  • A punctuation mark (depending on the tokenizer)

Think of tokenization as cutting a long sentence into small pieces that a machine learning model can process.

Example

  • Sentence
  • I love Machine Learning.
  • After tokenization

["I",

  • "love",
  • "Machine",
  • "Learning"]
  • Each item is called a token.
  • Why Do We Need Tokenization?
  • Machine Learning algorithms cannot directly process long text.
  • Instead of reading
  • I love AI
  • the model reads
\["I", "love", "AI"\]

Each token is then converted into numbers.

Without tokenization

Text
Computer ❌

With tokenization

Text
Tokens
Numbers
Machine Learning Model
  • Real-Life Analogy
  • Imagine reading a book without spaces.
  • IloveMachineLearning
  • Very difficult.
  • Humans naturally separate words.
  • Computers must also learn where one word ends and another begins.
  • Tokenization performs this separation.
  • Types of Tokenization
  • There are five major types.

1. Sentence Tokenization

Splits paragraphs into sentences.

Example

  • Machine Learning is amazing.
  • AI is transforming the world.
  • Result

[

"Machine Learning is amazing.",

"AI is transforming the world."

]

  • Used in
  • Document summarization
  • Question Answering
  • Machine Translation

2. Word Tokenization

  • The most common tokenizer.
  • Sentence
  • I love Machine Learning.
  • Result

["I",

  • "love",
  • "Machine",
  • "Learning"]

Most traditional NLP systems use word tokenization.

3. Character Tokenization

  • Each character becomes a token.
  • Sentence
  • AI
  • Result
\['A','I'\]
  • Useful for
  • OCR
  • Spelling correction
  • Language identification

4. Subword Tokenization

Instead of entire words,

words are divided into meaningful pieces.

Example

unbelievable

may become

["un",

"believ",

"able"]

Advantages

  • Handles unknown words
  • Smaller vocabulary
  • Better for Deep Learning

This is the tokenizer used by GPT and BERT.

5. Byte-Level Tokenization

  • Instead of words,
  • every byte becomes a token.
  • Useful for
  • Multiple languages
  • Emojis
  • Special symbols
  • GPT-2 uses Byte Pair Encoding (BPE), which works at the byte level.
  • Understanding Tokens
  • Consider
  • I love AI.
  • Words
  • I
  • love
  • AI
  • Tokens
\[I\]
\[love\]
\[AI\]

Vocabulary

{

I,

love,

AI

}

  • Vocabulary means
  • All unique tokens known to a model.
  • Token IDs
  • Computers do not store words.
  • They store IDs.

Example

TokenID
I15
love208
AI512
  • Sentence
  • I love AI
  • becomes
\[15,208,512\]

Now the computer can process it.

Why Not Store Words?

Computers work with numbers.

Example

  • Instead of
  • Machine
  • store
  • 1023
  • This saves memory and speeds up computation.
  • Unknown Words
  • Suppose the vocabulary contains
  • cat
  • dog
  • house

Now we receive

  • elephant
  • Traditional NLP cannot process it.
  • It becomes
  • <UNK>
  • Meaning
  • Unknown Token
  • Modern tokenizers solve this using subwords.

Example

  • Word
  • electromagnetism
  • Traditional
  • UNK
  • Subword tokenizer
  • electro
  • magnet
  • ism
  • The model still understands the word because it knows the pieces.
  • Tokenization in BERT
  • BERT uses
  • WordPiece Tokenization

Example

playing
play
  • ##ing
  • The prefix ##
  • means
  • "This continues the previous word."

Advantages

  • Smaller vocabulary
  • Handles unknown words
  • Higher accuracy
  • Tokenization in GPT
  • GPT uses
  • Byte Pair Encoding (BPE)

Example

unhappiness
un
  • happi
  • ness
  • Instead of memorizing millions of words,
  • GPT learns common word pieces.
  • This greatly reduces vocabulary size.
  • Tokenization in ChatGPT
  • Suppose you ask
  • Explain Artificial Intelligence.
  • ChatGPT first converts the sentence into tokens.
  • Example (illustrative only)
  • Explain
  • Artificial
  • Intelligence

.

\[2345, 9812, 412, 13\]
  • These IDs are then converted into embeddings and processed by the transformer.
  • Token Count
  • Large Language Models have limits.
  • GPT may support
  • 8K tokens
  • 32K tokens
  • 128K tokens
  • Notice

These are tokens

NOT

Words.

Example

100 words

  • 120–150 tokens
  • depending on punctuation and language.
  • Python Example (NLTK)
from nltk.tokenize import word_tokenize
text = "I love Machine Learning."
tokens = word_tokenize(text)
print(tokens)

Output

['I',

  • 'love',
  • 'Machine',
  • 'Learning',

'.']

Notice that punctuation can also be a token.

Python Example (spaCy)

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("I love Artificial Intelligence.")
for token in doc:
print(token.text)

Output

  • I
  • love
  • Artificial
  • Intelligence

.

Python Example (Hugging Face)

from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("bert-base-uncased")
tokens = tokenizer.tokenize(
    "Machine Learning is amazing."
)
print(tokens)
  • Possible output
  • ['machine',
  • 'learning',

'is',

'amazing',

'.']

Applications

  • Tokenization is used in
  • ChatGPT
  • Google Translate
  • BERT
  • GPT
  • Search Engines
  • Sentiment Analysis
  • Spam Detection
  • Voice Assistants
  • OCR
  • Document Search

Common Mistakes

  • Assuming one token equals one word.
  • Ignoring punctuation tokens.
  • Using word tokenization for languages where words are not separated by spaces (such as Chinese).
  • Removing punctuation before understanding its role in the task.
  • Best Practices
  • Choose the tokenizer that matches your model (e.g., BERT → WordPiece, GPT → BPE).
  • Preserve punctuation if it carries meaning.
  • Be aware of token limits when working with LLMs.
  • Test preprocessing choices on your specific dataset.

Interview Questions

  • What is tokenization?
  • Why is tokenization important in NLP?
  • What is the difference between words and tokens?
  • Explain sentence tokenization.
  • Explain word tokenization.
  • What is subword tokenization?
  • What is WordPiece?
  • What is Byte Pair Encoding (BPE)?
  • Why do GPT and BERT use subword tokenization instead of whole words?
  • What is an unknown (<UNK>) token, and how do modern tokenizers reduce its occurrence?

Chapter Summary

Tokenization is the gateway to Natural Language Processing. Every modern NLP model—from traditional machine learning systems to transformer-based models like BERT and GPT—starts by converting raw text into tokens. The choice of tokenizer influences vocabulary size, handling of unknown words, efficiency, and ultimately model performance. Understanding tokenization provides the foundation for the next chapters on Stemming, Lemmatization, and Text Vectorization.

Module 9 · Lesson 9.3

Stemming

Chapter 9.3 – Stemming

Learning Objectives

After completing this chapter, you will be able to

  • Understand what stemming is.
  • Learn why stemming is important in NLP.
  • Differentiate between stemming and lemmatization.
  • Understand popular stemming algorithms.
  • Implement stemming using Python.

Know when to use and when to avoid stemming.

1. Introduction

When humans read the following words

  • play
  • playing
  • played
  • plays

we immediately recognize that they all refer to the same basic concept: play.

However, a computer sees them as four different words unless we normalize them.

One way to normalize these words is stemming.

What is Stemming?

Definition

Stemming is the process of reducing a word to its root form (called a stem) by removing prefixes or suffixes, without considering whether the resulting stem is an actual dictionary word.

The stem is simply a shortened form of the word.

Example

  • Original words
  • playing
  • played
  • plays
  • player
  • After stemming
  • play
  • play
  • play
  • player

Notice that the algorithm removes common endings to group related words.

  • Another Example
  • Original
  • studying
  • studies
  • study
  • After stemming
  • studi
  • studi
  • studi

Here, "studi" is not a real English word. This illustrates an important point: stemming prioritizes reducing words to a common form rather than producing valid dictionary words.

Why Do We Need Stemming?

Imagine a search engine.

A user searches for

play football

A document contains

playing football

Without stemming

Search word → play

Document word → playing

The system may incorrectly treat them as different words.

With stemming

  • play
  • playing
  • played
plays
play

Now all related forms match, improving search and retrieval.

Real-World Analogy

Imagine organizing books in a library.

Instead of separate shelves for

  • Running
  • Runs
  • Runner
  • Ran

You place them together under the concept

  • Run
  • Stemming performs a similar grouping for text.
  • How Stemming Works

Suppose we have the sentence

  • Students are studying Artificial Intelligence.
  • Step 1: Tokenization
  • Students
  • are
  • studying
  • Artificial
  • Intelligence
  • Step 2: Remove Stop Words (optional)
  • Students
  • studying
  • Artificial
  • Intelligence
  • Step 3: Apply Stemming
  • student
  • studi
  • artifici
  • intellig

Many stems are not valid English words, but they still help the algorithm identify related terms.

Popular Stemming Algorithms

1. Porter Stemmer

Developed by Martin Porter (1980).

It is the most widely used stemming algorithm in English NLP.

Example

OriginalStem
playingplay
playedplay
studiesstudi
happinesshappi
connectionconnect

Advantages

  • Fast
  • Simple
  • Works well for information retrieval

Disadvantages

Can produce stems that are not dictionary words

Sometimes removes too much information

2. Snowball Stemmer

Snowball is an improved version of the Porter Stemmer.

Advantages

  • Supports multiple languages
  • Better accuracy than Porter
  • More configurable

Languages supported include English, French, German, Spanish, Italian, Dutch, Russian, and others.

3. Lancaster Stemmer

The Lancaster Stemmer is more aggressive.

Example

OriginalStem
maximummaxim
provisionprovid
runningrun

Advantages

Very fast

Disadvantages

  • May over-stem words and lose important distinctions.
  • Over-Stemming and Under-Stemming
  • Over-Stemming

Different words become the same stem even though they have different meanings.

Example

university

universe
univers

This can reduce accuracy.

Under-Stemming

Words that should be grouped together remain different.

Example

analysis

analyze
analysis
  • analyz
  • The relationship is not fully captured.
  • Stemming vs Lemmatization
FeatureStemmingLemmatization
Uses a dictionaryNoYes
Produces valid wordsNot alwaysYes
SpeedFasterSlower
AccuracyLowerHigher
Typical outputstudistudy

Stemming focuses on speed, while lemmatization focuses on linguistic correctness.

Applications of Stemming

Stemming is useful in

  • Search engines
  • Information retrieval
  • Document indexing
  • Spam filtering
  • Text classification
  • Keyword extraction
  • Topic modeling

For applications requiring precise language understanding (e.g., translation or question answering), lemmatization is often preferred.

Python Example (NLTK – Porter Stemmer)

from nltk.stem import PorterStemmer
stemmer = PorterStemmer()
words = ["playing", "played", "plays", "studies"]
for word in words:
print(word, "→", stemmer.stem(word))

Output

  • playing → play
  • played → play
  • plays → play
  • studies → studi
  • Python Example (Snowball Stemmer)
from nltk.stem import SnowballStemmer
stemmer = SnowballStemmer("english")
words = ["running", "runner", "runs"]
for word in words:
print(stemmer.stem(word))
  • Possible Output
  • run
  • runner
  • run
  • Python Example (Lancaster Stemmer)
from nltk.stem import LancasterStemmer
stemmer = LancasterStemmer()
print(stemmer.stem("maximum"))
print(stemmer.stem("running"))
  • Possible Output
  • maxim
  • run
  • Real-World Example

Suppose a search engine indexes these documents

  • "The student is studying AI."
  • "Students study mathematics."
  • "He studies every evening."

Without stemming, the system treats

  • student
  • students
  • study
  • studying
  • studies
  • as different terms.

With stemming, many of these words are reduced to a common stem, increasing the likelihood that relevant documents are retrieved for a user's search.

  • Advantages of Stemming
  • Reduces vocabulary size.
  • Improves search results.
  • Faster than lemmatization.
  • Reduces memory usage.
  • Simple to implement.
  • Disadvantages of Stemming
  • May produce non-dictionary words.
  • Can over-stem or under-stem.
  • Ignores grammatical context.
  • Less suitable for tasks requiring deep language understanding.
  • Best Practices
  • Use stemming for search engines and information retrieval.
  • Prefer lemmatization when preserving meaning is important.

Evaluate whether stemming improves your model's performance rather than assuming it always helps.

Use language-specific stemmers when available.

Common Mistakes

  • Assuming stems are always valid words.
  • Using stemming in applications where grammatical correctness matters.
  • Mixing stemming and lemmatization without understanding their effects.
  • Applying an English stemmer to text in another language.

Interview Questions

  • What is stemming?
  • Why is stemming used in NLP?
  • What is the difference between stemming and lemmatization?
  • Explain Porter Stemmer.
  • What is Snowball Stemmer?
  • What is Lancaster Stemmer?
  • What is over-stemming?
  • What is under-stemming?
  • When should you use stemming instead of lemmatization?
  • Give a real-world use case for stemming.

Chapter Summary

Stemming is a text normalization technique that reduces related words to a common stem by removing prefixes or suffixes. It is fast and widely used in search engines, document indexing, and information retrieval. However, because it does not consider grammar or dictionary forms, it may generate stems that are not valid words. In the next chapter, you'll learn Lemmatization, which uses linguistic knowledge to produce meaningful dictionary words and is often preferred for modern NLP applications.

Module 9 · Lesson 9.4

Lemmatization

Chapter 9.4 – Lemmatization

Learning Objectives

After completing this chapter, you will be able to

  • Understand what lemmatization is.
  • Learn why lemmatization is more accurate than stemming.
  • Differentiate between a root word, stem, and lemma.
  • Understand how dictionaries and grammar are used in lemmatization.
  • Implement lemmatization using Python.
  • Know when to use lemmatization in NLP projects.

1. Introduction

Imagine you have the following words

  • playing
  • played
  • plays
  • player

As humans, we know that these words are related to play.

A computer, however, treats them as different words unless we normalize them.

There are two major normalization techniques

Stemming

Lemmatization

Unlike stemming, lemmatization uses the actual meaning and grammar of a word to convert it into its dictionary form.

What is Lemmatization?

Definition

Lemmatization is the process of reducing a word to its base or dictionary form (called a lemma) by considering its meaning and grammatical role.

The resulting word is always a valid dictionary word.

Example

  • Original Words
  • playing
  • played
  • plays
  • After Lemmatization
  • play
  • play
  • play

Notice that play is an actual English word.

  • Another Example
  • Original
  • better
  • Lemma
  • good
  • A stemmer cannot usually identify this relationship, but a lemmatizer can because it understands grammar.
  • What is a Lemma?
  • A lemma is the standard dictionary form of a word.
  • Examples
WordLemma
runningrun
studiesstudy
childrenchild
micemouse
bettergood
wasbe

Notice that some transformations cannot be achieved by simply removing letters.

Why Do We Need Lemmatization?

Suppose a search engine stores the sentence

The children are running in the park.

A user searches

child run

Without lemmatization

  • children ≠ child
  • running ≠ run
  • The search may miss relevant documents.

After lemmatization

children → child

running → run

Now the document is correctly matched.

How Lemmatization Works

Suppose we have

The students were studying Artificial Intelligence.

Step 1

  • Tokenization
  • The
  • students
  • were
  • studying
  • Artificial
  • Intelligence

Step 2

  • Remove Stop Words
  • students
  • studying
  • Artificial
  • Intelligence

Step 3

Identify Part of Speech (POS)

WordPOS
studentsNoun
studyingVerb
ArtificialAdjective
IntelligenceNoun

Step 4

  • Apply Dictionary Rules
  • Result
  • student
  • study
  • artificial
  • intelligence

Every output is a valid English word.

Lemmatization vs Stemming

FeatureStemmingLemmatization
Uses dictionary❌ No✅ Yes
Uses grammar❌ No✅ Yes
Produces real words❌ Not always✅ Yes
SpeedFasterSlower
AccuracyLowerHigher
Examplestudistudy

Why Grammar Matters

Consider

He is better than me.

A stemmer might leave better unchanged.

A lemmatizer understands that

better
good
  • because it uses grammatical and lexical knowledge.
  • Role of Part-of-Speech (POS) Tagging
  • A word can have different meanings depending on its role.

Example

  • I am booking a ticket.
  • Here,
  • booking
  • is a verb.
  • Lemma
  • book

Now consider

  • This booking is confirmed.
  • Here,
  • booking
  • is a noun.
  • A lemmatizer uses POS tagging to decide the correct base form.
  • Real-Life Analogy
  • Imagine a dictionary.

Different forms of a word appear under a single dictionary entry.

For example

  • run
  • runs
  • running
  • ran

All point to the dictionary entry

  • run
  • That dictionary entry is the lemma.
  • Applications of Lemmatization

Lemmatization is widely used in

  • Search engines
  • Chatbots
  • Machine Translation
  • Question Answering
  • Text Summarization
  • Named Entity Recognition
  • Sentiment Analysis
  • Large Language Models (during some preprocessing workflows)
  • Legal document analysis
  • Healthcare text mining
  • Python Example (NLTK)
from nltk.stem import WordNetLemmatizer
lemmatizer = WordNetLemmatizer()
print(lemmatizer.lemmatize("running", pos="v"))
print(lemmatizer.lemmatize("studies", pos="v"))
print(lemmatizer.lemmatize("children"))

Output

  • run
  • study
  • child
  • Python Example (spaCy)
import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("The students were studying AI.")
for token in doc:
print(token.text, "→", token.lemma_)
  • Possible Output
  • The → the
  • students → student
  • were → be
  • studying → study
  • AI → AI
  • spaCy automatically performs POS tagging and lemmatization.
  • Example Comparison
  • Sentence
  • The boys were running faster.
  • After Stemming
  • boy
  • were
  • run
  • faster
  • After Lemmatization
  • boy
  • be
  • run
  • fast
  • The lemmatized output is more linguistically meaningful.
  • Advantages of Lemmatization
  • Produces valid dictionary words.
  • Preserves meaning better than stemming.
  • Improves search accuracy.
  • Better for sentiment analysis.
  • Better for question answering.
  • Better for machine translation.
  • Reduces duplicate word forms while maintaining semantics.
  • Disadvantages of Lemmatization
  • Slower than stemming.
  • Requires dictionaries and language resources.
  • Depends on accurate POS tagging.
  • More computationally expensive.
  • Stemming vs Lemmatization: When to Use?
Use CaseRecommended Technique
Search EngineStemming or Lemmatization
Spam DetectionStemming
Sentiment AnalysisLemmatization
ChatbotsLemmatization
Machine TranslationLemmatization
Question AnsweringLemmatization
Information RetrievalStemming (for speed)
Large Language ModelsUsually tokenizer-based preprocessing; classical lemmatization is task-dependent
  • Best Practices
  • Prefer lemmatization when word meaning is important.
  • Use POS tagging to improve lemmatization accuracy.

Evaluate whether lemmatization improves your model on the target dataset.

Combine lemmatization with other preprocessing steps such as tokenization and stop-word removal when appropriate.

Common Mistakes

  • Assuming stemming and lemmatization produce identical results.
  • Forgetting to provide the correct POS tag when using tools like NLTK.
  • Expecting lemmatization to always improve every NLP model—its usefulness depends on the task.
  • Applying English lemmatizers to text in other languages.

Interview Questions

  • What is lemmatization?
  • What is a lemma?
  • What is the difference between stemming and lemmatization?
  • Why is POS tagging important in lemmatization?
  • Explain WordNet Lemmatizer.
  • How does spaCy perform lemmatization?
  • Why is lemmatization slower than stemming?
  • When would you choose stemming over lemmatization?
  • Give real-world applications of lemmatization.
  • Why is lemmatization important for question answering and machine translation?

Chapter Summary

Lemmatization is a linguistically informed text normalization technique that converts words into their dictionary (lemma) forms by using vocabulary resources and grammatical information. Compared with stemming, it produces more meaningful results and is better suited for applications where preserving word meaning is important. While it requires more computation, it often improves the quality of downstream NLP tasks such as sentiment analysis, question answering, and machine translation.

What's Next?

In Chapter 9.5 – TF-IDF, you'll learn how cleaned and normalized text is transformed into numerical features that machine learning algorithms can understand and use for tasks such as document classification and information retrieval.

Module 9 · Lesson 9.5

TF-IDF

Chapter 9.5 – TF-IDF (Term Frequency – Inverse Document Frequency)

Learning Objectives

After completing this chapter, you will be able to

  • Understand why text must be converted into numbers.
  • Learn what TF-IDF is and why it is important.
  • Calculate TF, IDF, and TF-IDF manually.
  • Implement TF-IDF using Python.
  • Understand the advantages and limitations of TF-IDF.
  • Know where TF-IDF is used in real-world NLP applications.

1. Introduction

  • Imagine you have three documents.
  • Document 1
  • "Machine Learning is amazing."
  • Document 2
  • "Artificial Intelligence and Machine Learning."
  • Document 3
  • "I love Machine Learning."

Now suppose someone asks

"Which word is the most important?"

At first glance, you might say

Machine and Learning

But notice something...

The word Machine appears in every document.

Since it appears everywhere, it is less useful for distinguishing one document from another.

Now consider the word

  • Artificial
  • It appears only in Document 2.
  • This word is much more informative.
  • How do we measure this mathematically?
  • That's where TF-IDF comes in.
  • What is TF-IDF?

Definition

TF-IDF (Term Frequency – Inverse Document Frequency) is a statistical technique that measures how important a word is in a document relative to a collection of documents (called a corpus).

It gives

  • High scores to important words
  • Low scores to common words
  • Why Do We Need TF-IDF?
  • Machine Learning algorithms cannot understand words.
  • They understand only numbers.

Sentence

I love Artificial Intelligence

Needs to become

\[0.21, 0.65, 0.94, 0.80\]
  • TF-IDF converts text into numerical features while giving more importance to informative words.
  • Real-Life Analogy
  • Imagine you're in a classroom.

Every student says

"Good morning."

Since everyone says it, the phrase is not unique.

Now one student says

  • "Quantum Computing"
  • That phrase is rare and therefore carries much more information.
  • TF-IDF works the same way.
  • Common words receive lower importance.
  • Rare but meaningful words receive higher importance.
  • Understanding TF-IDF
  • TF-IDF has two components.
TF-IDF
Term Frequency (TF)

+

  • Inverse Document Frequency (IDF)
  • Part 1 – Term Frequency (TF)
  • Term Frequency measures

How often a word appears in one document.

Formula

\[TF = \frac{\text{Number of times the word appears in the document}} {\text{Total number of words in the document}}\]

Example

  • Document
  • Machine Learning Machine AI
  • Total words = 4
  • Machine appears = 2

Therefore

\[TF(Machine)=\frac{2}{4}=0.5\]

Learning appears once

\[TF(Learning)=\frac14=0.25\]

AI appears once

\[TF(AI)=\frac14=0.25\]
  • Part 2 – Inverse Document Frequency (IDF)
  • Some words appear in almost every document.
  • Examples
  • the
  • is
  • and
  • machine
  • Such words are less useful.
  • IDF reduces their importance.
  • Formula
\[IDF=\log\left(\frac{N}{DF}\right)\]

where

N = Total number of documents
DF = Number of documents containing the word

Example

  • Suppose we have
  • 5 documents.
  • Word
  • Machine
  • appears in all 5 documents.
  • Then
\[IDF=\log\left(\frac55\right)=0\]

Its importance becomes very low.

Now suppose

Artificial

appears in only one document.

\[IDF=\log\left(\frac51\right)\]

This value is much larger.

Therefore,

Artificial receives a higher importance score.

TF-IDF Formula

Finally,

\[TF-IDF = TF \times IDF\]
  • This combines
  • Local importance (TF)
  • Global importance (IDF)
  • Worked Example
  • Corpus
  • Document 1
  • I love AI
  • Document 2
  • I love Machine Learning
  • Document 3
  • AI is powerful
  • Suppose we calculate TF-IDF for
  • AI

Step 1

  • Term Frequency
  • AI appears
  • 1 time
  • Document length
  • 3 words
\[TF=\frac13=0.333\]

Step 2

  • Document Frequency
  • AI appears in
  • Document 1
  • Document 3
  • Total
  • 2 documents

Step 3

Total Documents

N = 3

Step 4

IDF

\[IDF=\log\left(\frac32\right)\]

≈ 0.176

Step 5

TF-IDF

\[0.333\times0.176\]

  • 0.058
  • That is the importance score of AI in Document 1.
  • Visual Understanding
  • Suppose we have
WordTFIDFTF-IDF
MachineHighLowMedium
ArtificialLowHighHigh
theHighVery LowVery Low
LearningMediumMediumMedium
  • Notice
  • TF alone is not enough.
  • IDF balances it.
  • Why Stop Words Get Low Scores
  • Words like
  • the
  • is
  • am
  • are
  • was
  • appear almost everywhere.

Therefore

  • Document Frequency
  • is very high.
  • Consequently,
  • IDF becomes very small.
  • Their TF-IDF score approaches zero.
  • Applications of TF-IDF

TF-IDF is widely used in

  • Search engines
  • Document ranking
  • Spam detection
  • Email classification
  • News categorization
  • Recommendation systems
  • Question answering (traditional systems)
  • Information retrieval
  • Keyword extraction
  • Duplicate document detection

Advantages

  • Easy to understand
  • Fast
  • Simple implementation
  • Good baseline model
  • Works well for small datasets
  • Excellent for document classification

Limitations

TF-IDF has several important limitations

1. Ignores Word Meaning

  • Sentence
  • Car
  • Sentence
  • Automobile

TF-IDF treats them as completely different words, even though they mean the same thing.

2. Ignores Word Order

  • Sentence 1
  • Dog bites man
  • Sentence 2
  • Man bites dog
  • Both contain the same words.

TF-IDF produces nearly identical vectors, even though the meanings are very different.

3. Ignores Context

Example

  • Apple released a new iPhone.
  • vs.
  • I ate an apple.

TF-IDF cannot distinguish between the company and the fruit.

4. Sparse Representation

Imagine a vocabulary of

100,000 words.

Most documents use only a few hundred words.

Therefore,

  • most TF-IDF values are
  • 0
  • This creates very large, sparse matrices.
  • TF-IDF vs Bag of Words
FeatureBag of WordsTF-IDF
Counts words
Weights rare words
Penalizes common words
Better accuracy
SimplicityVery HighHigh

TF-IDF vs Word2Vec

TF-IDFWord2Vec
Frequency-basedContext-based
Sparse vectorsDense vectors
Ignores meaningLearns semantic meaning
SimpleDeep Learning
FastMore computationally intensive

Word2Vec (next chapter) overcomes many of TF-IDF's limitations by learning relationships between words.

Python Example (scikit-learn)

from sklearn.feature_extraction.text import TfidfVectorizer
documents = [
    "I love AI",
    "Machine Learning is amazing",
    "I love Machine Learning"
]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(documents)
print(vectorizer.get_feature_names_out())
print(X.toarray())

Sample Output

\['ai', 'amazing', 'is', 'learning', 'love', 'machine'\]
  • The matrix contains the TF-IDF score for every word in every document.
  • Real-World Example
  • Suppose Google indexes millions of web pages.

A user searches

"Machine Learning Tutorial"

TF-IDF helps identify pages where

  • "Machine"
  • "Learning"
  • "Tutorial"
  • are important keywords, rather than just common words appearing everywhere.
  • While modern search engines use much more advanced methods today, TF-IDF remains a foundational concept.
  • Best Practices
  • Remove stop words before applying TF-IDF.
  • Normalize text (lowercase, tokenize, lemmatize) consistently.

Combine TF-IDF with classifiers like Naïve Bayes, Logistic Regression, or SVM for traditional NLP tasks.

Use TF-IDF as a strong baseline before moving to embedding-based models.

Common Mistakes

  • Assuming TF-IDF understands language meaning.
  • Using TF-IDF for tasks that require deep semantic understanding.
  • Ignoring preprocessing before computing TF-IDF.
  • Expecting TF-IDF to capture context or word order.

Interview Questions

What is TF-IDF?

  • Why do we need TF-IDF?
  • Explain the difference between TF and IDF.
  • Why are common words assigned lower TF-IDF scores?
  • How is TF-IDF different from Bag of Words?
  • What are the limitations of TF-IDF?
  • Why can't TF-IDF understand synonyms?
  • How does TF-IDF compare with Word2Vec?
  • Where is TF-IDF used in industry?
  • When would you choose TF-IDF over transformer embeddings?

Chapter Summary

TF-IDF is one of the most important feature extraction techniques in classical NLP. It converts text into numerical vectors while emphasizing words that are important within a document and uncommon across the corpus. Although modern embedding methods such as Word2Vec, BERT, and GPT embeddings capture semantic meaning more effectively, TF-IDF remains a fast, interpretable, and powerful baseline for document classification, search, and information retrieval.

What's Next?

In Chapter 9.6 – Word2Vec, you'll learn how neural networks revolutionized NLP by learning dense vector representations (embeddings) that capture semantic relationships between words—for example, understanding that "king" is related to "queen" and "car" is similar to "automobile".

Module 9 · Lesson 9.6

Word2Vec

Chapter 9.6 – Word2Vec

Learning Objectives

After completing this chapter, you will be able to

  • Understand why Word2Vec was developed.
  • Learn what word embeddings are.
  • Understand how Word2Vec represents words as vectors.
  • Differentiate between CBOW and Skip-Gram models.
  • Understand cosine similarity and semantic relationships.
  • Implement Word2Vec using Python.
  • Understand the advantages and limitations of Word2Vec.

1. Introduction

In the previous chapter, you learned about TF-IDF, which converts text into numerical vectors.

However, TF-IDF has a major limitation

It does not understand meaning.

For example

Car

  • Automobile
  • To humans, these words have almost the same meaning.
  • To TF-IDF, they are completely different.

This problem led to the development of Word Embeddings, and one of the first successful embedding techniques was Word2Vec, introduced by Google in 2013.

2. What is Word2Vec?

Definition

Word2Vec is a neural network-based algorithm that converts words into dense numerical vectors, where words with similar meanings have similar vector representations.

Unlike TF-IDF, Word2Vec learns semantic relationships between words.

3. Why Do We Need Word2Vec?

Consider these words

  • King
  • Queen
  • Prince
  • Princess
  • Humans know they are related.
  • Word2Vec learns this relationship from large amounts of text.
  • Similarly,
  • Car
  • Truck
  • Bus
  • Bike

are grouped together because they often appear in similar contexts.

4. From Words to Vectors

Instead of storing words as text, Word2Vec represents each word as a list of numbers called an embedding.

Example (illustrative only)

WordVector
King[0.21, -0.45, 0.88, ...]
Queen[0.19, -0.41, 0.90, ...]
Apple[0.72, 0.15, -0.30, ...]
Computer[-0.12, 0.84, 0.45, ...]

Each vector may have 100, 200, or 300 dimensions.

5. What is an Embedding?

An embedding is a numerical representation of a word in a high-dimensional space.

Words with similar meanings are located close together.

Imagine a map

Vehicle

Car Bus Truck

Fruit

Apple Mango Banana

In reality, Word2Vec uses hundreds of dimensions instead of two.

6. Distributional Hypothesis

Word2Vec is based on a famous linguistic idea

Words that appear in similar contexts tend to have similar meanings.

Example

The cat is sleeping.

The dog is sleeping.

Because cat and dog appear in similar contexts, Word2Vec learns that they are related.

7. How Word2Vec Works

Suppose the sentence is

I love Machine Learning.

The model first tokenizes it

\[I] [love] [Machine] [Learning\]

Then it learns relationships between neighboring words.

Instead of memorizing definitions, it learns from patterns of co-occurrence in millions or billions of sentences.

8. Two Architectures of Word2Vec

Word2Vec has two training methods

  • 1. Continuous Bag of Words (CBOW)
  • 2. Skip-Gram
  • 9. Continuous Bag of Words (CBOW)

Idea

Predict the current word using the surrounding words.

Example sentence

I love Machine Learning

Input

  • I
  • Machine
  • Learning

Output

love

CBOW uses the surrounding context to guess the missing word.

Advantages

  • Faster training.
  • Performs well on frequent words.
  • Efficient for large datasets.

Disadvantages

Less effective for rare words.

10. Skip-Gram

Idea

Predict the surrounding words from the current word.

Example

Input

love

Output

  • I
  • Machine
  • Learning

Skip-Gram works in the opposite direction to CBOW.

Advantages

Better for rare words.

Produces higher-quality embeddings.

Disadvantages

Slower than CBOW.

11. CBOW vs Skip-Gram

FeatureCBOWSkip-Gram
PredictsCenter wordContext words
Training SpeedFasterSlower
Rare WordsModerateBetter
Large DatasetsExcellentExcellent
AccuracyGoodHigher

12. Understanding Context Window

Suppose the sentence is

The cat sat on the mat

If the window size = 2

Target word

sat

Context words

cat

on

A larger window captures broader context, while a smaller window focuses on nearby relationships.

13. Word Similarity

Once trained, Word2Vec can measure how similar two words are.

Example

Word 1Word 2Similarity
KingQueenHigh
DoctorNurseHigh
CarBicycleModerate
AppleElephantLow

Similarity is usually measured using Cosine Similarity.

14. Famous Word2Vec Analogy

One of the most famous demonstrations of Word2Vec is

King − Man + Woman ≈ Queen

Other examples

  • Paris − France + Italy ≈ Rome
  • Doctor − Man + Woman ≈ Doctor (or Female Doctor, depending on the training data)
  • These relationships emerge because the vectors capture semantic patterns.

15. Applications of Word2Vec

Word2Vec is used in

  • Search engines
  • Document classification
  • Recommendation systems
  • Question answering
  • Chatbots
  • Machine translation
  • Topic modeling
  • Semantic search

Although newer models are now common, Word2Vec is still valuable for many applications.

16. Python Example (Gensim)

from gensim.models import Word2Vec
sentences = [
    ["i", "love", "machine", "learning"],
    ["machine", "learning", "is", "fun"],
\["artificial", "intelligence", "is", "powerful"\]

]

model = Word2Vec(
    sentences,
    vector_size=100,
    window=2,
    min_count=1,
    workers=4
)
print(model.wv["machine"])

This prints the learned embedding vector for the word machine.

17. Finding Similar Words

print(model.wv.most_similar("machine"))

Possible output

  • learning
  • artificial
  • intelligence

These are words that appear in similar contexts.

18. Advantages of Word2Vec

  • Learns semantic relationships.
  • Produces dense vectors.
  • Compact representation.
  • Faster than many deep language models.
  • Captures similarity between words.

Useful for transfer learning in traditional NLP.

19. Limitations of Word2Vec

1. One Vector Per Word

The word bank always has the same vector.

Examples

  • I deposited money in the bank.
  • The fisherman sat on the river bank.
  • Word2Vec cannot distinguish these meanings.

2. Ignores Full Sentence Context

It looks only at nearby words within a fixed window.

3. Static Embeddings

The embedding for apple is always the same, whether it refers to the fruit or the technology company.

Modern models like BERT create contextual embeddings, where the representation changes depending on the sentence.

20. Word2Vec vs TF-IDF

FeatureTF-IDFWord2Vec
Uses word frequencyYesNo
Learns semanticsNoYes
Dense vectorsNoYes
Understands synonymsNoYes
Context awareLimitedPartially (local context)
Deep learningNoYes

21. Word2Vec vs BERT

Word2VecBERT
Static embeddingsContextual embeddings
One vector per wordDifferent vector depending on context
Uses shallow neural networkUses Transformer architecture
FasterMore accurate but computationally heavier

22. Best Practices

  • Use pretrained Word2Vec models when available.
  • Choose an appropriate vector size (e.g., 100–300 dimensions).
  • Tune the context window based on the task.
  • Use Skip-Gram when rare words are important.

23. Common Mistakes

  • Assuming Word2Vec understands complete sentences.
  • Believing one vector can represent every meaning of a word.
  • Using too little training data.
  • Ignoring preprocessing before training.

24. Interview Questions

  • What is Word2Vec?
  • Why was Word2Vec introduced?
  • What is a word embedding?
  • Explain the Distributional Hypothesis.
  • What is the difference between CBOW and Skip-Gram?
  • What is a context window?
  • Why does Word2Vec use dense vectors?
  • What is cosine similarity?
  • What are the limitations of Word2Vec?
  • How is Word2Vec different from BERT?

Chapter Summary

Word2Vec was a major breakthrough in NLP because it moved beyond simple word counting and learned semantic representations of words. By representing words as dense vectors, it can identify similar meanings and relationships, making it much more powerful than TF-IDF for many tasks. However, Word2Vec produces static embeddings, meaning a word always has the same vector regardless of context. This limitation led to newer techniques such as GloVe, FastText, and eventually Transformer-based models like BERT and GPT.

What's Next?

In Chapter 9.7 – GloVe (Global Vectors for Word Representation), you'll learn another popular embedding technique that combines the strengths of global word co-occurrence statistics with distributed representations, improving on some aspects of Word2Vec.

Module 9 · Lesson 9.7

GloVe

Chapter 9.7 – GloVe (Global Vectors for Word Representation)

Learning Objectives

After completing this chapter, you will be able to

  • Understand what GloVe is.
  • Learn why GloVe was developed.
  • Understand how GloVe differs from Word2Vec.
  • Learn about co-occurrence matrices.
  • Understand the training process of GloVe.
  • Implement GloVe embeddings in Python.
  • Compare GloVe with TF-IDF and Word2Vec.

1. Introduction

In the previous chapter, you learned about Word2Vec, which learns word meanings by looking at nearby words (local context).

Although Word2Vec was revolutionary, researchers found one important limitation

  • Word2Vec learns from local context only.
  • It does not directly use information about how often words appear together across the entire corpus.
  • To overcome this limitation, researchers at Stanford University introduced GloVe in 2014.

2. What is GloVe?

Definition

GloVe (Global Vectors for Word Representation) is a word embedding algorithm that learns vector representations by analyzing global word co-occurrence statistics across an entire corpus.

Unlike Word2Vec, which learns from neighboring words, GloVe learns from how frequently words occur together throughout all documents.

3. Why Do We Need GloVe?

Suppose we have millions of documents.

The word

doctor

often appears with

  • hospital
  • patient
  • medicine
  • nurse

The word

teacher

often appears with

  • school
  • classroom
  • student
  • exam

Instead of only looking at nearby words in a single sentence, GloVe captures these relationships across the entire dataset.

4. Local Context vs Global Context

Word2Vec

Looks at nearby words.

Example

  • The doctor treated the patient.
  • Context of doctor
  • treated
  • patient
  • GloVe
  • Looks at all occurrences.

Across millions of sentences

doctor
hospital
  • patient
  • medicine
  • surgery
  • clinic

This broader view helps produce richer word representations.

5. The Main Idea Behind GloVe

The meaning of a word can be understood from how often it appears with other words.

For example

Suppose our corpus contains

King rules a kingdom.

Queen rules a kingdom.

King and Queen live in a palace.

The words King and Queen often appear with

  • kingdom
  • palace
  • rule

Because they share similar co-occurrence patterns, GloVe learns similar embeddings for them.

6. What is a Co-occurrence Matrix?

A co-occurrence matrix records how often words appear together.

Example corpus

  • I love AI
  • I love NLP
  • AI loves data

Vocabulary

IDWord
1I
2love
3AI
4NLP
5data

A simplified co-occurrence matrix

WordIloveAINLPdata
I02110
love20110
AI11001
NLP11000
data00100

The larger the value, the more frequently two words occur together.

7. How GloVe Works

The workflow is

Large Text Corpus

Build Vocabulary

Create Co-occurrence Matrix

Train Mathematical Model

Generate Word Embeddings

Unlike Word2Vec, which trains by predicting words, GloVe trains by factorizing co-occurrence information.

8. Mathematical Intuition

GloVe aims to learn vectors such that

Words with similar co-occurrence patterns have similar embeddings.

The model minimizes a loss function based on the difference between

The predicted relationship between two word vectors.

The observed co-occurrence count from the corpus.

You don't need to memorize the optimization formula at this stage; the key idea is that co-occurrence statistics drive the learning process.

9. Example of Learned Relationships

After training, GloVe may learn relationships like

King − Man + Woman ≈ Queen

Other examples

  • Paris − France + Italy ≈ Rome
  • Brother − Man + Woman ≈ Sister
  • These semantic relationships emerge from the learned vector space.

10. Applications of GloVe

GloVe embeddings are used in

  • Text classification
  • Document clustering
  • Sentiment analysis
  • Search engines
  • Chatbots
  • Question answering
  • Recommendation systems
  • Information retrieval

They were widely used before contextual embeddings such as BERT became popular.

11. Python Example (Using Pretrained GloVe)

import gensim.downloader as api
model = api.load("glove-wiki-gigaword-100")
print(model["computer"])

This returns a 100-dimensional embedding vector for the word computer.

Finding Similar Words

print(model.most_similar("doctor"))

Possible output

  • physician
  • nurse
  • surgeon
  • hospital

These words have embeddings close to doctor.

12. Word Similarity

GloVe can measure semantic similarity using cosine similarity.

Word 1Word 2Similarity
doctorphysicianHigh
kingqueenHigh
appleorangeHigh
appleairplaneLow

13. GloVe vs Word2Vec

FeatureWord2VecGloVe
Uses local contextPartially
Uses global corpus statistics
Based on prediction
Based on co-occurrence matrix
Produces dense embeddings
Semantic relationships

14. GloVe vs TF-IDF

FeatureTF-IDFGloVe
Frequency-basedUses frequencies indirectly
Understands semantics
Dense vectors
Captures similarity
Context awareLimitedBetter

15. Advantages of GloVe

  • Learns from the entire corpus.
  • Captures semantic relationships effectively.
  • Produces compact dense vectors.

Supports pretrained embeddings that can be reused across tasks.

Often performs well on classical NLP benchmarks.

16. Limitations of GloVe

1. Static Embeddings

The word bank always has the same vector.

Example

  • I deposited money in the bank.
  • The children played on the river bank.
  • GloVe cannot distinguish between these meanings.

2. Requires Large Corpora

High-quality embeddings need large amounts of text.

3. Context Independent

The embedding for a word does not change based on the sentence.

Modern transformer models solve this by generating contextual embeddings.

17. Real-World Example

Suppose an online bookstore recommends books.

A customer searches for

Deep Learning

Using GloVe embeddings, the system can recognize that

  • Neural Networks
  • Artificial Intelligence
  • Machine Learning

are semantically related, even if the exact phrase "Deep Learning" does not appear in every book description.

18. Best Practices

Use pretrained GloVe embeddings unless you have a very large custom corpus.

  • Fine-tune embeddings only when necessary.
  • Combine GloVe with deep learning models such as LSTMs or CNNs for traditional NLP tasks.
  • Consider contextual embeddings (e.g., BERT) for applications where word meaning depends heavily on context.

19. Common Mistakes

  • Assuming GloVe understands sentence context.
  • Believing GloVe can distinguish multiple meanings of the same word.
  • Training embeddings on too little data.
  • Confusing co-occurrence statistics with prediction-based learning.

20. Interview Questions

  • What is GloVe?
  • Why was GloVe developed?
  • What is a co-occurrence matrix?
  • How does GloVe differ from Word2Vec?
  • Why is GloVe called "Global Vectors"?
  • What are static embeddings?
  • What are the advantages of GloVe?
  • What are the limitations of GloVe?
  • How does GloVe compare with TF-IDF?
  • Why have transformer-based embeddings largely replaced GloVe in many modern NLP tasks?

Chapter Summary

GloVe is a word embedding algorithm that learns semantic relationships using global co-occurrence statistics from an entire corpus. Compared with Word2Vec, it incorporates broader information about how words appear together, producing meaningful dense vector representations. However, like Word2Vec, GloVe creates static embeddings, assigning the same vector to a word regardless of context. This limitation motivated the development of contextual embedding models such as BERT and GPT.

What's Next?

In Chapter 9.8 – FastText, you'll learn how Facebook AI Research improved traditional word embeddings by representing words as collections of character n-grams. This allows FastText to handle rare words, misspellings, and previously unseen words much better than Word2Vec and GloVe.

Module 9 · Lesson 9.8

FastText

Chapter 9.8 – FastText

Learning Objectives

After completing this chapter, you will be able to

Understand what FastText is.

  • Learn why FastText was developed.
  • Understand how FastText differs from Word2Vec and GloVe.
  • Learn about character n-grams.
  • Understand how FastText handles unknown words.
  • Implement FastText using Python.
  • Know the advantages and limitations of FastText.

1. Introduction

In the previous chapters, you learned

  • TF-IDF → Counts word importance.
  • Word2Vec → Learns word meaning from context.
  • GloVe → Learns meaning using global co-occurrence.

Although Word2Vec and GloVe were huge improvements, they still had one major problem.

Imagine the model has never seen the word

  • ChatGPT5
  • or
  • Electromagnetism
  • or
  • MicroservicesArchitecture

Word2Vec simply says

Unknown Word (UNK)

It cannot generate an embedding.

Facebook AI Research solved this problem by introducing FastText in 2016.

2. What is FastText?

Definition

FastText is a word embedding algorithm developed by Facebook AI Research (FAIR) that represents each word as a collection of character n-grams instead of treating it as a single unit.

Unlike Word2Vec,

Word
One Vector

FastText uses

Word
Character Pieces (n-grams)
Vector

This makes FastText much better at handling rare words and spelling variations.

3. Why Was FastText Developed?

Suppose your training data contains

  • play
  • playing
  • played
  • player

Later, your model sees

playfully

Word2Vec

Unknown Word ❌

FastText

  • play
  • playf
  • fully

...

Understands the word

Because FastText learns from parts of words, it can build an embedding even for words it has never seen before.

4. What are Character n-grams?

An n-gram is a sequence of n consecutive characters.

Example

Word

  • machine
  • 3-grams
  • mac
  • ach
  • chi
  • hin
  • ine
  • 4-grams
  • mach
  • achi
  • chin
  • hine

Instead of learning one vector for machine, FastText learns vectors for these smaller pieces.

5. How FastText Works

Suppose we have the word

learning

FastText splits it into character n-grams.

Example

<learning>

Possible 3-grams

  • <le
  • lea
  • ear
  • arn
  • rni
  • nin
  • ing
  • ng>

The angle brackets (< and >) represent the beginning and end of the word.

The final word embedding is obtained by combining the embeddings of all these n-grams.

6. Why Character n-grams Matter

Consider these words

  • teacher
  • teachers
  • teaching
  • teaches

They all share the root

  • teach
  • FastText learns this relationship naturally because many character n-grams overlap.
  • This makes it more robust than Word2Vec and GloVe.

7. Handling Unknown Words

Suppose the training vocabulary contains

  • computer
  • computers
  • computing

Now the model encounters

  • computerized
  • Word2Vec
  • Unknown Word
  • GloVe
  • Unknown Word
  • FastText

Breaks it into

  • comp
  • ompu
  • mput
  • pute
  • uter
  • teri

...

Creates embedding

Even though the exact word was never seen before.

8. Example

  • Training Words
  • play
  • player
  • playing
  • played
  • Unknown Word
  • playfulness

Because many character sequences are already known, FastText can still estimate a meaningful embedding.

9. FastText Architecture

Text Corpus

Tokenization

Generate Character n-grams

Train Neural Network

Create Word Embeddings

Unlike Word2Vec, the embedding is based on both the whole word and its subword components.

10. Word2Vec vs FastText

FeatureWord2VecFastText
Uses whole words
Uses character n-grams
Handles unknown words
Handles spelling mistakesPoorBetter
Memory usageLowerHigher
Morphological awarenessLimitedStrong

11. GloVe vs FastText

FeatureGloVeFastText
Uses co-occurrence matrix
Uses character n-grams
Unknown wordsPoorExcellent
Morphology supportLimitedStrong

12. FastText vs BERT

FastTextBERT
Static embeddingsContextual embeddings
Character n-gramsTransformer architecture
FasterMore accurate
Lower computational costHigher computational cost
One embedding per wordDifferent embedding depending on context

13. Python Example

from gensim.models import FastText
sentences = [
    ["machine", "learning", "is", "fun"],
    ["artificial", "intelligence", "is", "powerful"],
\["deep", "learning", "uses", "neural", "networks"\]

]

model = FastText(
    sentences,
    vector_size=100,
    window=3,
    min_count=1
)
print(model.wv["learning"])

This returns the embedding vector for learning.

14. Finding Similar Words

print(model.wv.most_similar("learning"))
  • Possible Output
  • machine
  • deep
  • artificial

These words appear in similar contexts and share semantic relationships.

15. Real-World Applications

FastText is useful for

  • Search engines
  • Spell checking
  • Autocomplete
  • Chatbots
  • Text classification
  • Sentiment analysis
  • Machine translation
  • Language identification
  • OCR correction
  • Social media analysis

It performs particularly well when text contains misspellings or many rare words.

16. Advantages of FastText

  • Handles unknown words.
  • Learns from subword information.
  • Supports many languages.
  • Works well for morphologically rich languages (e.g., Turkish, Finnish).
  • Better handling of spelling variations.
  • Produces high-quality embeddings with relatively low computational cost.

17. Limitations of FastText

1. Static Embeddings

The word

bank

always receives the same embedding.

Examples

  • I deposited money in the bank.
  • The children played near the river bank.
  • FastText cannot distinguish between these meanings.

2. Larger Model Size

Because embeddings are stored for many character n-grams, FastText models are generally larger than Word2Vec models.

3. No Deep Context Understanding

FastText looks at subword structure but does not understand the full sentence context.

18. Best Practices

  • Use pretrained FastText models when available.
  • Choose an appropriate n-gram length (commonly 3–6 characters).
  • Use FastText when your data contains many rare or misspelled words.
  • Consider contextual models like BERT for tasks where meaning changes based on context.

19. Common Mistakes

  • Assuming FastText understands sentence meaning.
  • Thinking FastText replaces transformers.
  • Ignoring preprocessing before training.
  • Using FastText when contextual embeddings are required.

20. Interview Questions

  • What is FastText?
  • Why was FastText developed?
  • What are character n-grams?
  • How does FastText handle unknown words?
  • What is the difference between Word2Vec and FastText?
  • What are the advantages of FastText over GloVe?
  • What are static embeddings?
  • Why is FastText useful for morphologically rich languages?
  • What are the limitations of FastText?
  • When would you choose FastText over BERT?

Chapter Summary

FastText extends Word2Vec by representing each word as a collection of character n-grams rather than a single token. This allows it to generate meaningful embeddings for rare, misspelled, or previously unseen words, making it especially useful for languages with complex word formation and noisy text such as social media posts. However, like Word2Vec and GloVe, FastText still produces static embeddings, meaning a word has the same representation regardless of context.

Evolution of Word Representations

Understanding how these techniques evolved helps place FastText in context

TechniqueMain IdeaLimitation
Bag of WordsCount wordsIgnores meaning
TF-IDFWeight important wordsIgnores semantics
Word2VecLearn word meaning from contextCannot handle unknown words well
GloVeLearn from global co-occurrenceStatic embeddings
FastTextLearn from character n-gramsStatic embeddings
BERTContextual embeddings using TransformersComputationally expensive
GPTContextual, generative Transformer modelRequires large-scale training and inference resources

What's Next?

In Chapter 9.9 – Transformers, you'll learn about the breakthrough architecture that transformed NLP. Transformers introduced the attention mechanism, enabling models to understand long-range relationships in text and forming the foundation of modern language models such as BERT, GPT, T5, and Llama.

Module 9 · Lesson 9.9

Transformers

Chapter 9.9 – Transformers

  • This is the most important chapter in modern AI.

If you understand Transformers well, you will understand how ChatGPT, GPT-4, GPT-5, Gemini, Claude, Llama, DeepSeek, Mistral, BERT, and almost every modern LLM works.

Learning Objectives

After completing this chapter, you will be able to

  • Understand why Transformers were invented.
  • Learn the limitations of RNNs and LSTMs.
  • Understand the Transformer architecture.
  • Learn the Attention mechanism.
  • Understand Self-Attention.
  • Learn Multi-Head Attention.
  • Understand Positional Encoding.
  • Learn Encoder and Decoder architecture.
  • Understand why Transformers revolutionized AI.

1. Introduction

Imagine reading this sentence

  • The animal didn't cross the road because it was too tired.
  • What does "it" refer to?
  • The road?
  • The animal?
  • Humans instantly understand that "it" = the animal.
  • How?
  • Because our brain remembers the entire sentence while reading.
  • Older AI models like RNNs struggled with this kind of long-range relationship.
  • Transformers solved this problem.
  • What is a Transformer?

Definition

A Transformer is a deep learning architecture that processes all words in a sentence simultaneously using an Attention mechanism, enabling it to understand relationships between words regardless of their distance.

  • Instead of reading words one by one,
  • Transformers process the whole sentence together.
  • Why Were Transformers Invented?
  • Before Transformers,
  • NLP used
  • RNN
  • LSTM
  • GRU
  • These models processed text sequentially.

Example

I
love
Artificial
Intelligence

The model had to wait until it processed one word before moving to the next.

Problems

  • Slow
  • Difficult to parallelize
  • Forgets long sentences
  • Training takes a long time

Example Problem

Sentence

The boy who was wearing a red shirt and carrying a blue backpack walked into the classroom because he was late.

Question

Who was late?

Humans know

  • The boy
  • RNNs may lose track because "he" is far from "boy."
  • Transformers solve this using Attention.
  • Evolution of NLP Models
Bag of Words
TF-IDF
Word2Vec
GloVe
FastText
RNN
LSTM
GRU

  • Transformers (2017)

  • BERT
  • GPT
  • T5
  • Llama
  • Claude
  • Gemini
  • DeepSeek
  • The Paper That Changed AI

In 2017, Google researchers published

  • Attention Is All You Need
  • This paper introduced the Transformer architecture.
  • Today,
  • almost every Large Language Model is based on it.
  • The Transformer Architecture
  • A Transformer consists of two main parts.
Input Sentence
Encoder
Decoder
Output Sentence

Examples

English
Transformer
French
  • Encoder
  • The Encoder
  • understands
  • the sentence.

Example

I love Machine Learning
Meaning Representation
  • Decoder
  • The Decoder
  • generates
  • the output.

Example

Meaning
Je'aime l'apprentissage automatique
  • Real Life Analogy
  • Imagine a student.
  • Encoder
  • Reads
  • Understands
  • Learns
  • Decoder
  • Writes answers
  • Explains concepts
  • Generates language
  • Why Attention?
  • Consider
  • The cat sat on the mat because it was tired.
  • When processing
  • it
  • the model should pay attention to
  • cat
  • NOT
  • mat
  • Attention decides which words are important.
  • What is Attention?

Definition

Attention is a mechanism that allows the model to focus on the most relevant words while processing a sentence.

Humans naturally do this.

Transformers teach computers to do the same.

Example

  • Sentence
  • The doctor examined the patient because she was sick.
  • When reading
  • she
  • the model gives higher attention to
  • patient
  • than
  • doctor
  • because it best fits the context.
  • Self-Attention

This is the heart of Transformers.

  • Instead of looking only at nearby words,
  • every word looks at
  • every other word.

Example

  • Sentence
  • Machine Learning is changing the world.
  • Word
  • Learning
  • looks at
  • Machine
  • is
  • changing
  • world
  • and decides
  • which words are important.
  • Visual Representation

Machine ←─────────┐

Learning ←──────┐ │

is ←───┐ │ │

changing ←─┐ │ │ │

world ─┘ ┘ ┘ ┘

Every word can attend to every other word.

This is why Transformers understand context so well.

Query, Key and Value (Q, K, V)

Self-attention uses three vectors for every word

  • Query (Q)
  • "What am I looking for?"
  • Key (K)
  • "What information do I have?"
  • Value (V)
  • "The actual information."

Think of a library

  • Query → Your search request.
  • Key → Book titles in the catalog.
  • Value → The content of each book.
  • The model compares queries with keys to decide which values are most relevant.
  • Attention Score
  • The model calculates
Query × Key
Similarity Score
Softmax
Attention Weights
Weighted Sum of Values
  • Words with higher attention scores contribute more to the final representation.
  • Multi-Head Attention
  • One attention mechanism may focus on
  • Grammar.
  • Another may focus on
  • Meaning.
  • Another on
  • Pronouns.
  • Another on
  • Objects.
  • Instead of one attention,
  • Transformers use many.
Sentence
Head 1
Grammar
Head 2
Meaning
Head 3
Pronouns
Head 4
Relationships
Combine Results
  • This allows the model to learn multiple perspectives simultaneously.
  • Positional Encoding
  • Transformers process words in parallel.
  • But then,
  • how does the model know
  • which word comes first?

Answer

Positional Encoding.

Each word receives information about its position.

Example

PositionWord
1I
2love
3AI
  • Without positional encoding,
  • the model would not know the order of words.
  • Why Positional Encoding Matters

Compare

  • Dog bites man
  • and
  • Man bites dog
  • The words are the same,
  • but the order changes the meaning.
  • Positional encoding preserves this information.
  • Advantages of Transformers
  • Parallel processing
  • Understand long sentences
  • Faster training on modern hardware
  • Better context understanding
  • Scalable to billions of parameters
  • State-of-the-art performance across many NLP tasks

Limitations

1. Large Memory Requirement

  • Self-attention compares every word with every other word.
  • If a sentence has n tokens,
  • the attention computation grows approximately with n².
  • Long documents therefore require much more memory.

2. Large Datasets

Transformers perform best when trained on massive amounts of text.

3. Expensive Training

Training modern Transformers requires powerful GPUs or TPUs and significant computational resources.

Applications

Transformers are used in

  • ChatGPT
  • Google Translate
  • BERT
  • GPT
  • Claude
  • Gemini
  • Llama
  • DeepSeek
  • Search Engines
  • Recommendation Systems
  • Medical AI
  • Legal AI
  • Code Generation
  • Image Captioning (with multimodal extensions)
  • Python Example

Using Hugging Face Transformers

from transformers import pipeline
classifier = pipeline(
    "sentiment-analysis"
)
print(classifier(
    "Transformers changed AI forever!"
))

Example Output

[

{'label': 'POSITIVE',

'score': 0.999}

]

Real-World Example

Imagine reading a book.

When you encounter the pronoun "she", you naturally look back to identify who "she" refers to.

A Transformer performs a similar operation by assigning attention to the relevant earlier words, enabling it to resolve references and maintain context.

Common Mistakes

  • Thinking Transformers read text word by word like RNNs.
  • Assuming attention only considers neighboring words.
  • Believing Transformers automatically understand everything—they learn patterns from training data.
  • Confusing the Transformer architecture with specific models like BERT or GPT.
  • Best Practices
  • Understand the Transformer architecture before learning BERT or GPT.
  • Learn self-attention thoroughly—it is the core innovation.

Remember that Transformers are an architecture, while BERT and GPT are models built using that architecture.

Be aware of computational costs for long sequences.

Interview Questions

  • What is a Transformer?
  • Why were Transformers introduced?
  • What problems do they solve compared to RNNs?
  • What is the Attention mechanism?
  • Explain Self-Attention.
  • What are Query, Key, and Value?
  • What is Multi-Head Attention?
  • Why is Positional Encoding needed?
  • What is the difference between Encoder and Decoder?
  • Why are Transformers considered a breakthrough in AI?

Chapter Summary

The Transformer architecture transformed NLP by replacing sequential processing with attention-based parallel processing. Its key innovation—Self-Attention—allows every word in a sentence to interact with every other word, enabling the model to capture long-range dependencies and rich contextual information. Building on this architecture, models such as BERT (encoder-based) and GPT (decoder-based) have achieved remarkable success across language understanding and generation tasks.

What's Next?

In Chapter 9.10 – BERT (Bidirectional Encoder Representations from Transformers), you'll learn how Google adapted the Transformer Encoder to build one of the most influential language understanding models, powering tasks such as search, question answering, and text classification. BERT marked the shift from static word embeddings to contextual embeddings, where a word's representation changes based on the sentence in which it appears.

Module 9 · Lesson 9.10

BERT

Chapter 9.10 – BERT (Bidirectional Encoder Representations from Transformers)

  • BERT is one of the biggest breakthroughs in Natural Language Processing (NLP).

Before BERT, computers struggled to understand the meaning of words in context. BERT changed that by introducing contextual understanding, allowing models to interpret a word based on the words around it.

Learning Objectives

After completing this chapter, you will be able to

  • Understand what BERT is.
  • Learn why BERT was developed.
  • Understand bidirectional language understanding.
  • Learn BERT's architecture.
  • Understand Masked Language Modeling (MLM).
  • Understand Next Sentence Prediction (NSP).
  • Learn how BERT is fine-tuned.
  • Compare BERT with Word2Vec, GloVe, and GPT.
  • Implement BERT using Python.

1. Introduction

Imagine reading these two sentences

  • Sentence 1
  • I deposited money in the bank.
  • Sentence 2
  • The fisherman sat on the bank of the river.
  • The word bank appears in both sentences.
  • But its meaning is different.
  • Humans immediately understand this.
  • Earlier NLP models like TF-IDF, Word2Vec, GloVe, and FastText could not.
  • They stored one vector per word.
  • For them,
bank = same meaning everywhere

BERT solved this problem.

2. What is BERT?

Definition

BERT (Bidirectional Encoder Representations from Transformers) is a Transformer-based language model developed by Google in 2018 that learns contextual representations of words by looking at both the left and right context simultaneously.

Unlike Word2Vec,

BERT understands that

bank (money)

bank (river)

3. Full Form of BERT

  • B
  • Bidirectional
  • E
  • Encoder
  • R
  • Representations
  • T
  • from
  • Transformers

4. Why Was BERT Developed?

Before BERT

Models processed text in only one direction.

Example

I love AI

Left-to-right

I
love
AI

or

Right-to-left

AI
love
I

The model never saw both directions together.

BERT does.

5. What Does "Bidirectional" Mean?

  • Suppose we have
  • The dog chased the cat.
  • To understand
  • chased
  • BERT looks at
  • The
dog
chased
the
  • cat
  • It uses both
  • Left Context
  • AND
  • Right Context
  • simultaneously.

This leads to much better language understanding.

6. BERT Architecture

BERT uses only the Encoder part of the Transformer.

Input Sentence
Embedding Layer
Transformer Encoder
Transformer Encoder
Transformer Encoder
Output Embeddings

There is no decoder.

BERT is designed for understanding language rather than generating it.

7. Input Representation

BERT combines three embeddings for every token

EmbeddingPurpose
Token EmbeddingRepresents the word
Position EmbeddingRepresents the word position
Segment EmbeddingIdentifies which sentence the token belongs to

Final input

Final Embedding

=

Token

+

Position

+

Segment

8. Special Tokens

BERT introduces special tokens.

\[CLS\]
  • Represents the entire sentence.
  • Used for
  • Sentiment Analysis
  • Classification
  • Spam Detection
\[SEP\]

Separates two sentences.

Example

\[CLS\]

I love AI

\[SEP\]

It is amazing

\[SEP\]
\[MASK\]

Used during training.

Example

  • I love [MASK]
  • The model predicts
  • AI

This is called Masked Language Modeling.

9. Masked Language Modeling (MLM)

This is BERT's primary training task.

Example

  • Original
  • The cat sat on the mat.
  • Training input
  • The cat sat on the [MASK].
  • Target
  • mat

BERT learns to predict the missing word.

10. Why Use Masking?

  • Instead of predicting only the next word,
  • BERT predicts missing words using
  • both
  • left
  • and
  • right
  • context.

Example

  • The doctor examined the [MASK].
  • Possible prediction
  • patient
  • BERT looks at
  • doctor
  • examined
  • to infer the answer.

11. Next Sentence Prediction (NSP)

Original BERT was also trained to determine whether one sentence logically follows another.

Example

  • Sentence A
  • I bought a new phone.
  • Sentence B
  • The battery lasts all day.
  • Prediction
  • Yes
  • Another example
  • Sentence A
  • I bought a new phone.
  • Sentence B
  • The elephant lives in Africa.
  • Prediction
  • No

This helped BERT learn relationships between sentences.

Note: Later research found that NSP is not always necessary, and many newer BERT variants use different pretraining objectives.

12. BERT Training Process

Large Text Corpus
Tokenization
Add [MASK]
Transformer Encoder
Predict Missing Words
Update Weights
Repeat Billions of Times

13. Fine-Tuning BERT

One of BERT's biggest strengths is fine-tuning.

A pretrained BERT model can be adapted to many tasks with relatively little task-specific data.

Example

Pretrained BERT
Sentiment Analysis
Movie Review Classifier

or

Pretrained BERT
Question Answering
Medical Assistant

14. Applications of BERT

BERT is widely used in

  • Google Search
  • Question Answering
  • Chatbots
  • Text Classification
  • Sentiment Analysis
  • Named Entity Recognition
  • Document Classification
  • Information Retrieval
  • Email Classification

15. BERT Variants

Several models are based on BERT.

ModelDescription
BERT Base12 encoder layers
BERT Large24 encoder layers
RoBERTaImproved BERT training strategy
DistilBERTSmaller and faster BERT
ALBERTParameter-efficient BERT
TinyBERTLightweight version for edge devices

16. Python Example

Using Hugging Face

from transformers import pipeline
classifier = pipeline(
    "sentiment-analysis",
    model="bert-base-uncased"
)
print(
    classifier(
        "Machine Learning is amazing!"
    )
)

Example Output

[

{'label': 'POSITIVE',

'score': 0.998}

]

17. BERT vs Word2Vec

FeatureWord2VecBERT
Static embeddingsYesNo
Context awareNoYes
BidirectionalNoYes
TransformerNoYes
Deep understandingLimitedStrong

18. BERT vs GPT

BERTGPT
Encoder-onlyDecoder-only
BidirectionalLeft-to-right generation
Designed for understandingDesigned for text generation
Best for classification and retrievalBest for generation and conversation

19. Advantages of BERT

  • Understands context.
  • Bidirectional language modeling.
  • Produces contextual embeddings.
  • Excellent for language understanding tasks.
  • Easy to fine-tune.

Achieves high accuracy on many NLP benchmarks.

20. Limitations of BERT

1. Not Designed for Text Generation

BERT excels at understanding text but is not intended for generating long passages.

2. Computationally Expensive

Large BERT models require significant memory and processing power.

3. Input Length Limit

Standard BERT models have a maximum input length (commonly 512 tokens).

Long documents must be split or handled with specialized architectures.

21. Real-World Example

Suppose a customer writes

"The food was amazing, but the service was slow."

A sentiment analysis system based on BERT can understand that the review contains both positive and negative opinions, rather than assigning a single sentiment based on isolated words.

22. Best Practices

  • Use pretrained BERT models whenever possible.
  • Fine-tune rather than training from scratch.
  • Choose the appropriate BERT variant for your hardware.
  • Match the tokenizer to the model (e.g., BERT tokenizer with BERT).

23. Common Mistakes

  • Assuming BERT generates text like GPT.
  • Forgetting to use the correct tokenizer.
  • Feeding sequences longer than the model supports without preprocessing.
  • Assuming every BERT variant uses identical training objectives.

24. Interview Questions

  • What is BERT?
  • What does BERT stand for?
  • Why is BERT bidirectional?
  • What is Masked Language Modeling?
  • What is Next Sentence Prediction?
  • What are [CLS], [SEP], and [MASK] tokens?
  • Why does BERT use only the Transformer Encoder?
  • How is BERT fine-tuned?
  • What are the limitations of BERT?
  • How does BERT differ from GPT?

25. Chapter Summary

BERT marked a major shift in NLP by introducing contextual word representations. Unlike earlier embedding techniques, BERT understands a word by considering both the words before and after it, making it exceptionally effective for language understanding tasks such as classification, question answering, and named entity recognition. Its encoder-only Transformer architecture, combined with Masked Language Modeling, laid the foundation for many modern NLP systems.

What's Next?

In Chapter 9.11 – GPT (Generative Pre-trained Transformer), you'll learn how OpenAI adapted the Transformer Decoder to create models capable of generating coherent, human-like text. While BERT is optimized for understanding language, GPT is optimized for producing it, making it the foundation of conversational AI systems like ChatGPT.

Module 9 · Lesson 9.11

GPT

Chapter 9.11 – GPT (Generative Pre-trained Transformer)

  • GPT is one of the most influential AI models ever created.

It powers conversational AI systems such as ChatGPT and has transformed how we interact with computers through natural language.

Learning Objectives

After completing this chapter, you will be able to

  • Understand what GPT is.
  • Learn why GPT was developed.
  • Understand the GPT architecture.
  • Learn how GPT generates text.
  • Understand pre-training and fine-tuning.
  • Learn the evolution from GPT-1 to GPT-5.
  • Compare GPT with BERT.
  • Implement GPT using Python.

1. Introduction

Imagine asking a computer

  • Write a story about a robot.
  • Older AI models could classify text or answer simple questions, but they struggled to generate long, coherent passages.
  • GPT was designed to solve this problem.
  • Unlike BERT, which is primarily designed to understand language, GPT is designed to generate language.

2. What is GPT?

Definition

GPT (Generative Pre-trained Transformer) is a decoder-only Transformer model that generates text by predicting the next token based on the tokens that came before it.

GPT reads text from left to right and continually predicts the most likely next token.

3. Full Form of GPT

  • G – Generative
  • P – Pre-trained
  • T – Transformer

4. Why Was GPT Developed?

Researchers wanted a model that could

  • Generate human-like text.
  • Complete sentences.
  • Answer questions.
  • Summarize documents.
  • Translate languages.
  • Write computer code.
  • Hold conversations.

The key idea was

Train one large language model on vast amounts of text, then adapt it to many different tasks.

5. GPT Architecture

GPT uses only the Decoder portion of the Transformer.

Input Prompt

Embedding Layer

Transformer Decoder

Transformer Decoder

Transformer Decoder

Next Token Prediction

Unlike BERT, GPT does not use the encoder.

6. How GPT Generates Text

Suppose the prompt is

Machine Learning is

GPT predicts the next token.

Example

Machine Learning is
fun

Now the sentence becomes

Machine Learning is fun

GPT again predicts the next token.

Machine Learning is fun
because

The process repeats until the model reaches a stopping condition, such as a special end-of-sequence token or a maximum length.

7. Autoregressive Generation

GPT is an autoregressive model.

This means

It predicts one token at a time, using all previously generated tokens as context.

Example

Step 1

The
Step 2
The cat
Step 3
The cat sat
Step 4
The cat sat on
Step 5

The cat sat on the mat.

Each prediction depends on everything generated so far.

8. Next Token Prediction

GPT is trained using a simple objective

  • Given
  • I love
  • Predict
  • AI
  • Given
  • I love AI
  • Predict
  • because
  • Given
  • I love AI because
  • Predict
  • it

The model repeats this process over billions of examples during training.

9. Why "Pre-trained"?

Training GPT from scratch requires enormous amounts of data and computation.

Instead, GPT is first pre-trained on a massive collection of publicly available text.

During pre-training, it learns

  • Grammar
  • Vocabulary
  • Facts and concepts
  • Writing styles
  • Patterns in language

It is not memorizing entire books; it is learning statistical patterns that help predict the next token.

10. Fine-Tuning

After pre-training, a GPT model can be adapted for specific tasks.

Examples

Pretrained GPT
├── Customer Support Bot
  • ├── Medical Assistant
  • ├── Coding Assistant
  • ├── Legal Assistant
  • └── Financial Assistant

Modern systems may also use techniques such as instruction tuning or reinforcement learning from human feedback (RLHF), depending on the model.

11. Tokenization in GPT

GPT does not process characters or whole sentences directly.

It first breaks text into tokens.

Example

Artificial Intelligence is amazing.

Possible tokens (illustrative)

  • Artificial
  • Intelligence
  • is
  • amazing

.

These tokens are converted into IDs and then into embeddings before entering the Transformer.

12. Context Window

GPT remembers only a limited number of recent tokens at once.

This limit is called the context window.

Example

Prompt
Token 1

Token 2

Token 3

...

Token N

Different GPT models support different maximum context lengths.

A larger context window allows the model to reason over longer documents and conversations.

13. Temperature

  • Temperature controls randomness in generation.
  • Low Temperature (e.g., 0.2)
  • More deterministic
  • More predictable
  • Better for factual tasks
  • High Temperature (e.g., 1.0)
  • More creative
  • More varied
  • Greater diversity in responses

14. Top-k Sampling

Instead of considering every possible next token,

GPT can consider only the k most likely tokens.

Example

If k = 5, the model chooses from the five most probable next tokens.

This reduces unlikely outputs.

15. Top-p (Nucleus) Sampling

  • Rather than selecting a fixed number of tokens,
  • Top-p selects the smallest set of tokens whose cumulative probability reaches a chosen threshold (for example, 0.9).
  • This often provides a better balance between creativity and quality than Top-k alone.

16. Evolution of GPT

ModelYearKey Advancement
GPT-12018Introduced generative pretraining
GPT-22019Larger model, stronger text generation
GPT-32020Few-shot learning with 175B parameters
GPT-42023Improved reasoning and multimodal capabilities
GPT-52025/2026Further improvements in reasoning, instruction following, efficiency, and multimodal capabilities

The exact capabilities and deployment details vary by product and configuration.

17. GPT vs BERT

GPTBERT
Decoder-onlyEncoder-only
Generates textUnderstands text
Left-to-rightBidirectional
Next-token predictionMasked Language Modeling
Best for writingBest for classification and understanding

18. Applications of GPT

GPT can be used for

  • Conversational AI
  • Content writing
  • Code generation
  • Document summarization
  • Translation
  • Email drafting
  • Question answering
  • Brainstorming
  • Education
  • Customer support

19. Python Example

Using Hugging Face

from transformers import pipeline
generator = pipeline(
    "text-generation",
    model="gpt2"
)
result = generator(
    "Artificial Intelligence is",
    max_length=30
)
print(result[0]["generated_text"])

Possible Output

Artificial Intelligence is transforming industries by helping people automate complex tasks...

20. Advantages of GPT

  • Generates fluent text.
  • Works across many tasks with prompting.
  • Learns from large-scale pretraining.
  • Can generate code, stories, summaries, and explanations.
  • Supports few-shot and zero-shot prompting.

21. Limitations of GPT

1. Hallucinations

GPT can generate text that sounds convincing but is incorrect.

2. Computational Cost

Large GPT models require significant computing resources for training and inference.

3. Knowledge Limitations

A model's responses depend on its training and any additional retrieval or tool use available at inference time. Without access to up-to-date information, it may not know about recent events.

4. Prompt Sensitivity

The quality of the response often depends on how clearly the prompt is written.

22. Real-World Example

Prompt

Write an email requesting leave for two days.

GPT can generate

Subject: Leave Request

Dear Manager,

I would like to request leave for two days due to personal reasons. I have ensured that my current tasks are documented and will coordinate with the team to minimize any disruption.

Thank you for your understanding.

Sincerely, [Your Name]

23. Best Practices

  • Write clear and specific prompts.
  • Verify important factual information.
  • Break complex tasks into smaller steps.
  • Use system instructions and examples when appropriate.

Combine GPT with retrieval systems for up-to-date knowledge when needed.

24. Common Mistakes

  • Assuming GPT always provides correct information.
  • Confusing GPT with BERT.
  • Expecting GPT to have real-time knowledge without external tools.
  • Ignoring token limits and context windows.

25. Interview Questions

  • What does GPT stand for?
  • How does GPT generate text?
  • What is next-token prediction?
  • Why is GPT called autoregressive?
  • What is the difference between GPT and BERT?
  • What is pre-training?
  • What is fine-tuning?
  • What is a context window?
  • What is temperature in text generation?
  • What are the limitations of GPT?

26. Chapter Summary

GPT is a decoder-only Transformer model designed for text generation. It learns by predicting the next token from previous tokens, enabling it to generate coherent paragraphs, answer questions, write code, summarize documents, and carry on conversations. Its combination of large-scale pretraining and flexible prompting has made it one of the foundational technologies behind modern generative AI.

BERT vs GPT — A Final Comparison

FeatureBERTGPT
ArchitectureEncoderDecoder
Main PurposeLanguage understandingLanguage generation
Training ObjectivePredict masked wordsPredict next token
Reading DirectionBidirectionalLeft-to-right
Best ForClassification, NER, QAChatbots, writing, coding, summarization

What's Next?

In Chapter 9.12 – Hugging Face, you'll learn how to use one of the most popular open-source AI ecosystems to load pretrained models like BERT, GPT-2, Llama, and many others with just a few lines of Python code. You'll also learn about tokenizers, pipelines, datasets, and the Model Hub, which have become standard tools for modern NLP development.

Module 9 · Lesson 9.12

Hugging Face

Chapter 9.12 – Hugging Face

  • Hugging Face is the GitHub of Artificial Intelligence.

If you want to build applications using BERT, GPT, Llama, Mistral, DeepSeek, Stable Diffusion, Whisper, or thousands of other AI models, Hugging Face is one of the most widely used platforms and ecosystems.

Learning Objectives

After completing this chapter, you will be able to

  • Understand what Hugging Face is.
  • Learn why Hugging Face is popular.
  • Explore the Hugging Face ecosystem.
  • Understand Models, Datasets, and Tokenizers.
  • Learn the Transformers library.
  • Use Pipelines for inference.
  • Load pretrained models.
  • Fine-tune models.
  • Deploy models.

1. Introduction

Imagine you want to build an AI chatbot.

Without Hugging Face

Collect Data
Build Model
Train for Weeks
Save Model
Write Inference Code
Deploy

This requires enormous effort and computing resources.

With Hugging Face

from transformers import pipeline
chatbot = pipeline("text-generation")

Done.

You can start experimenting with pretrained models in just a few lines of code.

2. What is Hugging Face?

Definition

Hugging Face is an open-source AI company and ecosystem that provides pretrained machine learning models, datasets, tokenizers, libraries, and tools for Natural Language Processing, Computer Vision, Speech, and Multimodal AI.

Think of Hugging Face as a platform where developers can

  • Download AI models
  • Train AI models
  • Share AI models
  • Fine-tune models
  • Deploy models

3. Why is Hugging Face Important?

Before Hugging Face

Training BERT from scratch could require

  • Massive datasets
  • Powerful GPUs
  • Days or weeks of training
  • Advanced ML expertise

With Hugging Face

You can often download a pretrained model and use it immediately for inference or fine-tuning.

4. Hugging Face Ecosystem

The ecosystem includes several components

Hugging Face

┌───────────────────┼────────────────────┐

│ │ │

▼ ▼ ▼

Transformers Datasets Tokenizers

│ │ │

▼ ▼ ▼

Model Hub Evaluation Deployment

5. Transformers Library

The Transformers library is the most popular Hugging Face package.

It allows you to use models such as

  • BERT
  • GPT-2
  • T5
  • RoBERTa
  • DistilBERT
  • Llama (where supported)
  • Whisper
  • Many others

Installation

pip install transformers

6. Model Hub

The Model Hub is a large repository of pretrained AI models contributed by researchers, companies, and the open-source community.

Examples include

  • BERT
  • GPT-2
  • RoBERTa
  • DistilBERT
  • T5
  • Falcon
  • Mistral
  • Llama (where licensing permits)
  • DeepSeek models
  • Whisper
  • CLIP

Instead of training from scratch, you can often download a suitable pretrained model.

7. Datasets Library

Machine learning requires data.

Hugging Face provides the Datasets library to simplify loading and processing datasets.

Examples

  • IMDB movie reviews
  • SQuAD question answering
  • AG News
  • CNN/DailyMail summarization
  • Common Voice speech datasets

Installation

pip install datasets

Example

from datasets import load_dataset
dataset = load_dataset("imdb")

8. Tokenizers Library

Tokenization is a critical step in NLP.

The Tokenizers library provides fast implementations compatible with many pretrained models.

Example

from transformers import AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained(
    "bert-base-uncased"
)

Then

tokens = tokenizer("Machine Learning")

The tokenizer converts text into the format expected by the model.

9. Pipelines

One of Hugging Face's easiest features is the pipeline API.

Instead of writing many lines of code,

you can use a pretrained model directly.

Example

from transformers import pipeline
classifier = pipeline(
    "sentiment-analysis"
)

Now

classifier(

"I love Artificial Intelligence!"

)

Output

[

{'label': 'POSITIVE',

'score': 0.999}

]

10. Popular Pipeline Tasks

Hugging Face supports many tasks.

TaskPipeline
Sentiment Analysissentiment-analysis
Text Generationtext-generation
Question Answeringquestion-answering
Summarizationsummarization
Translationtranslation
Text Classificationtext-classification
Named Entity Recognitionner
Masked Language Modelingfill-mask
Feature Extractionfeature-extraction

11. Example: Text Generation

from transformers import pipeline
generator = pipeline(
    "text-generation",
    model="gpt2"
)

generator(

"Artificial Intelligence is"

)

Possible Output

Artificial Intelligence is transforming many industries by automating complex tasks...

12. Example: Question Answering

qa = pipeline(
    "question-answering"
)

qa(

question="Who developed Python?",
context="Python was created by Guido van Rossum."

)

Output

Guido van Rossum

13. Example: Translation

translator = pipeline(
    "translation_en_to_fr"
)

translator(

"Machine Learning is amazing."

)

Possible Output

"L'apprentissage automatique est incroyable."

14. AutoModel and AutoTokenizer

Instead of manually choosing model classes,

Hugging Face provides automatic classes.

Example

from transformers import (
    AutoModel,
    AutoTokenizer
)
tokenizer = AutoTokenizer.from_pretrained(
    "bert-base-uncased"
)
model = AutoModel.from_pretrained(
    "bert-base-uncased"
)

These classes automatically load the correct architecture and tokenizer.

15. Fine-Tuning

Suppose you have a medical dataset.

Instead of training a language model from scratch

Pretrained BERT
Medical Dataset
Fine-Tuned Medical BERT

Fine-tuning is one of the most common workflows in modern NLP.

16. Deployment

After training or fine-tuning, a model can be deployed using

  • REST APIs
  • FastAPI
  • Flask
  • Docker
  • Cloud platforms
  • Hugging Face Inference Endpoints (managed deployment)

17. Real-World Applications

Hugging Face is used in

  • Chatbots
  • Search engines
  • Healthcare AI
  • Financial AI
  • Legal document analysis
  • Resume screening
  • Sentiment analysis
  • Translation systems
  • Voice assistants
  • Content moderation

18. Hugging Face vs TensorFlow vs PyTorch

Hugging FaceTensorFlowPyTorch
NLP ecosystemDeep Learning frameworkDeep Learning framework
Pretrained modelsModel buildingModel building
Easy inferenceTraining frameworkTraining framework
Model HubNoNo

Important: Hugging Face is not a replacement for TensorFlow or PyTorch. It builds on top of them and provides higher-level tools and pretrained models.

19. Advantages

  • Thousands of pretrained models.
  • Easy-to-use APIs.
  • Large open-source community.
  • Supports NLP, vision, speech, and multimodal AI.
  • Simplifies fine-tuning and inference.
  • Excellent documentation.

20. Limitations

  • Some large models require powerful GPUs.
  • Model quality varies depending on the source and training.
  • Fine-tuning large models can be computationally expensive.
  • Users must ensure they comply with each model's license and intended use.

21. Best Practices

  • Choose a model appropriate for your task.
  • Always use the matching tokenizer for the model.
  • Start with pretrained models before considering training from scratch.
  • Read the model card to understand limitations, datasets, and licensing.
  • Monitor model performance on your own data before deploying.

22. Common Mistakes

  • Using a tokenizer that doesn't match the model.
  • Assuming every model supports every NLP task.
  • Ignoring model documentation and licenses.
  • Loading very large models without sufficient hardware.

23. Interview Questions

  • What is Hugging Face?
  • What is the Transformers library?
  • What is the Model Hub?
  • What are pipelines?
  • What is AutoTokenizer?
  • What is AutoModel?
  • What is fine-tuning?
  • What is the Datasets library?
  • How is Hugging Face different from TensorFlow?
  • Why is Hugging Face widely used in industry?

24. Chapter Summary

Hugging Face has become one of the most important ecosystems for modern AI development. It provides pretrained models, datasets, tokenizers, and easy-to-use APIs that allow developers to build powerful NLP applications with minimal code. Rather than training models from scratch, developers can download, fine-tune, and deploy state-of-the-art models for tasks such as sentiment analysis, translation, question answering, and text generation.

The Complete NLP Journey So Far

Raw Text

Text Processing

Tokenization

Stemming / Lemmatization

TF-IDF

Word2Vec

GloVe

FastText

Transformers

BERT

GPT

Hugging Face

Build Real AI Applications

This progression reflects how NLP has evolved—from simple text preprocessing techniques to modern transformer-based systems and practical development tools.

What's Next?

In Chapter 9.13 – Sentiment Analysis, you'll learn how to build systems that automatically determine whether text expresses positive, negative, or neutral sentiment. You'll explore both traditional machine learning approaches (using TF-IDF and classifiers) and modern transformer-based approaches (using models such as BERT) to solve real-world problems like product review analysis and social media monitoring.

Module 9 · Lesson 9.13

Sentiment Analysis

Chapter 9.13 – Sentiment Analysis (Complete Guide)

Learning Objectives

By the end of this chapter, you will be able to

  • Understand what Sentiment Analysis is.
  • Learn why businesses use Sentiment Analysis.
  • Build a complete Sentiment Analysis pipeline.
  • Understand Lexicon-based, Machine Learning, and Deep Learning approaches.
  • Implement Sentiment Analysis in Python.
  • Evaluate model performance.
  • Understand real-world industry applications.
  • Learn interview questions and best practices.

1. What is Sentiment Analysis?

Imagine reading these three customer reviews

Review 1

  • ⭐⭐⭐⭐

"The laptop is amazing. Performance is outstanding."

Immediately, you know the customer is happy.

Now read this.

Review 2

  • ☆☆☆☆

"Worst laptop I have ever purchased."

You know the customer is unhappy.

Now read this.

Review 3

  • ⭐⭐☆☆
  • "The laptop was delivered yesterday."
  • This sentence contains no opinion.
  • A computer should also identify these emotions automatically.
  • This task is called Sentiment Analysis.

Definition

Sentiment Analysis (Opinion Mining) is the process of automatically determining whether a piece of text expresses a positive, negative, or neutral opinion.

  • It is one of the most popular applications of Natural Language Processing (NLP).
  • Why is it Called Opinion Mining?
  • Because the system "mines" opinions hidden inside text.

Example

  • "The camera quality is excellent,
  • but battery backup is poor."
  • The model extracts two opinions.
Camera
Positive
Battery
Negative
  • Why is Sentiment Analysis Important?
  • Every company receives millions of reviews every day.
  • For example,
  • Amazon
  • may receive
  • 10 Million Reviews
  • Imagine reading them manually.
  • Impossible.
  • AI performs this automatically within seconds.
  • Real-World Applications
  • Amazon
Customer Review
Positive / Negative
Product Rating

Flipkart

Product Feedback
Customer Satisfaction

Netflix

Movie Reviews
Recommend Movies

Twitter (X)

Millions of Tweets
Public Opinion
Election Analysis
Brand Monitoring

Banks

Customer Complaints
Urgent Issues
Customer Satisfaction Score

Hospitals

Patient Feedback
Quality Improvement

Types of Sentiment Analysis

There are several types.

1. Binary Sentiment Analysis

  • Only two outputs.
  • Positive
  • Negative

Example

"I love this phone."
Positive

2. Ternary Sentiment Analysis

  • Three outputs.
  • Positive
  • Negative
  • Neutral

Example

"The package arrived yesterday."
Neutral

3. Fine-Grained Sentiment

  • Instead of
  • Positive
  • Negative
  • predict

★★★★★

★★★★☆

★★★☆☆

★★☆☆☆

★☆☆☆☆

  • Useful in
  • Amazon
  • Google Reviews
  • Hotels
  • Restaurants

4. Aspect-Based Sentiment Analysis (ABSA)

Instead of classifying the entire review,

classify each feature separately.

Example

"The display is excellent, but battery backup is poor."

Output

AspectSentiment
DisplayPositive
BatteryNegative

This gives much richer insights.

5. Emotion Detection

Instead of Positive or Negative,

predict emotions.

Example

"I got promoted today."
Joy 😊

Example

"I lost my wallet."
Sadness 😢

Possible emotions include

  • Joy
  • Anger
  • Fear
  • Sadness
  • Surprise
  • Disgust
  • Sentiment Analysis Pipeline
  • Customer Reviews

Text Cleaning

Tokenization

Stop Word Removal

Lemmatization

Feature Extraction

(TF-IDF / Word Embeddings)

Machine Learning Model

Prediction

Dashboard / Business Decision

Step 1 – Data Collection

Data sources include

  • Amazon Reviews
  • Flipkart Reviews
  • IMDB Movie Reviews
  • Twitter Posts
  • Reddit Comments
  • Customer Emails
  • Surveys
  • Example Dataset
ReviewSentiment
Excellent phonePositive
Worst productNegative
Battery is okayNeutral
  • Step 2 – Text Preprocessing
  • Before
  • I LOVE this Phone!!! 😊😊
  • After preprocessing
  • love phone
  • Operations performed
  • Lowercase
  • Remove punctuation
  • Remove stop words
  • Lemmatization
  • Tokenization

Step 3 – Feature Extraction

Machines understand numbers.

Text
Vectors
  • Common techniques
  • Bag of Words
  • TF-IDF
  • Word2Vec
  • GloVe
  • FastText
  • BERT Embeddings
  • Step 4 – Model Training
  • Traditional Models
  • Naive Bayes
  • Logistic Regression
  • SVM
  • Random Forest
  • Deep Learning Models
  • LSTM
  • GRU
  • CNN
  • Modern Models
  • BERT
  • RoBERTa
  • DistilBERT
  • DeBERTa
  • Step 5 – Prediction

Input

  • The service was fantastic.
  • Prediction
  • Positive

Input

  • Terrible customer support.
  • Prediction
  • Negative
  • Approaches to Sentiment Analysis
  • There are three major approaches.

1. Rule-Based Approach

  • Uses dictionaries.
  • Positive Dictionary
  • Good
  • Excellent
  • Amazing
  • Wonderful
  • Negative Dictionary
  • Bad
  • Poor
  • Terrible
  • Worst
  • Sentence
  • "The movie was amazing."
  • Positive Score = 1
  • Negative Score = 0
  • Prediction
  • Positive

Advantages

  • Simple
  • Fast
  • No training required

Disadvantages

Cannot understand sarcasm.

2. Machine Learning Approach

  • Training Data
  • Amazing Movie → Positive
  • Worst Movie → Negative
  • Very Good → Positive
  • Poor Service → Negative
  • The model learns patterns.
  • Algorithms
  • Logistic Regression
  • Naive Bayes
  • SVM

Advantages

Higher accuracy

Learns automatically

Disadvantages

Requires labeled data.

3. Deep Learning Approach

Uses

Neural Networks
LSTM
Transformer
BERT
Prediction

Advantages

  • Highest accuracy
  • Understands context
  • Traditional vs Transformer
  • Sentence
  • The movie was not bad.
  • Traditional TF-IDF
  • Finds
Bad
Negative
  • Wrong Prediction
  • BERT
  • Understands
Not Bad
Positive

Correct Prediction

Python Example (TF-IDF)

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
reviews = [
    "I love this phone",
    "Worst product ever"
]
labels = [1,0]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(reviews)
model = LogisticRegression()
model.fit(X, labels)

Python Example (BERT)

from transformers import pipeline
classifier = pipeline(
    "sentiment-analysis"
)
result = classifier(
    "The phone is amazing."
)
print(result)

Output

  • POSITIVE
  • Challenges
  • Sarcasm
  • Sentence
  • Great!
  • Another Monday meeting.
  • Humans know
  • Sarcasm
  • AI finds it difficult.
  • Negation
  • Sentence
  • The movie is not bad.
  • Contains
  • Bad
  • But actually
  • Positive
  • Mixed Sentiments
  • Sentence
  • The screen is excellent,
  • Battery is terrible.
  • Contains
  • Positive
  • AND
  • Negative
  • Emojis
  • Sentence
  • Love this phone ❤️❤️❤️
  • Modern models can often use emojis as additional sentiment cues.
  • Slang
  • Sentence
  • This phone is lit!

The word lit is positive in modern slang but may be misunderstood by older models.

Evaluation Metrics

Common metrics include

  • Accuracy
  • Precision
  • Recall
  • F1 Score
  • Confusion Matrix
  • ROC-AUC (for binary classification)
  • For imbalanced datasets, F1 Score and Precision/Recall are often more informative than Accuracy alone.
  • Industry Applications
  • E-Commerce
Amazon
Customer Reviews
Positive %
Product Ranking

Banking

Customer Emails
Complaint Detection
Urgent Escalation

Healthcare

Patient Feedback
Hospital Rating
Service Improvement

Airlines

Passenger Reviews
Customer Satisfaction
  • Social Media
  • Twitter
  • Facebook
Instagram
Brand Reputation

Politics

Millions of Tweets
Public Opinion
Election Analysis

Best Practices

  • Clean text before training.
  • Use domain-specific data when possible.
  • Start with TF-IDF + Logistic Regression as a baseline.
  • Use BERT or similar Transformer models for complex language.
  • Continuously evaluate the model with real-world data.

Common Mistakes

  • Ignoring negation.
  • Ignoring sarcasm.
  • Training on very small datasets.
  • Using only Accuracy for evaluation.
  • Removing useful emojis or punctuation without considering the task.

Interview Questions

Beginner

  • What is Sentiment Analysis?
  • Why is Sentiment Analysis important?
  • What are the different types of Sentiment Analysis?

Intermediate

  • Explain Aspect-Based Sentiment Analysis.
  • Why is BERT better than TF-IDF?
  • How do you handle imbalanced sentiment datasets?

Advanced

  • How would you build a multilingual sentiment analysis system?
  • How would you detect sarcasm?
  • Which evaluation metrics would you choose for a production system and why?

Mini Project

Customer Review Analyzer

  • Dataset
  • Use the IMDB Movie Reviews dataset or Amazon Product Reviews.
  • Build
  • Text preprocessing
  • TF-IDF vectorization
  • Logistic Regression baseline
  • BERT-based sentiment classifier
  • Compare both models
  • Deploy as a simple web app using Streamlit, Flask, or FastAPI

Chapter Summary

Sentiment Analysis is the task of identifying the emotional tone of text. It is one of the most commercially valuable NLP applications, helping organizations analyze customer feedback, monitor brand reputation, and make data-driven decisions. Traditional methods use techniques such as TF-IDF with classifiers like Logistic Regression, while modern systems use Transformer-based models such as BERT, which better understand context, negation, and complex language.

Key Takeaway

Sentiment Analysis is where all the concepts you've learned—text processing, tokenization, lemmatization, embeddings, transformers, and BERT—come together to solve a real-world business problem.

It is often one of the first end-to-end NLP projects completed by data scientists because it demonstrates the full NLP pipeline from raw text to actionable insights.

Module 9 · Lesson 9.14

Text Classification

Chapter 9.14 – Text Classification

  • Text Classification is one of the most fundamental and widely used tasks in Natural Language Processing (NLP).

Every day, Gmail classifies emails as Spam or Not Spam, news websites categorize articles, customer support systems route tickets, and chatbots detect user intent. All of these are examples of Text Classification.

Learning Objectives

By the end of this chapter, you will be able to

  • Understand what Text Classification is.
  • Learn the different types of text classification.
  • Build a complete text classification pipeline.
  • Understand traditional and deep learning approaches.
  • Implement text classification using Python.
  • Evaluate classification models.
  • Learn real-world applications and interview questions.

1. Introduction

Imagine Gmail receives the following emails

Email 1

Congratulations! You won ₹10,00,000. Click here to claim your prize.

Humans immediately recognize this as spam.

Email 2

Your monthly bank statement is ready for download.

This is a legitimate email.

A computer should also distinguish between these two automatically.

This task is called Text Classification.

2. What is Text Classification?

Definition

Text Classification is the process of automatically assigning one or more predefined categories (labels) to a piece of text.

Unlike Sentiment Analysis, which predicts emotion, Text Classification predicts a category.

Example

Sentence

"Artificial Intelligence is changing healthcare."

Possible category

Technology

Another example

"The stock market closed higher today."

Category

Finance

3. Text Classification vs Sentiment Analysis

Text ClassificationSentiment Analysis
Predicts a categoryPredicts an opinion
Technology, Sports, FinancePositive, Negative, Neutral
Spam DetectionCustomer Reviews
News ClassificationProduct Ratings

4. Types of Text Classification

1. Binary Classification

Only two categories.

Example

Spam Detection

Email
Spam

or

Not Spam

Other examples

  • Fraud / Not Fraud
  • Approved / Rejected
  • Fake / Genuine

2. Multi-Class Classification

More than two categories.

Example

News Article

Choose one category.

Possible labels

  • Sports
  • Politics
  • Technology
  • Business
  • Entertainment
  • Only one label is assigned.

3. Multi-Label Classification

One document can belong to multiple categories.

Example

Article

"AI is transforming healthcare and finance."

Labels

  • Technology
  • Healthcare
  • Finance

Unlike multi-class classification, multiple labels are allowed.

4. Hierarchical Classification

Categories have parent-child relationships.

Example

News

├── Sports

│ ├── Cricket

│ ├── Football

│ └── Tennis

  • ├── Technology
  • └── Business
  • Used in large document repositories.

5. Text Classification Pipeline

Raw Documents

Text Processing

Tokenization

Stop Word Removal

Lemmatization

Feature Extraction

(TF-IDF / Embeddings)

Classification Model

Predicted Category

6. Data Collection

Typical sources include

  • Emails
  • News articles
  • Tweets
  • Product descriptions
  • Customer support tickets
  • Legal documents
  • Medical reports

Example dataset

TextCategory
"India won the match."Sports
"New AI model released."Technology
"Sensex rises by 500 points."Finance

7. Text Preprocessing

Raw text

I LOVE Machine Learning!!!

After preprocessing

love machine learning

Common preprocessing steps

  • Lowercasing
  • Removing punctuation
  • Tokenization
  • Stop-word removal
  • Lemmatization

8. Feature Extraction

Machine learning models require numerical input.

Common techniques

  • Traditional
  • Bag of Words (BoW)
  • TF-IDF
  • Word Embeddings
  • Word2Vec
  • GloVe
  • FastText
  • Contextual Embeddings
  • BERT
  • RoBERTa
  • DistilBERT

9. Classification Algorithms

  • Traditional Machine Learning
  • Naive Bayes
  • Logistic Regression
  • Support Vector Machine (SVM)
  • Decision Tree
  • Random Forest
  • XGBoost
  • Deep Learning
  • CNN
  • LSTM
  • GRU
  • Transformer Models
  • BERT
  • RoBERTa
  • DistilBERT
  • DeBERTa

10. Example – Spam Detection

Training data

EmailLabel
Win ₹1,00,000 now!Spam
Meeting at 3 PMNot Spam
Free vacation offerSpam
Project update attachedNot Spam

After training, the model predicts

Input

"Claim your free gift today!"

Output

Spam

11. Example – News Classification

Article

"The Indian cricket team won the World Cup."

Prediction

Sports

Article

"OpenAI released a new language model."

Prediction

Technology

12. Traditional Machine Learning Example

Pipeline

Documents
TF-IDF
Logistic Regression
Category

Advantages

  • Fast
  • Easy to interpret
  • Strong baseline performance

13. Transformer-Based Classification

Pipeline

Documents
Tokenizer
BERT
Classification Head
Category

Advantages

  • Understands context
  • Better accuracy
  • Handles ambiguous language

14. Python Example (TF-IDF + Logistic Regression)

from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
texts = [
    "I love cricket",
    "Artificial Intelligence is amazing",
    "Stock market is rising"
]
labels = [
    "Sports",
    "Technology",
    "Finance"
]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(texts)
model = LogisticRegression()
model.fit(X, labels)

15. Python Example (BERT)

from transformers import pipeline
classifier = pipeline(
    "text-classification"
)

classifier(

"Artificial Intelligence is transforming healthcare."

)

Possible output

Technology

(When using a generic pipeline, choose or fine-tune a model that has been trained for your target labels.)

16. Real-World Applications

Gmail

Email
Spam / Not Spam

News Websites

Article
Politics / Sports / Technology

Customer Support

Ticket

Billing / Technical / Refund / Complaint

HR Systems

Resume
Java Developer
  • Data Scientist
  • QA Engineer
  • Business Analyst
  • Banking
Transaction Description
Loan
  • Credit Card
  • Insurance
  • Healthcare
Medical Report
Cardiology

Neurology

Radiology

17. Evaluation Metrics

The most common metrics are

  • Accuracy
  • Precision
  • Recall
  • F1 Score
  • Confusion Matrix

For multi-class problems, metrics can be computed using macro, micro, or weighted averaging.

18. Challenges

1. Ambiguous Text

Example

"Apple launched a new product."

Does Apple mean

Fruit?

Company?

Context matters.

2. Imbalanced Classes

Suppose

99% Emails
Not Spam

Only

1%

Spam

A model that predicts "Not Spam" for everything achieves high accuracy but is practically useless.

3. Short Text

Tweet

"Awesome!"

Too little context can make classification difficult.

4. Multiple Languages

Example

"This phone is awesome yaar!"

Mixing languages (code-switching) can reduce performance if the model was not trained for it.

19. Advantages

  • Automates document organization.
  • Reduces manual effort.
  • Scales to millions of documents.
  • Enables real-time decision-making.
  • Supports business intelligence and automation.

20. Best Practices

  • Clean and normalize text.
  • Use TF-IDF + Logistic Regression as a strong baseline.
  • Use BERT or similar transformer models for higher accuracy.
  • Address class imbalance using resampling or class weights if necessary.
  • Evaluate using multiple metrics, not only accuracy.

21. Common Mistakes

  • Using too little labeled data.
  • Ignoring class imbalance.
  • Skipping text preprocessing.
  • Using only accuracy to evaluate performance.
  • Applying the wrong tokenizer for the chosen transformer model.

22. Interview Questions

Beginner

  • What is Text Classification?
  • What is the difference between Sentiment Analysis and Text Classification?
  • What are Binary and Multi-Class Classification?

Intermediate

  • What is Multi-Label Classification?
  • Why is TF-IDF useful?
  • Why is BERT better than TF-IDF for many NLP tasks?

Advanced

  • How would you classify millions of emails every day?
  • How would you handle imbalanced datasets?
  • Which evaluation metric would you use for spam detection and why?
  • How would you deploy a text classification model in production?

23. Mini Project

Email Spam Classifier

Objective

Build a model that classifies emails into

  • Spam
  • Not Spam
  • Steps
  • Collect a labeled spam dataset.
  • Clean and preprocess the emails.
  • Convert text into TF-IDF vectors.
  • Train a Logistic Regression classifier.
  • Compare performance with a BERT-based classifier.

Evaluate using Precision, Recall, F1 Score, and Confusion Matrix.

Deploy the model as a simple web application using Streamlit, FastAPI, or Flask.

Chapter Summary

Text Classification is the process of assigning predefined labels to text. It is a core NLP task used in spam detection, news categorization, customer support routing, intent detection, and many other applications. Traditional approaches combine TF-IDF with machine learning classifiers, while modern systems use Transformer-based models such as BERT to achieve higher accuracy by understanding context.

NLP Journey So Far

  • Text Processing
  • Tokenization
  • Stemming
  • Lemmatization
  • TF-IDF
  • Word2Vec
  • GloVe
  • FastText
  • Transformers
  • BERT
  • GPT
  • Hugging Face
  • Sentiment Analysis
  • Text Classification

You have now learned both the foundational techniques and two of the most important real-world NLP applications. The next chapters—Named Entity Recognition (NER) and Question Answering—will build on these concepts to extract structured information and answer questions from text.

Module 9 · Lesson 9.15

Named Entity Recognition

Chapter 9.15 – Named Entity Recognition (NER)

  • Named Entity Recognition (NER) is one of the most important Information Extraction tasks in NLP.

It enables computers to identify and classify important entities—such as people, organizations, locations, dates, currencies, and more—from unstructured text.

Learning Objectives

By the end of this chapter, you will be able to

  • Understand what Named Entity Recognition (NER) is.
  • Learn why NER is important.
  • Understand different types of named entities.
  • Build a complete NER pipeline.
  • Learn traditional and Transformer-based approaches.
  • Implement NER using Python.
  • Evaluate NER models.
  • Explore real-world applications.

1. Introduction

Imagine reading the following news article

"Satya Nadella visited Hyderabad on 15 July 2025 to inaugurate Microsoft's new AI Research Center."

As humans, we immediately recognize

  • Satya Nadella → Person
  • Hyderabad → Location
  • 15 July 2025 → Date
  • Microsoft → Organization

A computer should also identify these entities automatically.

This task is called Named Entity Recognition (NER).

2. What is Named Entity Recognition?

Definition

Named Entity Recognition (NER) is the process of identifying and classifying important entities in text into predefined categories such as Person, Organization, Location, Date, Time, Money, and more.

NER transforms unstructured text into structured information.

Example

Sentence

"Elon Musk is the CEO of Tesla."

NER Output

WordEntity
Elon MuskPerson
TeslaOrganization
  • Another Example
  • Sentence
  • "OpenAI is located in San Francisco."
  • NER Result
WordEntity
OpenAIOrganization
San FranciscoLocation

3. Why is NER Important?

Organizations process millions of documents daily.

Examples

  • News articles
  • Medical records
  • Legal documents
  • Bank transactions
  • Research papers
  • Customer emails

Reading and extracting important information manually is slow.

NER automates this process.

4. Real-World Analogy

Imagine a librarian reading thousands of books.

Instead of remembering every sentence,

the librarian extracts only

  • Author
  • Book Title
  • Publisher
  • Publication Date

NER performs a similar task for computers.

5. Types of Named Entities

The exact entity types depend on the application, but common categories include

Entity TypeExample
PersonSundar Pichai
OrganizationGoogle
LocationHyderabad
CountryIndia
Date15 July 2025
Time10:30 AM
Money₹50,000
Percentage75%
ProductiPhone 16
EventOlympics

Example

  • Sentence
  • "Apple launched the iPhone 16 in California on September 10, 2025."
  • NER Output
TextEntity
AppleOrganization
iPhone 16Product
CaliforniaLocation
September 10, 2025Date

6. NER Pipeline

Raw Text

Text Processing

Tokenization

Part-of-Speech Tagging

Named Entity Recognition Model

Extract Entities

Structured Output

7. Text Preprocessing

Example

Original

Dr. Ramesh works at Infosys in Bengaluru.

  • After tokenization
  • Dr.
  • Ramesh
  • works
  • at
  • Infosys
  • in
  • Bengaluru

Each token is analyzed by the NER model.

8. Traditional NER Approaches

Before deep learning,

NER systems used

  • Rule-based systems
  • Dictionaries (Gazetteers)
  • Hidden Markov Models (HMM)
  • Conditional Random Fields (CRF)

Advantages

Easy to understand

Disadvantages

  • Difficult to maintain
  • Poor generalization
  • Requires manual feature engineering

9. Deep Learning NER

Modern NER uses

  • BiLSTM
  • BiLSTM + CRF
  • Transformer Models
  • BERT
  • RoBERTa
  • DeBERTa

These models automatically learn language patterns and context.

10. How BERT Performs NER

  • Sentence
  • "Sreehari works at Microsoft in Hyderabad."
  • Tokenization
\[Sreehari\]
\[works\]
\[at\]
\[Microsoft\]
\[in\]
\[Hyderabad\]

BERT predicts an entity label for each token.

TokenPrediction
SreehariPerson
worksO
atO
MicrosoftOrganization
inO
HyderabadLocation

O means Outside (not part of any named entity).

11. BIO Tagging Scheme

Most NER datasets use the BIO format.

BIO stands for

  • B – Beginning of an entity
  • I – Inside an entity
  • O – Outside any entity

Example

Sentence

"Satya Nadella visited Hyderabad."

TokenBIO Tag
SatyaB-PER
NadellaI-PER
visitedO
HyderabadB-LOC
  • Why BIO?
  • Suppose we have
  • New York City

Without BIO

  • Location
  • Location
  • Location

The model cannot determine whether these words belong to one entity or three.

BIO solves this.

TokenBIO
NewB-LOC
YorkI-LOC
CityI-LOC

12. Python Example (spaCy)

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp(
    "Satya Nadella works at Microsoft in Hyderabad."
)
for ent in doc.ents:
print(ent.text, ent.label_)

Output

  • Satya Nadella PERSON
  • Microsoft ORG
  • Hyderabad GPE

13. Python Example (Hugging Face)

from transformers import pipeline
ner = pipeline(
    "ner",
    grouped_entities=True
)
result = ner(
    "Sundar Pichai is the CEO of Google."
)
print(result)
  • Possible Output
  • Sundar Pichai → PERSON
  • Google → ORG

14. Real-World Applications

Search Engines

Query

Microsoft CEO
Extract

Microsoft

CEO
Return relevant results

Resume Screening

Resume
Extract
  • Candidate Name
  • Skills
  • Company
  • Experience
  • Education
  • Banking
Statement
Extract
  • Account Number
  • Customer Name
  • Transaction Amount
  • Healthcare
Medical Report
Extract
  • Disease
  • Medicine
  • Doctor
  • Hospital
  • Legal AI
Contract
Extract
  • Company Names
  • Dates
  • Agreement Amount
  • Locations
  • News Analysis
Article
Extract
  • People
  • Organizations
  • Countries
  • Events

15. Challenges in NER

1. Ambiguous Entities

Sentence

"Apple released a new MacBook."

Apple
Organization

Sentence

"I ate an apple."

Apple
Fruit

The model must use context.

2. New Entities

New startups or products appear frequently.

The model must generalize to unseen names.

3. Multiple Languages

Example

"Sreehari works at Microsoft Bengaluru office."

Mixing languages and formats can make NER more difficult.

4. Misspellings

Example

  • Microsft
  • instead of
  • Microsoft
  • Robust models must handle spelling variations.

16. Evaluation Metrics

NER is commonly evaluated using

  • Precision
  • Recall
  • F1 Score

Unlike simple classification, evaluation considers whether the model correctly identifies both the entity boundaries and the entity type.

Example

  • True Entity
  • Satya Nadella
  • Prediction
  • Satya
  • Although partially correct,

the prediction is considered incomplete because it missed part of the entity.

17. Advantages

  • Converts unstructured text into structured data.
  • Saves manual effort.
  • Improves search and analytics.
  • Enables knowledge graph construction.
  • Supports automation in many industries.

18. Limitations

  • Sensitive to ambiguous language.
  • Performance depends on training data.
  • Domain-specific entities may require fine-tuning.

May struggle with rare names or informal text.

19. Best Practices

  • Use pretrained Transformer-based NER models as a starting point.
  • Fine-tune on domain-specific datasets (medical, legal, finance) when needed.
  • Evaluate entity-level Precision, Recall, and F1 Score.
  • Use consistent annotation guidelines when creating training data.

20. Common Mistakes

  • Assuming every capitalized word is a named entity.
  • Ignoring entity boundaries.
  • Using general-purpose NER models for specialized domains without adaptation.
  • Confusing entity extraction with keyword extraction.

21. Interview Questions

Beginner

  • What is Named Entity Recognition?
  • What are common entity types?
  • What is the difference between NER and Text Classification?

Intermediate

  • What is BIO tagging?
  • Why is BERT effective for NER?
  • What are the challenges in NER?

Advanced

  • How would you build a medical NER system?
  • How would you evaluate an NER model?
  • How would you handle unseen entities?
  • How would you deploy an NER system in production?

22. Mini Project

Resume Information Extractor

Objective

Build an application that extracts

  • Candidate Name
  • Email Address
  • Phone Number
  • Skills
  • Organization Names
  • Education
  • Experience
  • Steps
  • Collect resumes.
  • Preprocess the text.
  • Apply a pretrained NER model.
  • Fine-tune on resume data if necessary.

Display extracted information in a structured format (JSON or table).

Build a web interface using Streamlit, FastAPI, or Flask.

Chapter Summary

Named Entity Recognition (NER) is a core NLP task that identifies and classifies important entities such as people, organizations, locations, dates, and products. It transforms unstructured text into structured information, making it invaluable for search engines, healthcare, legal analysis, finance, and customer support. Modern Transformer-based models like BERT have significantly improved NER accuracy by understanding context and predicting entity labels for each token.

Learning Progress

  • Text Processing
  • Tokenization
  • Stemming
  • Lemmatization
  • TF-IDF
  • Word2Vec
  • GloVe
  • FastText
  • Transformers
  • BERT
  • GPT
  • Hugging Face
  • Sentiment Analysis
  • Text Classification
  • Named Entity Recognition

You now understand the major NLP techniques for understanding, classifying, and extracting information from text. The next chapter, Question Answering, will combine these concepts to build systems that can answer natural language questions from documents and knowledge sources.

Module 9 · Lesson 9.16

Question Answering

Chapter 9.16 – Question Answering (QA)

  • Question Answering (QA) is one of the most advanced applications of Natural Language Processing.

It enables computers to understand a question, search for relevant information, and generate the correct answer in natural language.

Google Search, ChatGPT, Microsoft Copilot, Claude, Gemini, Perplexity, Siri, Alexa, and many customer support bots all rely on Question Answering techniques.

Learning Objectives

After completing this chapter, you will be able to

  • Understand what Question Answering (QA) is.
  • Learn different types of QA systems.
  • Understand Extractive vs Abstractive QA.
  • Learn the QA pipeline.
  • Understand Retrieval-Augmented Generation (RAG).
  • Build QA systems using BERT and LLMs.
  • Implement QA using Python.
  • Learn real-world applications.

1. Introduction

Imagine you open a 500-page textbook.

Now someone asks

  • "Who invented Python?"
  • You don't want to read all 500 pages.
  • Instead,
  • you search,
  • find the correct paragraph,

and answer

Guido van Rossum

Modern AI does exactly this.

This task is called Question Answering (QA).

2. What is Question Answering?

Definition

Question Answering (QA) is an NLP task in which an AI system receives a question and returns the most relevant answer from a document, database, or its learned knowledge.

Unlike Search Engines,

QA returns the answer, not just a list of documents.

Example

Question

Who developed Python?

Answer

  • Guido van Rossum
  • Instead of
  • 10 Web Links

3. Search Engine vs Question Answering

Search EngineQuestion Answering
Returns documentsReturns direct answers
User reads resultsAI provides answer
Keyword matchingUnderstands meaning and context
Example: Google SearchExample: ChatGPT, BERT QA

4. Types of Question Answering

There are several types of QA systems.

1. Factoid Question Answering

Answers short factual questions.

Question

What is the capital of India?

Answer

New Delhi

Question

Who invented Python?

Answer

Guido van Rossum

2. Descriptive Question Answering

Provides detailed explanations.

Question

Explain Machine Learning.

Answer

Several paragraphs explaining the concept.

3. Yes / No Question Answering

Question

Is Python an object-oriented language?

Answer

Yes.

4. Multiple Choice QA

Question

Who developed Linux?

A. Bill Gates

B. Linus Torvalds

C. Steve Jobs

Answer

Linus Torvalds

5. Open-Domain QA

Questions can be about any topic.

Example

  • Why is the sky blue?
  • Who is Albert Einstein?
  • What is Blockchain?

Models like ChatGPT are examples of open-domain QA systems.

6. Closed-Domain QA

  • Limited to one subject.
  • Examples
  • Medical QA
  • Legal QA
  • Banking QA
  • Company Knowledge Base

Example

Question

What is the leave policy?

Only company HR documents are searched.

5. Extractive vs Abstractive QA

This is the most important concept.

  • Extractive Question Answering
  • The answer is copied directly from the document.
  • Document

Python was developed by Guido van Rossum in 1991.

Question

Who developed Python?

Answer

  • Guido van Rossum
  • No new words are generated.
  • Models
  • BERT
  • RoBERTa
  • Abstractive Question Answering
  • The model generates a new answer.
  • Document

Python was created by Guido van Rossum and first released in 1991.

Question

Tell me about Python's creator.

Answer

  • Python was created by Guido van Rossum and first released in 1991.
  • The wording can differ from the original document.
  • Models
  • GPT
  • T5
  • Llama
  • Gemini
  • Extractive vs Abstractive Comparison
Extractive QAAbstractive QA
Copies answer from textGenerates new answer
High factual accuracy (when answer exists in context)More natural language generation
BERTGPT
FasterMore computationally intensive

6. Question Answering Pipeline

User Question

Text Processing

Tokenization

Retriever (Optional)

Question Answering Model

Answer

7. Traditional QA System

Older systems worked like this

Question
Keyword Matching
Search Database
Return Sentence

Problems

  • Doesn't understand meaning
  • Cannot answer complex questions
  • Sensitive to wording

8. Modern QA Using BERT

Example

Document

Microsoft was founded by Bill Gates and Paul Allen.

Question

Who founded Microsoft?

BERT identifies the relevant span in the document

Answer

Bill Gates and Paul Allen

9. QA Using GPT

Question

Explain Quantum Computing.

GPT can generate

Definition

Examples

Applications

Advantages

  • Limitations
  • Unlike BERT,
  • GPT can produce detailed, conversational responses.

10. Retrieval-Augmented Generation (RAG)

  • Modern enterprise chatbots often use Retrieval-Augmented Generation (RAG).
  • Instead of relying only on what the model learned during training,
  • the system first searches trusted documents.
  • Workflow
Question
Retriever
Relevant Documents
LLM
Final Answer

Example

Question

What is our company's leave policy?

Retriever
HR Policy PDF
GPT

Accurate answer based on the HR document

This reduces hallucinations and allows answers based on up-to-date information.

11. Components of a QA System

A production QA system typically includes

  • User Interface
  • Tokenizer
  • Embedding Model
  • Vector Database (optional)
  • Retriever
  • Language Model
  • Ranking Module
  • Response Generator

12. Python Example (Hugging Face)

from transformers import pipeline
qa = pipeline("question-answering")
context = """

Python was created by Guido van Rossum

and released in 1991.

"""

result = qa(
    question="Who created Python?",
    context=context
)
print(result["answer"])

Output

Guido van Rossum

13. Python Example (RAG Concept)

Question
Convert to Embedding
Search Vector Database
Retrieve Top Documents
Pass Documents + Question to GPT
Generate Answer

In practice, frameworks such as LangChain, LlamaIndex, or custom retrieval pipelines are often used to implement this workflow.

14. Real-World Applications

ChatGPT

Question
Generative Answer

Google Search AI Overviews

Question
Summarized Answer

Company Chatbot

Employee
Ask HR Question
Answer from Company Documents

Medical Assistant

Doctor
Medical Question
Evidence-Based Answer

Banking

Customer
Loan Question
Policy-Based Answer

Education

Student
Ask Textbook Question
AI Tutor

15. Challenges

1. Ambiguous Questions

Question

Where is Apple?

Does it refer to

  • Apple Inc.?
  • An apple fruit?
  • Apple Store?

Context is important.

2. Long Documents

Searching thousands of pages efficiently requires retrieval systems and indexing.

3. Hallucinations

Generative models may produce confident but incorrect answers if they lack reliable information.

RAG helps reduce this problem.

4. Outdated Information

Without access to updated documents or external tools,

a model may not know recent events.

16. Evaluation Metrics

Question Answering models are commonly evaluated using

  • Exact Match (EM)
  • F1 Score
  • Precision
  • Recall
  • Human Evaluation (for generative QA)

Example

  • Ground Truth
  • Guido van Rossum
  • Prediction
  • Guido van Rossum
  • Exact Match
  • 100%
  • Prediction
  • Guido
  • Exact Match
  • No
  • F1 Score
  • Partial credit

17. Advantages

  • Natural interaction.
  • Saves time.
  • Reduces manual searching.
  • Improves customer support.
  • Supports enterprise knowledge management.
  • Enhances educational tools.

18. Limitations

  • May generate incorrect answers (hallucinations).
  • Depends on the quality of retrieved documents.
  • Long contexts increase computational cost.

Requires careful evaluation in critical domains such as healthcare and law.

19. Best Practices

  • Use Retrieval-Augmented Generation (RAG) for enterprise applications.
  • Keep knowledge sources up to date.
  • Validate responses in high-risk domains.
  • Choose Extractive QA when exact answers from documents are required.
  • Choose Generative QA when detailed explanations are needed.

20. Common Mistakes

  • Expecting LLMs to know every recent fact without retrieval.
  • Using generative QA when exact document wording is required.
  • Ignoring document quality.
  • Evaluating only with Exact Match for generative systems.

21. Interview Questions

Beginner

  • What is Question Answering?
  • What is the difference between Search and QA?
  • What is Extractive QA?

Intermediate

  • What is Abstractive QA?
  • What is RAG?
  • How does BERT perform QA?

Advanced

  • How would you build an enterprise QA chatbot?
  • Why are vector databases used in RAG?
  • How do you evaluate QA systems?
  • How would you reduce hallucinations?

22. Mini Project

AI PDF Question Answering System

  • Objective
  • Allow users to upload PDF documents and ask questions about them.
  • Workflow
  • Upload PDF.
  • Extract text.
  • Split into chunks.
  • Generate embeddings.
  • Store embeddings in a vector database.
  • Retrieve relevant chunks.

Use an LLM to answer questions based on the retrieved content.

Display the answer with references to the source text.

23. Chapter Summary

Question Answering is one of the most powerful NLP applications. It enables AI systems to answer user questions by understanding language, retrieving relevant information, and generating responses. Traditional systems relied on keyword matching, while modern systems use Transformers, BERT, GPT, and increasingly Retrieval-Augmented Generation (RAG) to provide accurate, context-aware answers. QA powers search assistants, enterprise chatbots, AI tutors, customer support systems, and many other intelligent applications.

Learning Progress

  • Text Processing
  • Tokenization
  • Stemming
  • Lemmatization
  • TF-IDF
  • Word2Vec
  • GloVe
  • FastText
  • Transformers
  • BERT
  • GPT
  • Hugging Face
  • Sentiment Analysis
  • Text Classification
  • Named Entity Recognition
  • Question Answering

You have now mastered the core NLP pipeline—from text preprocessing and embeddings to modern Transformer architectures and practical applications. The next chapter, 9.17 Chatbots, will show how these components are combined to build intelligent conversational AI systems like ChatGPT, customer support bots, and virtual assistants.

Module 9 · Lesson 9.17

Chatbots

Chapter 9.17 – Chatbots

  • Chatbots are one of the most successful real-world applications of Artificial Intelligence and Natural Language Processing (NLP).

They are used by companies like OpenAI, Google, Microsoft, Amazon, Meta, banks, hospitals, airlines, and e-commerce platforms to automate conversations, answer questions, and assist users 24×7.

Learning Objectives

After completing this chapter, you will be able to

  • Understand what a chatbot is.
  • Learn the history and evolution of chatbots.
  • Understand different types of chatbots.
  • Learn chatbot architecture.
  • Understand the role of NLP and LLMs.
  • Learn how Retrieval-Augmented Generation (RAG) powers enterprise chatbots.
  • Build a chatbot using Python.
  • Learn deployment strategies and best practices.

1. Introduction

Imagine visiting an online shopping website.

A small chat window appears

🤖 Hi! How can I help you today?

You type

Where is my order?

The chatbot immediately replies

Your order has been shipped and is expected to arrive tomorrow.

Instead of waiting for a human customer support representative, the chatbot answers instantly.

This is one of the most common applications of AI.

2. What is a Chatbot?

Definition

A chatbot is an AI application that communicates with users through natural language (text or speech) to answer questions, perform tasks, or assist with decision-making.

A chatbot tries to simulate a human conversation.

3. Evolution of Chatbots

First Generation (Rule-Based)

Simple keyword matching.

Example

User

Hello

Bot

  • Hi!
  • No understanding of language.
  • Second Generation (Machine Learning)
  • Used intent classification.

Example

User

I want to book a flight.

Intent

Book Flight
Response

Better than keyword matching.

  • Third Generation (Deep Learning)
  • Used neural networks such as LSTMs and Seq2Seq models.
  • More natural conversations.
  • Fourth Generation (LLM-Based)
  • Modern chatbots use Large Language Models.

Examples

  • ChatGPT
  • Microsoft Copilot
  • Google Gemini
  • Claude

These systems understand context, generate responses, and assist with complex tasks.

4. Types of Chatbots

1. Rule-Based Chatbots

Rules are manually defined.

Example

IF user says "Hello"

THEN reply "Hi!"

Advantages

Easy to build

Predictable

Disadvantages

Cannot handle unexpected questions.

2. Menu-Based Chatbots

User selects options.

Example

  • 1. Order Status
  • 2. Refund
  • 3. Technical Support

Common in banking IVR systems and customer support portals.

3. Retrieval-Based Chatbots

The chatbot selects the best response from a predefined knowledge base.

Example

Question

What are your business hours?

Retrieve matching answer.

Good for FAQs.

4. Generative Chatbots

The chatbot generates new responses.

Models include

  • GPT
  • Gemini
  • Claude
  • Llama

Advantages

More natural conversations.

Can answer questions not explicitly stored in a database.

5. Chatbot Architecture

User

User Interface (Web/Mobile)

Text / Voice Input

NLP Processing & Tokenization

Intent / Retrieval / LLM

Response Generation

User Reply

6. Components of a Chatbot

A modern chatbot typically contains

1. User Interface (UI)

Examples

  • Website
  • Mobile App
  • WhatsApp
  • Microsoft Teams
  • Slack

2. NLP Engine

Processes user input.

Tasks include

  • Tokenization
  • Intent Detection
  • Named Entity Recognition
  • Sentiment Analysis

3. Knowledge Base

Contains

  • FAQs
  • Company Documents
  • Product Information
  • Policies
  • Manuals

4. Language Model

Generates responses.

Examples

  • GPT
  • Llama
  • Mistral
  • Other LLMs

5. Memory

Stores conversation history.

Example

User

My name is John.

Later

User

  • What's my name?
  • Bot
  • Your name is John.
  • Conversation history helps maintain context.

7. Chatbot Conversation Flow

Example

User

Hello

Bot

Hi! How can I help you?

User

Track my order

Bot

Please provide your Order ID.

User

ORD12345

Bot

Your order is out for delivery.

8. Intent Recognition

Every message has an intent.

Example

User MessageIntent
Track my orderOrder Tracking
Cancel my orderOrder Cancellation
I need a refundRefund
Talk to supportHuman Agent

Intent recognition helps determine the next action.

9. Entity Recognition

The chatbot extracts useful information.

User

My order number is ORD12345.

NER Output

EntityValue
Order IDORD12345

The chatbot uses this value to retrieve order details.

10. Traditional Chatbots vs LLM Chatbots

Traditional ChatbotLLM Chatbot
Rule-basedAI-generated responses
Limited knowledgeBroad language understanding
Fixed responsesFlexible responses
Hard to scaleEasier to adapt with prompting and retrieval

11. Retrieval-Augmented Generation (RAG)

Modern enterprise chatbots often use RAG.

Workflow

User Question

Generate Embedding

Search Vector Database

Retrieve Relevant Documents

LLM Generates Answer

Response to User

Example

Question

What is our leave policy?

Retrieve HR document.

LLM answers based on the document.

This helps produce answers grounded in company information.

12. ChatGPT Architecture (Simplified)

User Prompt

Tokenizer

Transformer Model

Context Window

Next Token Prediction

Generated Response

13. Python Example (Simple Rule-Based Chatbot)

while True:
user = input("You: ")
if user.lower() == "hello":
print("Bot: Hi!")
elif user.lower() == "bye":
print("Bot: Goodbye!")

break

else:
print("Bot: Sorry, I don't understand.")

14. Python Example (Hugging Face)

from transformers import pipeline
chatbot = pipeline(
    "text-generation",
    model="gpt2"
)
response = chatbot(
    "What is Artificial Intelligence?",
    max_length=50
)
print(response[0]["generated_text"])

For production conversational systems, instruction-tuned chat models are generally preferred over base GPT-2.

15. Real-World Applications

  • Customer Support
  • Order tracking
  • Refund requests
  • Product information
  • Banking
  • Balance inquiry
  • Loan information
  • Credit card support
  • Healthcare
  • Appointment scheduling
  • Symptom guidance
  • Hospital information
  • Education
  • AI Tutor
  • Homework assistance
  • Course recommendations
  • Human Resources
  • Leave policy
  • Payroll questions
  • Employee onboarding
  • IT Helpdesk
  • Password reset
  • Software installation
  • Ticket creation

16. Advantages

  • Available 24×7.
  • Reduces support costs.
  • Handles many conversations simultaneously.
  • Provides consistent responses.
  • Improves customer experience.

17. Challenges

1. Ambiguous Questions

User

  • I have a problem.
  • What kind of problem?
  • The chatbot needs clarification.

2. Hallucinations

LLM-based chatbots may generate confident but incorrect information.

Using RAG and trusted knowledge sources helps reduce this risk.

3. Context Management

The chatbot should remember previous parts of the conversation when appropriate.

4. Security

Sensitive information should be protected.

Examples

  • Passwords
  • Credit card numbers
  • Personal medical information

18. Best Practices

Use RAG for enterprise chatbots.

Keep knowledge bases updated.

  • Add a fallback to human agents when needed.
  • Log conversations (while respecting privacy and compliance requirements) to improve performance.
  • Test with real users before deployment.

19. Common Mistakes

  • Using only keyword matching for complex tasks.
  • Ignoring conversation history.
  • Not validating responses in high-risk domains.
  • Assuming an LLM always gives correct answers.

20. Interview Questions

Beginner

  • What is a chatbot?
  • What are the different types of chatbots?
  • What is intent recognition?

Intermediate

  • What is entity extraction?
  • How does an LLM chatbot differ from a rule-based chatbot?
  • What is RAG?

Advanced

  • How would you build a customer support chatbot?
  • How would you reduce hallucinations?
  • How would you integrate a chatbot with a company database?
  • What security measures would you implement for an enterprise chatbot?

21. Mini Project

AI Customer Support Chatbot

  • Objective
  • Build a chatbot that answers questions about company policies.
  • Features
  • User authentication (optional)
  • PDF document upload
  • Document indexing
  • Vector database
  • RAG-based retrieval
  • LLM-powered answers
  • Conversation history
  • Feedback collection
  • Tech Stack
  • Python
  • FastAPI or Flask
  • LangChain or LlamaIndex (optional)
  • FAISS, Chroma, or another vector database
  • Hugging Face embeddings or OpenAI embeddings
  • GPT, Llama, or another LLM
  • Streamlit or React for the frontend

22. Chatbot Development Roadmap

Rule-Based Chatbot

Intent Detection

NER

Text Classification

Question Answering

Transformer Models

BERT / GPT

RAG

Enterprise AI Chatbot

23. Chapter Summary

Chatbots are AI systems that communicate with users through natural language. Early chatbots relied on rules and predefined responses, while modern chatbots use Transformer-based Large Language Models (LLMs) to understand context and generate natural responses. Enterprise chatbots often combine Retrieval-Augmented Generation (RAG) with company knowledge bases to provide accurate, up-to-date, and grounded answers. Chatbots are now widely used in customer support, healthcare, banking, education, and enterprise productivity.

Learning Progress

  • Text Processing
  • Tokenization
  • Stemming
  • Lemmatization
  • TF-IDF
  • Word2Vec
  • GloVe
  • FastText
  • Transformers
  • BERT
  • GPT
  • Hugging Face
  • Sentiment Analysis
  • Text Classification
  • Named Entity Recognition
  • Question Answering
  • Chatbots

You have now covered the core NLP technologies and several major real-world applications. The next chapter, 9.18 – LLM Fine-Tuning, will explain how pretrained language models can be adapted to specialized domains—such as medicine, law, finance, or your own organization's documents—to improve performance on specific tasks.

Module 9 · Lesson 9.18

LLM Fine-Tuning

Chapter 9.18 – LLM Fine-Tuning

  • Fine-Tuning is one of the most important skills in Generative AI.

Large Language Models (LLMs) like GPT, Llama, Mistral, Gemma, Falcon, DeepSeek, and Qwen are trained on massive amounts of general text. However, organizations often need these models to understand their own business, products, terminology, or workflows.

Fine-Tuning enables a pretrained model to specialize in a particular domain or task.

Learning Objectives

After completing this chapter, you will be able to

  • Understand what LLM Fine-Tuning is.
  • Learn why fine-tuning is needed.
  • Understand different types of fine-tuning.
  • Learn Parameter-Efficient Fine-Tuning (PEFT).
  • Understand LoRA and QLoRA.
  • Learn the complete fine-tuning pipeline.
  • Implement fine-tuning using Hugging Face.
  • Understand best practices and industry use cases.

1. Introduction

Imagine you hire a new employee.

The employee has

  • Good English
  • Strong reasoning skills
  • General world knowledge
  • But on the first day,

they don't know

  • Your company's products
  • Internal policies
  • Customer workflows
  • Business terminology
  • You train them.
  • After training,

they become an expert in your organization.

Fine-tuning works the same way.

2. What is LLM Fine-Tuning?

Definition

LLM Fine-Tuning is the process of taking a pretrained language model and further training it on a specific dataset so it performs better on a particular task or domain.

Instead of training a model from scratch,

we improve an existing model.

Example

General GPT

Knows general programming.

Fine-tuned GPT
Knows
  • Your company's coding standards
  • Internal APIs
  • Database schema
  • Business rules

3. Why Fine-Tuning?

Suppose you build a chatbot for a hospital.

General LLM
General medical knowledge
Hospital LLM
Hospital Policies
Doctor Names
Departments
Insurance Rules
Appointment Process

Fine-tuning helps the model specialize.

4. Training vs Fine-Tuning

Training from ScratchFine-Tuning
Starts with random weightsStarts with pretrained weights
Needs enormous datasetsNeeds much smaller datasets
Very expensiveMuch cheaper
Weeks or months of trainingHours or days (depending on model and hardware)
Used by AI research labsUsed by most companies

5. Fine-Tuning Workflow

Pretrained Model

Prepare Dataset

Tokenization

Training

Validation

Fine-Tuned Model

Deployment

6. Types of Fine-Tuning

1. Full Fine-Tuning

Every parameter is updated.

Example

GPT
Update

All Weights

Advantages

  • Highest flexibility

Disadvantages

  • Very expensive
  • Requires powerful GPUs

2. Feature Extraction

Freeze the language model.

Train only a small classifier.

Advantages

  • Fast
  • Low cost

Disadvantages

Less adaptable.

3. Parameter-Efficient Fine-Tuning (PEFT)

Instead of updating billions of parameters,

update only a small subset.

Advantages

  • Lower memory usage
  • Faster training
  • Lower storage requirements

7. What is PEFT?

  • PEFT stands for
  • Parameter-Efficient Fine-Tuning
  • Idea
  • Instead of changing
  • 7 Billion Parameters
  • Change only
  • 5 Million Parameters

The result is much cheaper while often achieving competitive performance.

8. LoRA (Low-Rank Adaptation)

  • LoRA is the most popular PEFT technique.
  • Instead of modifying the original model,
  • LoRA adds small trainable matrices.
  • Concept
  • Original Model

Freeze Parameters

Add LoRA Layers

Train Only LoRA

Advantages

  • Fast
  • Memory efficient
  • Widely used in industry

9. QLoRA

QLoRA combines

Quantization

LoRA

The base model is stored in lower precision (for example, 4-bit weights), while LoRA adapters are trained.

Advantages

Lower GPU memory requirements

Enables fine-tuning larger models on modest hardware

10. Instruction Fine-Tuning

Modern chat models are often instruction-tuned.

Dataset Example

Input

Explain Machine Learning.

Output

A well-structured explanation.

The model learns how to follow user instructions.

11. Supervised Fine-Tuning (SFT)

Dataset

InputOutput
Translate to FrenchBonjour
Summarize this articleSummary
Explain AIExplanation

The model learns from correct input-output examples.

12. Reinforcement Learning (Concept)

Some models receive additional alignment training based on human preferences.

A simplified view is

Model Response
Human Feedback
Improve Responses
Better Model

Modern alignment techniques can include methods such as Reinforcement Learning from Human Feedback (RLHF) or Direct Preference Optimization (DPO).

13. Dataset Preparation

Good data is the most important factor.

Example

{

"instruction": "Explain Machine Learning",

"input": "",

"output": "Machine Learning is a branch of AI..."

}

Quality matters more than quantity in many fine-tuning tasks.

14. Fine-Tuning Pipeline

Collect Dataset

Clean Data

Format Dataset

Tokenization

Train

Evaluate

Deploy

15. Python Example (Concept)

Using Hugging Face Transformers and Trainer (simplified)

from transformers import AutoTokenizer, AutoModelForCausalLM
model = AutoModelForCausalLM.from_pretrained(
    "gpt2"
)
tokenizer = AutoTokenizer.from_pretrained(
    "gpt2"
)

For actual fine-tuning, you would typically combine

  • transformers
  • datasets
  • peft (for LoRA/QLoRA)
  • trl (for supervised fine-tuning of chat models)
  • PyTorch

16. Fine-Tuning with LoRA (Concept)

Pretrained LLM

Freeze Base Model

Attach LoRA Adapters

Train Adapters

  • Save Adapter Weights
  • Instead of storing another copy of the entire model,
  • you store only the adapter weights.

17. Fine-Tuning vs RAG

This is one of the most important interview questions.

Fine-TuningRAG
Changes model behaviorKeeps model unchanged
Requires trainingNo model training required
Best for learning domain-specific behavior or styleBest for accessing external or frequently changing knowledge
Knowledge becomes part of the modelKnowledge stays in documents or databases
Higher setup costEasier to update knowledge

Example

Company Policy changes every month.

Should you fine-tune?

  • No.

Use RAG because policies change frequently.

Company wants the model to

  • Write in its brand voice
  • Follow a specific response style
  • Use a standard response format
  • ✅ Fine-tuning is appropriate.

18. Real-World Applications

Healthcare

Fine-tune on

  • Medical terminology
  • Clinical guidelines
  • Hospital documentation
  • Banking

Fine-tune on

  • Financial terminology
  • Loan products
  • Banking workflows
  • Legal

Fine-tune on

  • Contracts
  • Legal language
  • Regulations
  • Software Development

Fine-tune on

  • Internal APIs
  • Coding standards
  • Project documentation
  • Customer Support

Fine-tune on

  • Company tone
  • Standard response templates
  • Product-specific terminology

19. Advantages

  • Better domain performance.
  • Customized responses.
  • Improved task-specific accuracy.
  • Can enforce organization-specific style and terminology.
  • Reuses powerful pretrained models.

20. Limitations

1. Requires Quality Data

Poor-quality training data leads to poor performance.

2. Computational Cost

Even PEFT methods require appropriate hardware.

3. Risk of Overfitting

Small or repetitive datasets can reduce generalization.

4. Maintenance

As requirements change,

the model may need additional fine-tuning or a different approach.

21. Best Practices

  • Start with a strong pretrained model.
  • Clean and validate your dataset.
  • Use LoRA or QLoRA when possible.
  • Keep separate training, validation, and test datasets.
  • Use RAG for frequently changing knowledge.
  • Monitor model performance after deployment.

22. Common Mistakes

  • Fine-tuning when RAG would be sufficient.
  • Using poor-quality datasets.
  • Forgetting to evaluate on unseen data.
  • Training the full model when PEFT would meet the requirements.

23. Interview Questions

Beginner

  • What is LLM Fine-Tuning?
  • Why do we fine-tune LLMs?
  • What is the difference between training and fine-tuning?

Intermediate

  • What is LoRA?
  • What is QLoRA?
  • What is PEFT?
  • What is Instruction Fine-Tuning?

Advanced

  • Fine-Tuning vs RAG?
  • When should you choose LoRA over Full Fine-Tuning?
  • How would you fine-tune an LLM for customer support?

24. Mini Project

Company AI Assistant

  • Objective
  • Build an AI assistant for your organization.
  • Steps
  • Collect company documentation.
  • Decide whether the use case requires RAG, Fine-Tuning, or both.
  • Prepare instruction-response datasets (if fine-tuning).
  • Fine-tune a pretrained model using LoRA.
  • Deploy the assistant using FastAPI or Streamlit.
  • Evaluate with real employee questions.

25. Fine-Tuning Decision Guide

Need latest company knowledge?

├── Yes ──► Use RAG

└── No

Need domain-specific behavior,

style, or task performance?

├── Yes ──► Fine-Tune
└── No ──► Use Prompt Engineering

26. Chapter Summary

LLM Fine-Tuning enables pretrained language models to specialize in a particular task or domain. Instead of building a model from scratch, organizations adapt existing models using their own datasets. Modern approaches such as LoRA, QLoRA, and PEFT make fine-tuning significantly more practical by training only a small number of additional parameters. However, fine-tuning is not always the right solution—for frequently changing information, Retrieval-Augmented Generation (RAG) is often the better choice.

Learning Progress

  • Text Processing
  • Tokenization
  • Stemming
  • Lemmatization
  • TF-IDF
  • Word2Vec
  • GloVe
  • FastText
  • Transformers
  • BERT
  • GPT
  • Hugging Face
  • Sentiment Analysis
  • Text Classification
  • Named Entity Recognition
  • Question Answering
  • Chatbots
  • LLM Fine-Tuning

🎉 Congratulations! You have now completed the core concepts of modern NLP, from text preprocessing to large language models and model customization.

What's Next?

In Chapter 9.19 – NLP Project, you'll bring everything together by building a complete end-to-end NLP application. You'll design a real-world project that includes data collection, preprocessing, embeddings, vector databases, retrieval, LLM integration, evaluation, deployment, and a user interface, following the same principles used in production AI systems.

Module 9 · Lesson 9.19

NLP Project

Chapter 9.19 – End-to-End NLP Project

  • This chapter brings together everything you've learned in Module 9.

Instead of learning individual concepts, you'll build a complete production-ready NLP application similar to what AI engineers build in companies like OpenAI, Microsoft, Google, Amazon, and enterprise organizations.

Learning Objectives

By the end of this chapter, you will be able to

  • Design a complete NLP project.
  • Build an end-to-end AI application.
  • Apply text preprocessing techniques.
  • Use embeddings and vector databases.
  • Build a Retrieval-Augmented Generation (RAG) chatbot.
  • Deploy the application.
  • Understand production architecture.
  • Learn industry best practices.
  • Project Overview
  • Project Title
  • Enterprise AI Document Assistant
  • Problem Statement

Imagine you work for a company with thousands of documents

  • HR Policies
  • Employee Handbook
  • Product Manuals
  • Technical Documentation
  • Customer FAQs
  • Legal Agreements
  • SOP Documents
  • Training Materials

Employees often ask questions such as

  • What is the leave policy?
  • How do I reset my password?
  • What is the travel reimbursement policy?
  • Searching manually takes time.
  • We want to build an AI assistant that answers these questions instantly.
  • Final Product

The chatbot should allow users to

  • Upload PDF documents
  • Search documents intelligently
  • Ask questions in natural language
  • Get accurate answers
  • Display source references
  • Maintain conversation history
  • Support multiple PDFs

Technologies Used

ComponentTechnology
Programming LanguagePython
BackendFastAPI
FrontendStreamlit
NLP FrameworkHugging Face
LLMGPT / Llama / Mistral
EmbeddingsSentence Transformers
Vector DatabaseFAISS / ChromaDB
PDF ProcessingPyPDF2 / pdfplumber
DatabaseSQLite / PostgreSQL
DeploymentDocker + Cloud

High-Level Architecture

User

Streamlit UI

FastAPI Server

┌────────────┴────────────┐

▼ ▼

PDF Processing Chat History

│ │

▼ ▼

Text Chunking SQLite Database

Embedding Model

Vector Database (FAISS)

Retrieve Relevant Chunks

Large Language Model

  • Final Answer
  • Step 1 – Collect Documents
  • Example documents
  • HR_Policy.pdf
  • Employee_Handbook.pdf
  • Leave_Policy.pdf
  • Insurance_Guide.pdf
  • Travel_Policy.pdf
  • Step 2 – Extract Text

Example

import pdfplumber
text = ""

with pdfplumber.open("LeavePolicy.pdf") as pdf

for page in pdf.pages:
    text += page.extract_text()

Output

  • Employees are entitled to 24 paid leaves per year...
  • Step 3 – Text Cleaning
  • Before

Employees are entitled!!!

  • 24 paid leaves.
  • After
  • employees are entitled 24 paid leaves
  • Tasks
  • Lowercase
  • Remove unnecessary spaces
  • Remove special characters (where appropriate)
  • Normalize text
  • Step 4 – Text Chunking

LLMs cannot efficiently process very large documents all at once.

Split documents into chunks.

Example

  • Chunk 1
  • Employees receive
  • 24 annual leaves.

----------------------

  • Chunk 2
  • Medical insurance
  • covers parents.

----------------------

  • Chunk 3
  • Travel reimbursement
  • policy...
  • Typical chunk size
  • 300–1000 words (or a few hundred tokens), depending on the model and use case.
  • Step 5 – Generate Embeddings
  • Convert every chunk into vectors.
Chunk
Embedding Model
768-dimensional Vector
  • Popular models
  • all-MiniLM-L6-v2
  • bge-small-en
  • e5-base-v2
  • Other Sentence Transformer models
  • Step 6 – Store in Vector Database
  • Store embeddings in FAISS.
Chunk
Embedding
FAISS Index

Now searching becomes extremely fast.

  • Step 7 – User Question
  • User asks
  • What is the maternity leave policy?
Question
Embedding
Vector Search
Top 5 Similar Chunks
  • Step 8 – Retrieval
  • FAISS returns
  • Chunk 27
  • Employees receive
  • 180 days maternity leave.

This is the most relevant chunk.

Step 9 – Generate Answer

Prompt sent to the LLM

Context

Employees receive

180 days maternity leave.

Question

What is the maternity leave policy?

  • LLM Response
  • Employees are entitled to 180 days of maternity leave according to the company policy.
  • Step 10 – Display Source
  • Always show references.
Answer
Page 15
  • LeavePolicy.pdf
  • Users can verify the answer.
  • Complete Workflow
PDF
Extract Text
Cleaning
Chunking
Embeddings
Vector Database
User Question
Embedding
Similarity Search
Relevant Chunks
LLM
Answer
Source Citation

Folder Structure

EnterpriseAI/

  • ├── app.py
  • ├── api.py
  • ├── requirements.txt

├── data/

│ documents/

│ embeddings/

├── models/
├── vectorstore/
├── services/

│ pdf_loader.py

│ embedding.py

│ retriever.py

│ llm.py

├── database/

│ chat_history.db

  • └── frontend/
  • Database Design
  • Chat History
IDUserQuestionAnswerTime

Uploaded Files

File IDNameUpload Date

Python Example – Load Embedding Model

from sentence_transformers import SentenceTransformer
model = SentenceTransformer(
    "all-MiniLM-L6-v2"
)
embedding = model.encode(
    "Machine Learning"
)

Python Example – FAISS Search

import faiss
index = faiss.IndexFlatL2(384)
  • index.add(embeddings)
  • D, I = index.search(
  • query_embedding,
k=5

)

Python Example – LLM

prompt = f"""

Context

{context}

Question

{question}

Answer

"""

response = llm.generate(prompt)
  • (The exact API depends on the LLM or framework you choose.)
  • Frontend
  • Use Streamlit
Upload PDF
Ask Question
Display Answer
Display Source

Example

Question

What is the leave policy?

-------------------------

Answer

  • Employees receive
  • 24 annual leaves.
  • Source
  • LeavePolicy.pdf
  • Page 15
  • Deployment Architecture
  • User

Browser

Streamlit

FastAPI

LLM

FAISS

Documents

Production Improvements

Large organizations often add

  • Authentication
  • Role-based access
  • Audit logging
  • Feedback collection
  • Conversation history
  • Multi-language support
  • OCR for scanned PDFs
  • Caching
  • Monitoring and observability
  • Evaluation Metrics

Measure

  • Answer Accuracy
  • Context Precision
  • Retrieval Recall
  • Response Time
  • User Satisfaction
  • Hallucination Rate
  • Source Attribution Accuracy
  • Best Practices
  • Use chunk overlap to preserve context.
  • Keep documents updated.
  • Always show document sources.
  • Validate answers for high-risk domains.
  • Cache embeddings to improve performance.
  • Secure confidential documents.

Common Mistakes

  • Sending the entire PDF to the LLM.
  • Not using chunking.
  • Ignoring retrieval quality.
  • Not citing document sources.
  • Storing sensitive documents without proper security.
  • Industry Use Cases
  • HR Assistant
  • Employees ask policy questions.
  • Medical Assistant
  • Doctors query clinical guidelines.
  • Legal Assistant
  • Lawyers search contracts.
  • Banking Assistant
  • Customers ask about loan policies.
  • IT Helpdesk
  • Employees ask technical questions.
  • Education
  • Students upload textbooks and ask questions.
  • Mini Project Deliverables

Build an application that supports

  • ✅ PDF Upload
  • ✅ Chat Interface
  • ✅ Vector Search
  • ✅ RAG
  • ✅ Conversation History
  • ✅ Source Citation
  • ✅ Multi-document Search

Interview Questions

Beginner

  • What is Retrieval-Augmented Generation (RAG)?
  • Why do we need embeddings?
  • Why is chunking important?

Intermediate

  • What is a vector database?
  • Why is FAISS used?
  • How do embeddings improve semantic search?

Advanced

  • How would you build an enterprise document chatbot?
  • How would you reduce hallucinations?
  • How would you improve retrieval quality?
  • How would you scale the system to millions of documents?
  • Capstone Exercise
  • Build Your Own Enterprise AI Assistant
  • Features
  • Upload multiple PDFs.
  • Store document embeddings in FAISS or ChromaDB.
  • Use a Sentence Transformer embedding model.
  • Retrieve relevant document chunks.
  • Generate answers using an LLM.
  • Show document names and page numbers.
  • Save conversation history.
  • Deploy with FastAPI + Streamlit.
  • Containerize using Docker.
  • Complete NLP Journey
  • Raw Text

Text Processing

Tokenization

Stemming

Lemmatization

TF-IDF

Word2Vec

GloVe

FastText

Transformers

BERT

GPT

Hugging Face

Sentiment Analysis

Text Classification

Named Entity Recognition

Question Answering

Chatbots

LLM Fine-Tuning

Enterprise NLP Project

Chapter Summary

This project demonstrates how modern NLP systems are built in production. Starting from raw documents, you preprocess text, generate embeddings, store them in a vector database, retrieve relevant information, and use a Large Language Model to generate grounded answers. By combining embeddings, vector search, RAG, and LLMs, you can create intelligent assistants for domains such as HR, healthcare, legal services, banking, education, and IT support.

🎓 Congratulations!

You have now completed Module 9 – Natural Language Processing (except the final interview questions chapter). You have progressed from basic text preprocessing to building a production-style AI application that reflects current industry practices.

Next Chapter

9.20– NLP Interview Questions & Answers will prepare you for technical interviews by covering beginner, intermediate, and advanced questions, coding scenarios, system design discussions, and practical problem-solving expected in AI/ML and NLP roles.

Module 9 · Lesson 9.20

Interview Questions

Chapter 9.20 – NLP Interview Questions & Answers

  • This chapter prepares you for NLP interviews at companies such as Microsoft, Google, Amazon, Meta, OpenAI, Infosys, TCS, Accenture, Cognizant, Deloitte, IBM, Capgemini, and product startups.

It covers 100+ interview questions, progressing from beginner to advanced concepts, with explanations, practical scenarios, coding questions, and system design discussions.

  • Table of Contents
  • Basic NLP Questions
  • Intermediate NLP Questions
  • Advanced NLP Questions
  • LLM & Generative AI Questions
  • Coding Interview Questions
  • Scenario-Based Questions
  • System Design Questions
  • Rapid Fire Questions
  • HR + NLP Questions
  • Interview Tips

Section 1 – Basic NLP Interview Questions

Q1. What is NLP?

Answer

Natural Language Processing (NLP) is a branch of Artificial Intelligence that enables computers to understand, interpret, process, and generate human language.

  • Examples
  • ChatGPT
  • Google Translate
  • Siri
  • Alexa
  • Gmail Spam Detection

Q2. What are the steps in an NLP pipeline?

Answer

A typical NLP pipeline consists of

Raw Text
Text Cleaning
Tokenization
Stop Word Removal
Stemming / Lemmatization
Feature Extraction
Model Training
Prediction

Q3. What is Tokenization?

Answer

Tokenization is the process of splitting text into smaller units called tokens.

Example

  • Sentence
  • Machine Learning is amazing.
  • Tokens
  • Machine
  • Learning
  • is
  • amazing

Q4. Difference between Stemming and Lemmatization?

StemmingLemmatization
Removes word endingsUses dictionary meaning
FasterMore accurate
studies → studistudies → study
SimplerLinguistically informed

Q5. What are Stop Words?

  • Words like
  • the
  • is
  • am
  • and

They occur frequently but often contribute little meaning in many NLP tasks.

Q6. What is TF-IDF?

Answer

  • TF-IDF measures the importance of a word in a document relative to a collection of documents.
  • Formula
  • TF-IDF = TF × IDF

Q7. Difference between Bag of Words and TF-IDF?

Bag of WordsTF-IDF
Counts wordsWeights words
No importance weightingReduces influence of common words
SimplerUsually performs better

Q8. What is Word Embedding?

  • Word embeddings convert words into dense numerical vectors.
  • Examples
  • Word2Vec
  • GloVe
  • FastText

Q9. What is Word2Vec?

Word2Vec is a neural network-based algorithm that learns dense vector representations of words based on their context.

Q10. Difference between Word2Vec and GloVe?

Word2VecGloVe
Prediction-basedCo-occurrence-based
Local contextGlobal statistics
Learns by predicting wordsLearns from word co-occurrence

Section 2 – Intermediate Questions

Q11. What is FastText?

FastText extends Word2Vec by representing words as collections of character n-grams, making it better at handling rare and unseen words.

Q12. What is a Transformer?

A Transformer is a neural network architecture based on self-attention, allowing it to process all tokens in parallel and capture long-range dependencies.

Q13. What is Attention?

Attention allows the model to focus on the most relevant words while processing text.

Q14. What is Self-Attention?

Self-attention enables each token to consider every other token in the sequence when building its representation.

Q15. What are Query, Key, and Value?

Every token is projected into three vectors

  • Query (Q): What am I looking for?
  • Key (K): What information do I represent?
  • Value (V): What information do I provide?

Attention scores are computed by comparing Queries with Keys.

Q16. What is Positional Encoding?

Transformers process tokens in parallel, so positional encoding provides information about token order.

Q17. What is BERT?

BERT is a bidirectional encoder-only Transformer designed for language understanding tasks.

Q18. What is GPT?

GPT is a decoder-only Transformer designed primarily for text generation using next-token prediction.

Q19. Difference between BERT and GPT?

BERTGPT
EncoderDecoder
BidirectionalLeft-to-right
UnderstandingGeneration
Masked Language ModelingNext-token prediction

Q20. What is Hugging Face?

Hugging Face provides open-source libraries, pretrained models, datasets, tokenizers, and tools for NLP and other AI applications.

Section 3 – Advanced Questions

Q21. What is RAG?

Retrieval-Augmented Generation (RAG) combines document retrieval with a language model.

Workflow

Question
Retrieve Documents
LLM
Answer

This helps produce answers grounded in external documents.

Q22. Why Use a Vector Database?

Vector databases efficiently store and search embeddings using semantic similarity.

Examples

  • FAISS
  • ChromaDB
  • Pinecone
  • Milvus
  • Weaviate

Q23. What are Embeddings?

Embeddings are dense vector representations of text that capture semantic relationships.

Q24. What is Semantic Search?

Semantic search retrieves documents based on meaning rather than exact keyword matches.

Q25. What is Fine-Tuning?

Fine-tuning adapts a pretrained model to a specific task or domain using additional training data.

Q26. Difference between Fine-Tuning and RAG?

Fine-TuningRAG
Changes model parametersLeaves model unchanged
Learns behavior/styleRetrieves external knowledge
Requires trainingNo retraining required

Q27. What is LoRA?

LoRA (Low-Rank Adaptation) is a parameter-efficient fine-tuning technique that trains small adapter matrices instead of updating the full model.

Q28. What is QLoRA?

QLoRA combines model quantization with LoRA to reduce memory requirements during fine-tuning.

Q29. What is Hallucination?

Hallucination occurs when a language model generates information that sounds plausible but is incorrect or unsupported.

Q30. How Can Hallucinations Be Reduced?

  • Use RAG.
  • Improve prompts.
  • Use trusted knowledge sources.
  • Validate responses.
  • Apply human review in critical applications.

Section 4 – Coding Questions

Q31. Tokenize a Sentence

from nltk.tokenize import word_tokenize
text = "Machine Learning is amazing."
print(word_tokenize(text))

Q32. TF-IDF Example

from sklearn.feature_extraction.text import TfidfVectorizer
docs = [
    "I love AI",
    "AI is powerful"
]
vectorizer = TfidfVectorizer()
X = vectorizer.fit_transform(docs)
print(X.toarray())

Q33. Sentiment Analysis Using Hugging Face

from transformers import pipeline
classifier = pipeline("sentiment-analysis")
print(classifier("Machine Learning is amazing"))

Q34. NER Example

import spacy
nlp = spacy.load("en_core_web_sm")
doc = nlp("Satya Nadella works at Microsoft.")
for ent in doc.ents:
print(ent.text, ent.label_)

Q35. Question Answering

from transformers import pipeline
qa = pipeline("question-answering")
context = "Python was created by Guido van Rossum."
print(
    qa(
        question="Who created Python?",
        context=context
    )
)

Section 5 – Scenario-Based Questions

  • Q36.
  • You have millions of PDF files.
  • How would you build a chatbot?

Answer

  • Extract text.
  • Split into chunks.
  • Generate embeddings.
  • Store in a vector database.
  • Retrieve relevant chunks.
  • Use an LLM to answer.
  • Display source references.
  • Q37.
  • How would you detect spam emails?

Answer

  • Collect labeled data.
  • Preprocess text.
  • Generate features (TF-IDF or embeddings).
  • Train a classifier.
  • Evaluate.
  • Deploy.
  • Q38.
  • How would you build an HR chatbot?

Answer

  • HR documents.
  • RAG.
  • Vector database.
  • LLM.
  • Conversation history.
  • Authentication.
  • Source citations.

Section 6 – System Design Questions

Q39.

Design ChatGPT.

Expected discussion

  • Tokenizer
  • Transformer
  • Context Window
  • Attention
  • Retrieval (optional)
  • Response Generation
  • Safety
  • Monitoring
  • Q40.
  • Design an Enterprise Document Chatbot.

Architecture

PDF
Chunking
Embeddings
Vector Database
Retriever
LLM
Answer

Section 7 – Rapid Fire Questions

QuestionAnswer
What is NLP?Processing human language
What is BERT?Encoder Transformer
GPT?Decoder Transformer
RAG?Retrieval + Generation
LoRA?Efficient Fine-Tuning
Embeddings?Dense vectors
TF-IDF?Word importance
NER?Entity extraction
QA?Answer questions
Chatbot?Conversational AI

Section 8 – HR + NLP Questions

  • Why NLP?
  • I enjoy solving real-world language problems and building intelligent applications that interact naturally with users.
  • Why Generative AI?

Generative AI enables systems to assist with writing, coding, summarization, translation, question answering, and many other knowledge-intensive tasks.

Which NLP project are you most proud of?

Discuss

  • Problem statement
  • Architecture
  • Technologies used
  • Challenges
  • Results
  • Lessons learned

Section 9 – Common Interview Mistakes

  • Memorizing definitions without understanding.
  • Not explaining real-world use cases.
  • Ignoring evaluation metrics.
  • Confusing BERT and GPT.
  • Mixing up RAG and Fine-Tuning.
  • Focusing only on theory without practical implementation.
  • Section 10 – Tips to Crack NLP Interviews
  • 1. Understand the complete NLP pipeline.
  • 2. Build at least one end-to-end NLP project.
  • 3. Learn Hugging Face.
  • 4. Practice Python coding.
  • 5. Understand Transformer architecture.
  • 6. Learn embeddings and vector databases.
  • 7. Understand RAG thoroughly.
  • 8. Learn Prompt Engineering.
  • 9. Know Fine-Tuning concepts.
  • 10. Be able to explain your projects clearly.

Final Capstone Interview Question

Design an AI Assistant for Your Company

Question

Your company wants an AI assistant that answers employee questions from internal documents.

Expected Solution

Employee
Web Application
Authentication
Upload PDFs
Extract Text
Chunking
Embeddings
Vector Database
Retriever
LLM
Answer + Source Citation
Conversation History
  • Technologies
  • Python
  • FastAPI
  • Streamlit
  • Hugging Face
  • Sentence Transformers
  • FAISS / ChromaDB
  • GPT / Llama
  • Docker
  • Cloud Deployment
  • NLP Cheat Sheet
ConceptKey Point
TokenizationSplit text into tokens
StemmingRemove suffixes
LemmatizationConvert to dictionary form
TF-IDFWeight important words
Word2VecDense embeddings from context
GloVeGlobal co-occurrence embeddings
FastTextCharacter n-gram embeddings
TransformerSelf-attention architecture
BERTLanguage understanding
GPTLanguage generation
Hugging FaceAI ecosystem and model hub
Sentiment AnalysisDetect opinions
Text ClassificationAssign categories
NERExtract entities
Question AnsweringAnswer user questions
ChatbotsConversational AI
RAGRetrieval + Generation
Fine-TuningAdapt pretrained models
LoRAEfficient fine-tuning

Module 9 Summary

Congratulations! 🎉

You have completed Module 9 – Natural Language Processing, covering

  • Text Processing
  • Tokenization
  • Stemming
  • Lemmatization
  • TF-IDF
  • Word Embeddings
  • Transformers
  • BERT
  • GPT
  • Hugging Face
  • Sentiment Analysis
  • Text Classification
  • Named Entity Recognition
  • Question Answering
  • Chatbots
  • LLM Fine-Tuning
  • End-to-End NLP Project
  • Interview Preparation
  • Final Advice

Theory is important, but projects demonstrate your skills. For interview success, aim to complete at least these three hands-on projects:

  • Sentiment Analysis System using TF-IDF and BERT.
  • RAG-based Document Chatbot with FAISS/ChromaDB and an LLM.
  • Resume Information Extractor using Named Entity Recognition.

Being able to explain the problem, architecture, implementation choices, evaluation metrics, and trade-offs for these projects will significantly strengthen your NLP interview performance.