Skip to content

Commit

Permalink
Merge pull request #786 from liuyuyan2717/faster-chinese-hownetSwap
Browse files Browse the repository at this point in the history
Use cache to achieve faster generation of Chinese HowNet replacement …
  • Loading branch information
jxmorris12 authored Mar 31, 2024
2 parents b2344a3 + c158ab3 commit ad88963
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 []

0 comments on commit ad88963

Please sign in to comment.