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

Use cache to achieve faster generation of Chinese HowNet replacement … #786

Merged
merged 2 commits into from
Mar 31, 2024
Merged
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
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 []
Loading