-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathparse.py
94 lines (77 loc) · 3.19 KB
/
parse.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
import json, re
import pickle
import csv
import time
file = open("yelp_academic_dataset_review.json",'r')
#file = open("testReview.txt",'r')
lines = file.readlines()
locations = []
users = []
businesses = []
list = []
review_dict = {}
for line in lines:
# ignore these lines
if line[0] != '{':
continue
o = json.loads(str(line))
if o['type'] == "review":
# data without long ids
'''if o['user_id'] not in users:
users.append(o['user_id'])
if o['business_id'] not in businesses:
businesses.append(o['business_id'])
#if o['user_id'] in users and o['business_id'] in businesses:
print str(o['stars']) + ' |u ' + str(users.index(o['user_id'])) + ' |i ' + str(businesses.index(o['business_id']))'''
#data with long ids
review = {}
review['user_id'] = str(o['user_id'])
review['stars'] = str(o['stars'])
review['business_id'] = str(o['business_id'])
review['date'] = time.strptime(str(o['date']),'%Y-%m-%d')
#Is the user's id already in the dictionary?
if review['user_id'] in review_dict.keys():
# iterate through each review by that user
updated = False
for i,rev in enumerate(review_dict[review['user_id']]):
# if the business is the same, but the new review's date is more recent, update
if rev['business_id'] == review['business_id']:
updated = True
if review['date'] > rev['date']:
review_dict[review['user_id']][i] = review
break
if not updated:
review_dict[review['user_id']].append(review)
else:
# if the user id was never there, just create a new list and add it to the dict entry
review_list = []
review_list.append(review)
review_dict[review['user_id']] = review_list
#print str(o['stars']) + ' |u ' + str(o['user_id']) + ' |i ' + str(o['business_id'])
#pickle.dump(users, open('saveUsers.p','wb'))
#pickle.dump(businesses, open('saveBiz.p','wb'))
f = open('saveReviewDict.p','wb')
pickle.dump(review_dict, f)
f.close()
#for entry in review_dict.keys():
# for rating in review_dict[entry]:
# print rating['stars'] + ' |u ' + rating['user_id'] + ' |i ' + rating['business_id']
# for i in businesses:
# print i
#if o['type'] == "review":
# print str(o['stars']) + ' |u ' + str(o['user_id']) + ' |i ' + str(o['business_id'])
#
# '''if 'Price Range' in o['attributes']:
# if o['type'] == "business" and o['attributes']['Price Range'] == 4: #('Automotive' in o['categories']):# and o['city'] == 'Phoenix':
# loc = []
# #loc.append(o['categories'])
# loc.append(o['longitude'])
# loc.append(o['latitude'])
# #locations.append(o['latitude']) + "," + str(o['longitude']) + ")"
# locations.append(loc)'''
# test = open(r'C:\Users\Stephen\Documents\13-14 Junior\COS 424\yelp_phoenix_academic_dataset\test.csv', 'w+')
#
# for i in locations:
# for j in i:
# test.write(str(j)+',')
# test.write('\n')