Labs ICT
⭐ Pro Login

Text Classification & Sentiment

Categorizing text and detecting emotions.

Text Classification & Sentiment Analysis

Text classification is one of the most practical NLP tasks. Given a piece of text, assign it a category. Is this email spam or not? Is this review positive or negative? Is this news about sports, politics, or tech?

It's the gateway drug to NLP β€” simple enough to learn quickly, powerful enough to solve real problems.

Bag of Words (BoW)

The simplest approach: count how many times each word appears, ignore order entirely. It's like putting all the words in a bag and shaking it.

"The cat sat on the mat" and "The mat sat on the cat" produce the exact same representation. Surprisingly, this works well for many tasks!


    Bag of Words Representation
    ──────────────────────────────────────────────
    β”‚ Vocabulary: [cat, mat, on, sat, the]       β”‚
    β”‚                                             β”‚
    β”‚ "The cat sat on the mat":                   β”‚
    β”‚   [1, 1, 1, 1, 2]  ← "the" appears twice  β”‚
    β”‚                                             β”‚
    β”‚ "The mat sat on the cat":                   β”‚
    β”‚   [1, 1, 1, 1, 2]  ← same vector!         β”‚
    β”‚                                             β”‚
    β”‚ "A dog chased the cat":                     β”‚
    β”‚   [1, 0, 0, 0, 1]  ← "dog" not in vocab   β”‚
    ──────────────────────────────────────────────
    

TF-IDF

Bag of Words treats all words equally, but "the" appears in almost every document while "quantum" is rare. TF-IDF fixes this by weighting words by their importance.

Term Frequency (TF): How often a word appears in a document. More frequent = more important to that document.

Inverse Document Frequency (IDF): How rare a word is across all documents. Rare words are more informative.


    TF-IDF Scoring
    ──────────────────────────────────────────────
    β”‚ TF(t,d) = Count of term t in doc d         β”‚
    β”‚           ─────────────────────────────     β”‚
    β”‚           Total terms in doc d              β”‚
    β”‚                                             β”‚
    β”‚ IDF(t) = log(Total docs / Docs with t)     β”‚
    β”‚                                             β”‚
    β”‚ TF-IDF = TF Γ— IDF                          β”‚
    β”‚                                             β”‚
    β”‚ Example:                                    β”‚
    β”‚   "the" appears 1000/1000 docs β†’ IDF β‰ˆ 0   β”‚
    β”‚   "quantum" appears 5/1000 docs β†’ IDF β‰ˆ 5.3β”‚
    β”‚                                             β”‚
    β”‚ "quantum" gets HIGH weight (informative)    β”‚
    β”‚ "the" gets LOW weight (common noise)        β”‚
    ──────────────────────────────────────────────
    

Sentiment Analysis

The most popular text classification task: is this text positive, negative, or neutral? It powers product reviews analysis, social media monitoring, and customer feedback systems.

Document-level: Is this whole review positive? Straightforward.

Sentence-level: Each sentence gets a sentiment. Useful for detailed analysis.

Aspect-based: "The food was great but the service was terrible." Food = positive, Service = negative. More nuanced and more useful.


    Aspect-Based Sentiment
    ──────────────────────────────────────────────
    β”‚ "The camera is amazing but battery life     β”‚
    β”‚  is disappointing and the price is fair"    β”‚
    β”‚                                             β”‚
    β”‚ β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”¬β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”     β”‚
    β”‚ β”‚ Aspect    β”‚ Sentiment  β”‚ Confidence β”‚     β”‚
    β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”Όβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€     β”‚
    β”‚ β”‚ Camera    β”‚ Positive   β”‚ 0.95       β”‚     β”‚
    β”‚ β”‚ Battery   β”‚ Negative   β”‚ 0.89       β”‚     β”‚
    β”‚ β”‚ Price     β”‚ Neutral    β”‚ 0.72       β”‚     β”‚
    β”‚ β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”΄β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜     β”‚
    ──────────────────────────────────────────────
    

Classification Algorithms

You can use many algorithms for text classification, from simple to complex:

Naive Bayes: Uses Bayes' theorem with the "naive" assumption that features are independent. Surprisingly effective for spam detection and simple classification. Fast, interpretable, works well with small data.

Logistic Regression: A linear model that's easy to train and interpret. Great baseline β€” you'd be surprised how often it beats complex models.

SVM: Support Vector Machines find the optimal boundary between classes. Works well in high-dimensional spaces (like text).

Neural Networks: Deep learning models that learn features automatically. Need more data but can capture complex patterns.


    Algorithm Comparison
    ──────────────────────────────────────────────
    β”‚ Algorithm     β”‚ Data Needed β”‚ Accuracy  β”‚   β”‚
    ──────────────────────────────────────────────
    β”‚ Naive Bayes   β”‚ Small       β”‚ Good      β”‚   β”‚
    β”‚ Logistic Reg  β”‚ Small       β”‚ Good      β”‚   β”‚
    β”‚ SVM           β”‚ Small-Med   β”‚ Very Good β”‚   β”‚
    β”‚ Random Forest β”‚ Medium      β”‚ Good      β”‚   β”‚
    β”‚ CNN/RNN       β”‚ Large       β”‚ Best      β”‚   β”‚
    β”‚ BERT          β”‚ Medium-Largeβ”‚ SOTA      β”‚   β”‚
    ──────────────────────────────────────────────
    

Evaluation Metrics

Accuracy alone can be misleading, especially with imbalanced classes:

Precision: Of all predicted positives, how many are actually positive? Important when false positives are costly (e.g., marking legitimate emails as spam).

Recall: Of all actual positives, how many did we catch? Important when false negatives are costly (e.g., missing cancer detection).

F1-Score: Harmonic mean of precision and recall. Balances both concerns.


    Confusion Matrix
    ──────────────────────────────────────────────
    β”‚                β”‚ Predicted +  β”‚ Predicted - β”‚
    ──────────────────────────────────────────────
    β”‚ Actual +       β”‚ True Pos     β”‚ False Neg   β”‚
    β”‚ Actual -       β”‚ False Pos    β”‚ True Neg    β”‚
    ──────────────────────────────────────────────
    β”‚                                             β”‚
    β”‚ Precision = TP / (TP + FP)                  β”‚
    β”‚ Recall    = TP / (TP + FN)                  β”‚
    β”‚ F1        = 2 Γ— (P Γ— R) / (P + R)          β”‚
    ──────────────────────────────────────────────
    

Practical Tips

Start simple. Use TF-IDF + Logistic Regression as your baseline. It's fast, interpretable, and often "good enough." Only move to deep learning if you need better performance and have enough data.

Always look at your data first. What's the class distribution? Are there patterns? What preprocessing helps? A good look at your data beats any fancy algorithm.

For sentiment analysis specifically, consider using pre-trained models from Hugging Face. They handle nuances like negation ("not bad"), sarcasm, and domain-specific language much better than traditional approaches.

πŸ§ͺ Quick Quiz

What does sentiment analysis determine?