Emotion Prediction Web App to Heroku Deployment
Tags: Sentiment Analysis, Python, Flask, NLP, Heroku

The dataset has 14000+ words with their translation in other languages and the emotion they represent. For our applicaation we only consider the English word and the emotions it represents.

required data

The emotions with which we deal in this problem are Positive, Negative, Anger, Anticipation, Disgust, Fear, Joy, Sadness, Surprise and Trust.

To begin with we load the dataset in pandas dataframe, and select the required columns.

database=pd.read_csv('data.csv')

database=database[['English(en)','Positive','Negative',\

'Anger','Anticipation',\

'Disgust', 'Fear', 'Joy', 'Sadness',\ Surprise','Trust']]

spaCy is a free, open-source library for advanced Natural Language Processing (NLP) in Python. We use this library for the tokenization of the raw text and its lemmatization. For our use we download the English small pretrained model. It assigns context-specific token vectors, POS tags, dependency parse and named entities.

The model can be downloaded by running the code:

python -m spacy download en_core_web_sm

For more information regarding spacy visit their website, https://spacy.io/usage/spacy-101

We load this model into our program and separeate out the stopwords.Stopwords are those words which do not carry any specific meaning of their own, they are used as fillers in sentences. So removing the stopwords from our raw text saves us computational time.

nlp=spacy.load('en_core_web_sm')

stopwords=nlp.Defaults.stop_words

The stopwords predefined in Spacy are,

Stopwords

We than split the raw text, using the builtin string method .split() which splits the sentence on ‘ spaces‘ and returns a list of words, and then remove the stopwords from the list of words and also remove the punctuations presents in the list.

txt=txt.split()

for i in txt:

if i in stopwords:

txt.remove(i)

if i in string.punctuation:

txt.remove(i)

The list of words are joined and then the sentence is given to the NLP model.

txt=' '.join(txt)

doc=nlp(txt)

Consider the sentence, ‘An apple a day keeps the doctor away.’ After performing the above mentioned operations we will have the output sentence as,

This doc object contains tokens, we are interested in tokens and their lemma (base words). i.e. if in our sentence we have the word running, its base word ‘run’ will be returned. Same will be for other words like ran, runs etc.

So we loop over the doc object to get each and every token , and compare each token with our dataset of words present to get the emotion present with it. For example for the word — Doctor we will get,

The word ‘Doctor’ shows or signifies emotions like Trust and Positive vibes. This is done for all the words in the sentence and a count of all the emotions occuring in the sentence is kept.

emotions=[]

for token in doc:

a=0

a=database.loc[database['English (en)']==token.lemma_]

a=a.values

a=a[:,1:]

a=sum(a.tolist(),[])

columns=['Positive', 'Negative', 'Anger', 'Anticipation',

'Disgust', 'Fear', 'Joy', 'Sadness', 'Surprise','Trust']

dictionary={}

for A,B in zip(columns,a):

dictionary[A]=B

for key, value in dictionary.items():

if value == 1:

emotions.append(key)

So for our above sentence we get the following output,

The count of each emotion is taken using Counter and it is visually represented using matplotlib’s barplot.

The x-axis represents the Emotions and y-axis reperesents the count of emotions occuring.