【问题标题】:Why doesn't NLTK wn.all_synsets() function in wordnet return a list of synsets?为什么 wordnet 中的 NLTK wn.all_synsets() 函数不返回同义词列表?
【发布时间】:2016-06-14 11:17:23
【问题描述】:

我为这个问题编码:

有多少百分比的名词同义词没有下义词?您可以使用 wn.all_synsets('n') 获取所有名词同义词集。

这是我的代码:

import nltk
from nltk.corpus import wordnet as wn

all_noun = wn.all_synsets('n')
print(all_noun)
print(wn.all_synsets('n'))
all_num = len(set(all_noun))
noun_have_hypon = [word for word in wn.all_synsets('n') if len(word.hyponyms()) >= 1]
noun_have_num = len(noun_have_hypon)
print('There are %d nouns, and %d nouns without hyponyms, the percentage is %f' %
  (all_num, noun_have_num, (all_num-noun_have_num)/all_num*100))

当我运行这段代码时,输​​出是

<generator object all_synsets at 0x10927b1b0>

<generator object all_synsets at 0x10e6f0bd0>

有82115个名词,16693个没有下义词的名词,百分比是79.671193

但如果改变

noun_have_hypon = [word for word in wn.all_synsets('n') if len(word.hyponyms()) >= 1]

noun_have_hypon = [word for word in all_noun if len(word.hyponyms()) >= 1]

输出变为

<generator object all_synsets at 0x10917b1b0>

<generator object all_synsets at 0x10e46aab0>

有82115个名词,0个没有下义词的名词,百分比是100.000000

即使all_noun = wn.all_synsets('n'),为什么两个答案不相等,0x10927b1b0 & 0x10e6f0bd0 是什么意思?

【问题讨论】:

  • 0x10927b1b0 & 0x10e6f0bd0 只是内存位置,它只是意味着无论wordnet.all_synets()返回的对象都没有费心去定义一个有意义的__str__方法。
  • @grochmal,明白了!谢谢!!!!

标签: python nlp generator nltk wordnet


【解决方案1】:

它与 NLTK 关系不大,但更多的是 Generator Expressions vs. List Comprehension 之间的区别。

我们来看一个小例子:

首先,让我们创建一个返回简单列表的函数:

>>> def some_func_that_returns_a_list():
...     list_to_be_returned = []
...     for i in range(10):
...             list_to_be_returned.append(i)
...     return list_to_be_returned
... 
>>> some_func_that_returns_a_list()
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

请注意,在some_func_that_returns_a_list() 函数中,需要创建一个列表并在函数返回到调用它的代码位置之前放入值。

同样,我们可以使用生成器来实现需要返回的相同列表,但它使用yield 关键字有点不同:

>>> def some_func_that_returns_a_generator():
...     for i in range(10):
...             yield i
... 
>>> 

请注意,在函数中没有要返回的列表的实例化。

当你尝试调用函数时:

>>>some_func_that_returns_a_generator()
<generator object some_func_that_returns_a_generator at 0x7f312719a780>

您会收到生成器的字符串表示形式,即只是描述函数的内容。此时,没有实例化的值和生成器的指针,它应该小于实例化列表的函数:

>>> import sys
>>> sys.getsizeof(some_func_that_returns_a_generator())
80
>>> sys.getsizeof(some_func_that_returns_a_list())
200

由于生成器不会实例化您需要的结果列表的值,它只会一次弹出一个 yield 的项目,您需要“手动”循环通过生成器来获取列表,例如:

>>> list(some_func_that_returns_a_generator())
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
>>> [i for i in some_func_that_returns_a_generator()]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

但在这种情况下,它会“即时”创建列表,如果您不打算停止列表而是一次读取一个元素,那么生成器将是有利的(内存方面) .

另见:


因此,对于 NLTK wn.all_synsets() WordNet API,您可以简单地执行以下操作:

>>> from nltk.corpus import wordnet as wn
>>> nouns_in_wordnet = list(wn.all_synsets('n'))

但请注意,它会将作为名词的 Synset 的整个列表保存在内存中。

如果你想过滤超过 1 个上位词的名词,你可以避免使用 filter() 函数来实例化完整的名词列表:

>>> filter(lambda ss: len(ss.hypernyms()) > 0, wn.all_synsets('n'))

最后在不将 Synset 存储在内存中的情况下“即时”计算它,您可以这样做:

>>> len(filter(lambda ss: len(ss.hypernyms()) > 0, wn.all_synsets('n')))
74389

或者不那么冗长:

>>> sum(1 for ss in wn.all_synsets('n') if len(ss.hypernyms()) > 0)
74389

但很可能,您想访问 Synsets,因此您可能正在寻找:

>>> nouns_with_hyper = filter(lambda ss: len(ss.hypernyms()) > 0, wn.all_synsets('n'))
>>> len(nouns_with_hyper)
74389

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 2016-11-09
    • 2013-09-19
    • 2013-10-16
    • 2013-08-30
    • 2023-03-11
    • 2017-04-18
    相关资源
    最近更新 更多