From 56a130c0a3e681631899cf6971956f77db9b32d5 Mon Sep 17 00:00:00 2001 From: lxcxjxhx Date: Thu, 9 Jul 2026 17:42:48 +0800 Subject: [PATCH] fix: correct element-wise comparison in words_diff_ratio self.words and x.words are Python lists, so `self.words != x.words` returns a single boolean (True/False) instead of an element-wise comparison array. np.sum(True) always equals 1, making the ratio incorrect for any sequence length. Convert to numpy arrays before comparison to get proper element-wise results. Fixes #787 --- textattack/shared/attacked_text.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/textattack/shared/attacked_text.py b/textattack/shared/attacked_text.py index d2221bb3..132df478 100644 --- a/textattack/shared/attacked_text.py +++ b/textattack/shared/attacked_text.py @@ -483,7 +483,7 @@ def words_diff_ratio(self, x: AttackedText) -> float: Note that current text and `x` must have same number of words. """ assert self.num_words == x.num_words - return float(np.sum(self.words != x.words)) / self.num_words + return float(np.sum(np.array(self.words) != np.array(x.words))) / self.num_words def align_with_model_tokens( self, model_wrapper: textattack.models.wrappers.ModelWrapper