There are several Machine Learning and Deep Learning tools around to make sentiment analysis i.e. in bare words, the task to evalute a positive, negative or neutral feeling expressed in a text[1].

There are cloud and cognitive services like IBM Watson Alchemy Language or Microsoft Text Analytics API or the new entry, the Google Cloud Natural Language API.

There are computation frameworks that you can run on premisis like Python NLTK, Torch, Google TensorFlow. The last one is Facebook FastText[2].

But if you want to build things from the ground up, to understand how it works, you should start with something simpler, that let you train, classify, predict and test your ground truth[3]

And this is what we are going to do.

We will first active a virtualenv to let work with local installed Python packages. Let’s assume that this will be a boiler plate where everything we need will be under a `env` folder and nowhere else:

$ pip install virtualenv
$ virtualenv env
$ source env/bin/activate

Easy, isn’t it? You can check if your python version will now run in our boilerplate:

(env) $ which python
/env/bin/python
(env) $

Ok, if you see that congrats! Your virtualenv is properly set up. We can now start installing our sentiment analysis module finally! The Empathy Machines is a standalone NLP sentiment classifier written in Python and we will try it out then since it is ready to use. From the developer’s doc we know that we need some additional packages to be installed, so we will create a text file called requirements.txt, where we are going to put all our dependencies:

(env) $ mkdir sentiment
(env) $ cd sentiment
(env) $ cat >> requirements.txt
numpy>=1.11.1
scipy>=0.18.0
nltk>=3.2.1
scikit-learn>=0.17.1
empythy==0.5.6
^C

We can now ask pip program to install all these requirements:
(env) $ pip install --upgrade -r requirements.txt
Installing collected packages: numpy, scipy, empythy
Successfully installed empythy-0.5.6 numpy-1.11.1 scipy-0.18.0

So we can now write out our python program getsentiment.py

#!/usr/bin/env python

from empythy import EmpathyMachines;

nlp_classifier = EmpathyMachines()

print "training..."
nlp_classifier.train()

print "predicting..."
text="I feel very say today";
res=nlp_classifier.predict(text)
print "Sentiment for \"%s\" is %s" % (text,res)

This code will train the EmpathyMachines classifier instance against the default corpora, and will the do the prediction on our text:

(env) $ python getsentiment.py
training...
predicting...
Sentiment for "I feel very say today" is ['negative']

Wow, that’s cool, but what about adding some more interaction like a CLI?
So let’s modify the script adding some input parameters parsing with sys.argv

#!/usr/bin/env python

import sys
from empythy import EmpathyMachines;

if len(sys.argv) <= 1:
	print "\nUsage: python getsentiment.py input_text";
	exit();

nlp_classifier = EmpathyMachines()
print "training..."
nlp_classifier.train()

print "predicting..."
text=sys.argv[1]
res=nlp_classifier.predict(text)
print "Sentiment for \"%s\" is %s" % (text,res)

so now we can do some queries to our brand new sentiment api!

(env) $ python getsentiment.py "Hey I really love this day"
training...
predicting...
Sentiment for "Hey I really love this day" is ['positive']

Funny! We will see later how to turn this program into a public api, so that we can call it as a Web Service from the Internet.
In the meanwhile, star and fork this code from Tutorials

References

[1] Sentiment Analysis/Wikipedia

[2] FacebookReasearch FastText/Github

[3] Ground Truth/Wikipedia

Downloads
Sentiment Tutorials/Github