chaitu
chaitu

Reputation: 1216

How to iterate through the synset list generated from wordnet using python 3.4.2

I am using wordnet to find the synonyms for a particular word as shown below

synonyms = wn.synsets('good','a')

where wn is wordnet. This returns a list of synsets like

Synset('good.a.01')    
Synset('full.s.06')    
Synset('good.a.03')    
Synset('estimable.s.02')    
Synset('beneficial.s.01')

etc...

How to iterate through each synset and get the name and the pos tag of each synset?

Upvotes: 1

Views: 1871

Answers (2)

Ritveak
Ritveak

Reputation: 3788

I am using this in my code:

from nltk.corpus import wordnet

word = input()

for syn in wordnet.synsets(word):
    for l in syn.lemmas():
        print(syn.pos()) 
        print(l.name())

The syn.pos() can be called outside the inner loop as well, because each lemma contains words with same pos.

Upvotes: 1

yvespeirsman
yvespeirsman

Reputation: 3099

You can get the name and the pos tag of each synset like this:

from nltk.corpus import wordnet as wn

synonyms = wn.synsets('good','a')

for synset in synonyms:
    print(synset.name())
    print(synset.pos())

The name is the combination of word, pos and sense, such as 'full.s.06'. If you just want the word, you can split on the dot '.' and take the first element:

print(synset.name().split('.')[0]

Upvotes: 2

Related Questions