def test_RandomRecommender(self): backend = [config_global.SAVE_RANDOM_RECOMMENDER] self.saveJson(backend) fb = Random_Recommender() additional_filter_1 = {'domainid': 'domain1'} fb.train(userid, additional_filter_1) # train for the specific user and the filter resultSet_1 = fb.get_recommendation(userid, additional_filter_1, N=N, remove=False)
def test_Get_Recommendation_userid_disctinction(self): N = 3 fb = Random_Recommender( ) additional_filter_1 = { 'domainid' : 'domain1' } userid1 = 123 self.insertRecommendables(additional_filter_1, 0, 10) fb.train( userid1, additional_filter_1 ) # train for the specific user and the filter userid2 = 456 self.insertRecommendables(additional_filter_1, 0, 10) fb.train( userid2, additional_filter_1 ) # train for the specific user and the filter resultSet_1 = fb.get_recommendation( userid1, additional_filter_1, N=N, remove = False ) if self.debug: print resultSet_1 self.assertEqual(len(resultSet_1), N, 'the resulting recommendation have the wrong number') resultSet_2 = fb.get_recommendation( userid2, additional_filter_1, N=N, remove = False ) if self.debug: print resultSet_2 self.assertEqual(len(resultSet_2), N, 'the resulting recommendation have the wrong number')
def testSimpleMergeList(self): fb = Random_Recommender( ) additional_filter_1 = { 'domainid' : 'domain1' } userid = 123 N = 3 resultSet_1 = fb.get_recommendation( userid, additional_filter_1, N=N, remove = False, ranked = True ) if self.debug: print resultSet_1 resultSet_2 = fb.get_recommendation( userid, additional_filter_1, N=N, remove = False, ranked = True ) if self.debug: print resultSet_2 slm = SimpleListMerge() slm.add('a', resultSet_1) slm.add('b', resultSet_2) unchanged_list = slm.merge_naive( {'a':0.5, 'b' : 0.5} ) self.assertEqual(unchanged_list, resultSet_1, "merging two same lists results the same list")
def test_Get_Recommendation_with_multiple_constraints(self): userid = 123 N = 3 fb = Random_Recommender( ) additional_filter_1 = { 'domainid' : 'domain1', 'channelid' : 'channel1' } self.insertRecommendables(additional_filter_1, 0, 10) fb.train( userid, additional_filter_1 ) # train for the specific user and the filter resultSet_1 = fb.get_recommendation(userid, additional_filter_1, N=N, remove = False ) if self.debug: print resultSet_1 self.assertEqual(len(resultSet_1), N, 'the resulting recommendation have the wrong number')
def test_Get_Recommendation_with_constraint(self): userid = 123 N = 3 fb = Random_Recommender( ) additional_filter_1 = { 'domainid' : 'domain1' } self.insertRecommendables(additional_filter_1, 0, 10) additional_filter_2 = { 'domainid' : 'domain2' } self.insertRecommendables(additional_filter_2, 20, 30) self.insertRecommendables({}, 20, 30) fb.train( userid, additional_filter_1 ) # train for the specific user and the filter resultSet_1 = fb.get_recommendation( userid, additional_filter_1, N=N, remove = False ) if self.debug: print resultSet_1 self.assertEqual(len(resultSet_1), N, 'the resulting recommendation have the wrong number') resultSet_2 = fb.get_recommendation( userid, additional_filter_2, N=N, remove = False ) self.assertEqual(len(resultSet_2), 0, 'the resulting recommendation have the wrong number') resultSet_new = fb.get_recommendation( userid, {}, N=N, remove = True ) self.assertEqual(len(resultSet_new), 0, 'the resulting recommendation have the wrong number') fb.train( userid, {} ) # train for the specific user but with no constraint, meaning we can recommend everything resultSet_new = fb.get_recommendation( userid, {}, N=N, remove = True, ranked=True ) if self.debug: print resultSet_new self.assertEqual(len(resultSet_new), N, 'the resulting recommendation have the wrong number') resultSet_new_1 = fb.get_recommendation( userid, {}, N=N, remove = True, ranked=True ) if self.debug: print resultSet_new_1 self.assertEqual(len(resultSet_new_1), N, 'the resulting recommendation have the wrong number') self.assertNotEqual(resultSet_new_1,resultSet_new, 'items were not removed from the recommendation Set')
def test_Get_Recommendation_Ranked(self): userid = 123 N = 3 fb = Random_Recommender( ) additional_filter_1 = { 'domainid' : 'domain1' } self.insertRecommendables(additional_filter_1, 0, 10) additional_filter_2 = { 'domainid' : 'domain2' } self.insertRecommendables(additional_filter_2, 20, 30) self.insertRecommendables({}, 20, 30) fb.train( userid, additional_filter_1 ) # train for the specific user and the filter resultSet_1 = fb.get_recommendation( userid, additional_filter_1, N=N, remove = False, ranked = True ) if self.debug: print resultSet_1 self.assertEqual(len(resultSet_1), N, 'the resulting recommendation have the wrong number')
self.backends = backends fullParsedDataModel = FullContestMessageParser() fullParsedDataModel.parse(json_string) domain_id = fullParsedDataModel.domain_id user_id = fullParsedDataModel.user_id # now compile the constraints, which are important for an onsite Recommender: do not recommend item from another domain constraints = {'domainid': domain_id} random_recommender = Random_Recommender() N = 4 # TODO initialize a new training session if necessary random_recommender.train(user_id, constraints) self.resultSet = random_recommender.get_recommendation(user_id, constraints, N=N, remove=True) def recommend(self): """ lets hope there are recommendation ready for this user/constraint """ return self.resultSet