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 setUp(self):
		self.redis_con = redis.Redis("localhost")
		self.redis_con.flushall()

		userid = 123
		N = 3
		fb = Random_Recommender( )
		additional_filter_1 = { 'domainid' : 'domain1' }
		self.insertRecommendables(additional_filter_1, 0, 10)

		fb.train( userid, additional_filter_1 ) # train for the specific user and the filter
	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_recommendable_item(self):
		fb = Random_Recommender()

		# inserting two items for domain1
		fb.set_recommendables( 1, { 'domainid' : 'domain1' } )
		fb.set_recommendables( 2, { 'domainid' : 'domain1' } )
		g = fb.get_recommendable_item( { 'domainid' : 'domain1' } )

		self.assertIn(int(g), (1,2), "fetched wrong recommendable item")

		# for this domain there is no way of recommending anything, because we don't have any items
		g = fb.get_recommendable_item( { 'domainid' : 'domain2' } )
		self.assertIsNone(g, "fetched wrong recommendable item")
	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')
	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_compute_key(self):

		additional_filter = { }
		fb = Random_Recommender()

		full_key = fb.compute_key( additional_filter )

		self.assertEqual(fb.itemList , full_key, "the key constraints are wrong")

		a = 'domainid'
		b = 'domain2'
		additional_filter = { a : b }
		fb = Random_Recommender()

		full_key = fb.compute_key( additional_filter )

		self.assertEqual(fb.itemList + ':' + a + ':' + b, full_key, "the key constraints are wrong")


		a = 'domainid'
		b = 'domain2'
		c = 'categoryid'
		d = 'category1'
		additional_filter = { a : b, c : d }
		fb = Random_Recommender()

		full_key = fb.compute_key( additional_filter )


		self.assertEqual(fb.itemList + ':' + a + ':' + b  + ':' + c + ':' + d, full_key, "the key constraints are wrong")
	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_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 insertRecommendables(self, additional_filter, N1, N2):
		fb = Random_Recommender()


		for item_id in xrange(N1,N2):
			fb.set_recommendables( itemid = item_id, additional_filter = additional_filter )
    def __init__(self, json_string, async=False, api='contest', backends=[] ):
        '''
        Constructor
        '''
        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





Example #12
0
        if not async: # save the data instantly

            if api == 'contest':
                fullParsedDataModel = FullContestMessageParser()
                fullParsedDataModel.parse(message)
                fullParsedDataModel.save()

                item_id = fullParsedDataModel.item_id

                if config_global.SAVE_RAW_JSON in backends:
                    raw = rawJsonModel(message, mode='redis')
                    raw.save()

                if config_global.SAVE_RANDOM_RECOMMENDER in backends:
                    fb = Random_Recommender()
                    domain_id = fullParsedDataModel.domain_id
                    ## todo the recommender has to decide on its own what to save and therefore save constraints, even though the constrain management should be centralized
                    #constraints = {'domainid': domain_id}
                    fb.set_recommendables(item_id, constraints)

            if api == 'orp':
                # todo throw not implemented error
                pass


            elif api == 'id_list': ## this for debugging purposes
                userid = message['userid']
                itemid = message['itemid']
                timestamp = message['timestamp']
                domainid = message['domainid']