我尝试使用Wordnet作为thesarus,所以我有一个单词列表,我需要收集每个单词的同义词。我试过这个
from nltk.corpus import wordnet as wn
for i,j in enumerate(wn.synsets('dog')):
print (j.lemma_names)
此代码提供以下输出
<bound method Synset.lemma_names of Synset('dog.n.01')>
<bound method Synset.lemma_names of Synset('frump.n.01')>
<bound method Synset.lemma_names of Synset('dog.n.03')>
<bound method Synset.lemma_names of Synset('cad.n.01')>
<bound method Synset.lemma_names of Synset('frank.n.02')>
<bound method Synset.lemma_names of Synset('pawl.n.01')>
<bound method Synset.lemma_names of Synset('andiron.n.01')>
<bound method Synset.lemma_names of Synset('chase.v.01')>
但是我想在列表中只收集同义词,所以输出就像这样
['frump','cad','frank','pawl','andiron','追逐']
答案 0 :(得分:0)
正如您的输出所示, lemma_names 是一种方法而不是属性。打击代码按预期工作:
from nltk.corpus import wordnet as wn
result = [st.lemma_names()[0] for st in wn.synsets('dog')]
print(result)
输出结果为:
[u'dog', u'frump', u'dog', u'cad', u'frank', u'pawl', u'andiron', u'chase']
请注意,列表中的项目是Unicode字符串。这就是您在输出中看到前导 u 的原因。