[英]Printing synsets of words in a list in python
I wish to print synonyms of all the words of the list list我想打印列表列表中所有单词的同义词
from nltk.corpus import wordnet
syns = []
x = 0
lst = ['performance','camera', 'ram', 'cost', 'battery']
for r in lst:
syns = wordnet.synsets(r)
i = len(syns)
for x in range(0, i):
print(syns[x].lemmas()[x].name())
But I get the following error:但我收到以下错误:
Traceback (most recent call last):
File "ontology.py", line 9, in <module>
print(syns[x].lemmas()[x].name())
IndexError: list index out of range
I'd recommend using a dictionary indexed by words to hold your synonyms.我建议使用按单词索引的字典来保存您的同义词。 You will need 3 loops:
您将需要 3 个循环:
You can append each lemma to its corresponding synset list.您可以将每个引理附加到其相应的同义词集列表中。
words = ['performance','camera', 'ram', 'cost', 'battery']
syns = {w : [] for w in words}
for k, v in syns.items():
for synset in wordnet.synsets(k):
for lemma in synset.lemmas():
v.append(lemma.name())
print(syns)
{'battery': ['battery',
'battery',
'electric_battery',
'battery',
'battery',
'battery',
'stamp_battery',
'barrage',
'barrage_fire',
'battery',
'bombardment',
'shelling',
'battery',
'assault_and_battery'],
'camera': ['camera',
'photographic_camera',
'television_camera',
'tv_camera',
'camera'],
'cost': ['cost',
'monetary_value',
'price',
'cost',
'price',
'cost',
'toll',
'cost',
'be',
'cost'],
'performance': ['performance',
'public_presentation',
'performance',
'performance',
'execution',
'carrying_out',
'carrying_into_action',
'performance',
'operation',
'functioning',
'performance'],
'ram': ['random-access_memory',
'random_access_memory',
'random_memory',
'RAM',
'read/write_memory',
'Aries',
'Ram',
'Aries',
'Aries_the_Ram',
'Ram',
'ram',
'ram',
'tup',
'ram',
'ram_down',
'pound',
'force',
'drive',
'ram',
'crash',
'ram',
'jam',
'jampack',
'ram',
'chock_up',
'cram',
'wad']}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.