Analysing Sentiments with NLTK
In this article, we explore ways to analyse sentiments from a given text and how some machine learning techniques can help in the process.
Sentiment analysis is used in opinion mining, business analytics and reputation monitoring. It helps businesses understand the customers’ experience with a particular service or product by analysing their emotional tone from the product reviews they post, the online recommendations they make, their survey responses and other forms of social media text. Businesses can get feedback on how happy or dissatisfied the customer is, and use this insight to gain a competitive edge.
In this article, we explore how to conduct sentiment analysis on a piece of text using some machine learning techniques. Python happens to be one of the best programming language choices when it comes to machine learning and textual analytics as it is easy to learn, is open source, and is effective in catering to machine learning requirements like processing large data sets and performing mathematical computations. Natural Language ToolKit (NLTK) is one of the popular packages in Python that can aid in sentiment analysis.
About NLTK
NLTK is an open source natural language processing (NLP) platform available for Python. It is capable of textual tokenisation, parsing, classification, stemming, tagging, semantic reasoning and other computational linguistics. NLTK is a community driven project and is available for use on Linux, Mac OS X and Windows.
Let’s first get started by installing NLTK to glue with Python using the following steps.
1. NLTK can be installed using Pip, a package management tool that Python users might be familiar with. Pip comes, by default, on Python version 2.7.9 and later. However, if you are using an older version of Python and don’t have Pip already installed, use the following command to do so.
On Ubuntu:
sudo apt-get install python-pip On Fedora Linux: sudo yum install python-pip 2. With Pip, install NLTK using the following command: sudo pip install –U nltk
This completes the NLTK download and installation, and you are all set to import and use it in your Python programs.
In this article, we will analyse sentiments from a piece of text using the NLTK sentiment analyser and the Naïve’s Bayes Classifier. As a sample, I’ve taken some user reviews on restaurants in Bengaluru from www.zomato.com as shown below.
“Great place to be when you are in Bangalore.”
“The place was being renovated when I visited so the seating was limited.”
“Loved the ambience, loved the food”
“The food is delicious but not over the top.”
“Service - Little slow, probably because too many people.” “The place is not easy to locate”
“Mushroom fried rice was tasty”
Analysis using NLTK Vader SentimentAnalyser
NLTK comes with an inbuilt sentiment analyser module – nltk.sentiment.vader—that can analyse a piece of text and classify the sentences under positive, negative and neutral polarity of sentiments. A code snippet of how this could be done is shown below:
import nltk from nltk.sentiment.vader import SentimentIntensityAnalyzer
hotel_rev = [“Great place to be when you are in Bangalore.”, “The place was being renovated when I visited so the seating