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")
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')
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)
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
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))
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)
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
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])
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)
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")
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)
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)
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)
def test_averages(self): self.assertEqual(RecommenderEngine.averages(items, ratings), [('Bear', 2.0), ('Cat', 2.0), ('Dog', 1.6666666666666667)])
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))
''' Created on Dec 3, 2015 @author: Jonathan Yu ''' import RecommenderEngine, json import SimpleFoodReader import BookReader if __name__ == "__main__": (jitems,jratings) = BookReader.getData("bookratings.txt") #(jitems,jratings) = SimpleFoodReader.getData("foodratings_example.txt") items = json.loads(jitems) ratings = json.loads(jratings) avg = RecommenderEngine.averages(items,ratings) #just show book recommendations, not average ratings top = ", ".join([d[0] for d in avg[:5]]) bottom = ", ".join([d[0] for d in avg[len(avg) - 5:]]) print "The top recommendations are", top print "The bottom recommendations are", bottom
Created on Nov 24, 2015 @author: ola ''' import RecommenderEngine, json import SimpleFoodReader import BookReader import MovieReader if __name__ == "__main__": #(jitems,jratings) = SimpleFoodReader.getData("foodratings_example.txt") (jitems,jratings) = BookReader.getData("bookratings.txt") #(jitems,jratings) = MovieReader.getData("movieratings.txt") print "items = ",jitems print "ratings = ", jratings items = json.loads(jitems) ratings = json.loads(jratings) avg = RecommenderEngine.averages(items,ratings) print avg for key in ratings: slist = RecommenderEngine.similarities(key,ratings) print key,slist print "\t",RecommenderEngine.scores(slist,items,ratings,1) print "\t",RecommenderEngine.scores(slist,items,ratings,len(slist)) print RecommenderEngine.recommend(ratings.keys()[0], items, ratings, 5) #for the above test, I randomly chose a name and a count
@author: Jonathan Yu ''' import RecommenderEngine, json import MovieReader import random if __name__ == "__main__": (jitems, jratings) = MovieReader.getData("movieratings.txt") items = json.loads(jitems) ratings = json.loads(jratings) name = ratings.keys()[random.randint(0, len(ratings) + 1)] count = 15 recs = RecommenderEngine.recommend(name, items, ratings, count) recs = ", ".join(d[0] for d in recs[:10]) print "Using " + str( count ) + " other users' ratings, our top ten recommendations for " + name + " are " + recs + "." print name = ratings.keys()[random.randint(0, len(ratings) + 1)] count = 20 recs = RecommenderEngine.recommend(name, items, ratings, count) recs = ", ".join(d[0] for d in recs[:10]) print "Using " + str( count ) + " other users' ratings, our top ten recommendations for " + name + " are " + recs + "."
''' Created on Nov 28, 2015 @author: tedmarchildon ''' import RecommenderEngine, json import BookReader if __name__ == "__main__": (jitems,jratings) = BookReader.getData("bookratings.txt") print "items = ",jitems print "ratings = ", jratings items = json.loads(jitems) ratings = json.loads(jratings) avg = RecommenderEngine.averages(items,ratings) #print avg for key in ratings: slist = RecommenderEngine.similarities(key,ratings) print key,slist print "\t",RecommenderEngine.scores(slist,items,ratings,1) print "\t",RecommenderEngine.scores(slist,items,ratings,len(slist)) print '\t', RecommenderEngine.recommend(key, items, ratings, 3)
def test_similarities(self): self.assertEqual(RecommenderEngine.similarities("student1", ratings), [('student2', 5), ('student3', 4)])
''' Created on Dec 4, 2015 @author: Jonathan Yu ''' import RecommenderEngine, json import MovieReader import random if __name__ == "__main__": (jitems,jratings) = MovieReader.getData("movieratings.txt") items = json.loads(jitems) ratings = json.loads(jratings) name = ratings.keys()[random.randint(0, len(ratings) + 1)] count = 15 recs = RecommenderEngine.recommend(name, items, ratings, count) recs = ", ".join(d[0] for d in recs[:10]) print "Using " + str(count) + " other users' ratings, our top ten recommendations for " + name + " are " + recs + "." print name = ratings.keys()[random.randint(0, len(ratings) + 1)] count = 20 recs = RecommenderEngine.recommend(name, items, ratings, count) recs = ", ".join(d[0] for d in recs[:10]) print "Using " + str(count) + " other users' ratings, our top ten recommendations for " + name + " are " + recs + "."
def test_recommendations(self): self.assertEqual( RecommenderEngine.recommendations("student1", items, ratings, 2), [('Cat', 8.5), ('Dog', 6.5), ('Bear', 0.0)])
Created on Nov 24, 2015 @author: ola ''' import RecommenderEngine, json import SimpleFoodReader import BookReader import MovieReader if __name__ == "__main__": #(jitems,jratings) = SimpleFoodReader.getData("foodratings_example.txt") (jitems, jratings) = BookReader.getData("bookratings.txt") #(jitems,jratings) = MovieReader.getData("movieratings.txt") print "items = ", jitems print "ratings = ", jratings items = json.loads(jitems) ratings = json.loads(jratings) avg = RecommenderEngine.averages(items, ratings) print avg for key in ratings: slist = RecommenderEngine.similarities(key, ratings) print key, slist print "\t", RecommenderEngine.scores(slist, items, ratings, 1) print "\t", RecommenderEngine.scores(slist, items, ratings, len(slist)) print RecommenderEngine.recommend(ratings.keys()[0], items, ratings, 5) #for the above test, I randomly chose a name and a count
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 if __name__ == '__main__': name = 'Jeanice' items = ['lions', 'tigers', 'bears'] ratings = dict([('Nohemi', [1, -2, 1]), ('Ines', [-2, 2, 0]), ('Evelynn', [-1, -2, -1]), ('Jeanice', [1, -2, 1])]) size = 2 sim = RecommenderEngine.similarities(name, ratings) print('sim =', sim) # Printing for your convenience ret = RecommenderEngine.recommendations(name, items, ratings, size) print(ret[0][0]) print(ret[0][1])