-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathToneAnalyzer.py
More file actions
75 lines (55 loc) · 2.4 KB
/
Copy pathToneAnalyzer.py
File metadata and controls
75 lines (55 loc) · 2.4 KB
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
67
68
69
70
71
72
73
74
# -*- coding: utf-8 -*-
"""
Created on Wed Jul 26 13:19:44 2017
@author: Sahir Noor Ali
@Code: Tone Analyzer
@Pre-Req: Make sure that the WDC SDK is installed before the code is executed.
The following command on the command prompt will ensure it:
pip install --upgrade watson-developer-cloud
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
#--------------------------------------------------------------
#Imports
#--------------------------------------------------------------
#The ToneAnalyzer class from WDC
from watson_developer_cloud import ToneAnalyzerV3
#Pandas for data handling
import pandas as pd
#Time class to measure the time taken
import time
#--------------------------------------------------------------
#Read the data
data = pd.read_csv('Reviews.csv')
#Make a copy of 100 rows for smaller testing
small_data = data.head(100).copy()
#To view the documentation
#print(help(ToneAnalyzerV3))
#-------------------------------------------------------------------------
#Instantiate TA Object with your Credentials
#-------------------------------------------------------------------------
tone_analyzer = ToneAnalyzerV3(
username='ABC',
password='XYZ',
version='2016-05-19',
url = 'https://gateway.watsonplatform.net/tone-analyzer/api')
#-------------------------------------------------------------------------
#Get the current time on the clock
time_start = time.clock()
#------------------------------------------------------------------------
#Iterate Over All the Reviews and Append the Result:
#------------------------------------------------------------------------
for index, review in small_data['Text'].iteritems():
#Pass a single review to TA (one by one):
json_output = tone_analyzer.tone(review, content_type='text/plain')
#Traverse the heirarchy of result
for i in json_output['document_tone']['tone_categories']:
for j in i['tones']:
#Append the attributes to the data
small_data.set_value(index, j['tone_name'], j['score'])
#------------------------------------------------------------------------
#Get the current time again and subract from
#previous to measure the time taken
time_end = time.clock() - time_start
#Print the time taken
print(time_end)
#Save the enriched data to another CSV File
small_data.to_csv('OutputTA.csv')