TEXT CLASSIFICATION &
SENTIMENT ANALYSIS
A Complete Guide to
Understanding NLP Classification
Feature Engineering | ML Models | Sentiment Analysis | Evaluation Metrics
Dr. Ajay Prakash B V • AI & ML Department • Dr. Ambedkar Institute of Technology
Today's Roadmap — 9 Sections
0
1 Introduction to Text Classification
0
2 Real-Time Applications
0
3 Feature Engineering (BoW, TF-IDF, Embeddings)
0
4 Evaluation Metrics (Accuracy, Precision, Recall, F1)
0
5 Sentiment Analysis — Concepts & Definition
0
6 Types of Sentiment Analysis
0
7 Approaches (Lexicon-Based & ML)
0
8 Evaluation of Sentiment Models
0
9 Case Study — Aspect-Based Example
Text Classification & Sentiment Analysis 2 / 26
Introduction to
SECTION 01
Text Classification
What it is, why it matters, and how it works
What Is Text Classification?
Text Classification is the task of automatically assigning a predefined category (label) to a piece of text based on its
content. Given raw text, the system determines which class it belongs to from a fixed set of categories.
Types of Text Classification:
Binary Multi-Class
2 classes: Spam / Not Spam 3+ classes: Sports / Politics / Tech
Ex: Email filtering Ex: News categorization
Multi-Label Hierarchical
Multiple labels per doc Labels in tree structure
Ex: Movie: [Action, Comedy] Ex: Electronics → Phones
Text classification is the most widely-used NLP task — spam filters, news feeds, chatbots, and search engines all depend on it.
Text Classification & Sentiment Analysis 4 / 26
The Text Classification Pipeline — 5 Stages
→ → → →
Data Text Feature Model Evaluation
Collection Preprocessing Extraction Training & Deploy
Gather labeled text + Clean: lowercase, Convert text → Learn patterns (NB, Test accuracy, F1,
categories tokenize, stopwords numbers (TF-IDF) SVM, DT) deploy
Example — Spam Detection Pipeline:
Input: "Congratulations! You won a FREE iPhone! Click NOW!"
Preprocess → "congratulations won free iphone click" → TF-IDF → NB Model → SPAM
Text Classification & Sentiment Analysis 5 / 26
Real-Time Applications
SECTION 02 of Text Classification
How companies use text classification in practice
8 Major Applications — Industry Use Cases
Spam Detection Social Sentiment News Categorization Support Tickets
Gmail blocks 100M spam/day Brands monitor Twitter opinion Sports / Politics / Tech / Business Route to billing / logistics / tech
Document Org Medical Text Fake News Review Analysis
Extract sentiment from product
Classify contracts, invoices, records ICD codes, diagnosis, lab reports Flag misleading content at scale
reviews
McKinsey: NLP can automate 15-25% of knowledge worker tasks, saving millions of hours annually.
Text Classification & Sentiment Analysis 7 / 26
Feature Engineering
SECTION 03
for Text Data
Converting text into numbers that ML models can process
Bag of Words (BoW) — Count Word Occurrences
BoW represents each document as a vector of word counts. Each unique word becomes a column. Word order is completely
ignored — like throwing words into a bag.
Doc 1: "I love NLP" | Doc 2: "I love ML" | Doc 3: "NLP and ML are great"
I love NLP ML and are great
Doc 1 1 1 1 0 0 0 0
Doc 2 1 1 0 1 0 0 0
Doc 3 0 0 1 1 1 1 1
Cons: Ignores word order, common words dominate, very
Pros: Simple, intuitive, easy to implement, good baseline.
sparse.
N-grams — Capturing Word Sequences:
Unigram (N=1) Bigram (N=2) Trigram (N=3)
["I love", "love natural", "natural ["I love natural", "love natural
["I", "love", "natural", "language"]
language"] language"]
Text Classification & Sentiment Analysis 9 / 26
TF-IDF — The Gold Standard for Text Features
TF-IDF upweights rare, informative words and downweights common ones. It answers: "How important is this word to THIS
specific document relative to ALL documents?"
TF(t,d) = Count(t in d) / Total words in d
IDF(t) = log(Total documents / Documents containing t)
TF-IDF(t,d) = TF(t,d) × IDF(t)
Numerical Example (1000 documents):
Word Appears in IDF Meaning
"machine" 10 / 1000 docs log(1000/10) = 2.0 Rare → HIGH IDF → Important!
"data" 200 / 1000 docs log(1000/200) = 0.7 Moderate frequency → Medium IDF
"the" 990 / 1000 docs log(1000/990) ≈ 0.004 Very common → NEAR-ZERO IDF
TF-IDF is the default choice for traditional ML text classifiers (NB, SVM). It consistently outperforms raw BoW counts because it filters out
uninformative common words automatically.
Text Classification & Sentiment Analysis 10 / 26
Advanced: Word Embeddings & BERT
Word2Vec / GloVe Sentence Embeddings BERT (Contextual)
Same word gets DIFFERENT vectors based
Each word = dense vector (100-300 dims). Entire sentence = single vector.
on context.
Captures semantic similarity. Methods: Doc2Vec, Sentence-BERT.
"bank" (financial) ≠ "bank" (river).
king − man + woman ≈ queen Captures overall meaning.
State-of-the-art for classification.
Feature Methods Comparison:
Method Dims Semantics? Context? Best For
Bag of Words Very High No No Simple baselines
TF-IDF Very High Partial No Traditional ML
Word2Vec/GloVe 100-300 Yes No Similarity tasks
BERT 768 Yes Yes State-of-the-art
Text Classification & Sentiment Analysis 11 / 26
Evaluation Metrics for
SECTION 04 Text Classification
Measuring model performance beyond just accuracy
The Confusion Matrix — Foundation of All Metrics
Predicted: Positive Predicted: Negative TP: Correct positive
TN: Correct negative
Actual: Positive TP (True Positive) FN (False Negative)
FP: False alarm
Actual: Negative FP (False Positive) TN (True Negative) FN: Missed positive
Example: Spam detection on 100 emails (40 spam, 60 not spam)
Pred: Spam Pred: Not Spam From this matrix:
Accuracy = 92/100 = 92%
Actual: Spam TP = 35 FN = 5 Precision = 35/38 = 92.1%
Recall = 35/40 = 87.5%
Actual: Not Spam FP = 3 TN = 57 F1 = 2×(.921×.875)/(.921+.875) = 89.7%
Accuracy can be misleading! If 95% of emails are not spam, a model that always says 'not spam' gets 95% accuracy but catches ZERO spam.
Always check Precision, Recall, and F1.
Text Classification & Sentiment Analysis 13 / 26
Precision, Recall & F1-Score — Formulas & Meaning
Accuracy 92%
(TP + TN) / Total Overall correctness
Use when: Balanced classes
Precision 92.1%
TP / (TP + FP) Of predicted positive, how many correct?
Use when: Cost of false alarms is HIGH
Recall 87.5%
TP / (TP + FN) Of actual positive, how many found?
Use when: Cost of missing positives is HIGH
F1-Score 89.7%
2 × (P × R) / (P + R) Harmonic mean of P and R
Use when: Need balanced single metric
Text Classification & Sentiment Analysis 14 / 26
Sentiment Analysis
Concepts & Definition
SECTION 05
Identifying emotions and opinions in text
What Is Sentiment Analysis?
Sentiment Analysis (Opinion Mining) is the computational task of identifying the emotional
tone, opinion, or attitude expressed in text. It answers: "How does the author feel about
this subject?"
Why It Matters:
Brand Monitoring Market Research
Track opinion across millions of social posts Gauge customer satisfaction at scale
Political Analysis Product Insights
Monitor public opinion in real-time Identify what customers love/hate
4 Levels of Analysis:
Document Level Sentence Level Aspect Level Word Level
Text Classification & Sentiment Analysis 16 / 26
Types of
Sentiment Analysis
SECTION 06
Binary, Multi-Class, and Aspect-Based approaches
Binary & Multi-Class Sentiment Analysis
Binary Sentiment (2 classes) Multi-Class (3+ classes)
"Absolutely fantastic movie!" POSITIVE "Best phone ever! Love it!" 5★ Very Positive
"Terrible waste of time." NEGATIVE "Good, meets expectations." 4★ Positive
"Loved every moment!" POSITIVE "It's okay, nothing special." 3★ Neutral
"Worst product ever." NEGATIVE "Disappointed with quality." 2★ Negative
Aspect-Based Sentiment Analysis (ABSA) — Most Sophisticated
"The camera is excellent but battery life is terrible and it overheats during gaming."
Camera → POSITIVE Battery → NEGATIVE Heat Mgmt → NEGATIVE
Text Classification & Sentiment Analysis 18 / 26
Approaches for
Sentiment Analysis
SECTION 07
Lexicon-Based vs Machine Learning methods
Lexicon-Based Approach — Using Sentiment Dictionaries
Uses a pre-built dictionary where each word has a polarity score. No training data needed — just look up words and combine
scores.
Word Score Sentiment Popular Lexicon Tools:
excellent +3 Strongly Positive
VADER Best for social media, emojis
good +1 Positive
okay 0 Neutral TextBlob Polarity + subjectivity scores
bad −1 Negative
SentiWordNet WordNet-based, detailed scores
terrible −3 Strongly Negative
AFINN 2,477 words rated −5 to +5
Worked Example:
"The camera quality is excellent but the battery is terrible"
With ABSA: Camera → Positive (+3), Battery →
excellent (+3) + terrible (−3) = 0 → Neutral overall
Negative (−3)
Text Classification & Sentiment Analysis 20 / 26
Machine Learning Approach — Learning from Labeled Data
Treats sentiment analysis as supervised classification: train a model on labeled examples
(text + known sentiment), then predict sentiment for new text. Adapts to specific domains
and vocabularies.
Collect Preprocess Extract Train Evaluate
Labeled Data
→ Text
→ Features
→ Classifier
→ & Predict
Lexicon vs Machine Learning — Head-to-Head:
Criterion Lexicon-Based Machine Learning
Training data? Not needed Required (labeled)
Domain adaptability Limited High (learns domain language)
Accuracy Moderate (70-80%) Higher (85-95%)
Handles negation Difficult Learns from data patterns
Setup effort Very low (plug & play) Higher (data + training)
Best for Quick analysis, no data Production, high accuracy
Text Classification & Sentiment Analysis 21 / 26
Evaluation of
SECTION 08 Sentiment Models
Cross-validation, imbalance, and error analysis
Model Evaluation — Beyond Standard Metrics
Cross-Validation Dataset Imbalance
Amazon reviews: ~80% positive, ~20% negative. Model
Split data into k folds (typically k=5 or 10). Train on k−1 folds,
becomes biased toward majority class. Solutions: SMOTE
test on the remaining fold. Repeat k times and average
oversampling, undersampling, class weights, use F1 instead
results. Every data point is used for both training and testing.
of accuracy.
Common Error Types in Sentiment Analysis:
Sarcasm "Oh great, another update that breaks everything" Model sees 'great' → Positive
Negation "This product is not good" Model sees 'good' → Positive
Mixed
"Food was delicious but price outrageous" Conflicting signals cancel out
Sentiment
Implicit "Phone lasted 2 hours on full charge" No explicit negative words
Text Classification & Sentiment Analysis 23 / 26
Case Study —
SECTION 09
Aspect-Based Analysis
Analyzing a real product review with all approaches
Case Study: "The phone design is excellent but the battery
drains quickly."
VADER (Lexicon-Based) Aspect-Based Analysis
"excellent" → +0.75 Phone Design → POSITIVE
"but" → contrast signal Evidence: "excellent"
"drains quickly" → −0.45
Battery Life → NEGATIVE
Compound ≈ +0.25 → Weakly Positive Evidence: "drains quickly"
Approach Comparison:
Approach Prediction Aspect-Level? Actionable?
VADER Weakly Positive No Limited — hides battery issue
Binary ML Positive No Limited — overall only
Aspect-Based Design: +, Battery: − Yes High — knows exactly what to fix
Teaching Point: ABSA is the most valuable for businesses — overall sentiment hides critical insights. The manufacturer knows the design is
praised but battery needs improvement.
Text Classification & Sentiment Analysis 25 / 26
KEY TAKEAWAYS
Text Classification Assigns predefined categories to text — foundation of NLP applications
Feature Engineering BoW → TF-IDF → Word2Vec → BERT: each level captures more meaning
Evaluation Use Accuracy + Precision + Recall + F1 + Confusion Matrix together
Sentiment Types Binary (Pos/Neg) → Multi-class (5★) → Aspect-based (per feature)
Two Approaches Lexicon (no training, quick) vs ML (higher accuracy, needs data)
ABSA is King Aspect-based analysis gives the most actionable business insights
Error Awareness Sarcasm, negation, mixed sentiment — know your model's weaknesses
Thank You! Questions?