mirror of
https://github.com/QData/TextAttack.git
synced 2021-10-13 00:05:06 +03:00
30 lines
1.1 KiB
Python
30 lines
1.1 KiB
Python
from textattack.shared.utils import default_class_repr
|
|
|
|
class Transformation:
|
|
"""
|
|
An abstract class for transofrming a string of text to produce
|
|
a potential adversarial example.
|
|
|
|
"""
|
|
|
|
def __call__(self, tokenized_text, pre_transformation_constraints=[], indices_to_modify=None):
|
|
""" Returns a list of all possible transformations for `tokenized_text`."""
|
|
if indices_to_modify is None:
|
|
indices_to_modify = set(range(len(tokenized_text.words)))
|
|
else:
|
|
indices_to_modify = set(indices_to_modify)
|
|
for constraint in pre_transformation_constraints:
|
|
indices_to_modify = indices_to_modify & constraint(tokenized_text, self)
|
|
transformed_texts = self._get_transformations(tokenized_text, indices_to_modify)
|
|
for text in transformed_texts:
|
|
text.attack_attrs['last_transformation'] = self
|
|
return transformed_texts
|
|
|
|
def _get_transformations(self, tokenized_text, indices_to_modify):
|
|
raise NotImplementedError()
|
|
|
|
def extra_repr_keys(self):
|
|
return []
|
|
|
|
__repr__ = __str__ = default_class_repr
|