In [1]:
import json
from watson_developer_cloud import ToneAnalyzerV3
In [2]:
# not the best approach here but it will do
import getpass
print("conversation username:")
username = getpass.getpass()
print("conversation password:")
password = getpass.getpass()
In [3]:
tone_analyzer = ToneAnalyzerV3(
username=username,
password=password,
version='2016-05-19')
In [4]:
def parse_toneanalyzer_response(json_data):
"""Parses the JSON response from ToneAnalyzer to return
a dictionary of emotions and their corresponding score.
Parameters
----------
json_data: {dict} a json response from ToneAnalyzer (see Notes)
Returns
-------
dict : a {dict} whose keys are emotion ids and values are their corresponding score.
Notes
-----
for an example of json see type pytest_data/tones_1.json
"""
emotions = {}
for entry in json_data['document_tone']['tone_categories']:
if entry['category_id'] == 'emotion_tone':
for emotion in entry['tones']:
emotion_key = emotion['tone_name']
emotion_value = emotion['score']
emotions[emotion_key] = emotion_value
return(emotions)
In [5]:
json_response = tone_analyzer.tone(text="I'm so pumped up by rocking that whole demo!")
print(parse_toneanalyzer_response(json_response))
In [6]:
from io import StringIO
import requests
import json
import pandas as pd
# @hidden_cell
# This function accesses a file in your Object Storage. The definition contains your credentials.
# You might want to remove those credentials before you share your notebook.
def get_object_storage_file_with_credentials_ed0baafd744e4452b0ef8e582e0f83a3(container, filename):
"""This functions returns a StringIO object containing
the file content from Bluemix Object Storage."""
url1 = ''.join(['https://identity.open.softlayer.com', '/v3/auth/tokens'])
data = {'auth': {'identity': {'methods': ['password'],
'password': {'user': {'name': 'member_536659497418808f634cd60a91b6c12706d9d46a','domain': {'id': 'c00ac61e4791413396fb2d1701473203'},
'password': 'u1a4)e~Do#-PEyj#'}}}}}
headers1 = {'Content-Type': 'application/json'}
resp1 = requests.post(url=url1, data=json.dumps(data), headers=headers1)
resp1_body = resp1.json()
for e1 in resp1_body['token']['catalog']:
if(e1['type']=='object-store'):
for e2 in e1['endpoints']:
if(e2['interface']=='public'and e2['region']=='dallas'):
url2 = ''.join([e2['url'],'/', container, '/', filename])
s_subject_token = resp1.headers['x-subject-token']
headers2 = {'X-Auth-Token': s_subject_token, 'accept': 'application/json'}
resp2 = requests.get(url=url2, headers=headers2)
return StringIO(resp2.text)
df_data_1 = pd.read_csv(get_object_storage_file_with_credentials_ed0baafd744e4452b0ef8e582e0f83a3('WatsonTest', 'textbook_reviews_formatted.csv'))
df_data_1.head()
Out[6]:
In [7]:
data_map = {}
for index,row in df_data_1.iterrows():
#print(row['reviewText'])
response = tone_analyzer.tone(text=row['reviewText'])
data_map[row[0]] = parse_toneanalyzer_response(response)
#print(data_map[row[0]])
In [9]:
df_data_1['joy'] = df_data_1['Unnamed: 0'].map(lambda x : data_map[x].get('Joy',0.0))
df_data_1['anger'] = df_data_1['Unnamed: 0'].map(lambda x : data_map[x].get('Anger',0.0))
df_data_1['fear'] = df_data_1['Unnamed: 0'].map(lambda x : data_map[x].get('Fear',0.0))
df_data_1['sadness'] = df_data_1['Unnamed: 0'].map(lambda x : data_map[x].get('Sadness',0.0))
df_data_1['disgust'] = df_data_1['Unnamed: 0'].map(lambda x : data_map[x].get('Disgust',0.0))
In [10]:
df_data_1.groupby('asin').mean().sort_values(by='joy', ascending=False)
Out[10]:
In [ ]: