Sub topic 4
A Quick Baseline with a Sentiment Lexicon (VADER)
This chapter assumes basic Python, package installation with pip, and a basic understanding of training data, predictions, and labels. You will use VADER to score text without training a model.
What VADER does
VADER, or Valence Aware Dictionary and sEntiment Reasoner, is a lexicon-based sentiment tool. A lexicon is a dictionary that associates words and expressions with sentiment values. VADER is designed for short, informal text such as reviews, comments, and social posts.
VADER uses more than individual word scores. It also considers:
- Intensifiers, such as “very”
- Capital letters, such as “GREAT”
- Punctuation, such as repeated exclamation marks
- Contrast words, such as “but”
- Negation, such as “not good”
- Common emoticons and slang
Install the package in a terminal:
pip install vaderSentimentThe analyzer returns four scores:
| Score | Meaning |
|---|---|
| neg | Proportion of text with negative sentiment |
| neu | Proportion of text with neutral sentiment |
| pos | Proportion of text with positive sentiment |
| compound | Normalized overall score from -1 to 1 |
The three category scores usually add up to approximately 1. The compound score is usually the most convenient score for assigning one label.
Score text with VADER
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
posts = [
"The camera is excellent, and the battery lasts all day!",
"The delivery was late and the package was damaged.",
"The design is okay, but nothing special.",
]
for post in posts:
scores = analyzer.polarity_scores(post)
compound = scores["compound"]
if compound >= 0.05:
label = "positive"
elif compound <= -0.05:
label = "negative"
else:
label = "neutral"
print(f"{label:>8} {compound: .3f} | {post}")The thresholds of 0.05 and -0.05 are commonly used conventions, not universal laws. Choose thresholds based on your task and validate them against labeled examples.
A lexicon baseline is useful because it is fast, interpretable, and requires no training set. You can use it to inspect a new dataset, create an initial benchmark, or generate hypotheses before building a supervised classifier.
Understand the limits
VADER can struggle with domain-specific meanings, subtle context, sarcasm, and long passages. It may also miss the target when one sentence discusses several aspects with different opinions:
“Great screen, but the battery is awful.”
The overall score combines these signals. It does not separately label the screen and battery. That requires aspect-based sentiment analysis.
Use VADER as a baseline. Compare its predictions with human labels using accuracy, precision, recall, and F1. If errors are frequent or the vocabulary is specialized, a TF-IDF classifier or pretrained transformer may be a better next step.
Exercise
What label does the example program assign when the compound score is -0.03, and why?
Show answer
It assigns neutral because -0.03 is above the negative threshold of -0.05 and below the positive threshold of 0.05.