-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
190 lines (131 loc) · 5.87 KB
/
model.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
"""Models and database functions for Ratings project."""
from flask_sqlalchemy import SQLAlchemy
from correlation import pearson
# This is the connection to the PostgreSQL database; we're getting this through
# the Flask-SQLAlchemy helper library. On this, we can find the `session`
# object, where we do most of our interactions (like committing, etc.)
db = SQLAlchemy()
##############################################################################
# Model definitions
class User(db.Model):
"""User of ratings website."""
__tablename__ = "users"
user_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
email = db.Column(db.String(64), nullable=True)
password = db.Column(db.String(64), nullable=True)
age = db.Column(db.Integer, nullable=True)
zipcode = db.Column(db.String(15), nullable=True)
def __repr__(self):
""" Provide helpful representation when printed """
return f"<User user_id = {self.user_id} email = {self.email}>"
def similarity(self, user2):
"""compares user2's ratings to instance's ratings"""
self_ratings = {}
paired_ratings = []
for r in self.ratings:
self_ratings[r.movie_id] = r
for u2_rating in user2.ratings:
self_rating = self_ratings.get(u2_rating.movie_id)
if self_rating:
pair = (self_rating.score, u2_rating.score)
paired_ratings.append(pair)
if paired_ratings:
return pearson(paired_ratings)
else:
return 0.0
def predict_ratings(self, movie):
"""Predicts what user will rate movie based on similar users' ratings"""
other_ratings = movie.ratings
other_users = [r.user for r in movie.ratings]
users_similarity = []
for o_user in other_users:
o_user_similarity = self.similarity(o_user)
similarity_pair = (o_user_similarity, o_user)
users_similarity.append(similarity_pair)
#sorts list of user similarities by most to least similar
sorted_similariry = sorted(users_similarity, key=lambda users_similarity: users_similarity[0], reverse=True)
most_similar_user = sorted_similariry[0]
most_similar_user_rating = 0
for r in most_similar_user[1].ratings:
if r.movie_id == movie.movie_id:
most_similar_user_rating = r.score
predict_rating = most_similar_user_rating * most_similar_user[0]
return predict_rating
# Put your Movie and Rating model classes here.
# Movie model
class Movie(db.Model):
"""Movies for ratings on the website """
__tablename__ = "movies"
movie_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
title = db.Column(db.String(100), nullable =False)
release_at = db.Column(db.DateTime, nullable = False)
imdb_url = db.Column(db.String(200), nullable = False)
#Ratings Model
class Rating(db.Model):
"""Ratings database"""
__tablename__ = "ratings"
rating_id = db.Column(db.Integer, autoincrement=True, primary_key=True)
movie_id = db.Column(db.Integer, db.ForeignKey("movies.movie_id"), nullable=False)
user_id = db.Column(db.Integer, db.ForeignKey("users.user_id"), nullable=False)
score = db.Column(db.Integer, nullable=False)
#Define relationship to user
user = db.relationship("User",
backref=db.backref("ratings",
order_by=rating_id))
#Define relationship to movie
movie = db.relationship("Movie",
backref=db.backref("ratings",
order_by=rating_id))
def __repr__(self):
"""Provide helpful representation when printed."""
return f"""<Rating rating_id={self.rating_id}
movie_id={self.movie_id}
user_id={self.user_id}
score={self.score}>"""
def similarity(user1, user2):
user1_ratings_dict = {}
paired_ratings = []
for r in user1.ratings:
user1_ratings_dict[r.movie_id] = r
for u2_rating in user2.ratings:
u1_rating = user1_ratings_dict.get(u2_rating.movie_id)
if u1_rating:
pair = (u1_rating.score, u2_rating.score)
paired_ratings.append(pair)
if paired_ratings:
return pearson(paired_ratings)
else:
return 0.0
def predict_ratings(user, movie):
"""Predicts what user will rate movie based on similar users' ratings"""
other_ratings = movie.ratings
other_users = [r.user for r in movie.ratings]
users_similarity = []
for o_user in other_users:
o_user_similarity = user.similarity(o_user)
similarity_pair = (o_user_similarity, o_user)
users_similarity.append(similarity_pair)
#sorts list of user similarities by most to least similar
sorted_similariry = sorted(users_similarity, key=lambda users_similarity: users_similarity[0], reverse=True)
most_similar_user = sorted_similariry[0]
most_similar_user_rating = 0
for r in most_similar_user[1].ratings:
if r.movie_id == movie.movie_id:
most_similar_user_rating = r.score
predict_rating = most_similar_user_rating * most_similar_user[0]
return predict_rating
##############################################################################
# Helper functions
def connect_to_db(app):
"""Connect the database to our Flask app."""
# Configure to use our PstgreSQL database
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql:///ratings'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db.app = app
db.init_app(app)
if __name__ == "__main__":
# As a convenience, if we run this module interactively, it will leave
# you in a state of being able to work with the database directly.
from server import app
connect_to_db(app)
print("Connected to DB.")