Emotion Detection library
The Emotion Detection library uses Apache UIMA Ruta (RUle based Text Annotation) and a custom scoring model to detect emotions and sentiment in unstructured data, such as text from emails, instant messages, and voice transcripts.
- Anger
- Disgust
- Joy
- Sadness
- Fear
It assigns a score from 0-1 for each emotion. A higher value indicates a higher level of the emotion in the content. For example, an Anger score of 0.8 indicates that the anger is likely to be present in the text. A score of 0.5 or less indicates that anger is less likely to be present.
The library also detects the sentiment and indicates it as positive, negative, or neutral with a score of 0-1. For example, a positive sentiment score is 0.8 indicates that positive sentiment is likely expressed in the text. A score of 0.5 or less indicates that positive sentiment is less likely expressed in the text. The sentiment score is derived from the emotions present in the text.
How the library works
The solution uses dictionaries of emotions and rules to detect the emotions in text and a scoring model to score the emotions.
The dictionaries are contained in the anger_dict.txt, disgust_dict.txt, fear_dict.txt, joy_dict.txt, and sad_dict.txt files. Each dictionary is a collection of words that represent emotion in the text.
The rule file is based on Ruta Framework and it helps the system to annotate the text based on the dictionary lookup. For example, it annotates all the text that is found in the anger dictionary as Anger Terms. The position of this term is also captured. All the inputs are fed into the Scoring model to detect the sentence level emotions and also the document level emotion. The document level emotion is returned as the overall emotion at the document level.
The following code is an example of a rule definition.
PACKAGE com.ibm.sifs.analytics.emotion.types;
# Sample Rule
# load dictionary
WORDLIST anger_dict = 'anger_dict.txt';
WORDLIST joy_dict = 'joy_dict.txt';
# Declare type definitions
DECLARE Anger;
DECLARE Joy;
# Detect sentence
DECLARE Sentence;
PERIOD #{-> MARK(Sentence)} PERIOD;
MARKFAST(Anger, anger_dict, true);
MARKFAST(Joy, joy_dict, true);
# Same for other emotions
The emotion detection dictionary
Emotion detection is a java based library and is available as JAR. Currently, it is used in the Real-time Analytics component to detect the emotions in real time and score the emotions in the incoming documents.
- Initialize, which initializes the Emotion library by loading the dictionary and the rules. This function needs to be called only once, and must be started when dictionaries or rules are changed.
- Detect Emotion, which takes text as input and returns a JSON string as a response.

Definitions
public static void initialize(String dictionaryPath, String rulePath) throws Exception
public static String detectEmotion(String text)
Sample input
The investment you made with ABC company stocks are doing pretty good. It has increased
50 times. I wanted to check with you to see if we can revisit your investment portfolios for better
investments and make more profit. Please do check the following options and let me know. I can
visit you at your office or home or at your preferred place and we can discuss on new business.
Market is doing pretty good. If you can make right investments now, it can give good returns on
your retirement.
Sample response
{
"sentiment": {
"score": 1,
"type": "positive"
},
"emotions": {
"joy": "1.0",
"sad": "0.0",
"disgust": "0.0",
"anger": "0.0",
"fear": "0.0"
},
"keywords": {
"negative": [],
"joy": ["pretty", "retirement", "good", "profit"],
"sad": ["retirement"],
"disgust": [],
"anger": [],
"fear": ["retirement"]
},
"status": {
"code": "200",
"message": "success"
}
}
Starting the module
// Initialize Module (ONLY ONCE)
EmotionAnalyzer.initialize(<path to dictionaries>, <path to rule file>);
// Invoke the library (For every incoming document)
String resultJSON = EmotionAnalyzer.detectEmotion(text);
"status": {
"code": "200",
"message": "success"
}