1. Introduction
In this tutorial, we’ll explore aspect-based sentiment analysis (ABSA), a subfield of sentiment analysis that provides more detailed insights into text data.
Unlike traditional sentiment analysis, which determines the overall sentiment of a text, ABSA breaks down sentiments associated with specific aspects or features. We can use this technique in many areas such as customer feedback analysis, social media monitoring, and product reviews to extract more meaningful insights.
2. What Is Aspect-Based Sentiment Analysis?
In contrast to simple sentiment analysis, which treats a text as a whole and assigns a single sentiment label, ABSA allows the same text to have multiple different sentiments depending on the specific aspect being considered. An aspect refers to a specific feature or topic within a text that sentiment analysis evaluates separately.
For instance, let’s check this product review:
“The camera quality of this phone is amazing, but the battery life is disappointing.”
Traditional sentiment analysis might classify this as neutral due to mixed sentiments. However, ABSA identifies that “camera quality” is associated with a positive sentiment, while “battery life” has a negative sentiment.
There are three main components or steps in ABSA:
- Aspect extraction: identifying the aspects in the text, which are “camera quality” and “battery life” in the above example
- Sentiment classification: determining the sentiment of each aspect
- Aspect-sentiment pairing: linking sentiments with their respective aspects
To better understand the ABSA, we’ll present a simple example in Python.
3. How to Implement Aspect-Based Sentiment Analysis in Python?
We can implement ABSA using various approaches, from traditional NLP techniques to advanced deep-learning models. Let’s check out the most common ones:
- The lexicon-based approach relies on predefined sentiment dictionaries to determine the polarity of words related to specific aspects. While simple and interpretable, it doesn’t consider contextual sentiment
- Machine learning models can be trained on labeled datasets using supervised techniques such as support vector machines (SVM) or Naive Bayes. However, these models require extensive feature engineering
- Deep-learning models, such as transformed-based models BERT, RoBERTa, or DeBERTa, are more advanced. They can classify contextual sentiment more accurately but require time and resources for training
Further, instead of training models from scratch, we can leverage pretrained models from Hugging Face. Hugging Face is an open-source online platform that provides state-of-the-art machine learning models. On this platform, researchers and practitioners can share their models, datasets, and applications.
Hugging Face aims to democratize AI by making advanced machine-learning tools and resources accessible to a wider audience. Because of that, it’s often called the “GitHub for machine learning.”
3.1. Aspect-Based Sentiment Analysis with Pretrained Transformers
If we search for “ABSA” in Hugging Face, we’ll find that one of the most popular models available for this task is DeBERTa (Decoding-enhanced BERT with Disentangled Attention)—more precisely, DeBERTa v3 base, powered by PyABSA. This model is fine-tuned with over 180k examples from various ABSA datasets. It’s intended for training and inference on common ABSA datasets.
To use this model, we need to install the Hugging Face Transformers library. Also, we need to install the Spacy library for aspect extraction:
pip install spacy transformers
python -m spacy download en_core_web_sm
Now, let’s load libraries and the model in Python:
import spacy
from transformers import pipeline, AutoTokenizer, AutoModelForSequenceClassification
# load spacy model
nlp = spacy.load("en_core_web_sm")
# load pre-trained DeBERTa model for ABSA
model_name = "yangheng/deberta-v3-base-absa-v1.1"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForSequenceClassification.from_pretrained(model_name)
classifier = pipeline("text-classification", model=model, tokenizer=tokenizer)
Next, we define a function for aspect extracting. It will extract noun phrases from the text, like “camera quality” and “battery life.” After that, it checks if the phrase contains key grammatical roles for aspects, such as the nominal subject or direct object. If a noun phrase contains these grammatical roles, we’ll consider it an aspect.
In practice, we don’t always need to extract aspects from the text. Sometimes, we have a predefined list of phrases we are interested in. For example, we may want to check whether reviews on a website contain any sentiment about “camera” or “battery.” If a review doesn’t mention these terms, the model will return a neutral sentiment.
Here’s a function for aspect extraction:
def extract_aspects(text):
doc = nlp(text)
aspects = []
for chunk in doc.noun_chunks: # extract noun phrases
if any(token.dep_ in ("nsubj", "dobj") for token in chunk): # focus on key aspects
aspects.append(chunk.text) # store the full noun phrase
return aspects
Now, we define a function that analyzes the sentiment of every aspect in the given text:
def analyze_aspect_sentiment(text, aspects):
sentiment_results = {}
for aspect in aspects:
sentiment = classifier(text, text_pair=aspect)[0]['label'] # classify sentiment
sentiment_results[aspect] = sentiment
return sentiment_results
Finally, let’s apply these functions to our example sentence:
# example text
text = "The camera quality of this phone is amazing, but the battery life is disappointing."
# extract aspects
aspects = extract_aspects(text)
print("Extracted Aspects:", aspects)
# get sentiment for each aspect
aspect_sentiments = analyze_aspect_sentiment(text, aspects)
print("Aspect Sentiment Analysis:", aspect_sentiments)
After running the code above, we’ll get the following output:
Extracted Aspects: ['The camera quality', 'the battery life']
Aspect Sentiment Analysis: {'The camera quality': 'Positive', 'the battery life': 'Negative'}
As we can see, our code correctly extracted aspects from the text and classified their sentiment.
4. Applications of Aspect-Based Sentiment Analysis
ABSA is widely used in various industries where understanding opinions about specific aspects is important. Unlike traditional sentiment analysis, which provides a general sentiment for a text, ABSA allows businesses and researchers to extract more specific insights by linking sentiments to particular attributes.
ABSA helps businesses analyze customer reviews and feedback to identify strengths and weaknesses in their products or services. For example, a company can determine whether customers appreciate a smartphone’s camera but dislike its battery life, enabling targeted improvements.
We can use ABSA on social media to track public sentiment about specific aspects. Companies can use ABSA to study competitor products by analyzing user reviews and online discussions. This helps businesses understand what aspects customers value most and where competitors might be underperforming.
Hospitals and healthcare providers can analyze patient reviews to assess aspects like “doctor communication,” “waiting time,” and “treatment effectiveness,” leading to better patient care and service improvements.
Investors and analysts use ABSA to process financial news and reports to understand sentiment around some specific financial concepts.
5. Conclusion
In this article, we explored aspect-based sentiment analysis (ABSA), its components, applications, and a simple Python example.
ABSA helps businesses and researchers extract deeper insights from textual data by associating sentiments with specific aspects. While it has challenges, advancements in NLP and machine learning continue to improve its accuracy and usability.