Analytics Vidhya
Published in

Analytics Vidhya

Aspect Based Sentiment Analysis

Photo by Blake Wisz on Unsplash

Introduction

We live in a world which is more opinionated than ever. Any service that we consume leaves us either satisfied or unsatisfied. And with the advent of social media, we make our views public in no time. Vast sources of data are available in the form of reviews, customer satisfaction surveys, customer complaints, etc. Businesses can use this data to understand what customers are talking about, and make data driven decisions to improve their services.

Let’s talk in terms of Machine Learning now!

Sentiment Analysis is the process of understanding how satisfied customers are w.r.t. a service. The level of satisfaction is generally measured across three classes (positive, negative or neutral). Although the number of classes could vary. When we have labeled data, we can use a plethora of machine and deep learning classification models. However, if the labeled data is not available, we would have to fall back to lexicon based methods. There are quite a few more ways of doing sentiment analysis in an unsupervised fashion, however, this post will cover only the supervised methods.

The USP of this post is Aspect Based Sentiment Analysis(ABSA). This is the most practical form of sentiment analysis, since in real life data, customers talk about multiple aspects of the service and the sentiment can be different for each aspect.

Eg. There was only one guy attending all the tables so we had to wait quite long. But the food was extremely tasty, and worth the money!

In this review for a restaurant, the customer talks about three aspects: waiting period, taste, value for money. Therefore, it doesn’t make sense to have just one sentiment for the whole review. Using Aspect based sentiment analysis, we can get a deeper understanding of customer satisfaction and actionable items for the restaurant, i.e. increase staff for reducing the waiting period.

Creating the data

For ABSA, we can define aspects depending on the domain of the dataset. For restaurant reviews, aspects can be taste, cost, location, service etc. For an e-commerce product review dataset, aspects can be delivery, packaging, product quality, customer service, price etc.

Once we have decided on our aspects, we can get some reviews labeled for each aspect. The data would look something like this:

Notice that some of the cells are left blank. This suggests that the review does not talk about that aspect.

Cleaning the text

The most important step in any NLP problem is preprocessing the text. This can include:

  • Lowercasing
  • Remove non-ASCII characters
  • Remove punctuation
  • Remove extra lines and spaces
  • Remove stop words
  • Stemming
  • Basic spell correction
  • Slang correction
  • etc.

Python packages like nltk and spacy can be used for the majority of steps. For spell correction, a simpler approach is to use a dictionary of spelling errors with their respective corrections. Another aspect of preprocessing is dealing with emojis and emoticons. For any other NLP task, emoticons might not be necessary. However, for sentiment analysis, they play a vital role. The emot package in python is a good choice to detect, remove or replace the emoji/emoticon with a reserved token.

Model

Looking at the data above, we have converted our problem to a multi-task classification problem. Each aspect refers to an individual task which in turn is a four class classification. The classes are: Neutral, Positive, Negative and Absent where absent suggests that the aspect is not present in the review.

The model architecture looks something like this:

Model architecture for Aspect Based Sentiment Analysis

In the model, I first pass the tokenised input through an embedding layer followed by a bidirectional LSTM. After this, I make 4 branches in my model, one for each aspect. Each branch has a self attention layer which gives more weightage to the words which describe the given aspect. You can try without the self attention layer as well. But my experiments showed that using attention in a localised manner for every aspect helped the model learn the patterns and ngrams written for each aspect. Here, I have used a simple dot product attention. This can be changed to many sophisticated attention methods (which I will be covering in my upcoming posts! )
We then follow up with 2–3 fully connected dense layers. The output of each branch is transformed using a softmax across 4 classes; positive, negative, neutral and absent.

While there can be many more models for aspect based sentiment analysis, I find this architecture to be simple yet effective. It not only eliminates the need for multiple models for every aspect, but also jointly trains the presence/absence of aspect along with its sentiment.

Conclusion

Sentiment Analysis is one of the most widely used applications of NLP. It leverages the data existing in vast amounts on public platforms. And provides useful insights to businesses helping them improve their services, and in turn increase customer satisfaction. Aspect based sentiment analysis is a step ahead of the conventional sentiment analysis. Models like BERT have proven to be very effective for sentiment analysis. However, practical application has taught me that simpler models can give a decent performance with much lesser compute requirements and training time. Moreover, when working with languages other than english, it is quite hard to find good pre-trained models!

I am an NLP enthusiast working in this industry for over 6 years now. However, my blogging journey has just begun. Please support by giving a clap on this post. Future posts will include topics like attention mechanisms from Bahdanau till Multi-Head and self attention; tokenisers, language models, Seq2Seq models, and so on (hopefully, the list would be endless…)

Cheers!

Eram

--

--

--

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Recommended from Medium

How Does a Bike-Share Navigate Speedy Success?

Loading Well Log Data From DLIS using Python

Open Government Data — How it Could be used

My Entry Into Data Science

Exploratory Data Visualization with Categorical Features

Kaggle X Avocado Prices (1/2)

6 Standardizations that are just too damn hard to get

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Eram Munawwar

Eram Munawwar

An NLP and Deep Learning enthusiast who has been in this field for over 6 years. My focus areas lie in MRC, NLG and NLU. Concepts over Jargon!

More from Medium

Increasing Accuracy of Sentiment Classification using Negation Handling

The Importance of Data Pre-processing in Sentiment Analysis

Gauging customer sentiment of unlabeled data using VADER

Email Spam Detection using NLP