Sub topic 13
Going Beyond One Label: Aspect-Based Sentiment
This chapter assumes you know basic Python, can install packages with pip, and understand training data, predictions, and evaluation metrics. It also assumes you have seen ordinary sentiment classification, where one text receives one label such as positive or negative.
Why one sentiment label is sometimes too broad
A review can discuss several parts of a product:
> The camera takes excellent photos, but the battery dies quickly and the screen is too dim.
A single label hides useful information. The review is positive about the camera and negative about the battery and screen.
Aspect-based sentiment analysis is a method that finds an aspect, or a specific topic in the text, and assigns sentiment to that aspect. An aspect might be a product feature, service attribute, or experience.
| Text span | Aspect | Sentiment |
|---|---|---|
| “excellent photos” | camera | positive |
| “dies quickly” | battery | negative |
| “too dim” | screen | negative |
This produces several aspect-sentiment pairs instead of one document-level label.
The working model
An aspect-based system usually performs three related tasks:
- Aspect extraction finds terms such as
camera,battery, andscreen. - Sentiment association connects nearby words or phrases to each aspect.
- Sentiment classification assigns a label such as positive, negative, or neutral.
Some systems use a fixed list of known aspects. Others discover aspect terms from labeled data. A fixed list is easier to control for a product catalog. Discovery is more flexible but can find irrelevant or inconsistent terms.
A practical representation is a list of records:
[
{"aspect": "camera", "sentiment": "positive"},
{"aspect": "battery", "sentiment": "negative"},
]The same aspect can appear more than once with different sentiments. For example, “The battery charges quickly but loses power fast” contains both positive and negative evidence about the battery.
A small rule-based example
The following example looks for known aspects and sentiment words in a nearby window. It is useful for understanding the data flow, not for production accuracy.
import re
ASPECTS = ["camera", "battery", "screen"]
POSITIVE = {"excellent", "great", "bright", "quickly", "good"}
NEGATIVE = {"dim", "slow", "awful", "dies", "poor"}
def aspect_sentiment(text, window=5):
tokens = re.findall(r"[a-z']+", text.lower())
results = []
for index, token in enumerate(tokens):
if token not in ASPECTS:
continue
nearby = tokens[max(0, index - window):index + window + 1]
positive_hits = POSITIVE.intersection(nearby)
negative_hits = NEGATIVE.intersection(nearby)
if len(positive_hits) > len(negative_hits):
sentiment = "positive"
elif len(negative_hits) > len(positive_hits):
sentiment = "negative"
else:
sentiment = "neutral"
results.append({
"aspect": token,
"sentiment": sentiment,
"evidence": sorted(positive_hits | negative_hits),
})
return results
review = "The camera takes excellent photos, but the battery dies quickly."
for item in aspect_sentiment(review):
print(item)This approach has important weaknesses. A nearby word is not always related to the aspect. It does not understand negation, so “not great” may be marked positive. It may also assign the same evidence to multiple aspects when several aspects occur in one sentence.
Choosing the right granularity
Decide what counts as an aspect before collecting labels. For a phone, camera may be sufficient, or you may need front camera, zoom, and low-light photos. More detailed aspects provide better insights but require more annotation work.
Keep aspect names consistent. Map synonyms such as display and screen to one canonical name when they mean the same thing. Store the original text span as well, because it helps with auditing and error analysis.
A pipeline for labeled data
For supervised learning, each example can contain text spans and labels:
A labeled record might identify the character offsets of battery, the category battery, and the sentiment negative. This supports models that use the full sentence rather than a simple word window.
Common errors and evaluation
Check errors by aspect and by sentiment. A model may perform well on shipping because that aspect has many examples, but poorly on rare aspects such as warranty. Report precision, recall, and F1 for each important aspect or aspect-sentiment pair.
Also inspect sentences containing:
- Negation: “The camera is not sharp.”
- Contrast: “The screen is bright, but the battery is weak.”
- Comparisons: “The new model is faster than the old one.”
- Multiple mentions: “The first battery was poor, but the replacement is excellent.”
Exercise
Why can a single positive or negative label be misleading for a review that discusses several product features?
Show answer
The label combines opinions about different aspects, so it cannot show which features customers liked or disliked. Aspect-based sentiment keeps a separate sentiment label for each aspect.