Emotion detection from text Python

kalebu

Jordan Kalebu

Posted on October 1, 2020

Emotion detection from text Python

Hi guys

In this tutorial, I will guide you on how to detect emotions associated with textual data and how can you apply it in real-world applications.

Understanding emotions associated with text is commonly known as sentiment analysis

Where is used?

You can apply it to perform analysis of customer feedback by directly reading them as either positive or negative feedback instead of manually reading to detect the emotions

Requirements

There variety of libraries in python which can be used for natural language processing tasks including emotions detection from text including

  • Natural Language Toolkit (NLTK)
  • Gensim.
  • polyglot.
  • TextBlob.
  • CoreNLP.
  • spaCy.
  • Pattern.
  • Vocabulary.

Well based on simplicity and ease of getting started I have chosen to go with textblob throughout a tutorial

TextBlob provides a simple API for diving into common natural language processing (NLP) tasks such as part-of-speech tagging, noun phrase extraction, sentiment analysis, classification, translation, and more.

The good thing I like about it is its simplicity in getting started with natural language processing tasks.

Installation

In Window
on a window just you use normal pip command

pip install textblob
python -m textblob.download_corpora
Enter fullscreen mode Exit fullscreen mode

In Linux use pip3 command for python3

pip3 install textblob 
python3 -m textblob.download_corpora
Enter fullscreen mode Exit fullscreen mode

Let’s get started

In performing textual analysis using textblob we first have to create a textblob object as shown below

>>>from textblob import TextBlob
>>>text = 'I had an awesome day'
>>>blob_text = TextBlob(text)
Enter fullscreen mode Exit fullscreen mode

Once you have created a textblob object you can now access tons of textblob methods to manipulate textual data.

For example un tagging part of speech of a text can be as simple as shown below

TextBlob tags () method

>>>from textblob import TextBlob
>>>text = 'I had an awesome day'
>>>blob_text = TextBlob(text)
>>>tags = blob_text.tags
>>>print(tags)
[('I', 'PRP'), ('had', 'VBD'), ('an', 'DT'),
('awesome', 'JJ'), ('day', 'NN')]

Enter fullscreen mode Exit fullscreen mode

TextBlob Sentiment ( )

In order to perform sentiment analysis using textblob we have to use the sentiment () method as shown below;

>>sentiment = blob_text.sentiment 
>>>print(sentiment)
    Sentiment(polarity=1.0, subjectivity=1.0)

Enter fullscreen mode Exit fullscreen mode

As we can see above as we call the sentiment () it returns a Textblob object Sentiment with polarity and subjectivity.

TextBlob Polarity

In building an emotion detector, we are more concerned with the polarity, therefore to get exactly polarity from the Sentiment object we have to get it as it’s an attribute

>>>polarity = sentiment.polarity
>>>print(polarity)
    1.0
Enter fullscreen mode Exit fullscreen mode

Note:
The polarity of the textual data ranges from -1 to 1, where negative polarity indicate negative emotions with -1 as mostly negative and vice versa

Use case (Demo Project)

Let’s assume we have our app which allows users to provide feedbacks If they like the user experience or not, and then we are going to use textblob to count negative feedbacks and negative feedbacks

from textblob import TextBlob

feedbacks = ['I love the app is amazing ', 
             "The experience was bad as hell", 
             "This app is really helpful",
             "Damn the app tastes like shit ",
            'Please don\'t download the app you will regret it ']

positive_feedbacks = []
negative_feedbacks = []

for feedback in feedbacks:
  feedback_polarity = TextBlob(feedback).sentiment.polarity
  if feedback_polarity>0:
    positive_feedbacks.append(feedback)
    continue
  negative_feedbacks.append(feedback)

print('Positive_feebacks Count : {}'.format(len(positive_feedbacks)))
print(positive_feedbacks)
print('Negative_feedback Count : {}'.format(len(negative_feedbacks)))
print(negative_feedbacks)
Enter fullscreen mode Exit fullscreen mode

Output :

Once you run the above code the below results with appear, the script with separate between negative and positive feedback given by the customer automatically as shown below

$ -> python app.py
Positive_feebacks Count: 2
['I love the app is amazing ', 'This app is really helpful']
Negative_feedback Count : 3
['The experience was bad as hell', 'Damn the app tastes like shit ', "Please don't download the app you will regret it "]
Enter fullscreen mode Exit fullscreen mode

Congratulations you performed emotion detection from text using Python, now don’t be shy share it will your fellow friends on twitter, social media groups.

Hope you find this Interesting, In case of anything comment, suggestion, or faced any trouble check it out on the comment box and I will get back to you as fast as I can.

The Original Article can be found on kalebujordan.dev

GitHub logo Kalebu / Emotion-text-analyzer-

A python program that detects emotions (sentiment) associated with texts

This project demostrates how to approach building a program that can help in analyzing emotions or sometimes known as sentiment associated with textual data.

Getting started

To get started with this script, you have to clone or download the repository just as shown below;

git clone https://github.com/Kalebu/Emotion-text-analyzer-
cd Emotion-text-analyzer-
Emotion-text-analyzer--> 
Enter fullscreen mode Exit fullscreen mode

Dependencies

In this project, I used textblob as natural language processing library(NLP),therefor you to make sure it installed before running.

pip install textblob
Enter fullscreen mode Exit fullscreen mode

textblob ?

With textblob you can detect the sentiment analysis of a text in just one line, just as shown below;

>>> from textblob import TextBlob
>>> TextBlob('cool just like this').sentiment.polarity
0.35
Enter fullscreen mode Exit fullscreen mode

Demo Project

The app.py demostrates how you can use that simplicity in real life by analyzing customer reviews or feebacks.

Examples of feedbacks

Feebacks Examples are included directly in the source code but in mostly…




💖 💪 🙅 🚩
kalebu
Jordan Kalebu

Posted on October 1, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related