Build sentiment classifiers in 10 minutes using textblob and python.

Build sentiment classifiers in 10 minutes using textblob and python.
4.8 (95.56%) 9 votes

It seems like making sentiment classifier for your self is tough but trust me its not so. With so much libraries present now a days its not at all tough to make these. You just need to know about the libraries you will need.

Today we will see how to build a sentiment classifier in 10 min. For this you need to know simple python programming and what are sentiment classifiers. Lets start

Let’s build sentiment classifiers  in 10 minutes

For this we will be using textblob, a library for simple text processing. It provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.

We will do this in separate python environment for this we need virtualenv. How to install virtualenv.

sudo pip install virtualenv

Now we have installed virtualenv next step is to create virtaul environment for our little project. Run the below command to create virtualenv.

virtualenv sent

sent is the name of the environment. Now we have created an environment. The above command will create an environment and install setup tools in it. Now we need to launch the environment. For this, run the below command

. sent/bin/activate

Now for installing textblob use below commands

pip install textblob
python -m textblob.download_corpora

The second command will download the data files that textblob uses for its functionality and for nltk. Now look at the below script which will do the sentiment classification for you.

from textblob import TextBlob
testimonial = TextBlob("What a wonderful day.")
print testimonial.sentiment.polarity

The result of the above script will be as below

Sentiment(polarity = 1.0, subjectivity = 1.0)

Here if polarity is less than 0 the sentence is of negative sentiment other wise the sentence holds the positive sentiment.

So it was really simple to make a sentiment classifier. Now lets say you want a customized classifier, means you want to train the classifier yourself. You can do the below.

train = [
     ('What an amazing weather.', 'pos'),
     ('this is an amazing idea!', 'pos'),
     ('I feel very good about these ideas.', 'pos'),
     ('this is my best performance.', 'pos'),
     ("what an awesome view", 'pos'),
     ('I do not like this place', 'neg'),
     ('I am tired of this stuff.', 'neg'),
     ("I can't deal with all this tension", 'neg'),
     ('he is my sworn enemy!', 'neg'),
     ('my friends is horrible.', 'neg')
 ]
test = [
     ('the food was great.', 'pos'),
     ('I do not want to live anymore', 'neg'),
     ("I ain't feeling dandy today.", 'neg'),
     ("I feel amazing!", 'pos'),
     ('Ramesh is a friend of mine.', 'pos'),
     ("I can't believe I'm doing this.", 'neg')
 ]
from textblob.classifiers import NaiveBayesClassifier
cl = NaiveBayesClassifier(train)
print cl.classify("This is an amazing library!")
# Lets test the accuracy of the classifier
print cl.accuracy(test)

Here we trained the classifier with our own data to make a modified classifier and then tested the accuracy of the classifier

Thus we build  sentiment classifiers in very small time and tested the accuracy of these classifiers.

Wanna read more about python stuff read the below articles.

Increasing Python Code performance

Python dependency security vulnerability checker


Gaurav Yadav

Gaurav is a Full Stack Web Developer and Blogger. Sportsperson by heart and loves football. He has experience with various frameworks in php, python and javascript. Loves to explore new frameworks and evolve with the trending technology.

Leave a Reply

Your email address will not be published. Required fields are marked *