【发布时间】: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