-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenerate_json_from_users100.py
127 lines (93 loc) · 3.79 KB
/
generate_json_from_users100.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
from __future__ import division
from sklearn.neighbors import NearestNeighbors
import numpy as np
from sets import Set
import os
import os.path
import bson
import sys
import csv
import json
import math
import string
import random
import names
import operator
from collections import defaultdict
from scipy.sparse import csc_matrix
users_interests = []
users_songs = []
index_song_map = {}
user_index_map = {}
userid_index_map = {}
ommited_songs = {}
def findTriple(userHex, songHex, triples):
for i, triple in enumerate(triples):
if triple['user'] == userHex and triple['song'] == songHex:
return i
toHex = lambda x:"".join([hex(ord(c))[2:].zfill(2) for c in x])
def getUserInterestMatrix():
#XminmaxNorm = (X - min(X))/(max(X)-min(X))
songIds = []
userId = ""
newSongs = []
topUsers2_path = 'data/topUsers100/'
song_set = Set([])
users_info = []
songs = []
users = []
triples = []
#users_interests = [Set([])]
song_index_map = {}
# unique_songs = Set([])
for filename in os.listdir(topUsers2_path):
realPath = topUsers2_path + filename
with open(realPath, 'rb') as csvfile:
# each file is a user
csvreader = csv.reader(csvfile, delimiter=',')
user_heard = Set([])
songid_hex_map = {}
userHex = ''
songHex = ''
tripleHex = ''
for i, row in enumerate(csvreader):
if i != 0:
if i == 1:
userHex = ''.join([random.choice(string.hexdigits + string.digits) for n in xrange(24)])
users.append({'_id': userHex, 'username': names.get_first_name(), 'idString': userId, 'password': 'brown123', 'provider': 'local'})
userId = row[0]
songId = row[1]
songPlayCount = int(row[2])
songTitle = row[4]
songAuthor = row[9]
if songId in song_index_map:
# Triple already exists, just add to it.
if songId in user_heard:
currSongHex = songid_hex_map[songId]
currTriple = findTriple(userHex, songHex, triples)
oldCount = triples[currTriple].count
#triples[currTriple] = {user: userHex, song: songHex, count: (songPlayCount+oldCount)}
print "No debe llegar aqui nunca!!!!"
exit(1)
else:
songHex = ''.join([random.choice(string.hexdigits + string.digits) for n in xrange(24)])
songs.append({'_id': songHex, 'idString': songId, 'author': songAuthor, 'title': songTitle})
tripleHex = ''.join([random.choice(string.hexdigits + string.digits) for n in xrange(24)])
triples.append({'user': userHex, 'song': songHex, 'songTitle': songTitle, 'count': songPlayCount})
song_index_map[songId] = songHex
user_heard.add(songId)
songfile = open('Song.json', 'w')
json.dump([{'model': 'Song', 'documents': songs}], songfile);
songfile.close();
userfile = open('User.json', 'w')
json.dump([{'model': 'User', 'documents': users}], userfile);
userfile.close();
triplefile = open('Triple.json', 'w')
json.dump([{'model': 'Triple', 'documents': triples}], triplefile);
triplefile.close();
def main():
print "generating json"
getUserInterestMatrix()
print "done"
if __name__ == '__main__':
main()