Skip to content

Commit ad88963

Browse files
authored
Merge pull request #786 from liuyuyan2717/faster-chinese-hownetSwap
Use cache to achieve faster generation of Chinese HowNet replacement …
2 parents b2344a3 + c158ab3 commit ad88963

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

textattack/transformations/word_swaps/chn_transformations/chinese_word_swap_hownet.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,20 @@ class ChineseWordSwapHowNet(WordSwap):
1515
def __init__(self, topk=5):
1616
self.hownet_dict = OpenHowNet.HowNetDict(init_sim=True)
1717
self.topk = topk
18+
self.wordCache = {}
1819

1920
def _get_replacement_words(self, word):
2021
"""Returns a list containing all possible words with N characters
2122
replaced by a homoglyph."""
23+
if word in self.wordCache:
24+
return self.wordCache[word]
2225
results = self.hownet_dict.get_nearest_words(word, language="zh", K=self.topk)
2326
synonyms = []
2427
if results:
2528
for key, value in results.items():
26-
for w in value:
27-
synonyms.append(w)
29+
synonyms = synonyms + value[1:]
30+
self.wordCache[word] = synonyms.copy()
31+
break
2832
return synonyms
2933
else:
3034
return []

0 commit comments

Comments
 (0)