# 7. Sorting and Outputting the Results

#1

I have passed this course but I keep getting an error, I would like to see this course through. Can anyone help me identify what I’ve done wrong in the code?

import sys
import operator
import requests
import json
from watson_developer_cloud import PersonalityInsightsV2 as PersonalityInsights

def analyze(handle):

``````twitter_consumer_key = '*****'

``````

text = ""
for s in statuses:
if (s.lang ==‘en’):
text += s.text.encode(‘utf-8’)

*’

pi_result = personality_insights.profile(text)

``````return pi_result
``````

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

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

celebrity_handle = “@IBM

user_result = analyze(user_handle)
celebrity_result = analyze(celebrity_handle)

user = flatten(user_result)
celebrity = flatten(celebrity_result)

compared_results = compare(user,celebrity)

sorted_result = sorted(compared_results.items(), key=operator.itemgetter(1))

for keys, value in sorted_result[:5]:
print keys,
print(user[keys]),
print (’->’),
print (celebrity[keys]),
print (’->’),
print (compared_results[keys])

#2

I also get a non-result at the end of the course.

“Line 61 print (compared_results[keys])” returns below “Error: The number of words 57 is less than the minimum number of words required for analysis: 100, Code: 400”

Comparing different pairs of celebrities, e.g., {@scars008@halleberry} returns the same error of “number of words 57 is less than the minimum”

So I reported a bug, on the basis the compare analysis seems to run, albeit not meeting with success. Anyway the error pasted in full:

Traceback (most recent call last):
File “CelebrityMatch.py”, line 41, in
pi_result=personality_insights.profile(text)
File “/usr/local/lib/python2.7/dist-packages/watson_developer_cloud/personality_insights_v2.py”, line 61, in profile