-
Notifications
You must be signed in to change notification settings - Fork 4
/
movies.py
executable file
·156 lines (122 loc) · 4 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
#!/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
global db
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']))
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 predict(movie_id):
ratings = get_ratings(movie_id=movie_id)
target_movie = get_movie(movie_id)
for movie in rated_movies:
similarities = [ (pearson({}, {}) rating) for target_movie_id, rating in movie_pairs]
top_five = sorted(similarities)
top_five.reverse()
top_five = top_five[: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)
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()