Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect __eq__ method of AttackedText in textattack/shared/attacked_text.py #509

Merged
merged 17 commits into from
Sep 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion textattack/loggers/csv_logger.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ def flush(self):
self._flushed = True

def close(self):
self.fout.close()
# self.fout.close()
super().close()

def __del__(self):
if not self._flushed:
Expand Down
2 changes: 1 addition & 1 deletion textattack/models/helpers/word_cnn_for_classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def save_pretrained(self, output_path):

@classmethod
def from_pretrained(cls, name_or_path):
"""Load trained LSTM model by name or from path.
"""Load trained Word CNN model by name or from path.

Args:
name_or_path (:obj:`str`): Name of the model (e.g. "cnn-imdb") or model saved via :meth:`save_pretrained`.
Expand Down
9 changes: 7 additions & 2 deletions textattack/shared/attacked_text.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def __eq__(self, other):
"""
if not (self.text == other.text):
return False
if len(self.attack_attrs) != len(other.attack_attrs):
return False
for key in self.attack_attrs:
if key not in other.attack_attrs:
return False
Expand Down Expand Up @@ -193,7 +195,10 @@ def _text_index_of_word_index(self, i):
# Find all words until `i` in string.
look_after_index = 0
for word in pre_words:
look_after_index = lower_text.find(word.lower(), look_after_index)
look_after_index = lower_text.find(word.lower(), look_after_index) + len(
word
)
look_after_index -= len(self.words[i])
return look_after_index

def text_until_word_index(self, i):
Expand All @@ -217,7 +222,7 @@ def first_word_diff(self, other_attacked_text):
w2 = other_attacked_text.words
for i in range(min(len(w1), len(w2))):
if w1[i] != w2[i]:
return w1
return w1[i]
return None

def first_word_diff_index(self, other_attacked_text):
Expand Down