如何使用wordnet.synsets获取列表中多个单词的定义

问题描述

我可以得到一个单词的定义,如下:

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

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

*an expression of greeting*

但是,如何通过单词列表来达到相同的结果?

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

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

我该怎么做才能使df2显示列表中每个单词的第一个定义?

解决方法

注意:并非所有单词都可以在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)

输出:

['','','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']