如何打印Wordnet的全部内容(最好使用NLTK)?

问题描述 投票:4回答:3

NLTK提供打印布朗(或古腾堡)语料库中所有单词的功能。但是等效功能似乎不适用于Wordnet。

有没有办法通过NLTK做到这一点?如果没有,怎么可能呢?

这有效:

from nltk.corpus import brown as b
print b.words()

这会导致AttributeError:

from nltk.corpus import wordnet as wn
print wn.words()
python nlp nltk wordnet corpus
3个回答
9
投票

对于wordnet,它是一个词义资源,因此资源中的元素被感官索引(又名synsets)。

要遍历synsets

>>> from nltk.corpus import wordnet as wn
>>> for ss in wn.all_synsets():
...     print ss
...     print ss.definition()
...     break
... 
Synset('able.a.01')
(usually followed by `to') having the necessary means or skill or know-how or authority to do something

对于每个synset(sense / concept),都有一个附加到它的单词列表,称为lemmas:lemmas是我们在检查字典时使用的单词的规范(“root”)形式。

要使用单行程序获取wordnet中的引号的完整列表:

>>> lemmas_in_wordnet = set(chain(*[ss.lemma_names() for ss in wn.all_synsets()]))

有趣的是,wn.words()也将返回所有的lemma_names

>>> lemmas_in_words  = set(i for i in wn.words())
>>> len(lemmas_in_wordnet)
148730
>>> len(lemmas_in_words)
147306

但奇怪的是,使用wn.words()收集的单词总数存在一些差异。

将wordnet的“全部内容”打印成文本似乎过于雄心勃勃,因为wordnet的结构类似于层次结构图,其中同义词互相连接,每个synset都有自己的属性/属性。这就是为什么wordnet文件不能简单地保存为单个文本文件的原因。

要查看synset包含的内容:

>>> first_synset = next(wn.all_synsets())
>>> dir(first_synset)
['__class__', '__delattr__', '__dict__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__unicode__', '__weakref__', '_all_hypernyms', '_definition', '_examples', '_frame_ids', '_hypernyms', '_instance_hypernyms', '_iter_hypernym_lists', '_lemma_names', '_lemma_pointers', '_lemmas', '_lexname', '_max_depth', '_min_depth', '_name', '_needs_root', '_offset', '_pointers', '_pos', '_related', '_shortest_hypernym_paths', '_wordnet_corpus_reader', 'also_sees', 'attributes', 'causes', 'closure', 'common_hypernyms', 'definition', 'entailments', 'examples', 'frame_ids', 'hypernym_distances', 'hypernym_paths', 'hypernyms', 'hyponyms', 'instance_hypernyms', 'instance_hyponyms', 'jcn_similarity', 'lch_similarity', 'lemma_names', 'lemmas', 'lexname', 'lin_similarity', 'lowest_common_hypernyms', 'max_depth', 'member_holonyms', 'member_meronyms', 'min_depth', 'name', 'offset', 'part_holonyms', 'part_meronyms', 'path_similarity', 'pos', 'region_domains', 'res_similarity', 'root_hypernyms', 'shortest_path_distance', 'similar_tos', 'substance_holonyms', 'substance_meronyms', 'topic_domains', 'tree', 'unicode_repr', 'usage_domains', 'verb_groups', 'wup_similarity']

通过这个howto将有助于了解如何访问wordnet中所需的信息:http://www.nltk.org/howto/wordnet.html


1
投票

请尝试以下方法:

for word in wn.words():
    print word

这应该有效,因为wn.words()实际上是一个生成字符串序列的迭代器,而不是像b.words这样的字符串列表。 for循环使迭代器一次生成一个单词。


0
投票
from nltk.corpus import wordnet as wn
synonyms=[]
for word in wn.words():
    print (word,end=":")
    for syn in wn.synsets(word):
      for l in syn.lemmas():
        synonyms.append(l.name())
    print(set(synonyms),end="\n")
    synonyms.clear()
© www.soinside.com 2019 - 2024. All rights reserved.