我使用WordNet创建了一组单词及其同义词,因此,当用户输入“ creature”时,它将返回Animal。我接下来要做的是将不是类似于“ dog”的同义词/同义词的匹配词匹配到Animal。有图书馆吗?请参阅下面的代码:
thisdict ={ "Animal", "Metal", "Car"}
syns = {w : [] for w in thisdict}
for k, v in syns.items():
for synset in wordnet.synsets(k):
for lemma in synset.lemmas():
if not k.lower() == lemma.name():
syns[k].append(lemma.name())
def synonyms(syns):
for key in thisdict:
print(' '.join(syns[key])+'\n')
synonyms(syns)
输出:
'animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual'
我想要的输出:
'dog', 'cat', etc..