leet_letters (LeetLetters) returning an error when the leet candidates is empty
lydianish opened this issue · comments
Lydia Nishimwe commented
Bug description:
Running the LeetLetters
transformation on a sentence that produces no leet candidates return an IndexError: list index out of range
:
To reproduce the error:
For example, a sentence with no alphanumeric characters.
from nlaugmenter.transformations.leet_letters.transformation import LeetLetters
sentence = "-----------------------"
t = LeetLetters(seed=0, max_outputs=5, max_leet=0.05)
t.generate(sentence)
Cause:
The error comes from this section in the code:
leet_replacements = random.choices(
leet_candidates, k=max_leet_replacements
)
Since random.choices
does not accept an empty list as argument, it returns an error whenleet_candidates
is empty.
Possible fix:
if not leet_candidates:
leet_replacements = []
else:
leet_replacements = random.choices(
leet_candidates, k=max_leet_replacements
)