-
Notifications
You must be signed in to change notification settings - Fork 1
/
watson.py
66 lines (47 loc) · 2.15 KB
/
watson.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import json, os
from os.path import join, dirname
from watson_developer_cloud import ToneAnalyzerV3 as ToneAnalyzer
from watson_developer_cloud import LanguageTranslatorV2 as LanguageTranslator
from dotenv import load_dotenv
# load service credentials from .env file
load_dotenv(os.path.join(os.path.dirname(__file__), "crd.env"))
ta_username = os.environ.get("TA_USERNAME")
ta_password = os.environ.get("TA_PASSWORD")
# initialize watson service
tone_analyzer = ToneAnalyzer(username=ta_username, password=ta_password, version='2016-05-19')
tr_username = os.environ.get("TR_USERNAME")
tr_password = os.environ.get("TR_PASSWORD")
language_translator = LanguageTranslator(username=tr_username, password=tr_password)
def get_language_translator_results(data):
''' translates results i.e. tweets from Arabic to English.
Note if tweets are already in English, this function takes no effect '''
return language_translator.translate(text=data,source='ar',target='en')
def get_tone_analyser_results(data):
''' analyses data (json obj) using watson tone analyser '''
# pass the data i.e. tweets to watson service
results = tone_analyzer.tone(text=data)
# return an object from a string representing a json object
results = json.dumps(results, indent=2)
# return string representing a json object from an object
results = json.loads(str(results))
return results
def display_emotion_tone(data):
''' takes tone analyser results (json obj) and '''
# results will be stored here
answers = []
# loop through watson's output
for index, catagory in enumerate(data['document_tone']['tone_categories']):
# we are only interested in these tones
target = ['Joy', 'Anger', 'Confident', 'Agreeableness', 'Emotional Range', 'Sadness']
for tone in catagory['tones']:
if tone['tone_name'] in target :
# store type as a KEY
_type = str(tone['tone_name'])
# here we would like to call the tone 'Emotional' rather than 'Emotional Range'
if _type == "Emotional Range":
_type = "Emotional"
# store score as a VALUE
_score = (str(round(tone['score'] * 100,1)) + "%")
temp = {str(_type) : str(_score)}
answers.append(temp)
return answers