创建单词和同义词词典
Create Dictionary of Words and Synonyms
我是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,我在附加之前添加了 if not 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']
有没有办法select最后一行,['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual'], 匹配到 Animal in thisdict?
我认为这是您正在寻找的完整答案:
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'])
你的代码中发生的事情是你在添加每个元素后打印 sysn
因为你的打印语句在所有 for 循环中导致每次添加时打印 sysn
元素.
要获得您需要的输出,打印语句应该在所有循环之外,以便在完成添加元素后打印语句
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)
我是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,我在附加之前添加了 if not 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']
有没有办法select最后一行,['animate_being', 'beast', 'brute', 'creature', 'fauna', 'carnal', 'fleshly', 'sensual'], 匹配到 Animal in thisdict?
我认为这是您正在寻找的完整答案:
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'])
你的代码中发生的事情是你在添加每个元素后打印 sysn
因为你的打印语句在所有 for 循环中导致每次添加时打印 sysn
元素.
要获得您需要的输出,打印语句应该在所有循环之外,以便在完成添加元素后打印语句
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)