Ejemplo n.º 1
0
def makerecs(name, items, ratings, size, top):
    notseen = []
    seen = []
    newdic = {}
    recommend = RecommenderEngine.recommendations(name, items, ratings, size)
    for title in items:
        for k, v in recommend:
            if title == k:
                newdic[title] = v
    for num in range(len(ratings[name])):
        if ratings[name][num] == 0:
            title = items[num]
            value = newdic[title]
            notseen.append((title, value))
        else:
            title = items[num]
            value = newdic[title]
            seen.append((title, value))
    seen = [
        item for item in sorted(seen, key=operator.itemgetter(1), reverse=True)
    ]
    notseen = [
        item
        for item in sorted(notseen, key=operator.itemgetter(1), reverse=True)
    ]
    topsim = seen[:(top)]
    #if student has seen it
    toprec = notseen[:(top)]
    #if student hasn't seen it
    return ((topsim, toprec))
Ejemplo n.º 2
0
def makerecs(name, items, ratings, numUsers, top):
    '''
    This function calculates the top recommendations and returns a two-tuple consisting of two lists.
    The second list is the top items not seen/rated by name (string)
    '''
    newAverages = RecommenderEngine.recommendations(name, items, ratings,
                                                    numUsers)
    eaten = []
    notEaten = []
    i = 0
    for item in newAverages:
        if ratings[name][items.index(item[0])] != 0:
            eaten.append(newAverages[i])
            i += 1
        if ratings[name][items.index(item[0])] == 0:
            notEaten.append(newAverages[i])
            i += 1
    eaten = sorted(eaten, key=lambda x: x[0])
    eaten = sorted(eaten, key=lambda x: x[1], reverse=True)
    eaten = eaten[0:top]
    notEaten = sorted(notEaten, key=lambda x: x[0])
    notEaten = sorted(notEaten, key=lambda x: x[1], reverse=True)
    notEaten = notEaten[0:top]

    return (eaten, notEaten)
Ejemplo n.º 3
0
def makerecs(name, items, ratings, numUsers, top):
    '''
    This function calculates the top recommendations and returns a two-tuple consisting of two lists.
    The second list is the top items not seen/rated by name (string)
    '''

    newList = RecommenderEngine.recommendations(name, items, ratings, numUsers)
    topThree = []
    person = []
    for i in ratings:
        if i == name:
            person = ratings[i]
            break
    counter = 0
    for i in range(0, len(items)):
        if counter == top:
            break
        if person[i] == 0:
            tuple = (items[i], newList[i][1])
            topThree.append(tuple)
            counter += 1
    if len(topThree):
        for i in range(0, top):
            topThree.append(newList[i])
    return topThree
Ejemplo n.º 4
0
def driver():
    (items,ratings) = SmallDukeEatsReader.getdata()
    print("items = ",items)
    print("ratings = ", ratings)
    
    avg = RecommenderEngine.averages(items,ratings)
    if avg == [('DivinityCafe', 4.0), ('TheCommons', 3.0), ('Tandoor', 2.4285714285714284), ('IlForno', 1.8),
                ('FarmStead', 1.4), ('LoopPizzaGrill', 1.0), ('TheSkillet', 0.0), ('PandaExpress', -0.2), 
                ('McDonalds', -0.3333333333333333)]:
        print("averages work")
    else:
        print("averages fails")

    
    similarities = RecommenderEngine.similarities("Sung-Hoon", ratings)
    if similarities == [('Wei', 1), ('Sly one', -1), ('Melanie', -2), ('Sarah Lee', -6), ('J J', -14), ('Harry', -24), 
                        ('Nana Grace', -29)]:
        print("similarities work")
    else:
        print("similarities fails")

    recommendations = RecommenderEngine.recommendations("Sarah Lee", ["DivinityCafe", "FarmStead", "IlForno","LoopPizzaGrill", "McDonalds", "PandaExpress",
     "Tandoor", "TheCommons", "TheSkillet"], {"Sarah Lee":[3, 3, 3, 3, 0, -3, 5, 0, -3], "Melanie": 
    [5, 0, 3, 0, 1, 3, 3, 3, 1],"J J":[0, 1, 0, -1, 1, 1, 3, 0, 1], "Sly one":[5, 0, 1, 3, 0, 0, 3, 3, 3], 
    "Sung-Hoon": [0, -1, -1, 5, 1, 3, -3, 1, -3], "Nana Grace":[5, 0, 3, -5, -1, 0, 1, 3, 0], "Harry":
    [5, 3, 0, -1, -3, -5, 0, 5, 1], "Wei":[1, 1, 0, 3, -1, 0, 5, 3, 0]}, 3)
    
    if recommendations == [('Tandoor', 149.5), ('TheCommons', 128.0), ('DivinityCafe', 123.33333333333333), ('FarmStead', 69.5),
                           ('TheSkillet', 66.0), ('LoopPizzaGrill', 62.0), ('IlForno', 33.0), ('McDonalds', -69.5), ('PandaExpress', -165.0)]:
        print("recommendations work")
    else:
        print("recommendations fails")
Ejemplo n.º 5
0
def driver():
    '''
    Testing recommender assignment functions with Duke eateries
    '''
    (items, ratings) = SmallDukeEatsReader.getdata()
    #     print("items = ",items)
    #     print("ratings = ", ratings)

    avg = RecommenderEngine.averages(items, ratings)
    #     print("average",avg)
    if avg == correctAvg:
        print('averages works')
    else:
        print('average fails')

    for key in ratings:
        slist = RecommenderEngine.similarities(key, ratings)
        #         print(key,slist)
        r3 = RecommenderEngine.recommendations(key, items, ratings, 3)
        #         print("top",r3)

        if key == 'Sung-Hoon' and slist == correctSim:
            print('similarities works')
        elif key == 'Sung-Hoon' and slist != correctSim:
            print('similarities fails')
        if key == 'Sarah Lee' and r3 == correctRec:
            print('recommendations works')
        elif key == 'Sarah Lee' and r3 != correctRec:
            print('recommendations fails')
Ejemplo n.º 6
0
def makerecs(name, items, ratings, size, top):
    movies = []
    top5seen = []
    top5notseen = []

    bestups = RecommenderEngine.recommendations(name, items, ratings, size)

    f = open("data/movies.txt")
    for line in f:
        data = line.strip().split(",")
        if data[0] == name:
            movies.append(data[1])

    for tup in bestups:
        if tup[0] in movies:
            top5seen.append(tup)

    top5seen = [top5seen[x][0] for x in range(top)]

    for tup in bestups:
        if tup[0] not in movies:
            top5notseen.append(tup)

    top5notseen = [top5notseen[x][0] for x in range(top)]

    return top5seen, top5notseen
Ejemplo n.º 7
0
def driver():
    (items, ratings) = SmallDukeEatsReader.getdata()
    print("items = ", items)
    print("ratings = ", ratings)

    avg = RecommenderEngine.averages(items, ratings)
    print("average", avg)

    for key in ratings:
        slist = RecommenderEngine.similarities(key, ratings)
        print(key, slist)
        r3 = RecommenderEngine.recommendations(key, items, ratings, 3)
        print("top", r3)
Ejemplo n.º 8
0
def driver():
    '''(items,ratings) = SmallDukeEatsReader.getdata()
    print("items = ",items)
    print("ratings = ", ratings)'''

    avg = RecommenderEngine.averages(items, ratings)
    print("average", avg)
    slist = RecommenderEngine.similarities("student1", ratings)
    print(slist)
    for key in ratings:
        slist = RecommenderEngine.similarities(key, ratings)
        print(key, slist)

    r3 = RecommenderEngine.recommendations("student1", items, ratings, 2)
    print(r3)

    print(MovieReader.getdata()[:5])
    print(BookReader.getdata()[:5])
Ejemplo n.º 9
0
def makerecs(name, items, ratings, size, top):
    '''
    This function calculates the top recommendations and returns a two-tuple consisting of two lists. 
    The first list is the top items rated by the rater called name (string).
    The second list is the top items not seen/rated by name (string)
    '''
    recs=RecommenderEngine.recommendations(name,items,ratings,size)
    seen=[]
    notseen=[]
    for r in recs:
        item=r[0]
        loc=items.index(item)
        if ratings[name][loc]==0 and len(notseen)!=top:
            notseen.append(r)
        if ratings[name][loc]!=0 and len(seen)!=top:
            seen.append(r)
        if len(seen)==top and len(notseen)==top:
            return (seen,notseen)
    return(seen,notseen)
Ejemplo n.º 10
0
def driver():
    '''
    This function uses the file SmallDukeEatsReads to test functions 
    in RecommenderEngine by outputting validation messages.
    '''

    (items, ratings) = SmallDukeEatsReader.getdata()
    print("items = ", items)  #len 9
    print("ratings = ", ratings)

    avg = RecommenderEngine.averages(items, ratings)
    print("average", avg)
    if avg == [('DivinityCafe', 4.0), ('TheCommons', 3.0),
               ('Tandoor', 2.4285714285714284), ('IlForno', 1.8),
               ('FarmStead', 1.4), ('LoopPizzaGrill', 1.0),
               ('TheSkillet', 0.0), ('PandaExpress', -0.2),
               ('McDonalds', -0.3333333333333333)]:
        print("averages works")
    else:
        print("averages fails")

    for key in ratings:
        slist = RecommenderEngine.similarities(key, ratings)
        print(key, slist)
        if key == "Sung-Hoon":
            if slist == [('Wei', 1), ('Sly one', -1), ('Melanie', -2),
                         ('Sarah Lee', -6), ('J J', -14), ('Harry', -24),
                         ('Nana Grace', -29)]:
                print("similarities works")
            else:
                print("similarities fails")
        r3 = RecommenderEngine.recommendations(key, items, ratings, 3)
        if key == "Sarah Lee":
            if r3 == [('Tandoor', 149.5), ('TheCommons', 128.0),
                      ('DivinityCafe', 123.33333333333333),
                      ('FarmStead', 69.5), ('TheSkillet', 66.0),
                      ('LoopPizzaGrill', 62.0), ('IlForno', 33.0),
                      ('McDonalds', -69.5), ('PandaExpress', -165.0)]:
                print("recommendations works")
            else:
                print("recommendations fails")
        print("top", r3)
Ejemplo n.º 11
0
def driver():

    (items, ratings) = SmallDukeEatsReader.getdata()
    print("items = ", items)
    print("ratings = ", ratings)

    avg = RecommenderEngine.averages(items, ratings)
    print("average", avg)
    answer = [('DivinityCafe', 4.0), ('TheCommons', 3.0),
              ('Tandoor', 2.4285714285714284), ('IlForno', 1.8),
              ('FarmStead', 1.4), ('LoopPizzaGrill', 1.0), ('TheSkillet', 0.0),
              ('PandaExpress', -0.2), ('McDonalds', -0.3333333333333333)]
    if avg == answer:
        print("average works")
    else:
        print("average fails")

    for key in ratings:
        slist = RecommenderEngine.similarities(key, ratings)
        print(key, slist)
        answer = [('Wei', 1), ('Sly one', -1), ('Melanie', -2),
                  ('Sarah Lee', -6), ('J J', -14), ('Harry', -24),
                  ('Nana Grace', -29)]
        if answer == slist and key == 'Sung-Hoon':
            print("similarities works")
        if answer != slist and key == 'Sung-Hoon':
            print("similarities fails")
        r3 = RecommenderEngine.recommendations(key, items, ratings, 3)
        print("top", r3)
        answer = [('Tandoor', 149.5), ('TheCommons', 128.0),
                  ('DivinityCafe', 123.33333333333333), ('FarmStead', 69.5),
                  ('TheSkillet', 66.0), ('LoopPizzaGrill', 62.0),
                  ('IlForno', 33.0), ('McDonalds', -69.5),
                  ('PandaExpress', -165.0)]
        if answer == r3 and key == 'Sarah Lee':
            print("recommendations works")
        if answer != r3 and key == 'Sarah Lee':
            print("recommendations fails")
Ejemplo n.º 12
0
def driver():

    (items, ratings) = SmallDukeEatsReader.getdata()
    print("items = ", items)
    print("ratings = ", ratings)

    avg = RecommenderEngine.averages(items, ratings)
    if avg == [('DivinityCafe', 4.0), ('TheCommons', 3.0),
               ('Tandoor', 2.4285714285714284), ('IlForno', 1.8),
               ('FarmStead', 1.4), ('LoopPizzaGrill', 1.0),
               ('TheSkillet', 0.0), ('PandaExpress', -0.2),
               ('McDonalds', -0.3333333333333333)]:
        print("AVERAGES WORKS")
    else:
        print("AVERAGES FAILS")

    print("average", avg)

    for key in ratings:
        slist = RecommenderEngine.similarities(key, ratings)
        print(key, slist)
        r3 = RecommenderEngine.recommendations(key, items, ratings, 3)
        print("top", r3)
Ejemplo n.º 13
0
def makerecs(name, items, ratings, numUsers, top):
    '''
    This function calculates the top recommendations and returns a two-tuple consisting of two lists. 
    The first list is the top items rated by the rater called name (string).
    The second list is the top items not seen/rated by name (string)
    '''
    #[('A Beautiful Mind', 335.0), ('Black Swan', 335.0), ('Anchorman: The Legend of Ron Burgundy', 247.5)]
    avgTups = RecommenderEngine.recommendations(name, items, ratings, numUsers)
    #Correct. the movies the person has rated.
    hasRated = []
    #get the movies the person has rated.
    for i in range(len(items)):
        if ratings[name][i] != 0:  #check to see the movie has been rated
            hasRated.append(items[i])  #append that movie into hasRated.

    #pick out the top hasRated values to put in favorites.
    favorites = [
    ]  #Correct. [('Anchorman: The Legend of Ron Burgundy', 247.5), ('Avatar', 215.5), ('The Godfather', 201.0)]
    #the (movie, avgrating) for movies the person rated.
    for tuple in avgTups:
        if tuple[0] in hasRated:  #the movie has been rated
            favorites.append(tuple)
    #print(favorites)
    topfavs = favorites[:top]
    #print(topfavs)

    #pick out the top notRated values to put in recLst.
    recLst = [
    ]  #notRate[:top] [('A Beautiful Mind', 335.0), ('Black Swan', 335.0), ('A Nightmare on Elm Street', 96.0)]
    for tuple in avgTups:
        if tuple[
                0] not in hasRated:  #pick out movies that have not been rated.
            recLst.append(tuple)
    #print(recLst)
    toprec = recLst[:top]

    return (topfavs, toprec)
Ejemplo n.º 14
0
    for r in recs:
        item=r[0]
        loc=items.index(item)
        if ratings[name][loc]==0 and len(notseen)!=top:
            notseen.append(r)
        if ratings[name][loc]!=0 and len(seen)!=top:
            seen.append(r)
        if len(seen)==top and len(notseen)==top:
            return (seen,notseen)
    return(seen,notseen)

if __name__ == '__main__':
    name = "student1367"
    items = ['127 Hours', 'The Godfather', '50 First Dates', 'A Beautiful Mind', 'A Nightmare on Elm Street', 'Alice in Wonderland', 'Anchorman: The Legend of Ron Burgundy', 'Austin Powers in Goldmember', 'Avatar', 'Black Swan']

    ratings = {'student1367': [2, 3, -5, 2, 3, 1, 5, 1, 3, 4], 
               'student1046': [2, 3, -5, 2, 3, 1, 5, 1, 3, 4], 
               'student1206': [2, 3, -5, 2, 3, 1, 5, 1, 3, 4], 
               'student1103': [2, 3, -5, 2, 3, 1, 5, 1, 3, 4]}

    size = 2
 
    top = 3
    print(RecommenderEngine.recommendations(name,items,ratings,size))
    print(makerecs(name,items,ratings,size,top))

             



Ejemplo n.º 15
0
 def test_recommendations(self):
     self.assertEqual(
         RecommenderEngine.recommendations("student1", items, ratings, 2),
         [('Cat', 8.5), ('Dog', 6.5), ('Bear', 0.0)])