''' DS2000 Spring 2023 Sample code from class - recommendation sytem - driver Algorithm: * Have a bunch of TVShow objects =====> NEXT WEEK * Ask the user (Laney) for info about a show they 've watched =====> TODAY * Recommend shows with similar episode lengths, season, user age, and with high rating Possible todo --- ask the user for their rating, and recommend opposite of something they don't like, similar of something they like ''' import csv from tvshow import TVShow SHOW_FILE = "sec2_recommendations.csv" def read_file(filename): ''' Function: read_file Parameter: filename, a string Returns: 2d list of strings Does: iterates over the file, each row becomes a sublist ''' data = [] with open(filename, "r") as infile: csvfile = csv.reader(infile) next(csvfile) for row in csvfile: data.append(row) return data def make_show_objs(lst): ''' Function: make_show_objs Parameters: 2d list of strings Returns: 1d list of TVShow objects Does: literates over the list, each row becomes an object ''' objs = [] for row in lst: title = row[0] newness = int(row[1]) pop = int(row[2]) length = int(row[3]) rating = float(row[4]) show = TVShow(title, newness = newness, popularity = pop, length = length, rating = rating) objs.append(show) return objs def main(): # Gather data - read in from the file show_lst = read_file(SHOW_FILE) # Gather data - ask the user for info about their show title = input("What show?\n") new = int(input("How new is it? 1=old, 10=new\n")) length = int(input("Length of episodes?\n")) popular = int(input("How popular?\n")) # computation - make an object of the TVShow class user_show = TVShow(title, length = length, popularity = popular, newness = new) # computation - make a list of show objects show_objs = make_show_objs(show_lst) # computation - filter the shows into recommendations recs = [] for lst_show in show_objs: if lst_show.recommend(user_show): recs.append(lst_show) # communication - print out the recommended shows print("DS2000 thinks Laney should watch...\n") for rec in recs: print(rec) # rank the recommendations - pick the one with smallest # euclidean distance to watch first! min_show = None min_distance = float("inf") for rec in recs: distance = rec.euclidean(user_show) if distance < min_distance: min_distance = distance min_show = rec # Communicate - recommend the first show to watch print("\n\nBut start with ......") print(min_show) main()