-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
executable file
·308 lines (251 loc) · 9.03 KB
/
preprocess.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
#!/usr/bin/env python
from argparse import ArgumentParser
from bisect import bisect_right
import csv
import json
import math
import os
import sys
import numpy as np
def load_json(filename):
"""
Load JSON from a file.
"""
data = []
with open(filename, 'r') as f:
for line in f:
line_data = json.loads(line)
data.append(line_data)
return data
def save_json(filename, data):
"""
Save JSON to a file.
"""
with open(filename, 'w') as f:
for item in data:
json.dump(item, f, separators=(',',':'))
f.write("\n")
def save_tab(filename, data, keys):
"""
Save data as a tab-delimited file.
"""
with open(filename, 'w') as f:
for item in data:
vals = []
for key in keys:
vals.append(unicode(item[key]))
vals = '\t'.join(vals)
f.write(vals)
f.write('\n')
def calc_popularity(filtered):
"""
Calculate a single popularity label for each item in the dataset.
"""
for item in filtered:
popularity = item['stars']
if item['review_count']:
popularity += math.log(item['review_count'])
if item['checkins']:
popularity += math.log(item['checkins'])
for key in ('review_count', 'stars', 'checkins'):
item.pop(key)
item['popularity'] = popularity
return filtered
def disc_attribute(filtered, attr, num_bins=3):
"""
Discretize a given attribute into the given number of bins, by percentiles.
"""
bin_edges = []
values = [x[attr] for x in filtered]
for i in range(1, num_bins):
edge = np.percentile(values, float(i)/num_bins * 100)
bin_edges.append(edge)
for item in filtered:
value = item[attr]
value = bisect_right(bin_edges, value)
item[attr] = value
return filtered
def save_svmlight(filename, data, save_keys):
"""
Save data in svmlight / libsvm format.
"""
# Create the mapping between category names and their IDs first
id_map = {}
counter = 1
for key in sorted(save_keys):
if key == 'categories':
continue
id_map[key] = counter
counter += 1
with open(filename, 'w') as f:
for item in data:
attrs = [str(item['popularity'])]
for key, value in item.iteritems():
if key == 'categories':
for cat in value:
cat = 'c_' + cat
if cat not in id_map:
id_map[cat] = counter
counter += 1
attrs.append('{id}:1'.format(id=id_map[cat]))
elif key in save_keys:
attrs.append('{id}:{v}'.format(id=id_map[key], v=value))
else:
continue
attrs = [attrs[0]] + sorted(attrs[1:],
key=lambda x: int(x.split(':')[0]))
attrs = ' '.join(attrs)
f.write(attrs)
f.write('\n')
with open('svmlight_mapping.txt', 'w') as f:
for key, value in id_map.iteritems():
f.write("{k}\t{v}\n".format(k=key, v=value))
def map_categories(filtered):
"""
Take filtered data and map the categories to more general categories.
"""
cats = set()
with open('filtered_categories.txt', 'r') as f:
for line in f:
line = line.strip()
cats.add(line)
cat_map = {}
with open('category_mapping.txt', 'r') as f:
for line in f:
line = line.strip()
parts = line.split('\t')
if len(parts) > 1:
key = parts[0]
vals = parts[1:]
for val in vals:
assert val in cats
cat_map[key] = vals
for item in filtered:
item_cats = item['categories']
item_cat_mapped = set()
for cat in item_cats:
if cat in cat_map:
item_cat_mapped.update(cat_map[cat])
item_cats = sorted(item_cat_mapped)
item['categories'] = item_cats
return filtered
def process_checkins(data_dir):
"""
Get the total number of checkins for each business and return a dict.
"""
businesses = {}
path = os.path.join(data_dir, 'yelp_academic_dataset_checkin.json')
data = load_json(path)
for business in data:
total_tips = sum(business['checkin_info'].values())
businesses[business['business_id']] = total_tips
return businesses
def process_businesses(data_dir, save_keys=None):
"""
Load business data and save a filtered feature set for relevant businesses.
"""
# The attributes we want to directly copy from the input feature set.
ATTRS = ['review_count', 'name', 'business_id', 'stars', 'latitude',
'longitude', 'categories']
checkins = process_checkins(data_dir)
path = os.path.join(data_dir, 'yelp_academic_dataset_business.json')
data = load_json(path)
filtered = []
for business in data:
# We only care about open businesses
if not business['open']:
continue
# We only want restaurants
if not 'Restaurants' in business['categories']:
continue
# And we only want ones with price range attributes (13% filtered out)
if not business['attributes'].get('Price Range'):
continue
attrs = dict(((a, business[a]) for a in ATTRS))
attrs['price_range'] = business['attributes']['Price Range']
attrs['checkins'] = checkins.get(business['business_id'], 0)
filtered.append(attrs)
filtered = map_categories(filtered)
filtered = calc_popularity(filtered)
filtered = disc_attribute(filtered, attr='popularity')
if not save_keys:
save_json('processed.json', filtered)
else:
save_svmlight('processed.svmlight', filtered, save_keys)
#save_tab('processed.tsv', filtered, save_keys)
def calc_arunima_popularity(filtered):
"""
Calculate a single popularity label for each item in the dataset.
"""
for item in filtered:
popularity = item['wtavgstarrating_without_1']
if item['wtavgtotalreviews']:
popularity += math.log(item['wtavgtotalreviews'])
if item['checkins']:
popularity += math.log(item['checkins'])
for key in ('wtavgtotalreviews', 'wtavgstarrating_without_1',
'checkins'):
item.pop(key)
item['popularity'] = popularity
return filtered
def convert_number(text):
# If only whitespace, or empty, just return.
if not text.strip():
return text
try:
value = float(text)
except ValueError:
return text
if math.isnan(value):
return 0
elif value == int(value):
return int(value)
else:
return value
def load_closed_businesses(data_dir):
path = os.path.join(data_dir, 'yelp_academic_dataset_business.json')
data = load_json(path)
closed = set()
for business in data:
if not business['open']:
closed.add(business['business_id'])
return closed
def process_arunima(data_dir):
ATTRS = ['review_count', 'longitude', 'latitude', 'price',
'wtavgtotalreviews', 'wtavgstarrating_without_1', 'stars', 'cat1',
'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7', 'cat8']
checkins = process_checkins(data_dir)
closed = load_closed_businesses(data_dir)
filtered = []
filename = os.path.join(data_dir, 'Arunima_processed', 'wtagereview_details_without1')
with open(filename, 'rb') as f:
reader = csv.DictReader(f, delimiter='\t')
for row in reader:
if not row['price']:
# Skip entries without a price group
continue
#if row['business_id'] in closed:
# Skip the closed businesses
# continue
attrs = dict(((a, convert_number(row[a])) for a in ATTRS))
attrs['checkins'] = checkins.get(row['business_id'], 0)
attrs['_latitude'] = attrs['latitude']
attrs['_longitude'] = attrs['longitude']
attrs.pop('latitude')
attrs.pop('longitude')
filtered.append(attrs)
filtered = calc_arunima_popularity(filtered)
filtered = disc_attribute(filtered, attr='popularity')
save_svmlight('processed.svmlight', filtered, ['price', '_latitude', '_longitude', 'cat1', 'cat2', 'cat3', 'cat4', 'cat5', 'cat6', 'cat7', 'cat8'])
def main(argv):
parser = ArgumentParser(description="Preprocess Yelp dataset.",
prog=argv[0])
parser.add_argument('input_dir', nargs='?',
help="path to directory containing Yelp JSON files", default="data")
parser.add_argument('output_file', nargs='?',
help="path to the processed output file", default="processed.json")
args = parser.parse_args(argv[1:])
#process_businesses(args.input_dir, ['price_range', 'categories', 'latitude', 'longitude'])
process_arunima(args.input_dir)
if __name__ == '__main__':
main(sys.argv)