Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge branch 'master' of github.com:courserecommender/courserecommender

  • Loading branch information...
commit 66b2a113edde5fa699a431888d2976fdc5351bcb 2 parents 522ebab + d41d249
@josePhoenix josePhoenix authored
Showing with 37 additions and 18 deletions.
  1. +34 −11 courserecommender/models.py
  2. +3 −7 courserecommender/server.py
View
45 courserecommender/models.py
@@ -1,7 +1,7 @@
import flask
from itertools import groupby
import math
-from random import sample
+import random
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import backref, relationship, sessionmaker
@@ -35,13 +35,35 @@ class User(Base, Store):
cluster_id = Column(Integer, ForeignKey("clusters.id"))
cluster = relationship("Cluster", backref=backref("users"))
- def get_similar_users(self):
- users_in_cluster = self.cluster.users
+ def save(self):
+ if self.cluster_id == None:
+ cluster = Cluster.cluster_for_user(self)
+ self.cluster_id = cluster.id
+ super(User, self).save()
+
+ def recommended_courses(self):
+ users_in_cluster = self.cluster.users[:]
users_in_cluster.remove(self)
- random_users = random.sample(users_in_cluster, min(10, len(users_in_cluster)))
+ random_users = random.sample(users_in_cluster, min(5, len(users_in_cluster)))
+
+ good_courses = [u.good_courses() for u in random_users]
+ course_recommendations = []
+ for courses in good_courses:
+ for course in courses:
+ if not course in self.courses() and not course in course_recommendations:
+ course_recommendations.append(course)
+
+ return random.sample(course_recommendations, min(5, len(course_recommendations)))
- u.rankingsrandom_users
+ def courses(self):
+ return self.__class__.session().query(Course).join("rankings", "user").filter(User.id == self.id).all()
+ def good_courses(self):
+ return self.__class__.session().query(Course).join("rankings", "user").filter(User.id == self.id).filter(Ranking.value > 0).all()
+
+ @classmethod
+ def find_all_by_username(klass, username):
+ return self.session().query(User).filter(User.username==username).all()
@classmethod
def similarity(klass, a, b):
@@ -84,7 +106,7 @@ class Ranking(Base, Store):
course_id = Column(Integer, ForeignKey('courses.id'))
value = Column(Integer)
user = relationship("User", backref=backref("rankings"))
- course = relationship("Course", backref=backref("course"))
+ course = relationship("Course", backref=backref("rankings"))
@classmethod
def find_all_by_user_ids(klass, user_ids):
@@ -124,16 +146,17 @@ def make_clusters(klass):
user.save()
@classmethod
- def add_user(klass, user):
+ def cluster_for_user(klass, user):
clusters = klass.all()
+ if len(clusters) == 0:
+ return Cluster()
centroids = [cluster.get_centroid() for cluster in clusters]
cluster_index, similarity = max(
enumerate(User.similarity(item, centroid) for centroid in centroids),
key=lambda (index,similarity): similarity
)
- best_cluster = clusters[cluster_index]
- user.cluster_id = best_cluster.id
- user.save()
+ return clusters[cluster_index]
+
@classmethod
def centroidify(klass, cluster):
@@ -153,7 +176,7 @@ def centroidify(klass, cluster):
@classmethod
def clusterize(klass, items, k, similarity_function, iterations):
- centroids = sample(items, k)
+ centroids = random.sample(items, k)
for i in xrange(iterations):
clusters = [[] for i in xrange(k)]
View
10 courserecommender/server.py
@@ -11,9 +11,6 @@
app.secret_key = 'A0Zr98j/3yX R~XHH!j4mNLWX/,?RT'
db = models.init(os.environ.get("DATABASE_URL", "sqlite:///development.sqlite3"))
-def _get_user():
- return g.db.query(models.User).filter(models.User.username==request.form['username'])[0]
-
@app.before_request
def before_request():
g.db = db()
@@ -44,12 +41,11 @@ def homepage():
@app.route('/auth', methods=['GET', 'POST'])
def auth():
if request.method == "POST":
- print g.db.query(models.User).filter(models.User.username==request.form['username'])
- user = g.db.query(models.User).filter(models.User.username==request.form['username']).count()
+ print models.User.find_all_by_username(request.form['username'])
+ user = len(models.User.find_all_by_username(request.form['username']))
if not user:
new_user = models.User(username=request.form['username'])
- g.db.add(new_user)
- g.db.commit()
+ new_user.save()
session['username'] = request.form['username']
return redirect('/')
else:
Please sign in to comment.
Something went wrong with that request. Please try again.