Text Emotions Detection with Machine Learning

A human can express his emotions in any form, such as face, gestures, speech and text. The detection of text emotions is a content-based classification problem. In this article, I will take you through how to solve the problem of text emotions detection with machine learning using Python.

Text Emotions Detection with Machine Learning

In machine learning, the detection of textual emotions is the problem of content-based classification, which is the task of natural language processing. Detecting a person’s emotions is a difficult task, but detecting the emotions using text written by a person is even more difficult as a human can express his emotions in any form.

Also, Read – 200+ Machine Learning Projects Solved and Explained.

Usually, emotions are expressed as joy, sadness, anger, surprise, hate, fear, etc. Recognizing this type of emotion from a text written by a person plays an important role in applications such as chatbots, customer support forum, customer reviews etc. In the section below, I will take you through a machine learning project on Text Emotions Detection using Python where I will build a machine learning model to classify the emotions of a text.

Text Emotions Detection using Python

For detecting emotions from the text, I will perform a few steps that will start with preparing the data. Then the next step will be tokenization where the textual data will be converted into tokens and from these tokens, we have to identify the emotional words.

These emotional words will be the keyword to classify the emotions of a text. Next, we’ll frame this task in such a way that a text will be taken as an input and the emoji that represents the emotions in that text is generated as the output.

Now let’s get started with the task of Text Emotions Detection with Machine Learning using Python. I will start this task by importing the necessary Python libraries and the dataset:

Number of instances: 7480

Now I will create two Python functions for tokenization and generating the features of an input sentence:

Now I will create a Python function to store the labels, our labels will be based on emotions such as Joy, Fear, Anger, and so on:

Now I will split the data into training and test sets:

Now I’m going to train four machine learning models and then choose the model that works best on the training and testing sets:

ClassifierTraining AccuracyTest Accuracy
SVC0.90675130.4512032
LinearSVC0.99883020.5768717
RandomForestClassifier0.99883020.5541444
DecisionTreeClassifier0.99883020.4552139

Detecting Emotion

Now, I’m going to assign an emoji to each label that is emotions in this problem, then I’ll write 4 input sentences, then I’ll use our trained machine learning model to take a look at the emotions of our input sentences:

joy       (1. 0. 0. 0. 0. 0. 0.)  1084
anger     (0. 0. 1. 0. 0. 0. 0.)  1080
sadness   (0. 0. 0. 1. 0. 0. 0.)  1079
fear      (0. 1. 0. 0. 0. 0. 0.)  1078
disgust   (0. 0. 0. 0. 1. 0. 0.)  1057
guilt     (0. 0. 0. 0. 0. 0. 1.)  1057
shame     (0. 0. 0. 0. 0. 1. 0.)  1045
This looks so impressive 😳
I have a fear of dogs 😱
My dog died yesterday 😢
I don't love you anymore..! 😂

The generated output is superb. This is how we can use machine learning for the task of detecting emotions from the text. This is an important task of human-machine interactions for anyone looking for a future in natural language processing. Hope you liked this article on text emotions detection with machine learning using Python. Please feel free to ask your valuable questions in the comments section below.

Thecleverprogrammer
Thecleverprogrammer
Articles: 75

2 Comments

Leave a Reply