-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinstall.py
More file actions
229 lines (195 loc) · 6.99 KB
/
Copy pathinstall.py
File metadata and controls
229 lines (195 loc) · 6.99 KB
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
import os
import sys
import csv
import json
import pyprind
import urllib2
import common
from collections import defaultdict
from operator import itemgetter
from pymongo import MongoClient
from usersimilarity import UserSimilarity
from slopeone import SlopeOne
from shutil import copyfile
try:
import tmdbsimple as tmdb
TMDB_PRESENT = True
except:
TMDB_PRESENT = False
tmdb.API_KEY = common.tmdb_key
errors = []
TMDB_CACHE_PATH = 'data/tmdb_cache.json'
TMDB_MATCH_PATH = 'data/u.item.tmdb'
TMDB_ERRORS_PATH = 'data/errors.json'
TMDB_MAPPING = 'https://gist.githubusercontent.com/amitab/7869d7336b80dfc3c4e8/raw/u.item.tmdb'
users = defaultdict(dict)
movies = defaultdict(dict)
meta = defaultdict(dict)
id_to_movie = {}
try:
print "Loaded TMDB Cache"
tmdb_cache = json.load(open(TMDB_CACHE_PATH, 'r'))
except:
print "No TMDB Cache"
tmdb_cache = {}
def load_meta(path):
print "Loading Metadata"
file = open(path + '/u.genre', 'r')
reader = csv.reader( file , delimiter = '|')
meta['genres'] = [x[0] for x in reader if x]
file.close()
file = open(path +'/u.occupation', 'r')
meta['occupations'] = [x for x in file.readlines() if x]
file.close()
meta['occupations'] = set()
meta['zip_codes'] = set()
meta['genders'] = set()
#load movie data
def load_movie_db(path, with_tmdb=TMDB_PRESENT):
i_f = open( path + '/u.item', 'r' )
reader = csv.reader( i_f , delimiter = '|')
for line in reader:
for i, word in enumerate(line):
try:
line[i] = unicode(word, 'utf-8')
except:
line[i] = line[i].decode('latin-1')
movies[line[1]] = {
'id': int(line[0]),
'name': line[1],
'date': line[2],
'url': line[4],
'genres': [meta['genres'][i] for i in range(0, 19) if line[5 + i] == "1"]
}
id_to_movie[line[0]] = line[1]
i_f.close()
if not with_tmdb:
return
genres = [x['name'] for x in tmdb.Genres().movie_list()['genres']]
meta['genres'] = list(set(genres + meta['genres']))
tmdb_data = open( TMDB_MATCH_PATH, 'r' )
tmdb_reader = csv.reader( tmdb_data , delimiter = "|")
matches = [(line[1], line[0]) for line in tmdb_reader]
tmdb_data.close()
my_prbar = pyprind.ProgBar(len(matches), width = 70, title='Loading Tmdb info')
errors = []
print "Starting Movie Import from TmDB"
for m_key, tmdb_id in matches:
try:
key = unicode(m_key, 'utf-8')
except:
key = m_key.decode('latin-1')
movies[key]['tmdb_id'] = int(tmdb_id)
try:
if str(tmdb_id) in tmdb_cache:
info = tmdb_cache[str(tmdb_id)]
else:
info = tmdb.Movies(int(tmdb_id)).info()
tmdb_cache[str(tmdb_id)] = info
except Exception as e:
print e
print 'Error Occured to fetch movie_id: ' + key + ', tmdb_id: ' + str(tmdb_id)
errors.append((key, tmdb_id))
if '404' not in e.message:
raise e
continue
info.pop('id', None)
movies[key]['genres'].extend([
x['name'] for x in info.pop('genres', [])
])
movies[key]['genres'] = list(set(movies[key]['genres']))
movies[key].update(info)
my_prbar.update(item_id = int(movies[key]['id']), force_flush=True)
with open(TMDB_ERRORS_PATH, 'w') as outfile:
json.dump(errors, outfile, indent=4, sort_keys=True)
# load user data
def load_user_db(path):
i_f = open( path + '/u.user', 'r' )
reader = csv.reader( i_f , delimiter = '|')
for line in reader:
users[line[0]] = {
'id': int(line[0]),
'age': int(line[1]),
'sex': line[2],
'occupation': line[3],
'zip_code': line[4],
'ratings': [],
'likes': []
}
meta['genders'].add(line[2])
meta['occupations'].add(line[3])
meta['zip_codes'].add(line[4])
i_f.close()
# load user ratings
def load_ratings(path):
i_f = open( path + '/u.data', 'r' )
reader = csv.reader( i_f , delimiter = "\t")
for line in reader:
users[line[0]]['ratings'].append({
'movie_id': int(line[1]),
'rating': int(line[2]),
'timestamp': int(line[3])
})
if line[2] == '5':
genres = movies[id_to_movie[line[1]]]['genres']
try:
genres.remove('unknown')
except:
pass
users[line[0]]['likes'] = list(set(users[line[0]]['likes'] + genres))
i_f.close()
def finalize():
meta['occupations'] = list(meta['occupations'])
meta['zip_codes'] = list(meta['zip_codes'])
meta['genders'] = list(meta['genders'])
for user in users:
users[user]['ratings'] = sorted(users[user]['ratings'], key=itemgetter('rating'), reverse=True)
slope = SlopeOne()
similarity = UserSimilarity()
print "Dropping database {}".format(common.database.name)
common.client.drop_database(common.database.name)
print "Importing Users into {}".format(common.users.name)
common.users.insert_many(users.values())
print "Importing Movies into {}".format(common.movies.name)
common.movies.insert_many(movies.values())
print "Importing Metadata into {}".format(common.meta.name)
common.meta.insert_many([meta])
common.initialize()
dev = slope.get_deviation_matrix()
sim = similarity.get_similarity_matrix()
print "Importing Deviation Matrix into {}".format(common.deviations.name)
common.deviations.insert_many(dev.values())
print "Importing User Similarity Matrix into {}".format(common.user_similarity.name)
common.user_similarity.insert_many(sim.values())
def prepare_parser():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-p', '--data-path', type=str, required=True)
parser.add_argument('-t', '--with-tmdb', action='store_true', default=False)
return parser
if __name__ == '__main__':
parser = prepare_parser()
args = parser.parse_args()
if args.with_tmdb:
if not TMDB_PRESENT:
print "Option --with-tmdb provided, but module tmdbsimple is not present."
sys.exit(1)
if not os.path.exists('data'):
os.makedirs('data')
if not os.path.isfile(TMDB_MATCH_PATH):
print "Loading TMDB match info"
filedata = urllib2.urlopen(TMDB_MAPPING)
with open(TMDB_MATCH_PATH, 'wb') as f:
f.write(filedata.read())
try:
load_meta(args.data_path)
load_movie_db(args.data_path, args.with_tmdb)
load_user_db(args.data_path)
load_ratings(args.data_path)
finalize()
except Exception as e:
print "Exception: ", e
if args.with_tmdb:
with open(TMDB_CACHE_PATH, 'w') as outfile:
json.dump(tmdb_cache, outfile, indent=4, sort_keys=True)
print "Saved TMDB cache in {}".format(TMDB_CACHE_PATH)