OpenSource For You

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 recommenda­tions they make, their survey responses and other forms of social media text. Businesses can get feedback on how happy or dissatisfi­ed the customer is, and use this insight to gain a competitiv­e 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 programmin­g 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 requiremen­ts like processing large data sets and performing mathematic­al computatio­ns. 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 tokenisati­on, parsing, classifica­tion, stemming, tagging, semantic reasoning and other computatio­nal linguistic­s. 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 installati­on, 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 restaurant­s 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 SentimentA­nalyser

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 SentimentI­ntensityAn­alyzer

hotel_rev = [“Great place to be when you are in Bangalore.”, “The place was being renovated when I visited so the seating

 ??  ??

Newspapers in English

Newspapers from India