Ejemplo n.º 1
0
class ScoringWorker(ConsumerThread):
    def __init__(self, worker_id, task_queue, completed_queue):
        ConsumerThread.__init__(self, worker_id, task_queue, completed_queue)
        self._library = AlgorithmLibrary([DummyAlgorithm(), KeywordAlgorithm()])

    def consume(self, user_id):
        user = User.get(id=user_id)

        self._logger.info('Consuming user %s.', user.id)
        articles = list(user.all_articles())
        self._logger.info('Article count: %d', len(articles))

        if len(articles) <= 0:
            return

        try:
            model = user.preference_model()

            # algorithm = self._library.get(model.algorithm_version)
            algorithm = self._library.get(model.algorithm_version)
            if algorithm:
                algorithm.load_params(model.parameters)

        except UserPreferenceModel.DoesNotExist:
            algorithm = None

        if algorithm:
            scores = algorithm.score(articles)
        else:
            scores = itertools.repeat(0, len(articles))

        self._logger.info('Scores: %s.', scores)

        for article, score in zip(articles, scores):
            user.score_article(article, score)
Ejemplo n.º 2
0
 def __init__(self, worker_id, task_queue, completed_queue):
     ConsumerThread.__init__(self, worker_id, task_queue, completed_queue)
     self._library = AlgorithmLibrary([DummyAlgorithm(), KeywordAlgorithm()])