<commit_before># coding: utf-8
from operator import itemgetter


# The first 3 functions are exported for testing purposes.
__all__ = ['all_arcs', 'revise', 'arc_consistency_3']

fst = itemgetter(0)


def revise(domains, variables, constraint):
    """
    Given variables X_i, X_j = variables, removes the values from X_i's domain that
    do not meet the constraint between X_i and X_j.

    That is, given x in X_i's domain, x will be removed from the domain, if
    there is no value y in X_j's domain that makes constraint(x,y) True.

    ``constraint`` is a callable from the constraint list.
    """
    xi, xj = variables
    di = domains[xi]  # domain of variable X_i
    dj = domains[xj]  # domain of variable X_j
    revised = False

    for x in di:
        if not any(constraint(variables, (x, y))  # constraint is a callable
                   for y in dj):
            di.remove(x)
            revised = True
    return revised


def all_arcs(constraints):
    """
    For each constraint ((X, Y), const) adds:
        ((X, Y), const)
        ((Y, X), const)
    """
    arcs = set()<commit_msg>Big refactor of the revise function<commit_after># coding: utf-8
from operator import itemgetter

from csp import _call_constraint


# The first 3 functions are exported for testing purposes.
__all__ = ['all_arcs', 'revise', 'arc_consistency_3']

fst = itemgetter(0)


def revise(domains, arc, constraints):
    """
    Given the arc X, Y (variables), removes the values from X's domain that
    do not meet the constraint between X and Y.

    That is, given x1 in X's domain, x1 will be removed from the domain, if
    there is no value y in Y's domain that makes constraint(X,Y) True, for
    those constraints affecting X and Y.
    """
    x, y = arc
    related_constraints = [(neighbors, constraint)
                           for neighbors, constraint in constraints
                           if set(arc) == set(neighbors)]

    modified = False

    for x_value in domains[x]:
        for neighbors, constraint in related_constraints:
            constraint_results = (_call_constraint({x: x_value, y: y_value},
                                                   neighbors, constraint)
                                  for y_value in domains[y])

            if not any(constraint_results):
                domains[x].remove(x_value)
                modified = True

    return modified


def all_arcs(constraints):
    """
    For each constraint ((X, Y), const) adds:
        ((X, Y), const)
        ((Y, X), const)
    """
    arcs = set()
{'fraction_non_alphanumeric': 0.09090909090909091, 'fraction_numerical': 0.0039016777214202106, 'mean_word_length': 2.3083870967741937, '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': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5966232', 'n_tokens_mistral': 773, 'n_tokens_neox': 729, 'n_words': 289}