Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import json
- def emotion_detector(text_to_analyze):
- 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"}
- input_json = { "raw_document": { "text": text_to_analyze } }
- response = requests.post(URL, json = input_json, headers=header)
- formated_response = json.loads(response.text)
- if response.status_code == 200:
- return formated_response
- elif response.status_code == 400:
- formated_response = {
- 'anger': None,
- 'disgust': None,
- 'fear': None,
- 'joy': None,
- 'sadness': None,
- 'dominant_emotion': None}
- return formated_response
- def emotion_predictor(detected_text):
- if all(value is None for value in detected_text.values()):
- return detected_text
- if detected_text['emotionPredictions'] is not None:
- emotions = detected_text['emotionPredictions'][0]['emotion']
- anger = emotions['anger']
- disgust = emotions['disgust']
- fear = emotions['fear']
- joy = emotions['joy']
- sadness = emotions['sadness']
- max_emotion = max(emotions, key=emotions.get)
- #max_emotion_score = emotions[max_emotion]
- formated_dict_emotions = {
- 'anger': anger,
- 'disgust': disgust,
- 'fear': fear,
- 'joy': joy,
- 'sadness': sadness,
- 'dominant_emotion': max_emotion
- }
- return formated_dict_emotions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement