-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo.py
96 lines (79 loc) · 2.89 KB
/
Demo.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import sys
import requests
import json
import operator
import twitter
import script as pi
from watson_developer_cloud import PersonalityInsightsV2 as PersonalityInsights
#This function is used to receive and analyze
#the last 200 tweets of a Twitter handle using
#the Watson PI API
def analyze(handle):
#The Twitter API credentials
twitter_consumer_key = ''
twitter_consumer_secret = ''
twitter_access_token = ''
twitter_access_secret = ''
#Invoking the Twitter API
twitter_api = twitter.Api(consumer_key=twitter_consumer_key,
consumer_secret=twitter_consumer_secret,
access_token_key=twitter_access_token,
access_token_secret=twitter_access_secret)
#Retrieving the last 200 tweets from a user
statuses = twitter_api.GetUserTimeline(screen_name=handle, count=200, include_rts=False)
#Putting all 200 tweets into one large string called "text"
text = ""
for s in statuses:
if (s.lang =='en'):
text += s.text.encode('utf-8')
#Analyzing the 200 tweets with the Watson PI API
pi_result = PersonalityInsights(username=pi.u, password=pi.p).profile(text)
#Returning the Watson PI API results
return pi_result
#This function is used to flatten the result
#from the Watson PI API
def flatten(orig):
data = {}
for c in orig['tree']['children']:
if 'children' in c:
for c2 in c['children']:
if 'children' in c2:
for c3 in c2['children']:
if 'children' in c3:
for c4 in c3['children']:
if (c4['category'] == 'personality'):
data[c4['id']] = c4['percentage']
if 'children' not in c3:
if (c3['category'] == 'personality'):
data[c3['id']] = c3['percentage']
return data
#This function is used to compare the results from
#the Watson PI API
def compare(dict1, dict2):
compared_data = {}
for keys in dict1:
if dict1[keys] != dict2[keys]:
compared_data[keys] = abs(dict1[keys] - dict2[keys])
return compared_data
#The two Twitter handles
user_handle =
celebrity_handle = "@IBM"
#Analyze the user's tweets using the Watson PI API
user_result = analyze(user_handle)
celebrity_result = analyze(celebrity_handle)
#Flatten the results received from the Watson PI API
user = flatten(user_result)
celebrity = flatten(celebrity_result)
#Compare the results of the Watson PI API by calculating
#the distance between traits
compared_results = compare(user,celebrity)
#Sort the results
sorted_results = sorted(compared_results.items(), key=operator.itemgetter(1))
#Print the results to the user
for keys, value in sorted_results[:5]:
print keys ,
print (user[keys]),
print ('->'),
print (celebrity[keys]),
print ('->'),
print (compared_results[keys])