File size: 959 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
"""

METEOR Constraints
--------------------------


"""


import nltk

from textattack.constraints import Constraint


class METEOR(Constraint):
    """A constraint on METEOR score difference.

    Args:
        max_meteor (int): Max METEOR 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_meteor, compare_against_original=True):
        super().__init__(compare_against_original)
        if not isinstance(max_meteor, int):
            raise TypeError("max_meteor must be an int")
        self.max_meteor = max_meteor

    def _check_constraint(self, transformed_text, reference_text):
        meteor = nltk.translate.meteor([reference_text], transformed_text)
        return meteor <= self.max_meteor

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