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) $

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

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

#!/usr/bin/env python

from empythy import EmpathyMachines;

nlp_classifier = EmpathyMachines()

print "training..."

print "predicting..."
text="I feel very say today";
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
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 input_text";

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

print "predicting..."
print "Sentiment for \"%s\" is %s" % (text,res)

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

(env) $ python "Hey I really love this day"
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


[1] Sentiment Analysis/Wikipedia

[2] FacebookReasearch FastText/Github

[3] Ground Truth/Wikipedia

Sentiment Tutorials/Github