我希望打印列表列表
中所有单词的同义词 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())
但我收到以下错误:
Traceback (most recent call last):
File "ontology.py", line 9, in <module>
print(syns[x].lemmas()[x].name())
IndexError: list index out of range
答案 0 :(得分:1)
我建议使用由单词索引的字典来保存您的同义词。你需要3个循环:
您可以将每个引理附加到其对应的synset列表中。
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']}