Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
WordInsertionMaskedLM Class
-------------------------------
"""

import re

import torch
from transformers import AutoModelForMaskedLM, AutoTokenizer
Expand Down Expand Up @@ -162,7 +162,7 @@ def _get_transformations(self, current_text, indices_to_modify):
word_at_index = current_text.words[index_to_modify]
for word in new_words[i]:
word = word.strip("Ġ")
if word != word_at_index:
if word != word_at_index and re.search("[a-zA-Z]", word):
transformed_texts.append(
current_text.insert_text_before_word_index(
index_to_modify, word
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
------------------------------------------------

"""
import re

import torch
from transformers import AutoModelForMaskedLM, AutoTokenizer
Expand Down Expand Up @@ -169,7 +170,7 @@ def _get_transformations(self, current_text, indices_to_modify):
word_at_index = current_text.words[index_to_modify]
for word in merged_words[i]:
word = word.strip("Ġ")
if word != word_at_index:
if word != word_at_index and re.search("[a-zA-Z]", word):
temp_text = current_text.delete_word_at_index(index_to_modify + 1)
transformed_texts.append(
temp_text.replace_word_at_index(index_to_modify, word)
Expand Down
7 changes: 6 additions & 1 deletion textattack/transformations/word_swaps/word_swap_masked_lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@


import itertools
import re

import torch
from transformers import AutoModelForMaskedLM, AutoTokenizer
Expand Down Expand Up @@ -290,7 +291,11 @@ def _get_transformations(self, current_text, indices_to_modify):
word_at_index = current_text.words[index_to_modify]
for word in replacement_words[i]:
word = word.strip("Ġ")
if word != word_at_index and len(utils.words_from_text(word)) == 1:
if (
word != word_at_index
and re.search("[a-zA-Z]", word)
and len(utils.words_from_text(word)) == 1
):
transformed_texts.append(
current_text.replace_word_at_index(index_to_modify, word)
)
Expand Down