我可以得到一個詞的定義如下: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']
添加回答
舉報
0/150
提交
取消