File tree Expand file tree Collapse file tree 1 file changed +6
-2
lines changed
textattack/transformations/word_swaps/chn_transformations Expand file tree Collapse file tree 1 file changed +6
-2
lines changed Original file line number Diff line number Diff 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 []
You can’t perform that action at this time.
0 commit comments