def test_graph_sample_profile_listing(self): graph = GraphFactory.load_sample_profile_listing_graph() self.assertEqual(str(graph), 'Graph(vertices: 15, edges: 23)') query_results = graph.query().v('p-1').id().to_list() output = ['p-1'] self.assertEqual(query_results, output) query_results = graph.query().v('p-1').out('bookmarked').id().to_list() output = ['l-1', 'l-2', 'l-3'] self.assertEqual(query_results, output) query_results = graph.query().v('p-1').out('bookmarked').in_( 'bookmarked').id().to_list() output = [ 'p-1', 'p-2', 'p-3', 'p-3', 'p-1', 'p-4', 'p-5', 'p-1', 'p-5' ] self.assertEqual(query_results, output) query_results = graph.query().v('p-1').out('bookmarked').in_( 'bookmarked').id().distinct().to_list() output = ['p-1', 'p-2', 'p-3', 'p-4', 'p-5'] self.assertEqual(query_results, output) query_results = graph.query().v('p-1').out('bookmarked').in_( 'bookmarked').distinct().id().to_list() output = ['p-1', 'p-2', 'p-3', 'p-4', 'p-5'] self.assertEqual(query_results, output) query_results = graph.query().v('p-1').out('bookmarked').in_( 'bookmarked').distinct().exclude_ids(['p-1']).id().to_list() output = ['p-2', 'p-3', 'p-4', 'p-5'] self.assertEqual(query_results, output)
def initiate(self): """ Initiate any variables needed for recommendation_logic function """ self.graph = GraphFactory.load_db_into_graph() self.all_profiles = models.Profile.objects.all() self.all_profiles_count = len(self.all_profiles)
def test_load_db_into_graph(self): graph = GraphFactory.load_db_into_graph() self.assertEqual(str(graph), 'Graph(vertices: 223, edges: 536)') bigbrother_dict = graph.query().v('p-1').to_dict().next() expected_dict = { 'highest_role': 'APPS_MALL_STEWARD', 'username': '******' } self.assertEqual(bigbrother_dict, expected_dict) # More meaningful to know title of listings bookmarked than just ids bigbrother_bookmarks = graph.query().v('p-1').out( 'bookmarked').to_dict().key('title').to_list() # all_file_bookmarks_list = (FileQuery() # .load_yaml_file('listings.yaml') # .each_key('library_entries') # .to_list()) # # print(all_file_bookmarks_list) expected_bookmarks = [ 'Acoustic Guitar', 'Bread Basket', 'Chain boat navigation', 'Chart Course', 'Electric Guitar', 'Electric Piano', 'Gallery of Maps', 'Informational Book', 'Killer Whale', 'Lightning', 'Lion Finder', 'Monkey Finder', 'Parrotlet', 'Piano', 'Snow', 'Sound Mixer', 'Stop sign', 'Tornado', 'Violin', 'White Horse', 'Wolf Finder' ] self.assertEqual(sorted(bigbrother_bookmarks), sorted(expected_bookmarks))
def test_graph_recommendation_db(self): graph = GraphFactory.load_db_into_graph() results = graph.algo().recommend_listings_for_profile('p-1') # bigbrother expected_results = [ ('l-2', 2), ('l-96', 1), ('l-90', 1), ('l-9', 1), ('l-82', 1), ('l-81', 1), ('l-77', 1), ('l-70', 1), ('l-69', 1), ('l-68', 1), ('l-63', 1), ('l-5', 1), ('l-47', 1), ('l-44', 1), ('l-147', 1), ('l-14', 1), ('l-101', 1), ('l-10', 1) ] self.assertEqual(results, expected_results)
def test_graph_recommendation(self): graph = GraphFactory.load_sample_profile_listing_graph() results = graph.algo().recommend_listings_for_profile('p-1') expected_results = [('l-5', 2), ('l-8', 1), ('l-7', 1), ('l-6', 1), ('l-4', 1)] self.assertEqual(results, expected_results)
def recommendation_logic(self): """ Recommendation logic """ all_profiles = models.Profile.objects.all() all_profiles_count = all_profiles.count() graph = GraphFactory.load_db_into_graph() current_profile_count = 0 for profile in all_profiles: current_profile_count = current_profile_count + 1 logger.info('Calculating Profile {}/{}'.format( current_profile_count, all_profiles_count)) profile_id = profile.id results = graph.algo().recommend_listings_for_profile( 'p-{}'.format(profile_id)) # bigbrother for current_tuple in results: listing_raw = current_tuple[0] # 'l-#' listing_id = int(listing_raw.split('-')[1]) score = current_tuple[1] # No need to rebase since results are within the range of others based on testing: self.add_listing_to_user_profile(profile_id, listing_id, score)
def test_graph_recommendation_db(self): graph = GraphFactory.load_db_into_graph() results = graph.algo().recommend_listings_for_profile( 'p-1') # bigbrother output = [('l-114', 1), ('l-113', 1), ('l-112', 1), ('l-1', 1)] self.assertEqual(results, output)
def test_graph_recommendation_db(self): graph = GraphFactory.load_db_into_graph() results = graph.algo().recommend_listings_for_profile('p-1') # bigbrother expected_results = [('l-2', 2), ('l-96', 1), ('l-90', 1), ('l-9', 1), ('l-82', 1), ('l-81', 1), ('l-77', 1), ('l-70', 1), ('l-69', 1), ('l-68', 1), ('l-63', 1), ('l-47', 1), ('l-44', 1), ('l-147', 1), ('l-14', 1), ('l-101', 1), ('l-10', 1)] self.assertEqual(results, expected_results)
def test_load_db_into_graph(self): graph = GraphFactory.load_db_into_graph() self.assertEqual(str(graph), 'Graph(vertices: 158, edges: 402)') bigbrother_dict = graph.query().v('p-1').to_dict().next() expected_dict = {'highest_role': 'APPS_MALL_STEWARD', 'username': '******'} self.assertEqual(bigbrother_dict, expected_dict) bigbrother_bookmarks = graph.query().v('p-1').out('bookmarked').id().to_list() expected_bookmarks = ['l-11'] self.assertEqual(bigbrother_bookmarks, expected_bookmarks)
def test_graph_sample_profile_listing_categories(self): graph = GraphFactory.load_sample_profile_listing_graph() self.assertEqual(str(graph), 'Graph(vertices: 15, edges: 23)') query_results = ( graph.query().v('p-1') # Profile .out('bookmarked') # Listing .out('listingCategory').id().to_list() ) # Get listings of target profile ids expected_categories = ['c-1', 'c-2', 'c-1'] self.assertEqual(expected_categories, query_results)
def test_graph_recommendation(self): graph = GraphFactory.load_sample_profile_listing_graph() results = graph.algo().recommend_listings_for_profile('p-1') expected_results = [ ('l-5', 2), ('l-8', 1), ('l-7', 1), ('l-6', 1), ('l-4', 1) ] self.assertEqual(results, expected_results)
def test_graph_sample_profile_listing_in(self): graph = GraphFactory.load_sample_profile_listing_graph() self.assertEqual(str(graph), 'Graph(vertices: 15, edges: 23)') query_results = graph.query().v('l-1').id().to_list() expected_results = ['l-1'] self.assertEqual(query_results, expected_results) query_results = graph.query().v('l-1').in_('bookmarked').id().to_list() expected_results = ['p-1', 'p-2', 'p-3'] self.assertEqual(query_results, expected_results) query_results = graph.query().v('l-1', 'l-2', 'l-3').id().to_list() expected_results = ['l-1', 'l-2', 'l-3'] self.assertEqual(query_results, expected_results)
def test_graph_sample_profile_listing_side_effect(self): graph = GraphFactory.load_sample_profile_listing_graph() self.assertEqual(str(graph), 'Graph(vertices: 15, edges: 23)') profile_listing_categories_ids = [] query_results = (graph.query().v('p-1').out('bookmarked').side_effect( lambda current_vertex: [ profile_listing_categories_ids.append(current) for current in current_vertex.query().out('listingCategory').id().to_list() ]).id().to_list()) # Get listings of target profile ids expected_categories = ['c-1', 'c-2', 'c-1'] expected_listing = ['l-1', 'l-2', 'l-3'] self.assertEqual(expected_categories, profile_listing_categories_ids) self.assertEqual(expected_listing, query_results)
def test_load_db_into_graph(self): graph = GraphFactory.load_db_into_graph() self.assertEqual(str(graph), 'Graph(vertices: 223, edges: 536)') bigbrother_dict = graph.query().v('p-1').to_dict().next() expected_dict = {'highest_role': 'APPS_MALL_STEWARD', 'username': '******'} self.assertEqual(bigbrother_dict, expected_dict) # More meaningful to know title of listings bookmarked than just ids bigbrother_bookmarks = graph.query().v('p-1').out('bookmarked').to_dict().key('title').to_list() # all_file_bookmarks_list = (FileQuery() # .load_yaml_file('listings.yaml') # .each_key('library_entries') # .to_list()) # # print(all_file_bookmarks_list) expected_bookmarks = [ 'Acoustic Guitar', 'Bread Basket', 'Chain boat navigation', 'Chart Course', 'Electric Guitar', 'Electric Piano', 'Gallery of Maps', 'Informational Book', 'Killer Whale', 'Lightning', 'Lion Finder', 'Monkey Finder', 'Parrotlet', 'Piano', 'Snow', 'Sound Mixer', 'Stop sign', 'Tornado', 'Violin', 'White Horse', 'Wolf Finder' ] self.assertEqual(sorted(bigbrother_bookmarks), sorted(expected_bookmarks))
def test_load_db_into_graph(self): graph = GraphFactory.load_db_into_graph() self.assertEqual(str(graph), 'Graph(vertices: 222, edges: 529)') bigbrother_dict = graph.query().v('p-1').to_dict().next() expected_dict = { 'highest_role': 'APPS_MALL_STEWARD', 'username': '******' } self.assertEqual(bigbrother_dict, expected_dict) bigbrother_bookmarks = graph.query().v('p-1').out( 'bookmarked').id().to_list() expected_bookmarks = [ 'l-108', 'l-122', 'l-127', 'l-154', 'l-156', 'l-158', 'l-169', 'l-175', 'l-180', 'l-186', 'l-1', 'l-23', 'l-29', 'l-30', 'l-49', 'l-50', 'l-59', 'l-73', 'l-87', 'l-93', 'l-94' ] self.assertEqual(sorted(bigbrother_bookmarks), sorted(expected_bookmarks))