Sub topic 1
What Sentiment Analysis Is and How to Frame It
Sentiment analysis classifies text according to the opinion or emotion it expresses. For example, a product review might be positive, negative, or neutral. A social post might express strong dissatisfaction even when it contains no explicit word such as “bad.”
At the intermediate level, the main challenge is not calling a model. It is defining the prediction task precisely. A model can only learn the labels and rules represented in its data.
Prerequisites
This chapter assumes basic Python programming, familiarity with lists and dictionaries, and a basic understanding of supervised learning. You should know that training data contains examples used to fit a model, while test data is used to measure predictions on unseen examples.
Define the Unit of Analysis
First decide what one prediction represents. This is the unit of analysis.
Common choices include:
- One complete product review
- One sentence from a review
- One social media post
- One customer-support message
- One sentence about a particular product feature
The choice changes the meaning of the label. Consider this review:
> The camera is excellent, but the battery is terrible.
A single review-level label forces you to combine two opinions. Sentence-level analysis preserves more detail, but it may lose context from nearby sentences.
Also define the prediction time. If you classify a post before a human responds, the input should not include later replies or edits. This prevents information leakage, which occurs when training or prediction uses information that would not be available in the real application.
Choose a Label Scheme
A label scheme specifies the possible outputs and what each output means. A simple three-class scheme is:
| Label | Meaning | Example |
|---|---|---|
| positive | The author expresses a favorable opinion | “The delivery was fast and reliable.” |
| negative | The author expresses an unfavorable opinion | “The package arrived damaged.” |
| neutral | The text has no clear favorable or unfavorable opinion | “The package arrived on Tuesday.” |
Binary classification uses only positive and negative labels. Multiclass classification uses three or more labels. You might also use a five-point scale, such as very negative through very positive, but annotators must consistently distinguish neighboring levels.
Write decision rules before collecting labels. For example, decide whether a factual statement, a mixed opinion, or a question belongs to the neutral class. Record how to handle quoted text, comparisons, and posts that contain several opinions.
def assign_label(text):
"""Apply a small, explicit policy for a three-class task."""
lower = text.lower()
if "excellent" in lower or "love" in lower:
return "positive"
if "terrible" in lower or "hate" in lower:
return "negative"
return "neutral"
examples = [
"I love the delivery speed.",
"The packaging is terrible.",
"The order arrived Tuesday.",
]
for example in examples:
print(assign_label(example), example)This function is a policy demonstration, not a useful sentiment model. Real text requires labeled examples and a method that can generalize beyond exact keywords.
Separate Sentiment from Other Tasks
Sentiment is not the same as topic, emotion, or intent. “The battery lasts two days” is about a battery, but it does not clearly express approval or disapproval. “I am furious” expresses emotion. “Can I return this?” expresses an intent.
Define what your system should do when a text contains no sentiment. If neutral examples matter in production, include them during training and evaluation instead of treating every text as positive or negative.
Exercise
Exercise
A review says, “The screen is beautiful, but the battery dies quickly.” Should a three-class review-level task automatically label it positive or negative?
Show answer
No. It is a mixed opinion. Define a policy first, such as assigning the dominant opinion, adding a mixed label, or splitting the review into aspect-level units.