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.
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
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
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..." 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
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
#!/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 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"
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