From ab207b3f31556b6f53c6638bbf16adb85bc63799 Mon Sep 17 00:00:00 2001 From: Hykilpikonna Date: Mon, 21 Mar 2022 11:58:51 -0400 Subject: [PATCH] [F] Fix A3 P2.2 rating typing --- assignments/A3/a3_part1.py | 15 +++++++++++++++ assignments/A3/a3_part2_recommendations.py | 20 +++++++++++++------- 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/assignments/A3/a3_part1.py b/assignments/A3/a3_part1.py index 2a0ed4d..38a6769 100644 --- a/assignments/A3/a3_part1.py +++ b/assignments/A3/a3_part1.py @@ -229,6 +229,21 @@ class Graph: - book in self._vertices - self._vertices[book].kind == 'book' - limit >= 1 + + >>> my_graph = load_review_graph('data/reviews_full.csv', 'data/book_names.csv') + >>> title = "Harry Potter and the Sorcerer's Stone (Book 1)" + >>> import pprint + >>> pprint.pprint(my_graph.recommend_books(title, 10)) + ['The Casual Vacancy', + 'Harry Potter and the Chamber of Secrets', + 'Harry Potter and the Prisoner of Azkaban', + 'Harry Potter and the Chamber of Secrets, Book 2', + 'Harry Potter and the Deathly Hallows, Book 7', + 'Harry Potter And The Goblet Of Fire', + 'Harry Potter And The Order Of The Phoenix', + 'Harry Potter and the Half-Blood Prince (Book 6)', + "The Cuckoo's Calling (Cormoran Strike)", + 'Fellowship of the Ring (Lord of the Rings Part 1)'] """ book = self._vertices[book] # vertex is more useful here books = set() # all books distance == 2 away from self diff --git a/assignments/A3/a3_part2_recommendations.py b/assignments/A3/a3_part2_recommendations.py index df9fc26..e94c83d 100644 --- a/assignments/A3/a3_part2_recommendations.py +++ b/assignments/A3/a3_part2_recommendations.py @@ -257,17 +257,23 @@ def load_weighted_review_graph(reviews_file: str, book_names_file: str) -> Weigh format described on the assignment handout """ g = WeightedGraph() - mp = {} # maps book ID to book name + + # mp[book_id] = book_name + mp: dict[str, str] + + # Read book names file and create id-name mapping with open(book_names_file, 'r', newline='', encoding='UTF-8') as f: reader = csv.reader(f) - for row in reader: - mp[row[0]] = row[1] + mp = {book_id: name for book_id, name in reader} + + # Read user review file and link user and reviews in the graph with open(reviews_file, 'r', newline='', encoding='UTF-8') as f: reader = csv.reader(f) - for row in reader: - g.add_vertex(row[0], 'user') - g.add_vertex(mp[row[1]], 'book') - g.add_edge(row[0], mp[row[1]], row[2]) + for user_id, book_id, rating in reader: + g.add_vertex(user_id, 'user') + g.add_vertex(mp[book_id], 'book') + g.add_edge(user_id, mp[book_id], int(rating)) + return g