I8 Β· Natural Language Processing in Practice

Sentiment Analysis

Sentiment analysis automatically identifies the opinion or emotion expressed in text, usually as positive, negative or neutral, sometimes as a score, a star rating or specific emotions such as anger or joy. Aspect-based sentiment goes further and finds the sentiment towards each feature, such as "battery: negative, camera: positive". Approaches range from lexicon-based rules, which add up the polarity of known words, through machine-learning classifiers on bag-of-words features, to fine-tuned transformer models.

15 min Intermediate Β· Sublevel Advanced Jump to the activity

What you need: The earlier topics of this module.

Definition

A new phone launches and within an hour there are 50,000 comments online. Are people happy, angry or joking? No team can read them all, but a sentiment model can sort them in seconds, if it understands "bas yahi kami thi πŸ™„" is not praise.

Sentiment analysis automatically identifies the opinion or emotion expressed in text, usually as positive, negative or neutral, sometimes as a score, a star rating or specific emotions such as anger or joy. Aspect-based sentiment goes further and finds the sentiment towards each feature, such as "battery: negative, camera: positive". Approaches range from lexicon-based rules, which add up the polarity of known words, through machine-learning classifiers on bag-of-words features, to fine-tuned transformer models.

In simple words

Think of a teacher reading feedback slips and sorting them into three piles: happy, unhappy and neither. Sentiment analysis does the sorting automatically, for thousands of slips at once.

Overview

  • Sentiment analysis detects the opinion or emotion expressed in text.
  • The simplest form classifies text as positive, negative or neutral.
  • Negation, sarcasm, emojis and code-mixed language make it hard.
  • Methods range from word lists (lexicons) to fine-tuned transformers.
  • Businesses use it to track customer opinion in reviews and social media.
  • Results should be checked by reading samples, because errors can be systematic.

Why it matters

Companies track product reviews and support chats, governments monitor public grievances, news organisations measure reactions and researchers study public opinion. Knowing how sentiment models work, and fail, helps you trust their numbers appropriately.

Key terms

Polarity
Whether text is positive, negative or neutral.
Sentiment lexicon
A list of words with polarity scores, such as "excellent" +3 and "awful" βˆ’3.
Aspect-based sentiment
Sentiment towards each feature mentioned in the text.
Negation handling
Recognising that "not good" reverses "good".
Sarcasm
Saying the opposite of what is meant, such as "great, another power cut".
Class imbalance
When one sentiment, often positive, dominates the data.

Types

Sentiment analysis comes in several types, from overall polarity to detailed emotions and aspects.

1. Polarity classification

Labelling text as positive, negative or neutral.

  • How it works: A classifier trained on labelled reviews outputs a class and a probability.
  • Example: "Delivery was quick and the food was hot" is positive.
  • When to use it: Use it to monitor overall satisfaction in reviews, surveys and tweets. Read a sample of predictions to check that negations and sarcasm are handled.

2. Fine-grained (rating) sentiment

Predicting a scale, such as 1 to 5 stars.

  • How it works: It is treated as multi-class or ordinal classification, or as regression on the rating.
  • Example: Estimating a star rating for comments written without one.
  • When to use it: Use it when strength of opinion matters, not just direction. Neighbouring ratings such as 3 and 4 are often confused, so judge errors by distance.

3. Aspect-based sentiment

Finding the sentiment towards each aspect mentioned in the text.

  • How it works: The model identifies aspects (price, battery, camera) and the opinion attached to each.
  • Example: "Great camera but the battery dies by noon": camera positive, battery negative.
  • When to use it: Use it for product teams who need to know exactly what to fix.

4. Emotion detection

Classifying emotions such as joy, anger, sadness, fear and surprise.

  • How it works: Models trained on emotion-labelled text, often with several labels allowed per message.
  • Example: Flagging angry customer-support messages for priority handling.
  • When to use it: Use it for support triage and mental-health research, with care for privacy. Emotion labels are subjective, so check agreement between human labellers first.

5. Method types: lexicon, machine learning and transformers

Rule-based word lists, classic classifiers and fine-tuned language models.

  • How it works: Lexicons such as VADER score words and handle negation and emphasis; TF-IDF with logistic regression is a strong baseline; fine-tuned transformers handle context and sarcasm best.
  • Example: A multilingual BERT model fine-tuned on Hinglish movie reviews.
  • When to use it: Start with a baseline and move to transformers when accuracy on hard cases matters.

Explained in detail

Three approaches

ApproachHow it worksStrengths and limits
Lexicon rulesAdd word scores, flip after negations, boost for "very" and "!!!"No training data needed and explainable; misses context and domain words
Classic machine learningTF-IDF or bag-of-words features with logistic regression or naive BayesFast and strong with labelled data; weak on unseen phrasing
Fine-tuned transformersPre-trained language model plus a classification headBest accuracy, handles context; needs more compute and is harder to explain

The VADER lexicon method, published by C. J. Hutto and Eric Gilbert in 2014, was built for social media: it gives each word a score from about βˆ’4 to +4, increases intensity for capital letters, exclamation marks and words like "extremely", flips polarity after negations and weights the part after "but" more heavily. A rule-based model like this is a good starting point when there is no labelled data. When there are a few thousand labelled examples, a logistic-regression classifier on TF-IDF features with bigrams usually beats it. A transformer fine-tuned on the same data usually wins again, by several percentage points, especially on subtle or mixed reviews.

Hard cases

Sarcasm ("wah, kya service hai, 3 ghante se wait"), mixed opinions ("great camera, terrible battery"), domain-specific words ("unpredictable" is good for a thriller but bad for a car's brakes), comparisons ("better than the last model, but still slow"), emojis and code-mixed Hinglish all confuse simple models. Ratings data is usually imbalanced, with many more positive reviews than negative, so a model can look accurate while missing most complaints; report per-class recall and the macro-averaged F1 score. Finally, sentiment scores across millions of posts are useful for trends, but individual predictions can be wrong, so avoid acting against a person on the basis of one automatic label.

  • Aspect-based sentiment first finds aspects such as "delivery" or "price" and then scores each, which is far more useful to a business than one overall score.
  • Emotion detection extends polarity to categories such as anger, fear, joy and sadness, useful for routing urgent complaints.

Labelling sentiment data is itself tricky: two careful people often disagree on 10 to 20 per cent of reviews, especially neutral or mixed ones. Writing short guidelines and measuring agreement first tells you the best accuracy any model can realistically reach.

How it works

  1. Define the labels: positive, negative, neutral, or a finer scale.
  2. Start with a lexicon baseline, then label a few thousand real examples.
  3. Train a TF-IDF classifier with bigrams; then try fine-tuning a transformer.
  4. Evaluate per-class recall and macro F1, including sarcastic and mixed examples.
  5. Report trends with uncertainty, and keep humans reviewing important cases.

In code

Neither method is perfect. The lexicon flips "bad" after "never" and correctly scores "never bad" as positive, but it calls the mixed review neutral. The small classifier labels "never bad" negative because it has never seen that phrase, a reminder to test tricky cases.

sentiment.py
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression

LEXICON = {"good": 2, "great": 3, "love": 3, "excellent": 3, "bad": -2, "terrible": -3, "slow": -1, "worst": -3}
NEGATIONS = {"not", "never", "no"}

def lexicon_score(text):
    score, flip = 0, False
    for w in text.lower().replace("!", " ! ").split():
        if w in NEGATIONS:
            flip = True
            continue
        s = LEXICON.get(w, 0)
        score += -s if flip else s
        flip = False if s else flip
    return score

train = ["great phone love it", "excellent camera", "battery is good", "fast delivery good packing",
         "terrible battery", "worst service ever", "screen is bad", "very slow and not good"]
y = ["pos", "pos", "pos", "pos", "neg", "neg", "neg", "neg"]
vec = TfidfVectorizer(ngram_range=(1, 2))
clf = LogisticRegression().fit(vec.fit_transform(train), y)

tests = ["not good at all", "great camera but terrible battery", "love the delivery", "never bad"]
for t in tests:
    print(f"{t:<34} lexicon={lexicon_score(t):+d}  model={clf.predict(vec.transform([t]))[0]}")

Output

not good at all                    lexicon=-2  model=neg
great camera but terrible battery  lexicon=+0  model=neg
love the delivery                  lexicon=+3  model=pos
never bad                          lexicon=+2  model=neg

Try it yourself

Listening to a city

A municipal corporation analyses 20,000 social-media posts a week about civic services.

Positive, negative or tricky?

Classify each review.

  • "Delivery was quick and the packing was neat"

  • "Stopped working after two days"

  • "Great screen but the speaker is awful"

  • "Wah, kya battery hai, 2 ghante me khatam"

Common mistakes

  • Removing negation words during cleaning.
  • Reporting accuracy on imbalanced data instead of per-class recall.
  • Treating one automatic label as a reliable judgement about a person.

Check yourself

Question 1

1. "Great camera, terrible battery" is best handled by…

Question 2

2. Why is sarcasm hard?

Question 3

3. 90% of reviews are positive. A model labels everything positive. Its accuracy is 90%, but…

Summary

  • Sentiment analysis labels opinion as positive, negative or neutral, or by aspect and emotion.
  • Lexicons need no data; classifiers and fine-tuned transformers learn from labelled examples.
  • Watch for negation, sarcasm, mixed opinions, code-mixing and class imbalance.

Finished reading?

Sources: Hutto and Gilbert (2014), VADER: a rule-based model for sentiment analysis of social media text Β· Jurafsky and Martin, Speech and Language Processing (3rd ed. draft)