in src/fmeval/transforms/semantic_perturbations.py [0:0]
def perturb(self, text: str) -> List[str]:
"""Return a list where each element is the original text with whitespaces potentially added or removed.
:param text: The input text to be perturbed.
:returns: A list of perturbed text outputs.
"""
def update_char(char: str, p: float):
"""Return an updated character, with whitespace potentially added or removed.
:param char: The input character.
:param p: A number in the interval [0, 1) used to determine whether
whitespace will be added/removed.
:returns: An updated character, with whitespace potentially added or removed from the input character.
"""
if char.isspace() and p < self.remove_prob:
return ""
if (not char.isspace()) and p < self.add_prob:
return char + " "
return char
perturbed_texts = []
for _ in range(self.num_perturbations):
perturbed_text = []
for ch in text:
prob = self.rng.random()
perturbed_text += [update_char(ch, prob)]
perturbed_texts.append("".join(perturbed_text))
return perturbed_texts