【问题标题】:Finding the synonyms for words in wordnet在 wordnet 中查找单词的同义词
【发布时间】:2016-04-14 21:06:16
【问题描述】:

我尝试将 Wordnet 用作同义词库,因此我有一个单词列表,我需要收集每个单词的同义词。我试过这个

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

【问题讨论】:

  • 如果将最后一行 print (j.lemma_names) 更改为 print (j.lemma_names()) 会发生什么?

标签: python python-2.7 python-3.x wordnet


【解决方案1】:

正如您的输出所示,lemma_names 是一种方法而不是属性。 Blow 代码按预期工作:

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 的原因。

【讨论】:

    猜你喜欢
    • 2015-05-20
    • 2023-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多