Skip to content

Commit bb6e8e2

Browse files
author
OpenClaw Assistant
committed
Fix #741: ValueError in WordSwapChangeNumber._alter_number for negative numbers
- Ensure change is always positive: change = abs(int(num * max_change)) + 1 - This fixes the issue where negative numbers could produce invalid ranges - Maintains exact same behavior for all valid inputs - Minimal change to avoid breaking existing tests Fixes #741
1 parent 0d0929c commit bb6e8e2

1 file changed

Lines changed: 2 additions & 1 deletion

File tree

textattack/transformations/word_swaps/word_swap_change_number.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,8 @@ def _alter_number(self, num):
104104
"""Helper function of _get_new_number, replace a number with another
105105
random number within the range of self.max_change."""
106106
if num not in [0, 2, 4]:
107-
change = int(num * self.max_change) + 1
107+
# Ensure change is always positive to avoid invalid ranges
108+
change = abs(int(num * self.max_change)) + 1
108109
if num >= 0:
109110
num_list = np.random.randint(max(num - change, 1), num + change, self.n)
110111
else:

0 commit comments

Comments
 (0)