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

zad*_*zny 6 python nlp corpus nltk wordnet

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

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

这有效:

from nltk.corpus import brown as b
print b.words()
Run Code Online (Sandbox Code Playgroud)

这会导致AttributeError:

from nltk.corpus import wordnet as wn
print wn.words()
Run Code Online (Sandbox Code Playgroud)

alv*_*vas 11

对于wordnet,它是一个词义资源,因此资源中的元素由感官(aka 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
Run Code Online (Sandbox Code Playgroud)

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

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

>>> lemmas_in_wordnet = set(chain(*[ss.lemma_names() for ss in wn.all_synsets()]))
Run Code Online (Sandbox Code Playgroud)

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

>>> lemmas_in_words  = set(i for i in wn.words())
>>> len(lemmas_in_wordnet)
148730
>>> len(lemmas_in_words)
147306
Run Code Online (Sandbox Code Playgroud)

但奇怪的是,使用的词汇总数存在一些差异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']
Run Code Online (Sandbox Code Playgroud)

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

  • 我很高兴这个答案有所帮助。顺便说一句,“ NTU”是模棱两可的,`wn.synsets('NTU')= [Synset('Nanyang_Technologic_University.n.1'),Synset('National_Taiwan_University.n.1')]] (2认同)