创建单词和同义词词典

问题描述 投票:0回答:1

我是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','sexy'],并将它与Animal in thisdict?

python-3.x dictionary wordnet
1个回答
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'])
© www.soinside.com 2019 - 2024. All rights reserved.