Implementing Aspect Based Sentiment Analysis using Python
By: Rohan Goel
Tags: Stanfordnlp, Python, NLP

Aspect Based Sentiment Analysis also known as Feature Based Sentiment Analysis is a technique to find out various features, attributes, or aspects from a given text and their respective sentiments.

In this article, we will look at how we can implement ABSA using Python and various NLP tools such as StanfordNLP and NLTK.

The paper that I have referenced for the implementation of the code is published by Nachiappan Chockalingam in which he had explained everything in very detail about ABSA. I highly recommend you to first read this amazing paper and then jump onto the code, as it will make it very clear of what’s happening in the code.

Paper: Simple and Effective Feature Based Sentiment Analysis on Product Reviews using Domain-Specific Sentiment Scores

Let’s Get Started

Installing Essential Libraries

Open up your terminal and install the following Libraries

pip install pandas

pip install numpy

pip install nltk

pip install stanfordnlp

For PyTorch installation go onto this PyTorch website.

Importing Libraries

Open up your Jupyter Notebook and import the following libraries,

Now download the Stanford English model and some nltk tools that will be later used for extracting the Dependency relation in the text and other text preprocessing tasks.

Create a sample text review on which we will perform ABSA.

txt = "The Sound Quality is great but the battery life is very bad."

LowerCase the text and tokenize the Sentence.

Now for each sentence in the <sentList> tokenize it and perform POS Tagging and store it into a Tagged List.

Now there are many instances where a feature is represented by multiple words so we need to handle that first by joining multiple words features into a one-word feature.

Tokenize and POS Tag the new sentence.

Now we will use the Stanford NLP Dependency Parser to get the relations between each word.

Now we will select only those sublists from the <dep_node> that could probably contain the features.

Now using the <dep_node> list and the <featureList> we will determine to which of the words these features in the feature list are related to.

So as you can see we have got the feature words and for each word a list of words it is related to.

Now select only the feature Nouns List from the <fcluster>.

So with this, we have got the list of the features and their respective sentiment words within a sentence, now all you have to do is to check whether the sentiment word is positive, negative or neutral.

Full Code

Conclusion

So with this, we have seen a basic implementation of Aspect Based Sentiment Analysis using various NLP tools and techniques.

The code explained here can be very much improved by adding various NLP preprocessing techniques like Coreference Resolution, Slang Words Removal, Negation Handling, Sarcasm Detection, etc.

If you found this article useful do Clap and Share and feel free to ask any doubts regarding the article. Always open to any suggestions or ways to improve the code :)

and feel free to ask any doubts regarding the article. Always open to any suggestions or ways to improve the code :) Connect with me on LinkedIn or GitHub.

References

Updates and Fixes

If anyone is facing the errors with StanfordNLP module, try using this alternative module called “stanza” as mentioned in the comments.