diff --git a/EmotionDetection/__init__.py b/EmotionDetection/__init__.py new file mode 100644 index 000000000..40cdfd16e --- /dev/null +++ b/EmotionDetection/__init__.py @@ -0,0 +1 @@ +from . import emotion_detection, test_emotion_detection \ No newline at end of file diff --git a/EmotionDetection/emotion_detection.py b/EmotionDetection/emotion_detection.py new file mode 100644 index 000000000..ec24849aa --- /dev/null +++ b/EmotionDetection/emotion_detection.py @@ -0,0 +1,28 @@ + +import requests +import json + + +def emotion_detector(text_to_analyze): + #def (): + url = 'https://sn-watson-emotion.labs.skills.network/v1/watson.runtime.nlp.v1/NlpService/EmotionPredict' + headers = {"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=headers) + # return response.text + formatted_response = json.loads(response.text) + final_response = { + 'anger': formatted_response["emotionPredictions"][0]['emotion']['anger'], + 'fear': formatted_response["emotionPredictions"][0]['emotion']['fear'], + 'joy': formatted_response["emotionPredictions"][0]['emotion']['joy'], + 'sadness': formatted_response["emotionPredictions"][0]['emotion']['sadness'], + 'disgust': formatted_response["emotionPredictions"][0]['emotion']['disgust'], + } + dominant_score, dominant_emotion = 0, '' + for key in final_response.keys(): + if final_response[key] > dominant_score: + dominant_score = final_response[key] + dominant_emotion = key + + final_response['dominant_emotion'] = dominant_emotion + return final_response \ No newline at end of file diff --git a/EmotionDetection/test_emotion_detection.py b/EmotionDetection/test_emotion_detection.py new file mode 100644 index 000000000..d8d79ca68 --- /dev/null +++ b/EmotionDetection/test_emotion_detection.py @@ -0,0 +1,8 @@ +from EmotionDetection import emotion_detection + +def test_emotion_detector(): + assert(emotion_detection.emotion_detector("I am glad this happened")['dominant_emotion'] == 'joy') + assert(emotion_detection.emotion_detector("I am really mad about this")['dominant_emotion'] == 'anger') + assert(emotion_detection.emotion_detector("I feel disgusted just hearing about this")['dominant_emotion'] == 'disgust') + assert(emotion_detection.emotion_detector("I am so sad about this")['dominant_emotion'] == 'sadness') + assert(emotion_detection.emotion_detector("I am really afraid that this will happen")['dominant_emotion'] == 'fear') diff --git a/server.py b/server.py new file mode 100644 index 000000000..eb7fd16e9 --- /dev/null +++ b/server.py @@ -0,0 +1,2 @@ +from Flask import flask +from \ No newline at end of file