Skip to content
Tutorials Center
My learning

Topics

Lesson 28 of 63 ยท Text Classification

Sentiment Analysis

Build a lexicon-based sentiment scorer with negation and intensifiers, train a machine-learning classifier, and see where both fail, from sarcasm to tiny data.

Sentiment analysis asks whether a piece of text expresses a positive, negative or neutral opinion. Companies run it over product reviews, support tickets and social media to see how customers feel; researchers run it over news and tweets. It is the most famous text classification task, and a good one to study because it shows both what is easy about language (strong opinion words) and what is hard (negation, context, irony).

There are two classic approaches. A lexicon-based method uses a dictionary of words with sentiment scores and a few rules; it needs no training data and is completely transparent. A machine-learning method learns the weights from labeled examples, as in logistic regression on TF-IDF features. We build a small version of each and compare them.

Two ways to score a review

Lexicon Look words up in a scored dictionary and add them up.
Machine learning Learn word weights from labeled reviews.

Lexicons need no data; learned models adapt to your domain.

1 / 2

A lexicon-based scorer

The idea is simple: assign each opinion word a number (positive for praise, negative for criticism, bigger for stronger words), sum the numbers for a text, and read the sign. Our lexicon is tiny, with 25 words; real lexicons have thousands of entries and are built by annotators or by computing which words co-occur with praise and complaint.

The first scorer, score_simple, only sums. The second, score_rules, adds two common refinements. A booster such as very or extremely multiplies the next opinion word. A negator such as not or never within the three previous tokens flips the word and halves its strength: not good is negative but milder than bad, and not bad is mildly positive, not strongly. The n't contraction is split off first so that didn't is caught too.

Python Lexicon sentiment with negation and boosters

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Compare the two columns. For plain praise and complaint they agree. They differ exactly where language is not a bag of independent words. The food was not good. is +2.0 positive for the simple scorer and -1.0 negative once negation is handled; I didn't love it. goes from +3.0 to -1.5; Not bad at all. goes from -2.0 to +1.0; and The service was never rude. flips from -3.0 to +1.5. Boosters make Really great film, extremely fun! score 8.5 instead of 5. A text with no opinion words, It arrived on Tuesday., scores 0 and is labeled neutral.

The last sentence shows the limit. Oh great, another delay. Just what I needed. is sarcasm: the writer is annoyed, but every opinion word is positive, so both scorers say positive (+3.0). Sarcasm needs context and world knowledge (a delay is bad), and no word list captures that.

The negation window is a heuristic. Looking back three tokens catches was not good but also wrongly flips a word in no one said it was good, and it misses long-range negation. Real systems use parse information or learned models for harder cases.

A machine-learning classifier

The learned approach starts from labeled examples. We give a TF-IDF plus logistic regression pipeline 32 hand-written reviews (16 positive, 16 negative) and test it on 10 new ones that include negation. Two feature sets are compared: unigrams only, and unigrams plus bigrams, so that not good can become a feature of its own (see N-grams). Afterward we inspect a few weights.

Python Training a sentiment classifier

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The next step is to read the output as a whole, not only the accuracy. Both models get 9 of 10 test reviews right, and both miss the same one: I did not hate it, a double negative that the training data never covered. With 32 training examples this is expected, and it is a fair picture of the machine-learning weakness: the model knows only what the training data shows.

The weights are the other half of the story, and they are surprising. The unigram model gives good a weight of +0.01, practically zero, and gives bad a positive weight of +0.20, as if bad were praise. The reason is the training data: bad appears once, inside the positive review not bad at all, quite good in fact, and never in a negative one, while good appears in both a positive and a negative review (not good at all). A model without word order cannot represent negation, so it fits what it sees. The bigram model can learn not good (-0.13) and not bad (+0.13) as separate features, which is the right idea, but it still keeps a +0.13 weight on bad. With 32 reviews you get plausible accuracy and odd weights; expect bigrams and clean weights to pay off with thousands of reviews, not dozens. Reading the weights, as in the earlier lesson, is how you catch this.

Choosing between the approaches

Lexicon Machine learning
Training data None Needed (hundreds to millions of labels)
Transparency Every score is explainable Weights can be inspected
Domain fit Generic, may be wrong in your domain Learns your domain's vocabulary
Negation, context Hand-written rules Learned from examples (or from a neural model)
Setup cost Minutes Labeling effort

A sensible workflow is to start with a lexicon as a quick baseline, label a few hundred examples from your own domain, train a classifier on TF-IDF, and compare both with proper metrics. Modern systems often fine-tune a pre-trained transformer, covered in pre-trained models, which handles context far better. You will build a complete version in Project: Movie Review Sentiment Analyzer.

Where sentiment analysis goes wrong

  • Sarcasm and irony: Great, my phone died again looks positive to word-based methods.
  • Domain shift: a model trained on movie reviews can be wrong on medical text, where positive might mean a bad test result.
  • Comparisons and targets: The screen is great but the battery is awful mixes opinions about different aspects. Aspect-based sentiment analysis scores each target separately.
  • Neutral text: many sentences have no opinion; a two-class model forced to choose will guess.
  • Imbalanced labels: if 90% of reviews are positive, use precision, recall and F1, not accuracy.
  • Bias: models can attach sentiment to names, dialects or identity words that merely appear in training data.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a plain sum of word scores label "The food was not good." as positive?

    A bag of independent word scores ignores word order and context, so negation has no effect unless you add a rule or a feature for it.
  2. 2. In our rule-based scorer, what does a negator do to the next opinion word?

    The rule multiplies the value by -0.5, so "not good" becomes mildly negative and "not bad" mildly positive, rather than fully reversed.
  3. 3. Which sentence is hardest for both a lexicon and a bag-of-words classifier?

    The writer is annoyed, but the only opinion word, "great", is positive. Detecting sarcasm requires context and world knowledge, not just word weights.
  4. 4. What is a major advantage of a machine-learning sentiment model over a generic lexicon?

    A trained model adapts to the words that signal opinion in your data, for example "unpredictable" in thriller reviews, but it needs enough labeled examples to do so.

Summary

  • Sentiment analysis classifies the opinion expressed in text as positive, negative or neutral.
  • A lexicon scorer sums word scores; negation windows and boosters fix many errors without any training data.
  • A machine-learning classifier learns word weights from labeled reviews and adapts to its domain, but it only knows what its training data shows.
  • Bigrams let a linear model represent phrases like "not good", but they need enough data to help.
  • Sarcasm, domain shift, mixed opinions and neutral text remain hard; evaluate with the right metrics, not just accuracy.

Next, handle texts with more than one label in Multi-class and Multi-label Classification.