-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspect_price_distribution.py
54 lines (42 loc) · 1.57 KB
/
inspect_price_distribution.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import argparse
import json
import logging
import os
from common import get_price, get_age_group, load_items
def facet(review, product, user, facet_type):
facet_type = facet_type.lower()
if facet_type == 'none':
return 'all'
elif facet_type == 'age':
return get_age_group(user, review)
else:
raise ValueError('Invalid facet type')
def main():
logging.basicConfig(level=logging.INFO, format='%(message)s')
parser = argparse.ArgumentParser(description='Inspect price distributions')
parser.add_argument('prefix', help='Directory containing json files')
parser.add_argument('facet', help='Facet', choices=['age', 'none'])
args = parser.parse_args()
logging.info('Loading products...')
product_by_id = load_items(os.path.join(args.prefix, 'product.json'),
'product_id')
logging.info('Loading users...')
user_by_id = load_items(os.path.join(args.prefix, 'user.json'),
'user_id')
print '"facet","price"'
with open(os.path.join(args.prefix, 'review.json')) as f:
for line in f:
review = json.loads(line)
product = product_by_id[review['product_id']]
price = get_price(product)
if not price:
continue
user = user_by_id[review['user_id']]
fct = facet(review, product, user, args.facet)
if not fct:
continue
print '"{0}","{1}"'.format(fct, price)
if __name__ == '__main__':
main()