<commit_before># Authors: <NAME>
# License: 3-clause BSD

""" Implements a Hidden Alignment Conditional Random Field (HACRF). """


class Hacrf(object):
    """ Hidden Alignment Conditional Random Field with L2 regularizer.

    See *A Conditional Random Field for Discriminatively-trained Finite-state String Edit Distance* by McCallum, Bellare, and Pereira,
        and the report *Conditional Random Fields for Noisy text normalisation* by <NAME>.
    """

    def __init__(self):
        pass
<commit_msg>Fleshing out the fit method - trying to think what will be necessary in the inference module.<commit_after># Authors: <NAME>
# License: 3-clause BSD

""" Implements a Hidden Alignment Conditional Random Field (HACRF). """

import numpy as np
from scipy.optimize import fmin_l_bfgs_b


class Hacrf(object):
    """ Hidden Alignment Conditional Random Field with L2 regularizer.

    See *A Conditional Random Field for Discriminatively-trained Finite-state String Edit Distance* by McCallum, Bellare, and Pereira,
        and the report *Conditional Random Fields for Noisy text normalisation* by <NAME>etsee.
    """

    def __init__(self):
        self._optimizer_result = None
        self.parameters = None

    def fit(self, X, y):
        """Fit the model according to the given training data.

        Parameters
        ----------
        X : List of ndarrays, one for each training example.
            Each training example's shape is (string1_len, string2_len, n_features, where
            string1_len and string2_len are the length of the two training strings and n_features the
            number of features.

        y : array-like, shape (n_samples,)
            Target vector relative to X.

        Returns
        -------
        self : object
            Returns self.
        """
        n_classes = len(set(y))
        n_points = len(y)
        if len(X) != n_points:
            raise Exception('Number of training points should be the same as training labels.')

        # Create a new model object for each training example
        models = [_Model(state_machine, n_classes, x, ty) for x, ty in zip(X, y)]

        derivative = np.zeros(self.parameters.shape)

        def _objective(parameters):
            derivative.fill(0.0)
            ll = 0.0  # Log likelihood
            # TODO: Embarrassingly parallel
            for model in models:
                model.set_parameters(parameters)
                model.forward_backward()
                model.add_derivative(derivative)
                ll += model.ll
            return -ll, -derivative

        self._optimizer_result = fmin_l_bfgs_b(_objective, self.parameters)
        return self

    def predict_proba(self):
        pass

    def predict(self):
        pass

{'fraction_non_alphanumeric': 0.07923299565846599, 'fraction_numerical': 0.004341534008683068, 'mean_word_length': 2.3678440925700364, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '6734308', 'n_tokens_mistral': 774, 'n_tokens_neox': 733, 'n_words': 273}