我是Python的初学者,并且我有一个单词字典,如下所示:
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():
v.append(lemma.name())
print(syns)
目前,Animal的syns输出为:
{'Animal': ['animal']}
{'Animal': ['animal', 'animate_being']}
{'Animal': ['animal', 'animate_being', 'beast']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly']}
{'Animal': ['animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly', 'sensual']}
我的问题是,有没有一种方法可以创建字典,其中每一行都包含一个单词及其同义词,例如:
Animal: 'animal', 'animate_being', 'beast', 'brute', 'creature', 'fauna', 'animal', 'carnal', 'fleshly', 'sensual'
Cat: ...
编辑 多亏了DannyMoshe,如果没有的话,我已经添加了key.lower()== lemma.name():在追加之前,现在有以下输出:
['animate_being']
['animate_being', 'beast']
['animate_being', 'beast', 'brute']
['animate_being', 'beast', 'brute', 'creature']
['animate_being', 'beast', 'brute', 'creature', 'fauna']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly']
['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual']
有没有一种方法可以选择最后一行,['animate_being','beast','brute','creature','fauna','carnal','fleshly','sensual']并进行匹配以此动物告诫动物?
答案 0 :(得分:0)
我认为这是您正在寻找的完整答案:
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())
print(syns['Animal'])
或者如果您只是想将同义词作为字符串:
print ' '.join(syns['Animal'])
答案 1 :(得分:0)
我认为这就是你需要的 希望对您有所帮助。...
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():
v.append(lemma.name())
print(syns)