Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions EmotionDetection/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import emotion_detection
16 changes: 16 additions & 0 deletions EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import requests
import json

def emotion_detector(text_to_analyze):
# Get emotion detect details
url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict'
header = {"grpc-metadata-mm-model-id": "emotion_aggregated-workflow_lang_en_stock"}
myobj = { "raw_document": { "text": text_to_analyze } }
response = requests.post(url, json = myobj, headers = header)

# Get Emotion Scores
emotions = json.loads(response.text)['emotionPredictions'][0]['emotion']

# Get Dominant Emotion
emotions['dominant_emotion'] = max(emotions, key=emotions.get)
return emotions
24 changes: 24 additions & 0 deletions server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from flask import Flask, request, render_template, url_for, redirect
from EmotionDetection.emotion_detection import emotion_detector

app = Flask(__name__)

@app.route('/emotionDetector', methods = ['GET'])
def emotion_detect():
text_to_analyze = request.args.get('textToAnalyze', '')
if not text_to_analyze:
return render_template('index.html')

emotions = emotion_detector(text_to_analyze)
response = f"For the given statement, the system response is "
response += f"'anger': {emotions.get('anger')}, "
response += f"'disgust': {emotions.get('disgust')}, "
response += f"'fear': {emotions.get('fear')}, "
response += f"'joy': {emotions.get('joy')}, "
response += f"'sadness': {emotions.get('sadness')}. "
response += f"The dominant emotion is {emotions.get('dominant_emotion')}"

return response

if __name__ == "__main__":
app.run(debug=True)
25 changes: 25 additions & 0 deletions test_emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import unittest
from EmotionDetection.emotion_detection import emotion_detector

class TestEmotionDetector(unittest.TestCase):
def test1(self):
results = emotion_detector('I am glad this happened')
self.assertEqual(results['dominant_emotion'], 'joy')

def test2(self):
results = emotion_detector('I am really mad about this')
self.assertEqual(results['dominant_emotion'], 'anger')

def test3(self):
results = emotion_detector('I feel disgusted just hearing about this')
self.assertEqual(results['dominant_emotion'], 'disgust')

def test4(self):
results = emotion_detector('I am so sad about this')
self.assertEqual(results['dominant_emotion'], 'sadness')

def test5(self):
results = emotion_detector('I am really afraid that this will happen')
self.assertEqual(results['dominant_emotion'], 'fear')

unittest.main()