Emotion Detection Model with Machine Learning

Detection of emotions means recognizing the emotional state of a person – for example, anger, confusion or deception on vocal and non-vocal channels. The most common technique analyzes the characteristics of the voice signal, with the use of words as additional input, if available. In this article, I will take you through am Emotion Detection Model with Machine Learning.

In this article, I will describe the recurrent architecture of the neural network for the detection of emotions in textual conversations, which participated in SemEval-2019 task 3 “EmoContext”, that is, an annual workshop on semantic evaluation. The goal of the task is to classify emotions (i.e. Happy, Sad, Angry and the like) into a conversational data set of 3 turns.

Also, Read – Contact Tracing with Machine Learning.

Emotion Detection Model with Machine Learning

The “EmoContext” focuses on the detection of contextual emotion in a text conversation. In EmoContext, given a textual USER statement with 2 turns of context in a conversation, we have to classify whether the emotion of the next USER statement is “happy”, “sad”, “angry” or “other”. 

For this task of building an emotion detection model with machine learning, I will start with loading, preprocessing and tokenizing the data:

Loading Word Embeddings

Word Embedding is a trained representation of a text in which words that have the same meaning have a similar representation. It is this approach to the representation of words and documents that can be considered as one of the key advances in machine learning on complex problems of natural language processing.

Now, I will create a function to load the word embeddings for our data:

Text Tokenization for Emotion Detection Model

Text Tokenization involves breaking a sequence of strings into pieces such as words, keywords, phrases, symbols, and other things called tokens. Tokens can be words, sentences, or even entire sentences. In the tokenization process, certain characters like punctuation marks are started.

Now, I will take our data through the process of text tokenization before building our emotion detection model with machine learning. The code below will do the text tokenization of our data:

Emotion Detection Model

I will use a deep learning model here which is known as Bidirectional LSTM model. In LSTM, the recurring cells are connected in a special way to avoid the problems of leakage and explosion gradient. Traditional LSTMs only keep information from the past because they only process the sequence one way. 

While bidirectional LSTM combines the output of two hidden LSTM layers moving in opposite directions, where one moves forward in time and the other moves back in time, thereby simultaneously capturing information from past and future states. Now let’s build our model:

Model Evaluation and Classification Report

Now let’s evaluate our emotion detection model and prepare a classification report. A classification report is used to measure the quality of the predictions of a classification task. Simply put, it’s a report that shows how many predictions are true and how many are false.

Now, let’s evaluate the performance of our machine learning model, and get the classification report for the emotion detection model:

f1_e 0.7313432835820894
precision_e 0.6583493282149712
recoll_e 0.8225419664268585
              precision    recall  f1-score   support

           0       0.97      0.93      0.95      2338
           1       0.64      0.76      0.69       142
           2       0.70      0.84      0.76       125
           3       0.65      0.87      0.74       150

   micro avg       0.91      0.91      0.91      2755
   macro avg       0.74      0.85      0.79      2755
weighted avg       0.92      0.91      0.92      2755Code language: CSS (css)

What is Competitive Programming?

The results are quite good and we can say that our model is now ready to be deployed for production. I hope you liked this article on Emotion Detection model with Machine Learning. Feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning.

Follow Us:

Thecleverprogrammer
Thecleverprogrammer
Articles: 77

Leave a Reply