''' 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 = "sec1_recommendations.csv" def read_file(filename): ''' Function: read_file Parameters: filename, a string Returns: contents of the file as a 2d list Does: iterates over each row, generating a 2d list of strings ''' data = [] with open(filename, "r") as infile: csvfile = csv.reader(infile) next(csvfile) for row in csvfile: data.append(row) return data def make_objects(lst): ''' Function: make_objects Parameter: 2d list of strings Returns: 1d list of TVSHow objects Does: iterates over the given list, each row turns into an object ''' show_objs = [] for row in lst: title = row[0] rating = float(row[1]) pop = int(row[2]) length = int(row[3]) show = TVShow(title, length = length, popularity = pop, rating = rating) show_objs.append(show) return show_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") length = int(input("Length of episodes?\n")) popular = int(input("How popular?\n")) rating = int(input("What is your rating?\n")) # computation - generate a list of tv show objects show_objs = make_objects(show_lst) # computation - make an object of the TVShow class show = TVShow(title, length = length, popularity = popular, rating = rating) # computation - make a list of recommended shows recs = [] for curr_show in show_objs: if curr_show.recommend(show): recs.append(curr_show) # print recommendations print("DS2000 thinks you should watch...") for rec in recs: print(rec) # to start with, recommend the show with the closest distance # to user's show min_show = None min_dist = float("inf") for rec in recs: distance = rec.euclidean(show) if distance < min_dist: min_dist = distance min_show = rec # Finally, report the show to start with! print("\n\n...But start with:", min_show) main()