Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there a way in Python 2.7 using NLTK to just get the word and not the extra formatting that includes "synset" and the parentheses and the "n.01" etc?

For instance if I do

        wn.synsets('dog')

My results look like:

[Synset('dog.n.01'), Synset('frump.n.01'), Synset('dog.n.03'), Synset('cad.n.01'), Synset('frank.n.02'), Synset('pawl.n.01'), Synset('andiron.n.01'), Synset('chase.v.01')]

How can I instead get a list like this?

dog
frump
cad
frank
pawl
andiron
chase

Is there a way to do this using NLTK or do I have to use regular expressions? Can I use regular expressions within a python script?

like image 338
TheFishes Avatar asked Jul 09 '14 21:07

TheFishes


2 Answers

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.

like image 136
Frank Riccobono Avatar answered Sep 21 '22 10:09

Frank Riccobono


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
like image 38
user3776949 Avatar answered Sep 22 '22 10:09

user3776949