Human beings can easily perceive the emotions from any texts and experience it naturally. But what about the machines, are they able to detect the emotions from a text as we do? Text to Emotion API let’s you find the appropriate emotions embedded in the text data automatically using a few lines of code and a simple API.
How it works
Text to Emotion API uses complex NLP algorithms to mine underlying emotions from a given text. Basically the flow is as follows:
- Remove the unwanted textual part from the message.
- Perform the natural language processing techniques.
- Bring out the well pre-processed text from the text pre-processing.
- Find the appropriate words that express emotions or feelings.
- Check the emotion category of each word.
- Store the count of emotions relevant to the words found.
The output of the endpoint is a JSON dictionary with keys as emotion categories (happy, surprise, angry, sad, fear) and values as emotion scores. The score is a value between 0.00 to 1.00. The higher the value, the more likely our text to express that emotion.
Sample
For example; let’s take the following famous speech from Hamlet (W.Shakespeare). It is a complicated and emotional speech and the API is capable of showing the mixed feelings of Prince Hamlet:
To be, or not to be, that is the question: Whether 'tis nobler in the mind to suffer The slings and arrows of outrageous fortune, Or to take Arms against a Sea of troubles, And by opposing end them: to die, to sleep;
The simple cURL command to perform the text to emotion analysis is:
curl --location --request POST 'https://api.apilayer.com/text_to_emotion' \
--header 'apikey: YOUR API KEY' \
--data-raw 'To be, or not to be, that is the question:
Whether '\''tis nobler in the mind to suffer
The slings and arrows of outrageous fortune,
Or to take Arms against a Sea of troubles,
And by opposing end them: to die, to sleep;'
And the response from the API is:
{
"Happy": 0.14,
"Angry": 0.0,
"Surprise": 0.29,
"Sad": 0.29,
"Fear": 0.29
}
More code samples (for more programming languages) are available on the Live Demo section right above.
How accurate is the API?
Human emotions are too complex for computers to identify. Even humans are easily mistaken by the speech especially when sarcasm involves. Without mimics and the background information, it is sometimes impossible to predict the emotions accurately. But text to emotion API will perform above the mentioned expectations in the most cases. Just subscribe to this API, give it a try for your use case (it is free) and see if it works for you.
Handling of Emojis
The API is able to identify the emotion from the emojis which describes human behavior. Let’s take an example:
curl --location --request POST 'https://api.apilayer.com/text_to_emotion' \
--header 'apikey: YOUR API KEY' \
--data-raw 'It\'s not funny. This must be a joke! 😡🤬'
And the response from the API is:
{
"Happy": 0.33,
"Angry": 0.67,
"Surprise": 0.0,
"Sad": 0.0,
"Fear": 0.0
}
Textually, there is a chance of being a joke but looking at the emojis, the API correctly classifies the emotion of the text as anger.
Use cases
The API is mostly useful for the following industrial use cases:
- Customer Engagement: The signals that will be provided by the text to emotion API can help customer services teams to understand and interact with their clients more appropriately. It helps to speed up the process of understanding customer needs and complaints.
- Customer support with chatbots: Customer support with chatbots: Chatbots help companies to provide 24-hour support to their users. By enabling emotion detection, chatbots will be able to provide more human-like interaction which will help to increase customer satisfaction.
- Social Media Monitoring: Keeping a close eye on the social media posts will help brands to take action before small problems happen to be a crisis.
How is Emotion Detection different from Sentiment Analysis?
Sentiment Analysis helps you to understand the polarity of a given text by labeling them as positive, negative or neutral. Emotion detection takes it to a more granular level by bringing out the real feelings that the user feels while typing their content. Anger and fear will be categorized as negative sentiment by Sentiment Analysis but they definitely require different communication approaches. Based on your needs, you can also give a look to the Sentiment Analysis API.