''' 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 = "sec3_recommendations.csv" def read_file(filename): ''' Function: read_file Parameter: filename, a string Returns: 2d list of strings Does: iterates over csv file, turning each row into item in 2d list ''' 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: iterates over list, turns each row into an object ''' shows = [] for row in lst: title = row[0] length = int(row[1]) seasons = int(row[2]) rating = float(row[3]) age = int(row[4]) show = TVShow(title, length = length, seasons = seasons, rating = rating, age = age) shows.append(show) return shows def main(): # gather data - read 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")) seasons = int(input("How many seasons?\n")) age = int(input("How old are you?\n")) # computation - make a list of TVShow objects show_objs = make_show_objs(show_lst) # computation - make an object of the TVShow class user_show = TVShow(title, age = age, length = length, seasons = seasons) # computation - filter the list of show_objs to make recommendations recs = [] for show in show_objs: if show.recommend(user_show): recs.append(show) # computation - find the nearest euclidean distance min_show = None min_dist = float("inf") for rec in recs: dist = rec.euclidean(user_show) print("Got distance:", dist) if dist < min_dist: min_dist = dist min_show = rec # communication - tell the user what we recommend print("\n\nDS2000 thinks Laney should watch...") for rec in recs: print(rec) # communication - tell the user the smallest distance print("\nBut start with...", min_show) main()