diff --git a/textattack/transformations/word_swaps/chn_transformations/chinese_word_swap_hownet.py b/textattack/transformations/word_swaps/chn_transformations/chinese_word_swap_hownet.py index 73ba26db..53f5d71e 100644 --- a/textattack/transformations/word_swaps/chn_transformations/chinese_word_swap_hownet.py +++ b/textattack/transformations/word_swaps/chn_transformations/chinese_word_swap_hownet.py @@ -15,16 +15,20 @@ class ChineseWordSwapHowNet(WordSwap): def __init__(self, topk=5): self.hownet_dict = OpenHowNet.HowNetDict(init_sim=True) self.topk = topk + self.wordCache = {} def _get_replacement_words(self, word): """Returns a list containing all possible words with N characters replaced by a homoglyph.""" + if word in self.wordCache: + return self.wordCache[word] results = self.hownet_dict.get_nearest_words(word, language="zh", K=self.topk) synonyms = [] if results: for key, value in results.items(): - for w in value: - synonyms.append(w) + synonyms = synonyms + value[1:] + self.wordCache[word] = synonyms.copy() + break return synonyms else: return []