问题描述
我尝试将 Wordnet 用作同义词库,因此我有一个单词列表,我需要为每个单词收集其同义词.我试过了
I try to use Wordnet as a thesarus, so I have a list of words and I need to collect for every word its synonyms. I tried this
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')>
但我只想在列表中收集同义词,所以输出将是这样的
But I want to collect in a list only the synonyms, so the output will be like this
['frump', 'cad', 'frank', 'pawl', 'andiron', 'chase']
['frump', 'cad', 'frank', 'pawl', 'andiron', 'chase']
推荐答案
正如您的输出所示,lemma_names 是一种方法而不是属性.Blow 代码按您的预期工作:
As your output indicates, lemma_names is a method rather than a property. Blow code works as you expected:
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.
Please note that the items in the list are of Unicode string. This is why you see the leading u in the output.
这篇关于在wordnet中查找单词的同义词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!