Spaces:
Sleeping
Sleeping
File size: 1,291 Bytes
4a1df2e |
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 |
"""
Stopword Modification
--------------------------
"""
import nltk
from textattack.constraints import PreTransformationConstraint
from textattack.shared.validators import transformation_consists_of_word_swaps
class StopwordModification(PreTransformationConstraint):
"""A constraint disallowing the modification of stopwords."""
def __init__(self, stopwords=None, language="english"):
if stopwords is not None:
self.stopwords = set(stopwords)
else:
self.stopwords = set(nltk.corpus.stopwords.words(language))
def _get_modifiable_indices(self, current_text):
"""Returns the word indices in ``current_text`` which are able to be
modified."""
non_stopword_indices = set()
for i, word in enumerate(current_text.words):
if word not in self.stopwords:
non_stopword_indices.add(i)
return non_stopword_indices
def check_compatibility(self, transformation):
"""The stopword constraint only is concerned with word swaps since
paraphrasing phrases containing stopwords is OK.
Args:
transformation: The ``Transformation`` to check compatibility with.
"""
return transformation_consists_of_word_swaps(transformation)
|