-
Notifications
You must be signed in to change notification settings - Fork 0
/
movies.py
executable file
·230 lines (179 loc) · 5.99 KB
/
movies.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
#!/usr/bin/env python
import sys
from correlation import pearson_similarity as pearson
import pymongo
from collections import defaultdict
import traceback
import model
from model import User
db = None
def movie_details(movie_id):
movie = db.movies.find_one({"_id": movie_id})
if not movie:
print "No movie with id %d"%movie_id
print """\
%d: %s
%s"""%(movie['_id'], movie['title'], ", ".join(movie['genres']))
return """\
%d: %s
%s"""%(movie['_id'], movie['title'], ", ".join(movie['genres']))
movie_d = []
movie_d.append(movie['_id'])
movie_d.append(movie['title'])
movie_d.append(movie['genres'])
return movie_d
# return """
# %d: %s
# %s"""%(movie['_id'], movie['title'], ", ".join(movie['genres']))
pass
def movie_details_title(movie_title):
movie = db.movies.find_one({"_title": title})
if not movie:
print "No movie with that title %s"%title
print """\
%d: %s
%s"""%(movie['_id'], movie['title'], ", ".join(movie['genres']))
return """\
%d: %s
%s"""%(movie['_id'], movie['title'], ", ".join(movie['genres']))
movie_t = []
movie_t.append(movie['title'])
movie_t.append(movie['genres'])
return movie_t
# return """
# %d: %s
# %s"""%(movie['_id'], movie['title'], ", ".join(movie['genres']))
pass
def error(msg = "Unknown command"):
print "Error:", msg
def quit():
print "Goodbye!"
sys.exit(0)
def average_rating(movie_id):
rating_records = get_ratings(movie_id=movie_id)
ratings = [ rec['rating'] for rec in rating_records ]
avg = float(sum(ratings))/len(ratings)
print "%.2f"%(avg)
def user_details(user_id):
user = User.get(user_id)
print user
def user_rating(movie_id, user_id):
rating = get_rating(movie_id, user_id)
if not rating:
print "Sorry, user %d has not rated movie %d"%(user_id, movie_id)
return
movie = get_movie(movie_id)
print "User %d rated movie %d (%s) at %d stars"%(\
user_id, movie_id, movie['title'],
rating)
def rate_movie(movie_id, rating):
movie = get_movie(movie_id)
db.ratings.update({"movie_id": movie_id, "user_id": 0},
{"$set": {"rating": rating}}, upsert=True)
print "You rated movie %d: %s at %d stars."%(\
movie_id, movie['title'],
rating)
def get_movie(movie_id):
return db.movies.find_one(movie_id)
def get_ratings(movie_id=None, user_id=None):
query = {}
if movie_id is not None:
query['movie_id'] = movie_id
if user_id is not None:
query['user_id'] = user_id
records = db.ratings.find(query)
return [ rec for rec in records ]
def get_rating(movie_id, user_id):
record = db.ratings.find_one({"movie_id": movie_id, "user_id": user_id})
if record:
return record['rating']
def get_my_movies():
ratings_user0 = get_ratings(user_id=0)
rated_movies = []
for record in ratings_user0:
rated_movies.append(record['movie_id'])
# print rated_movies
return rated_movies
def convert_ratings_to_dict(list_of_ratings):
# [ {uid: rating} ]
movie_ratings = {}
for rating in list_of_ratings:
movie_ratings[rating['user_id']] = rating['rating']
return movie_ratings
def get_all_rating_dicts(movie_ids):
all_ratings = []
for id in movie_ids:
ratings = get_ratings(movie_id=id)
rating_dict = convert_ratings_to_dict(ratings)
all_ratings.append(rating_dict)
return all_ratings
def predict(movie_id):
ratings = get_ratings(movie_id=movie_id)
target_movie = get_movie(movie_id)
my_movie_ids = get_my_movies() #list of movies
target_film_ratings = convert_ratings_to_dict(ratings)
comparison_film_ratings = get_all_rating_dicts(my_movie_ids)
similarities = []
for base_movie in my_movie_ids:
base_movie_ratings = convert_ratings_to_dict(get_ratings(movie_id=base_movie))
# print "pearson score", pearson(target_film_ratings, base_movie_ratings)
similarities.append((pearson(target_film_ratings, base_movie_ratings), get_rating(base_movie, 0)))
similarities.sort()
similarities.reverse()
print "TYPE TWO", type(similarities)
# top_five = similarities[-5:]
num = 0.0
den = 0.0
# Use a weighted mean rather than a strict top similarity
for sim, m in top_five:
num += (float(sim) * m)
den += sim
rating = num/den
print "Best guess for movie %d: %s is %.2f stars"%\
(movie_id, target_movie['title'], rating)
def parse(line, dispatch):
tokens = line.split()
if not tokens:
return error()
cmd = tokens[0]
command = dispatch.get(cmd)
if not command:
return error()
if len(tokens) != len(command):
return error("Invalid number of arguments")
function = command[0]
if len(command) == 1:
return function()
try:
type_tuples = zip(command[1:], tokens[1:])
typed_arguments = [ _type(arg) for _type, arg in type_tuples ]
return function(*typed_arguments)
except Exception, e:
traceback.print_exc()
return error("Invalid argument to %s"%(cmd))
def connect_db(host, port, user, password, db_name):
connect_string = "mongodb://%s:%s@%s:%d/%s" % \
(user, password, host, port, db_name)
c = pymongo.connection.Connection(connect_string)
print c[db_name]
return c[db_name]
def main():
global db
# db = pymongo.connection.Connection("localhost")
db = connect_db("dbh36.mongolab.com", 27367, "movie_user", "password", "movies")
db = db['movies']
model.db = db
dispatch = {
"movie": (movie_details, int),
"q": (quit,),
"avg": (average_rating, int),
"user": (user_details, int),
"rating": (user_rating, int, int),
"rate": (rate_movie, int, int),
"predict": (predict, int)
}
while True:
line = raw_input("> ")
parse(line, dispatch)
if __name__ == "__main__":
main()