<commit_before>from .. import Classifier


class KNeighborsClassifier(Classifier):
    """
    See also
    --------
    sklearn.neighbors.KNeighborsClassifier

    http://scikit-learn.org/0.18/modules/generated/sklearn.neighbors.KNeighborsClassifier.html
    """

    SUPPORTED_METHODS = ['predict']

    # @formatter:off
    TEMPLATES = {
        'java': {
            'type':     '{0}',
            'arr':      '{{{0}}}',
            'arr[]':    '{type}[] {name} = {{{values}}};',
            'arr[][]':  '{type}[][] {name} = {{{values}}};',
            'indent':   '    ',
        },
    }
    # @formatter:on

    def __init__(
            self, language='java', method_name='predict', class_name='Tmp'):
        super(KNeighborsClassifier, self).__init__(language, method_name, class_name)

    def port(self, model):
        """
        Port a trained model to the syntax of a chosen programming language.

        Parameters
        ----------
        :param model : KNeighborsClassifier
            An instance of a trained KNeighborsClassifier classifier.
        """
        super(self.__class__, self).port(model)

        self.n_classes = len(self.model.classes_)
        self.n_templates = len(self.model._fit_X)
        self.n_features = len(self.model._fit_X[0])
        self.n_neighbors = self.model.n_neighbors

        self.algorithm = self.model.algorithm
        self.power_param = self.model.p

        if self.algorithm is not 'brute':
            from sklearn.neighbors.kd_tree import KDTree
            from sklearn.neighbors.ball_tree import BallTree
            tree = self.model._tree
            if isinstance(tree, (KDTree, BallTree)):
                self.tree = tree

        self.metric = self.model.metric

        # print('algorithm', self.model.algorithm)
        # print('classes_', self.model.classes_)
        # print('metric', self.model.metric)
        # print('metric_params', self.model.metric_params)
        # print('n_neighbors', self.model.n_neighbors)
        # print('radius', self.model.radius)
        # print('algorithm', self.model.algorithm)
        # print('weights', self.model.weights)

        # print('_fit_X', self.model._fit_X)
        # print('_y', self.model._y)
        # print('_tree', self.model._tree)
        # print('p', self.model.p)

        # print('_tree.data', self.model._tree.data)

        # for i in dir(self.model):
        #     print(i, "  ", type(getattr(self.model, i)))

        # self.n_features = len(self.model.coef_[0])
        # self.n_classes = len(self.model.classes_)

        if self.method_name == 'predict':
            return self.predict()

    def predict(self):
        """
        Port the predict method.

        Returns
        -------
        :return: out : string
            The ported predict method.
        """
        return self.create_class(self.create_method())

    def create_method(self):
        """
        Build the model method or function.

        Returns
        -------
        :return out : string
            The built method as string.<commit_msg>Remove unused comments & add weights check<commit_after>from .. import Classifier


class KNeighborsClassifier(Classifier):
    """
    See also
    --------
    sklearn.neighbors.KNeighborsClassifier

    http://scikit-learn.org/0.18/modules/generated/sklearn.neighbors.KNeighborsClassifier.html
    """

    SUPPORTED_METHODS = ['predict']

    # @formatter:off
    TEMPLATES = {
        'java': {
            'type':     '{0}',
            'arr':      '{{{0}}}',
            'arr[]':    '{type}[] {name} = {{{values}}};',
            'arr[][]':  '{type}[][] {name} = {{{values}}};',
            'indent':   '    ',
        },
    }
    # @formatter:on

    def __init__(
            self, language='java', method_name='predict', class_name='Tmp'):
        super(KNeighborsClassifier, self).__init__(language, method_name, class_name)

    def port(self, model):
        """
        Port a trained model to the syntax of a chosen programming language.

        Parameters
        ----------
        :param model : KNeighborsClassifier
            An instance of a trained KNeighborsClassifier classifier.
        """
        super(self.__class__, self).port(model)

        self.n_classes = len(self.model.classes_)
        self.n_templates = len(self.model._fit_X)
        self.n_features = len(self.model._fit_X[0])
        self.n_neighbors = self.model.n_neighbors

        self.algorithm = self.model.algorithm
        self.power_param = self.model.p

        if self.algorithm is not 'brute':
            from sklearn.neighbors.kd_tree import KDTree
            from sklearn.neighbors.ball_tree import BallTree
            tree = self.model._tree
            if isinstance(tree, (KDTree, BallTree)):
                self.tree = tree

        self.metric = self.model.metric
        if self.model.weights is not 'uniform':
            msg = "Only 'uniform' weights are supported for this classifier."
            raise NotImplementedError(msg)

        if self.method_name == 'predict':
            return self.predict()

    def predict(self):
        """
        Port the predict method.

        Returns
        -------
        :return: out : string
            The ported predict method.
        """
        return self.create_class(self.create_method())

    def create_method(self):
        """
        Build the model method or function.

        Returns
        -------
        :return out : string
            The built method as string.
{'fraction_non_alphanumeric': 0.13427433948606587, 'fraction_numerical': 0.002352515381831343, 'mean_word_length': 1.9352097716409984, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2273355', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1593, 'n_words': 335}