Skip to main content
 首页 » 编程设计

python之为什么 wordnet 中的 NLTK wn.all_synsets() 函数不返回同义词集列表

2024年10月17日7mfryf

我为问题编码:

What percentage of noun synsets have no hyponyms? You can get all noun synsets using 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>

There are 82115 nouns, and 16693 nouns without hyponyms, the percentage is 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>

There are 82115 nouns, and 0 nouns without hyponyms, the percentage is 100.000000

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

请您参考如下方法:

它与 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')) 

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

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

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

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

>>> 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 

但最有可能的是,您想要访问同义词集,因此您可能正在寻找:

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