I'm querying Google+ data with TF-IDF and save the data as a JSON
file. While working with this file I get an error.
Code
import json
import nltk
DATA = 'C:/Users/Dung Ring/Desktop/kpdl/107033731246200681024.json'
data = json.loads(open(DATA).read())
QUERY_TERMS = ['SOPA']
activities = [activity['object']['content'].lower().split() \
for activity in data \
if activity['object']['content'] != " "]
# TextCollection provides tf, idf, and tf_idf abstractions so
# that we don't have to maintain/compute them ourselves
tc = nltk.TextCollection(activities)
relevant_activities = []
for idx in range(len(activities)):
score = 0
for term in [t.lower() for t in QUERY_TERMS]:
score += tc.tf_idf(term, activities[idx])
if score > 0:
relevant_activities.append({'score': score, 'title': data[idx]['title'],
'url': data[idx]['url']})
# Sort by score and display results
relevant_activities = sorted(relevant_activities, key=lambda p: p['score'], reverse=True)
for activity in relevant_activities:
print activity['title']
print '\tLink: %s' % (activity['url'], )
print '\tScore: %s' % (activity['score'], )
print
Error message
Traceback (most recent call last):
File "ex9.py", line 11, in <module>
if activity['object']['content']!= ""]
TypeError: string indices must be integers
I use Python 2.7.
Either
activity
oractivity['object']
is a string and not a dictionary as you expect. Printdata
and check.