File size: 1,029 Bytes
4943752
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
"""

chrF Constraints
--------------------------


"""

import nltk.translate.chrf_score

from textattack.constraints import Constraint


class chrF(Constraint):
    """A constraint on chrF (n-gram F-score) difference.

    Args:
        max_chrf (int): Max n-gram F-score allowed.
        compare_against_original (bool): If `True`, compare new `x_adv` against the original `x`.
            Otherwise, compare it against the previous `x_adv`.
    """

    def __init__(self, max_chrf, compare_against_original=True):
        super().__init__(compare_against_original)
        if not isinstance(max_chrf, int):
            raise TypeError("max_chrf must be an int")
        self.max_chrf = max_chrf

    def _check_constraint(self, transformed_text, reference_text):
        ref = reference_text.words
        hyp = transformed_text.words
        chrf = nltk.translate.chrf_score.sentence_chrf(ref, hyp)
        return chrf <= self.max_chrf

    def extra_repr_keys(self):
        return ["max_chrf"] + super().extra_repr_keys()