File size: 1,764 Bytes
4943752 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
"""
WordInsertion Class
-------------------------------
Word Insertion transformations act by inserting a new word at a specific word index.
For example, if we insert "new" in position 3 in the text "I like the movie", we get "I like the new movie".
Subclasses can implement the abstract ``WordInsertion`` class by overriding ``self._get_new_words``.
"""
from textattack.transformations import Transformation
class WordInsertion(Transformation):
"""A base class for word insertions."""
def _get_new_words(self, current_text, index):
"""Returns a set of new words we can insert at position `index` of `current_text`
Args:
current_text (AttackedText): Current text to modify.
index (int): Position in which to insert a new word
Returns:
list[str]: List of new words to insert.
"""
raise NotImplementedError()
def _get_transformations(self, current_text, indices_to_modify):
"""
Return a set of transformed texts obtained by insertion a new word in `indices_to_modify`
Args:
current_text (AttackedText): Current text to modify.
indices_to_modify (list[int]): List of positions in which to insert a new word.
Returns:
list[AttackedText]: List of transformed texts
"""
transformed_texts = []
for i in indices_to_modify:
new_words = self._get_new_words(current_text, i)
new_transformted_texts = []
for w in new_words:
new_transformted_texts.append(
current_text.insert_text_before_word_index(i, w)
)
transformed_texts.extend(new_transformted_texts)
return transformed_texts
|