diff --git a/textattack/transformations/word_swaps/word_swap_qwerty.py b/textattack/transformations/word_swaps/word_swap_qwerty.py index 1bdec2a9..34a1a853 100644 --- a/textattack/transformations/word_swaps/word_swap_qwerty.py +++ b/textattack/transformations/word_swaps/word_swap_qwerty.py @@ -90,10 +90,12 @@ def _get_replacement_words(self, word): if self.random_one: i = random.randrange(start_idx, end_idx + 1) - candidate_word = ( - word[:i] + random.choice(self._get_adjacent(word[i])) + word[i + 1 :] - ) - candidate_words.append(candidate_word) + adjacent_chars = self._get_adjacent(word[i]) + if len(adjacent_chars) > 0: + candidate_word = ( + word[:i] + random.choice(adjacent_chars) + word[i + 1 :] + ) + candidate_words.append(candidate_word) else: for i in range(start_idx, end_idx + 1): for swap_key in self._get_adjacent(word[i]):