打印 python 列表中单词的同义词集
Printing synsets of words in a list in python
我想打印列表中所有单词的同义词列表
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
我建议使用按单词索引的字典来保存您的同义词。您将需要 3 个循环:
- 最外层的循环遍历每个单词
- 中间循环遍历单词的每个同义词集
- 最里面的循环遍历每个同义词集的每个引理
您可以将每个词附加到其对应的同义词集列表。
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']}
我想打印列表中所有单词的同义词列表
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
我建议使用按单词索引的字典来保存您的同义词。您将需要 3 个循环:
- 最外层的循环遍历每个单词
- 中间循环遍历单词的每个同义词集
- 最里面的循环遍历每个同义词集的每个引理
您可以将每个词附加到其对应的同义词集列表。
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']}