Xin
Xin

Reputation: 674

How to obtain definitions for multiple words in a list with wordnet.synsets()

I can get a definition of a word as follows:

from nltk.stem import WordNetLemmatizer
from nltk.corpus import wordnet

wordnet.synsets('hello')[0].definition()

*an expression of greeting*

However, how can I achieve the same result with a list of words?

df =  ['Unnamed 0',
 'business id',
 'name',
 'postal code',
]

df2 = []
for x in df:
    df2.append(wordnet.synsets(x))

What can I do to df2 to make it show the first definition of each of the words in my list?

Upvotes: 0

Views: 577

Answers (1)

thorntonc
thorntonc

Reputation: 2126

Note: Not all words will be found in wordnet.

from nltk.corpus import wordnet

df = ['Unnamed 0','business id','name','postal code']
df = [x.strip().replace(' ', '_') for x in df]

df2 = []
for x in df:
    syns = (wordnet.synsets(x))
    df2.append(syns[0].definition() if len(syns)>0 else '')

print(df2)

Output:

['', '', 'a language unit by which a person or thing is known', 'a code of letters and digits added to a postal address to aid in the sorting of mail']

Upvotes: 1

Related Questions