How do I print out just the word itself in a WordNet synset using Python NLTK?

你离开我真会死。 提交于 2019-12-01 17:12:20

If you want to do this without regular expressions, you can use a list comprehension.

[synset.name.split('.')[0] for synset in wn.synsets('dog') ]

What you're doing here is saying that, for each synset return the first word before the period.

Try this:

for synset in wn.synsets('dog'):
    print synset.lemmas[0].name

You want to iterate over each synset for dog, and then print out the headword of the synset. Keep in mind that multiple words could attach to the same synset, so if you want to get all the words associated with all the synsets for dog, you could do:

for synset in wn.synsets('dog'):
    for lemma in synset.lemmas:
        print lemma.name

Using lemma name might work but there is a canonical variable for the synset name for the Synset object, try:

>>> from nltk.corpus import wordnet as wn
>>> wn.synset('dog.n.1')
Synset('dog.n.01')
>>> wn.synset('dog.n.1').name
'dog.n.01'
>>> wn.synset('dog.n.1').name.partition('.')[0]
'dog'
>>> for ss in wn.synsets('dog'):
...     print ss.name.partition('.')[0]
... 
dog
frump
dog
cad
frank
pawl
andiron
chase
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!