def recompute_top_choices(self):
        for suggestable in Session.query(model.Suggestable):
            debug_print( suggestable )
            suggestable.top_choices = []
            Session.add(suggestable) 

            suggestions = Session.query(model.Suggestion).filter(
                    or_(
                        model.Suggestion.high_choice_id==suggestable.id, 
                        model.Suggestion.low_choice_id==suggestable.id
                        )
                    )
            recomendations = defaultdict(lambda:0)
            for suggestion in suggestions:
                high = suggestion.high_choice
                low = suggestion.low_choice
                rec = high if high.id != suggestable.id else low
                if rec.category.name != 'team':
                    continue

                weight = rec.weight or 1
                recomendations[rec.id] += suggestion.weight/weight

            rec_list = [ (x,y) for x,y in recomendations.items()]
            rec_list.sort(key=lambda x:-x[1])
            for rec in rec_list[:5]:
                top_choice = model.TopChoice()
                top_choice.chooser = suggestable
                top_choice.choice_id = rec[0]
                top_choice.weight = rec[1]
                Session.add(top_choice)
        Session.commit()
    def test_add_several(self):
        rec = dbRecomender()
        rec.add_row([4,5,6])
        for item in Session.query(model.Suggestable).all():
            print "weight: ", item.name,
            print item.weight
            assert item.weight == 1

        high = []
        low = []
        for item in Session.query(model.Suggestion).all():
            print item
            print "high: ", item.high_choice_id,
            print "low:  ", item.low_choice_id
            high.append(item.high_choice)
            low.append(item.low_choice)
            assert not item.high_choice.id < item.low_choice.id

        high =  map(lambda x: int(x.id), high)
        low =  map(lambda x: int(x.id), low)
        high.sort()
        low.sort()
        print high
        print low
        assert high == [2,3,3]
        assert low == [1,1,2]
Beispiel #3
0
 def dict_ajax(self):
     selected = request.POST.getone("selected")
     deselected = request.POST.getone("deselected")
     sid_hash = request.POST.getone("sid_hash")
     if selected:
         like = model.Like(sid_hash, selected)
         Session.save(like)
         Session.commit()
     if deselected:
         unliked = Session.query(model.Like).filter_by(sid_hash=sid_hash, item=deselected).first()
         print unliked
         Session.delete(unliked)
         Session.commit()
     all_likes = []
     for row in Session.query(model.Like.item).filter_by(sid_hash=sid_hash):
         all_likes.append(row[0])
     print all_likes
     suggestions = []
     c.recs = []
     if len(all_likes) > 0:
         for suggestion in g.rec.suggest(all_likes):
             if suggestion[0] in g.teams:
                 suggestions.append(suggestion[0])
         c.recs = suggestions[:3]
     return render("/xml_recs.mak")
    def deep_recomend(self,items):
        recomendations = {}
        for item in items:
            if not item: continue
            suggestions = Session.query(model.Suggestion).filter(
                    or_(
                        model.Suggestion.high_choice_id==item, 
                        model.Suggestion.low_choice_id==item
                        )
                    )
            for suggestion in suggestions.all():
                high = suggestion.high_choice
                low = suggestion.low_choice
                rec = high if high.id != item else low
                if rec.category.name != 'team':
                    continue

                results = Session.query(model.Suggestable).filter_by(id=rec)

                weight = rec.weight or 1

                if rec.id in recomendations.keys():
                    recomendations[rec.id] += suggestion.weight/weight
                else:
                    recomendations[rec.id] = suggestion.weight/weight

        rec_list = [ (x,y/len(items)) for x,y in recomendations.items()]
        rec_list.sort(key=lambda x:-x[1])
        return filter(lambda x: x[0] not in items, rec_list)
Beispiel #5
0
 def ajax(self):
     selected = request.POST.getone("selected").lower()
     deselected = request.POST.getone("deselected").lower()
     print "---!!!###!!!---"
     print selected
     print deselected
     sid_hash = request.POST.getone("sid_hash")
     recomender = dbRecomender()
     if selected:
         recomender.add_by_sid(sid_hash, selected)
     if deselected:
         print "$$$$$$$$$$$\ndeselecting%s\n$$$$$$$$$$$$$$$" % deselected
         recomender.remove_by_sid(sid_hash, deselected)
     recomendations = recomender.recomend_by_sid(sid_hash)
     c.recs = map(lambda x: x[0], recomendations)
     c.recs = c.recs[:3]
     c.avail = []
     for selection in Session.query(model.Interaction).filter_by(sid_hash=sid_hash).first().choices:
         for avail in selection.available_choices:
             if avail.choice.id not in c.recs:
                 c.avail.append(avail.choice.id)
     response.content_type = "text/xml"
     r = render("/xml_recs.mak")
     print r
     return r
    def test_recomend(self):
        rec = dbRecomender()
        rec.add_row(['z','a','b','c','d'])
        rec.add_row(['z','a','b','d'])
        rec.add_row(['z','a'])

        s = {}
        for item in Session.query(model.Suggestable).all():
            print item
            s[item.name] = item.id

        for sug in Session.query(model.Suggestion).all():
            print sug
            
        recomendations = rec.recomend([s['z'],s['b']])
        print recomendations
        assert recomendations == [(1, 1.0), (5, 1.0), (3, 0.83333333333333326)] 
    def modify_by_sid(self, sid_hash, item, weight):
        results = Session.query(model.Interaction).filter_by(sid_hash=sid_hash)
        if (results.count() != 0):
            interaction = results.first()
        else:
            interaction = model.Interaction(sid_hash)
        choices = map(lambda x: x.id, interaction.choices)
        if interaction.choices:
            self.add(item,choices,weight)
        item_suggestable = Session.query(model.Suggestable).filter_by(id=item).first()
        if item_suggestable and weight > 0:
            interaction.choices.append(item_suggestable)
        elif weight < 0:
            interaction.choices.remove(item_suggestable)

        Session.add(interaction)
        Session.commit()
 def recomend_by_sid(self,sid_hash):
     interaction = Session.query(model.Interaction).filter_by(sid_hash=sid_hash).first()
     if interaction:
         choices = map(lambda x: x.id, interaction.choices)
     if ( len(choices) > 0 ):
         return self.recomend(choices)
     else:
         return []
 def recomend(self,items):
     recomendations = defaultdict(lambda:0)
     for item in items:
         suggestable = Session.query(model.Suggestable).filter_by(id=item).first()
         debug_print(suggestable)
         if not suggestable: continue
         for top_choice in suggestable.top_choices:
             recomendations[top_choice.choice_id] += top_choice.weight
     rec_list = sorted(recomendations.iteritems(), key=itemgetter(1), reverse=True)
     debug_print(rec_list)
     return [ rec for rec in rec_list if rec[0] not in items ][:3]
Beispiel #10
0
    def add(self,item,relations,weight=1):
        debug_print("Adding %s to %s" % (", ".join(map(str,relations)),item))
        result = Session.query(model.Suggestable).filter_by(id=item)
        if (result.count() > 0):
            item_suggestable = result.first()
        else:
            return

        item_suggestable.weight += weight
        Session.add(item_suggestable)

        for relation in relations:
            if (relation == item):
                continue

            result = Session.query(model.Suggestable).filter_by(id=relation)
            relation_suggestable = result.first()
            if not relation_suggestable: continue

            results = Session.query(model.Suggestion).filter(
                    or_(
                        and_( model.Suggestion.high_choice_id == relation_suggestable.id,
                              model.Suggestion.low_choice_id == item_suggestable.id
                              ),
                        and_( model.Suggestion.high_choice_id == item_suggestable.id,
                              model.Suggestion.low_choice_id == relation_suggestable.id
                              )
                        )
                    )
            if (results.count() > 0):
                suggestion = results.first()
                suggestion.weight += weight
                if suggestion.weight <= 0:
                    Session.delete(suggestion)
                else:
                    Session.add(suggestion)
            else:
                suggestion = model.Suggestion(item_suggestable,relation_suggestable)
                Session.add(suggestion)
            Session.commit()
Beispiel #11
0
 def index(self):
     # Return a rendered template
     # return render('/choose.mako')
     # or, return a response
     print request.environ.get("REMOTE_ADDR")
     sid = request.environ.get("REMOTE_ADDR") + str(time.time())
     c.majors = {}
     c.teams = []
     for item in Session.query(model.Category).all():
         if item.name == "team":
             c.teams = item.members
         else:
             c.majors[item.name] = item.members
             for member in item.members:
                 print member
     print c.categories
     c.sid_hash = hashlib.md5(sid).hexdigest()
     # soup =  BeautifulSoup(render('/rec.mak'))
     return render("/rec.mak")
 def test_add_one(self):
     rec = dbRecomender()
     rec.add(1,[])
     one = Session.query(model.Suggestable).filter_by(id=1).first()
     print one.weight
     assert one.weight == 1
Beispiel #13
0
 def info(self, id):
     team = Session.query(model.Suggestable).filter_by(id=id).first()
     if team:
         return team.html
     else:
         return ""