<commit_before>#!/usr/bin/env python
"""
This is a module that contains the main class and functionalities of the recommender systems.
"""
import numpy
from lib.content_based import ContentBased
from lib.evaluator import Evaluator
from lib.LDA import LDARecommender
from util.data_parser import DataParser
from util.recommender_configuer import RecommenderConfiguration


class RecommenderSystem(object):
    """
    A class that will combine the content-based and collaborative-filtering,
    in order to provide the main functionalities of recommendations.
    """
    def __init__(self):
        """
        Constructor of the RecommenderSystem.
        """
        DataParser.process()
        self.ratings = DataParser.get_ratings_matrix()
        # TODO: split abstracts
        self.abstracts = DataParser.get_abstracts().values()
        self.config = RecommenderConfiguration()
        self.n_factors = self.config.get_hyperparameters()['n_factors']
        self.n_iterations = self.config.get_options()['n_iterations']
        # self.content_based = ContentBased(self.abstracts, self.n_factors, self.n_iterations)
        if self.config.get_content_based() == 'LDA':
            self.content_based = LDARecommender(self.abstracts, self.n_factors, self.n_iterations)
        elif self.config.get_content_based() == 'LDA2Vec':
            raise NotImplemented('LDA2Vec is not yet implemented.')
        else:
            raise NameError("Not a valid content based " + self.config.get_content_based())
        self.hyperparameters = self.config.get_hyperparameters()
        if self.config.get_collaborative_filtering() == 'ALS':
        # self.collaborative_filtering = CollaborativeFiltering(ratings, self.n_factors,
        #                                                       self.hyperparameters['collaborative-filtering-lambda'])
            pass
        else:
            raise NameError("Not a valid collaborative filtering " + self.config.get_collaborative_filtering())
        if self.config.get_error_metric() == 'RMS':
            # TODO: initialize with abstracts
            self.evaluator = Evaluator(self.ratings)
        else:
            raise NameError("Not a valid error metric " + self.config.get_error_metric())

    def process(self):
        """
        Process an iteration of the algorithm on the given data.
        """
        self.content_based.train()
        theta = self.content_based.get_word_distribution()
        # TODO: Use collaborative filtering and evaluator
        # u, v = self.collaborative_filtering.train(theta)
        error = self.evaluator.get_rmse(theta)
        return error

    def recommend_items(self, user_id, num_recommendations=10):<commit_msg>Update recommender_system.py<commit_after>#!/usr/bin/env python
"""
This is a module that contains the main class and functionalities of the recommender systems.
"""
import numpy
from lib.content_based import ContentBased
from lib.evaluator import Evaluator
from lib.LDA import LDARecommender
from util.data_parser import DataParser
from util.recommender_configuer import RecommenderConfiguration


class RecommenderSystem(object):
    """
    A class that will combine the content-based and collaborative-filtering,
    in order to provide the main functionalities of recommendations.
    """
    def __init__(self):
        """
        Constructor of the RecommenderSystem.
        """
        DataParser.process()
        self.ratings = DataParser.get_ratings_matrix()
        # TODO: split abstracts
        self.abstracts = DataParser.get_abstracts().values()
        self.config = RecommenderConfiguration()
        self.n_factors = self.config.get_hyperparameters()['n_factors']
        self.n_iterations = self.config.get_options()['n_iterations']
        self.content_based = ContentBased(self.abstracts, self.n_factors, self.n_iterations)
        if self.config.get_content_based() == 'LDA':
            self.content_based = LDARecommender(self.abstracts, self.n_factors, self.n_iterations)
        elif self.config.get_content_based() == 'LDA2Vec':
            raise NotImplemented('LDA2Vec is not yet implemented.')
        else:
            raise NameError("Not a valid content based " + self.config.get_content_based())
        self.hyperparameters = self.config.get_hyperparameters()
        if self.config.get_collaborative_filtering() == 'ALS':
        # self.collaborative_filtering = CollaborativeFiltering(ratings, self.n_factors,
        #                                                       self.hyperparameters['collaborative-filtering-lambda'])
            pass
        else:
            raise NameError("Not a valid collaborative filtering " + self.config.get_collaborative_filtering())
        if self.config.get_error_metric() == 'RMS':
            # TODO: initialize with abstracts
            self.evaluator = Evaluator(self.ratings)
        else:
            raise NameError("Not a valid error metric " + self.config.get_error_metric())

    def process(self):
        """
        Process an iteration of the algorithm on the given data.
        """
        self.content_based.train()
        theta = self.content_based.get_word_distribution()
        # TODO: Use collaborative filtering and evaluator
        # u, v = self.collaborative_filtering.train(theta)
        error = self.evaluator.get_rmse(theta)
        return error

    def recommend_items(self, user_id, num_recommendations=10):
{'fraction_non_alphanumeric': 0.08938752988780578, 'fraction_numerical': 0.0014713996689350744, 'mean_word_length': 2.8732193732193734, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4791193', 'n_tokens_mistral': 1500, 'n_tokens_neox': 1459, 'n_words': 348}