Search code examples
python-3.xnlpwordnetsentiment-analysis

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?


Solution

  • 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]