Skip to content
Open

pull #173

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
27 changes: 27 additions & 0 deletions 2a_emotion_detection.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import requests
import json

def emotion_detector(text_to_analyze):
"""
This function sends a POST request to the Watson NLP Emotion Predict API
and returns the response text.
"""

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",
"Content-Type": "application/json"
}

payload = {
"raw_document": {
"text": text_to_analyze
}
}

try:
response = requests.post(url, headers=headers, json=payload)
return response.text
except Exception as e:
return f"Error: {str(e)}"
3 changes: 3 additions & 0 deletions 2b_application_creation.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
>>> print(result)
{"emotionPredictions":[{"emotion":{"anger":0.0043339236, "disgust":0.00037549555, "fear":0.0034732423, "joy":0.9947189, "sadness":0.012704818}, "target":"", "emotionMentions":[{"span":{"begin":0, "end":30, "text":"I am so happy I am doing this."}, "emotion":{"anger":0.0043339236, "disgust":0.00037549555, "fear":0.0034732423, "joy":0.9947189, "sadness":0.012704818}}]}], "producerId":{"name":"Ensemble Aggregated Emotion Workflow", "version":"0.0.1"}}
>>>
58 changes: 58 additions & 0 deletions 3a_output_formatting.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import requests
import json

def emotion_detector(text_to_analyze):
"""
Sends text to Watson NLP Emotion API and returns a formatted dictionary
with emotion scores and dominant emotion.
"""

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",
"Content-Type": "application/json"
}

payload = {
"raw_document": {
"text": text_to_analyze
}
}

try:
response = requests.post(url, headers=headers, json=payload)
response_dict = json.loads(response.text)

# Extract emotion scores
emotions = response_dict["emotionPredictions"][0]["emotion"]

anger = emotions["anger"]
disgust = emotions["disgust"]
fear = emotions["fear"]
joy = emotions["joy"]
sadness = emotions["sadness"]

# Find dominant emotion
emotion_scores = {
"anger": anger,
"disgust": disgust,
"fear": fear,
"joy": joy,
"sadness": sadness
}

dominant_emotion = max(emotion_scores, key=emotion_scores.get)

# Return formatted result
return {
"anger": anger,
"disgust": disgust,
"fear": fear,
"joy": joy,
"sadness": sadness,
"dominant_emotion": dominant_emotion
}

except Exception as e:
return {"error": str(e)}
3 changes: 3 additions & 0 deletions 3b_formatted_output_test.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
>>> print(result)
{'anger': 0.0043339236, 'disgust': 0.00037549555, 'fear': 0.0034732423, 'joy': 0.9947189, 'sadness': 0.012704818, 'dominant_emotion': 'joy'}
>>>
5 changes: 5 additions & 0 deletions 4b_packaging_test .txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
>>> from EmotionDetection import emotion_detector
>>> result = emotion_detector("I hate working long hours")
>>> print(result)
{'anger': 0.64949876, 'disgust': 0.03718168, 'fear': 0.05612277, 'joy': 0.00862553, 'sadness': 0.1955148, 'dominant_emotion': 'anger'}
>>>
29 changes: 29 additions & 0 deletions 5a_unit_testing.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
from EmotionDetection import emotion_detector


class TestEmotionDetector(unittest.TestCase):

def test_joy(self):
result = emotion_detector("I am glad this happened")
self.assertEqual(result["dominant_emotion"], "joy")

def test_anger(self):
result = emotion_detector("I am really mad about this")
self.assertEqual(result["dominant_emotion"], "anger")

def test_disgust(self):
result = emotion_detector("I feel disgusted just hearing about this")
self.assertEqual(result["dominant_emotion"], "disgust")

def test_sadness(self):
result = emotion_detector("I am so sad about this")
self.assertEqual(result["dominant_emotion"], "sadness")

def test_fear(self):
result = emotion_detector("I am really afraid that this will happen")
self.assertEqual(result["dominant_emotion"], "fear")


if __name__ == "__main__":
unittest.main()
6 changes: 6 additions & 0 deletions 5b_unit_testing_result.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
ct-emb-ai$ python3 test_emotion_detection.py
.....
----------------------------------------------------------------------
Ran 5 tests in 0.604s

OK
1 change: 1 addition & 0 deletions EmotionDetection/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from .emotion_detection import emotion_detector
58 changes: 58 additions & 0 deletions EmotionDetection/emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import requests
import json

def emotion_detector(text_to_analyze):
"""
Sends text to Watson NLP Emotion API and returns a formatted dictionary
with emotion scores and dominant emotion.
"""

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",
"Content-Type": "application/json"
}

payload = {
"raw_document": {
"text": text_to_analyze
}
}

try:
response = requests.post(url, headers=headers, json=payload)
response_dict = json.loads(response.text)

# Extract emotion scores
emotions = response_dict["emotionPredictions"][0]["emotion"]

anger = emotions["anger"]
disgust = emotions["disgust"]
fear = emotions["fear"]
joy = emotions["joy"]
sadness = emotions["sadness"]

# Find dominant emotion
emotion_scores = {
"anger": anger,
"disgust": disgust,
"fear": fear,
"joy": joy,
"sadness": sadness
}

dominant_emotion = max(emotion_scores, key=emotion_scores.get)

# Return formatted result
return {
"anger": anger,
"disgust": disgust,
"fear": fear,
"joy": joy,
"sadness": sadness,
"dominant_emotion": dominant_emotion
}

except Exception as e:
return {"error": str(e)}
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
# Repository for final project
# Project name - Final project
29 changes: 29 additions & 0 deletions test_emotion_detection.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import unittest
from EmotionDetection import emotion_detector


class TestEmotionDetector(unittest.TestCase):

def test_joy(self):
result = emotion_detector("I am glad this happened")
self.assertEqual(result["dominant_emotion"], "joy")

def test_anger(self):
result = emotion_detector("I am really mad about this")
self.assertEqual(result["dominant_emotion"], "anger")

def test_disgust(self):
result = emotion_detector("I feel disgusted just hearing about this")
self.assertEqual(result["dominant_emotion"], "disgust")

def test_sadness(self):
result = emotion_detector("I am so sad about this")
self.assertEqual(result["dominant_emotion"], "sadness")

def test_fear(self):
result = emotion_detector("I am really afraid that this will happen")
self.assertEqual(result["dominant_emotion"], "fear")


if __name__ == "__main__":
unittest.main()