GEM-benchmark / NL-Augmenter

NL-Augmenter 🦎 → 🐍 A Collaborative Repository of Natural Language Transformations

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

leet_letters (LeetLetters) returning an error when the leet candidates is empty

lydianish opened this issue · comments

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
       )