from textattack import Attack as TAAttack
from textattack.constraints.overlap import MaxWordsPerturbed
from textattack.constraints.pre_transformation import (
MinWordLength,
RepeatModification,
StopwordModification,
)
from textattack.goal_functions import UntargetedClassification
from textattack.search_methods import GreedySearch
from textattack.transformations import (
CompositeTransformation,
WordSwapNeighboringCharacterSwap,
WordSwapQWERTY,
WordSwapRandomCharacterDeletion,
WordSwapRandomCharacterInsertion,
)
from abs_text_attack.core.interfaces import Attack
[документация]
class Pruthi(Attack):
[документация]
@staticmethod
def build(model_wrapper, kwargs = {}):
min_word_len_params = kwargs.get("min_word_len_params", {})
max_words_perturbed_params = kwargs.get("max_words_perurbed_params", {})
transformation = CompositeTransformation(
[
WordSwapNeighboringCharacterSwap(
random_one=False, skip_first_char=True, skip_last_char=True
),
WordSwapRandomCharacterDeletion(
random_one=False, skip_first_char=True, skip_last_char=True
),
WordSwapRandomCharacterInsertion(
random_one=False, skip_first_char=True, skip_last_char=True
),
WordSwapQWERTY(
random_one=False, skip_first_char=True, skip_last_char=True
),
]
)
constraints = [
MinWordLength(min_length=min_word_len_params.get("min_length", 4)),
StopwordModification(),
MaxWordsPerturbed(max_num_words=max_words_perturbed_params.get("max_num_words", 1)),
RepeatModification(),
]
goal_function = UntargetedClassification(model_wrapper)
search_method = GreedySearch()
return TAAttack(goal_function, constraints, transformation, search_method)