Celebrity Match Demo Not Working

Hey all,

I followed the instructions exactly (inputting my Twitter handle and Twitter API credentials), but the interface doesn’t seem to be working. I’ve clicked “Run” multiple times with no results shown in the right pane. Copying my code below. Is this just a bug with CodeAcademy??

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 = 'XXXX'
	twitter_consumer_secret = 'XXXX'
	twitter_access_token = 'XXXX'
	twitter_access_secret = 'XXXX'

	#Invoking the Twitter API
	twitter_api = twitter.Api(consumer_key=twitter_consumer_key,

	#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 = "@mattbrown8454"
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])

  1. Don’t post your API credentials, they should be kept private.
  2. It’s a codecademy problem.

Did it get solved for you? I am having the same problem

Never got solved for me. I’m a Pro member so I broached the topic with their Customer Support Live Chat… they said they would add it to their bug list.

In any case, I just kept moving through the other lessons which 90% of the time worked well. I really like this course actually. IBM Watson’s services are really mind-blowing, and the instructions here in CodeAcademy are very simple and intuitive.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.