-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
123 lines (95 loc) · 3.95 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
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String, DateTime
from sqlalchemy.orm import sessionmaker, relationship, backref, scoped_session
from datetime import datetime
from sqlalchemy import ForeignKey
from correlation import pearson
ENGINE = None
Session = None
ENGINE = create_engine("sqlite:///ratings.db", echo=False)
session = scoped_session(sessionmaker(bind=ENGINE,
autocommit = False,
autoflush = False))
Base = declarative_base()
Base.query = session.query_property()
### Class declarations go here
class User(Base):
__tablename__ = "users"
id = Column(Integer, primary_key = True)
email = Column(String(64), nullable=True, unique=True)
password = Column(String(64), nullable=True)
age = Column(Integer, nullable=True)
gender = Column(String(1), nullable=True)
occupation = Column(String(40), nullable=True)
zipcode = Column(String(15), nullable=True)
def __str__(self):
output = "ID: %r, EMAIL: %s, PASSWORD: %r,\n" % (self.id, self.email,
self.password)
output += "AGE: %r, GENDER: %s, OCCUPATION: %s,\n" % (self.age, self.gender,
self.occupation)
output += "ZIPCODE: %s" % self.zipcode
return output
def similarity(self, other):
user_ratings = {}
paired_ratings = []
for rating in self.ratings:
user_ratings[rating.movie_id] = rating
for rating in other.ratings:
overlapping_ratings = user_ratings.get(rating.movie_id)
if overlapping_ratings:
paired_ratings.append( (overlapping_ratings.rating, rating.rating) )
if paired_ratings:
return pearson(paired_ratings)
else:
return 0.0
def predict_rating(self, movie):
ratings = self.ratings
other_ratings = movie.ratings
print self.similarity(other_ratings[0].user_id)
similarities = [ (self.similarity(r.user), r) \
for r in other_ratings ]
similarities.sort(reverse = True)
if not similarities:
return None
else:
print r.rating
print similarities
numerator = sum([ r.rating * similarity for similarity, r in similarities ])
denominator = sum([similarity[0] for similarity in similarities])
return numerator/denominator
class Movie (Base):
__tablename__ = "movies"
id = Column(Integer, primary_key = True)
name = Column(String(120), nullable=False)
released_at = Column(DateTime, nullable=True)
imdb_url = Column(String(120), nullable=True)
def __str__(self):
new_date = datetime.strftime(self.released_at, "%d-%b-%Y")
output = "ID: %r, TITLE: %s,\n" % (self.id, self.name)
output += "RELEASED: %r, URL: %s" % (new_date, self.imdb_url)
return output
class Rating(Base):
__tablename__ = "ratings"
id = Column(Integer, primary_key= True)
movie_id = Column(Integer, ForeignKey('movies.id'))
user_id = Column(Integer, ForeignKey('users.id'))
rating = Column(Integer(1), nullable = False)
movie = relationship("Movie",
backref = backref("ratings", order_by=id))
user = relationship("User",
backref = backref("ratings",order_by = id))
def __str__(self):
output = "ID: %r, MOVIE ID: %r,\n" % (self.id, self.movie_id)
output += "USER ID: %r, RATING: %r" % (self.user_id, self.rating)
return output
### End class declarations
# def connect():
# global ENGINE
# global Session
# return Session()
def main():
"""In case we need this for something"""
pass
if __name__ == "__main__":
main()