Attack a single example. Args: example (:obj:`str`, :obj:`OrderedDict[str, str]` or :class:`~textattack.shared.AttackedText`): Example to attack. It can be a single string or an `OrderedDict` where keys represent the input fields (e.g. "premise",
(self, example, ground_truth_output)
| 417 | return result |
| 418 | |
| 419 | def attack(self, example, ground_truth_output): |
| 420 | """Attack a single example. |
| 421 | |
| 422 | Args: |
| 423 | example (:obj:`str`, :obj:`OrderedDict[str, str]` or :class:`~textattack.shared.AttackedText`): |
| 424 | Example to attack. It can be a single string or an `OrderedDict` where |
| 425 | keys represent the input fields (e.g. "premise", "hypothesis") and the values are the actual input textx. |
| 426 | Also accepts :class:`~textattack.shared.AttackedText` that wraps around the input. |
| 427 | ground_truth_output(:obj:`int`, :obj:`float` or :obj:`str`): |
| 428 | Ground truth output of `example`. |
| 429 | For classification tasks, it should be an integer representing the ground truth label. |
| 430 | For regression tasks (e.g. STS), it should be the target value. |
| 431 | For seq2seq tasks (e.g. translation), it should be the target string. |
| 432 | Returns: |
| 433 | :class:`~textattack.attack_results.AttackResult` that represents the result of the attack. |
| 434 | """ |
| 435 | assert isinstance( |
| 436 | example, (str, OrderedDict, AttackedText) |
| 437 | ), "`example` must either be `str`, `collections.OrderedDict`, `textattack.shared.AttackedText`." |
| 438 | if isinstance(example, (str, OrderedDict)): |
| 439 | example = AttackedText(example) |
| 440 | |
| 441 | assert isinstance( |
| 442 | ground_truth_output, (int, str) |
| 443 | ), "`ground_truth_output` must either be `str` or `int`." |
| 444 | goal_function_result, _ = self.goal_function.init_attack_example( |
| 445 | example, ground_truth_output |
| 446 | ) |
| 447 | if goal_function_result.goal_status == GoalFunctionResultStatus.SKIPPED: |
| 448 | return SkippedAttackResult(goal_function_result) |
| 449 | else: |
| 450 | result = self._attack(goal_function_result) |
| 451 | return result |
| 452 | |
| 453 | def __repr__(self): |
| 454 | """Prints attack parameters in a human-readable string. |
no test coverage detected