为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

拉丁的传说 2023-05-16 14:25:14
我可以得到一个词的定义如下:from nltk.stem import WordNetLemmatizerfrom nltk.corpus import wordnetwordnet.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 做些什么来让它显示列表中每个单词的第一个定义?
查看完整描述

1 回答

?
慕神8447489

TA贡献1780条经验 获得超1个赞

注意:并不是所有的词都能在 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']



查看完整回答
反对 回复 2023-05-16
  • 1 回答
  • 0 关注
  • 92 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信