Identifying Emotions from Text

Written by on February 17, 2019 in Natural Language Processing, Programming, Python with 0 Comments

Identifying the predominant sentiment in unstructured text is used widely these days. There are several REST API services that allow you to submit a piece of text and get back the corresponding sentiment analysis. Meaningcloud, Aylien, Google’s Cloud Natural Language API, and  IBM Natural Language Understanding Service are just a few.

Emotion detection, especially from facial expressions and speech, is also catching up. Google and Microsoft have APIs to do this. However, for my article today, the focus is on detecting emotions from “unstructured text“, and I am going to look at IBM’s API as part of its Natural Language Understanding Service.

By the way, if you are interested in learning about the differences between “sentiment” and “emotion“, I encourage you to go through this article and this one.

The set of emotions detected by different APIs varies somewhat. IBM, for instance, detects:

  • Anger
  • Disgust
  • Fear
  • Joy
  • Sadness

Another company, ParallelDots detects the following emotions:

  • Angry
  • Bored
  • Excited
  • Fear
  • Happy
  • Sad
  • Sarcastic

The standard reference for the set of emotions is the work by Paul Ekman. This Wikipedia article gives interesting details on various emotion categories.

Getting back to today’s experiment, I decided to use IBM’s Python SDK instead of directly accessing their HTTPS end point.

The program itself is trivial:

Program to Dump Emotions

Program to Dump Emotions

The first sentence to try is:

“It was scary to drive alone on the highway.”

Here is the output from the program:

Identified Emotions

Identified Emotions

This shows that “fear” is the primary emotion. I am sure everyone will agree.

Here is the second sentence:

“I can’t wait to see the President in person!”

The program identifies the following emotion weightage:

Identified Emotions

Identified Emotions

In this case, the main emotion is “Joy“. Although this is as expected, I was hoping for a greater weightage for “Joy“, say, more than 0.7. It is not clear why it is just 0.32.

The next sentence is:

“The talk was dull and uninteresting. The audience was literally yawning throughout the program.”

Let us see how the program determines the emotions:

Identified Emotions

Identified Emotions

Interesting. “sadness” and “disgust” predominate, with almost equal weightage. This seems OK to me.

Let us try this one:

“Our whole family rejoiced when my son got the first prize.”

Obviously, this is a good example of “Joy” being the primary emotion. What does the program have to say?

Identified Emotions

Identified Emotions

Great. The program’s output agrees with our intuition.

Here is our last example:

“I don’t understand why our politicians are so arrogant.”

The output for this is:

Identified Emotions

Identified Emotions

So, “disgust” and “anger” predominate. Couldn’t agree more!

Using the SDK itself is quite straightforward, and with 30000 free NLU items per month, this service is definitely worth checking out. Next week, I plan to look at ParallelDots API for detecting emotions.

Bye, until then! Have a fantastic weekend!

Tags: ,


If you enjoyed this article, subscribe now to receive more just like it.

Subscribe via RSS Feed

Leave a Reply

Your email address will not be published. Required fields are marked *