''' DS2000 Spring 2022 Building a rcommendation system from Review objects Some refinements we could try... 1. Gather genre data from the reviews, recommend most-watched genres 2. When the show was on, current vs old timey 3. Maturity rating (pg, pg-13, etc.) 4. User demographics. Recommend to someone similar to you. 5. Recommend only to someone who has shows in common with you 6. Recommend the overall most-watched, or most popular shows 7. Use platform compatibility. Don't recommend something you can't watch. ''' import csv from review import Review REVIEW_FILE = "reviews_sec1.csv" def csv_to_dict(filename): ''' Function: csv_to_dictt Parameter: filename, a string returns: a list of dictionaries, where key = column header, value = value from a row ''' list_of_dict = [] with open(filename, "r") as infile: csvfile = csv.reader(infile, delimiter = ",") keys = next(csvfile) for row in csvfile: d = {} for i in range(len(keys)): d[keys[i]] = row[i] list_of_dict.append(d) return list_of_dict def make_review_objs(list_of_dict, person): ''' function: make_laney_objs paramter: list of dictionaries, where one dictionary = one show and the name of a person to look for (string) returns: list of Review objects ''' reviews = [] for item in list_of_dict: name = item["Name"] if name == person: title = item["Title"] rating = int(item["Rating"]) platform = item["Platform"] review = Review(name, title, rating, platform = platform) reviews.append(review) return reviews def make_recommendations(laney_reviews, friend_reviews, platforms): ''' Function: make _recomjendations Paramaeters: list of review objects (laney's), list of review objects (friend's), list of acceptable platforms (list of strings) Returns: list of review objects(recommendations) ''' recs = [] for review in friend_reviews: if review.rating > 3 and review not in laney_reviews: if review.platform in platforms: recs.append(review) return recs def shows_in_common(laney, friend): ''' Function: shows in common parameters: 2 lists of review objects (Laney, and recommender) Return: # of shows in common ''' count = 0 for review in laney: if review in friend: count += 1 return count def main(): # Step one -- gather data # Make a list of dictionaries for all the reviews reviews = csv_to_dict(REVIEW_FILE) # print(reviews) # Make a list of review objects for Laney laney_objs = make_review_objs(reviews, "Laney") platforms = [] for review in laney_objs: platforms.append(review.platform) # Make a list of review objects for my recommender friend = input("Who is recommending to Laney?\n") friend_objs = make_review_objs(reviews, friend) # Get number of shows in common in_common = shows_in_common(laney_objs, friend_objs) if in_common == 0: print("You and", friend, "have nothing in common, but " "here are their recs...") elif in_common == 1: print("You might want to watch...") else: print("You should DEFINITELY watch omg...") # Make recommendations! recs = make_recommendations(laney_objs, friend_objs, platforms) for rec in recs: print(rec) main()