TextAttack icon indicating copy to clipboard operation
TextAttack copied to clipboard

Word Swap Qwerty Can Fail With Non Textual Characters

Open OGordon100 opened this issue 1 year ago • 0 comments

Describe the bug When running a qwerty word swap, there can be a crash if the input string contains numeric/special characters

To Reproduce Steps to reproduce the behavior:

    from textattack import transformations
    from textattack.augmentation import Augmenter
    transformation = transformations.WordSwapQWERTY()
    augmenter = Augmenter(transformation=transformation)
    augmenter.augment("12345@-")

This can occur with a single non textual character, e.g. [email protected] , with the random generator in this case selecting i=8

Expected behavior Non-alphabet characters are excluded

Screenshots or Traceback

Traceback (most recent call last):
  File "C:\Users\Oliver Gordon\AppData\Local\JetBrains\Toolbox\apps\PyCharm-P\ch-0\223.8617.48\plugins\python\helpers\pydev\pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 6, in <module>
  File "C:\Users\Oliver Gordon\Documents\GitHub\Artificial-Data-Modelling\venvm\lib\site-packages\textattack\augmentation\augmenter.py", line 125, in augment
    transformed_texts = self.transformation(
  File "C:\Users\Oliver Gordon\Documents\GitHub\Artificial-Data-Modelling\venvm\lib\site-packages\textattack\transformations\transformation.py", line 57, in __call__
    transformed_texts = self._get_transformations(current_text, indices_to_modify)
  File "C:\Users\Oliver Gordon\Documents\GitHub\Artificial-Data-Modelling\venvm\lib\site-packages\textattack\transformations\word_swaps\word_swap.py", line 46, in _get_transformations
    replacement_words = self._get_replacement_words(word_to_replace)
  File "C:\Users\Oliver Gordon\Documents\GitHub\Artificial-Data-Modelling\venvm\lib\site-packages\textattack\transformations\word_swaps\word_swap_qwerty.py", line 95, in _get_replacement_words
    word[:i] + random.choice(self._get_adjacent(word[i])) + word[i + 1 :]
  File "C:\Users\Oliver Gordon\AppData\Local\Programs\Python\Python39\lib\random.py", line 346, in choice
    return seq[self._randbelow(len(seq))]
IndexError: list index out of range

System Information (please complete the following information):

  • OS: Windows
  • Textattack version: 0.3.8

Additional context Add any other context about the problem here.

OGordon100 avatar Feb 28 '23 10:02 OGordon100