Quick intro to Aspect-Based Sentiment Analysis

Determining the sentiment of a text with respect to a specific aspect

Fabio Chiusano
NLPlanet

--

Feedback icons created by Freepik on Flaticon.

Hello fellow NLP enthusiasts! We recently learned what sentiment analysis is, therefore it’s natural to talk about an evolution of it, i.e. aspect-based sentiment analysis. Enjoy! 😄

Aspect-Based Sentiment Analysis (ABSA), also known as fine-grained opinion mining, is the task of determining the sentiment of a text with respect to a specific aspect. Aspect-based sentiment analysis is a relatively new field of research that has emerged in response to the limitations of traditional sentiment analysis methods.

Traditional sentiment analysis methods treat a text as a whole and assign it a single sentiment label (e.g., positive, negative, or neutral). This is adequate for many tasks, but there are also many situations where it would be useful to know the sentiment of a text with respect to a specific aspect. For example, consider a review of a restaurant. The reviewer might give the restaurant a positive overall rating, but mention that the service was poor. In this case, it would be useful to know that the sentiment towards the aspect “service” is negative, even though the overall sentiment is positive.

ABSA applications

Some real-world applications of aspect-based sentiment analysis are:

  1. Monitoring customer satisfaction levels with a specific product or service. This can be done by tracking customer reviews and social media comments. By understanding what customers are saying about a company, businesses can make necessary changes to improve customer satisfaction.
  2. Monitor public opinion of companies (yours and your competitors). This can be done by tracking social media comments and customer reviews. By understanding what the public is saying, businesses can make necessary changes to improve their image.
  3. Understanding how employees feel about their job and company. This can be done by tracking employee reviews and social media comments. By understanding what employees are saying, businesses can make necessary changes to improve employee satisfaction.

Training an ABSA model

Aspect-based sentiment analysis is a difficult task because it requires the identification of “aspects” in text, as well as the assignment of sentiment labels to those aspects. There are a number of ways to approach ABSA, but one common approach is to first identify aspects in text, and then use an ABSA model to label the sentiment of each aspect.

Aspect identification is a task that can be approached in a number of ways, but one common approach is to use a rule-based method, like using a dictionary. For example, every time we find the words “iPhone X” or “MacBook Pro” we may consider them as aspects.

Once aspects have been identified, we need to train an ABSA classifier to classify sentiment of an aspect relative to a sentence as context. For example, we may use the classifier to get the sentiment of the aspect “service” in the context sentence “We had a great experience at the restaurant, food was delicious, but the service was kinda bad”.

There is a number of different approaches to building ABSA classifiers, but one common approach is to use a supervised machine learning method. In this case, we’d need a training dataset, i.e. a collection of texts that have been labeled with aspects and their sentiment. For example:

  • The sentence “We had a great experience at the restaurant, food was delicious, but the service was kinda bad”, with aspect “service” and label “negative”.
  • The sentence “We had a great experience at the restaurant, food was delicious, but the service was kinda bad”, with aspect “food” and label “positive”.

You can find datasets for ABSA here.

Fortunately, there are public pre-trained models as well! For example, you may try this DeBERTa fine-tuned over ABSA datasets. Let’s see how to use it.

Using a public pre-trained model

First, we install the transformers library along with the SentencePiece tokenizer (which is needed by some models of the library, such as DeBERTa).

Then we import the necessary libraries and load two different models:

Let’s make the following tests:

  • Compute the sentiment toward the aspect “food” from the context sentence “We had a great experience at the restaurant, food was delicious, but the service was kinda bad”. It results as “positive” with score ~0.997 .
  • Compute the sentiment toward the aspect “service” from the context sentence “We had a great experience at the restaurant, food was delicious, but the service was kinda bad”. It results as “negative” with score ~0.994 .
  • Compute the overall sentiment of the sentence “We had a great experience at the restaurant, food was delicious, but the service was kinda bad”. It results as “negative” with score ~0.770 .

Despite the context sentence having a negative overall sentiment, the ABSA model is able to correctly assign a positive sentiment to “food” and a negative sentiment to “service”.

Thank you for reading! If you are interested in learning more about NLP, remember to follow NLPlanet on Medium, LinkedIn, Twitter, and join our new Discord server!

--

--

Fabio Chiusano
NLPlanet

Freelance data scientist — Top Medium writer in Artificial Intelligence