A class for performing data augmentation using TextAttack. Returns all possible transformations for a given string. Currently only supports transformations which are word swaps. Args: transformation (textattack.Transformation): the transformation that suggests n
| 13 | |
| 14 | |
| 15 | class Augmenter: |
| 16 | """A class for performing data augmentation using TextAttack. |
| 17 | |
| 18 | Returns all possible transformations for a given string. Currently only |
| 19 | supports transformations which are word swaps. |
| 20 | |
| 21 | Args: |
| 22 | transformation (textattack.Transformation): the transformation |
| 23 | that suggests new texts from an input. |
| 24 | constraints: (list(textattack.Constraint)): constraints |
| 25 | that each transformation must meet |
| 26 | pct_words_to_swap: (float): [0., 1.], percentage of words to swap per augmented example |
| 27 | transformations_per_example: (int): Maximum number of augmentations |
| 28 | per input |
| 29 | high_yield: Whether to return a set of augmented texts that will be relatively similar, or to return only a |
| 30 | single one. |
| 31 | fast_augment: Stops additional transformation runs when number of successful augmentations reaches |
| 32 | transformations_per_example |
| 33 | advanced_metrics: return perplexity and USE Score of augmentation |
| 34 | |
| 35 | Example:: |
| 36 | >>> from textattack.transformations import WordSwapRandomCharacterDeletion, WordSwapQWERTY, CompositeTransformation |
| 37 | >>> from textattack.constraints.pre_transformation import RepeatModification, StopwordModification |
| 38 | >>> from textattack.augmentation import Augmenter |
| 39 | |
| 40 | >>> transformation = CompositeTransformation([WordSwapRandomCharacterDeletion(), WordSwapQWERTY()]) |
| 41 | >>> constraints = [RepeatModification(), StopwordModification()] |
| 42 | |
| 43 | >>> # initiate augmenter |
| 44 | >>> augmenter = Augmenter( |
| 45 | ... transformation=transformation, |
| 46 | ... constraints=constraints, |
| 47 | ... pct_words_to_swap=0.5, |
| 48 | ... transformations_per_example=3 |
| 49 | ... ) |
| 50 | |
| 51 | >>> # additional parameters can be modified if not during initiation |
| 52 | >>> augmenter.enable_advanced_metrics = True |
| 53 | >>> augmenter.fast_augment = True |
| 54 | >>> augmenter.high_yield = True |
| 55 | |
| 56 | >>> s = 'What I cannot create, I do not understand.' |
| 57 | >>> results = augmenter.augment(s) |
| 58 | |
| 59 | >>> augmentations = results[0] |
| 60 | >>> perplexity_score = results[1] |
| 61 | >>> use_score = results[2] |
| 62 | """ |
| 63 | |
| 64 | def __init__( |
| 65 | self, |
| 66 | transformation, |
| 67 | constraints=[], |
| 68 | pct_words_to_swap=0.1, |
| 69 | transformations_per_example=1, |
| 70 | high_yield=False, |
| 71 | fast_augment=False, |
| 72 | enable_advanced_metrics=False, |
no outgoing calls