In [1]:
import json
from watson_developer_cloud import ToneAnalyzerV3

Ask for credentials


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()


conversation username:
········
conversation password:
········

Create a ToneAnalyzer connector object


In [3]:
tone_analyzer = ToneAnalyzerV3(
   username=username,
   password=password,
   version='2016-05-19')

Function for parsing ToneAnalyzer JSON response


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))


{u'Anger': 0.118448, u'Joy': 0.666785, u'Fear': 0.044258, u'Sadness': 0.099872, u'Disgust': 0.090883}

Load textbooks.csv into a pandas dataframe


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]:
Unnamed: 0 asin reviewText title price
0 0 0070428077 There is no real formatting in this book, just... Machine Learning 50.27
1 1 0070428077 I read this book about 7 years ago while in th... Machine Learning 50.27
2 2 0070428077 If you are interested in the topic of machine ... Machine Learning 50.27
3 3 0070428077 This book serves as an excellent introduction ... Machine Learning 50.27
4 4 0070428077 Intelligent machines can be characterized, at ... Machine Learning 50.27

Process all reviews via ToneAnalyzer


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]:
Unnamed: 0 price joy anger fear sadness disgust
asin
0262193981 89.0 58.49 0.602187 0.103348 0.050392 0.339367 0.038366
1461471370 267.0 56.47 0.592355 0.075750 0.041199 0.184450 0.040237
1617290181 296.0 29.48 0.580712 0.118559 0.062267 0.266279 0.031640
1782161406 309.5 18.49 0.558162 0.108174 0.094054 0.437589 0.061358
0321321367 100.0 59.07 0.556551 0.133806 0.186068 0.294485 0.101630
0387952845 136.0 18.60 0.532165 0.109921 0.081381 0.234393 0.053619
1449367615 257.5 31.20 0.516575 0.138582 0.054572 0.255586 0.038545
1420067184 199.5 40.50 0.514658 0.123667 0.084937 0.456737 0.066764
0387310738 114.0 25.60 0.508823 0.143709 0.107897 0.336323 0.057385
0387848576 128.0 45.45 0.494179 0.092361 0.080211 0.206236 0.055056
0123748569 49.5 20.99 0.482101 0.085481 0.066819 0.260143 0.049576
1449358659 228.0 26.67 0.468830 0.095979 0.082347 0.458306 0.049835
0201157675 71.5 15.53 0.455495 0.074267 0.051183 0.181266 0.052204
0070428077 10.0 50.27 0.453624 0.119111 0.074131 0.263882 0.047276
1479324183 281.0 2.99 0.450030 0.082509 0.088774 0.276648 0.060684
0120884070 30.5 13.85 0.444960 0.084643 0.081928 0.291862 0.069567
1782162143 316.5 20.44 0.443479 0.131125 0.060903 0.276250 0.034436
1617291560 304.0 42.49 0.436357 0.093300 0.065419 0.274450 0.040101
1439810184 205.5 34.00 0.422965 0.166792 0.042912 0.156668 0.057470
1107422221 169.5 120.65 0.420399 0.108897 0.085122 0.252813 0.096077
111844714X 181.5 113.76 0.411284 0.182884 0.108055 0.290764 0.048933
0123747651 38.0 26.75 0.407968 0.092021 0.149078 0.312175 0.062388
0321246268 94.0 20.56 0.402369 0.246772 0.076671 0.528781 0.098681
1441998896 211.5 30.40 0.396361 0.138575 0.090078 0.261942 0.101475
1449303714 219.0 40.27 0.391862 0.195448 0.081838 0.471926 0.053419
1449361323 242.5 31.89 0.381402 0.095837 0.081755 0.309667 0.073352
026201243X 78.0 48.12 0.351389 0.222803 0.070772 0.324663 0.058890
0470526823 143.0 28.26 0.338254 0.176769 0.079936 0.349070 0.057814
0071344446 23.0 30.39 0.331174 0.102919 0.073207 0.378181 0.071350
0262013193 83.5 38.58 0.326654 0.137201 0.100862 0.251379 0.093282
0123814790 63.0 22.50 0.325618 0.085292 0.072206 0.267864 0.046464
061572499X 155.0 2.99 0.324619 0.210164 0.147587 0.366334 0.137660
111866146X 192.5 26.77 0.286817 0.154239 0.094443 0.278869 0.097225
0750676132 161.5 27.85 0.247697 0.125717 0.163543 0.262248 0.114711
0615684378 148.5 23.36 0.168138 0.108297 0.068295 0.401975 0.025670

In [ ]: