forked from manasRK/word2vec-recommender
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmetadata_preProcessing.py
65 lines (49 loc) · 1.01 KB
/
metadata_preProcessing.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: Akhil Gupta
# @Date: 2016-06-20
# @Email: [email protected]
# @Github username: @codeorbit
# @Last Modified by: Akhil Gupta
# @Last Modified time: 2016-07-1
import json
import gzip
import redis
data_obj = redis.Redis("localhost", port=6379, db=2)
def preProcess(row):
print row
temp = []
productId = row["asin"]
try:
boughtTogether = row["related"]["bought_together"]
for ele in boughtTogether:
temp.append("b_t_"+ele)
except:
pass
try:
alsoBought = row["related"]["also_bought"]
for ele in alsoBought:
temp.append("a_b_"+ele)
except:
pass
try:
alsoViewed = row["related"]["also_viewed"]
for ele in alsoViewed:
temp.append("a_v_"+ele)
except:
pass
if temp ==[]:
pass
else:
data_obj.set(productId, temp)
def parse(path):
g = gzip.open(path, 'r')
for l in g:
yield (eval(l))
def main():
count = 0
for row in parse("metadata.json.gz"):
count+=1
print count
preProcess(row)
main()