in augly/text/augmenters/word_replacement.py [0:0]
def substitute(self, data: str) -> str:
"""
Returns a text where random words are replaced using the specified mapping
@param data: the text to which the word substitution will be applied
"""
results = []
tokens = tokenize(data)
aug_word_cnt = self._generate_aug_cnt(
len(tokens), self.aug_min, self.aug_max, self.aug_p
)
filtered_word_idxes = self.pre_skip_aug(tokens)
if self.priority_words is None:
self.priority_words = self.word_mapping.mapping.keys()
aug_word_idxes = set(
get_aug_idxes(
self,
tokens,
filtered_word_idxes,
aug_word_cnt,
Method.WORD,
)
)
if not aug_word_idxes:
return data
is_diff = False
for t_i, token in enumerate(tokens):
if t_i not in aug_word_idxes:
results.append(token)
continue
new_token, has_changed = self.word_mapping.replace(token)
is_diff = is_diff or has_changed
results.append(new_token)
return detokenize(results) if is_diff else data