NLTK 中的 FreqDist 未对输出进行排序

2024-04-08

我是 Python 新手,我正在尝试自学语言处理。 python 中的 NLTK 有一个名为 FreqDist 的函数,可以给出文本中单词的频率,但由于某种原因它无法正常工作。

这是教程让我写的:

fdist1 = FreqDist(text1)
vocabulary1 = fdist1.keys()
vocabulary1[:50]

所以基本上它应该给我一个文本中 50 个最常见单词的列表。但是,当我运行代码时,结果是 50least常用词按最不常见到最常见的顺序排列,而不是相反。我得到的输出如下:

[u'succour', u'four', u'woods', u'hanging', u'woody', u'conjure', u'looking', u'eligible', u'scold', u'unsuitableness', u'meadows', u'stipulate', u'leisurely', u'bringing', u'disturb', u'internally', u'hostess', u'mohrs', u'persisted', u'Does', u'succession', u'tired', u'cordially', u'pulse', u'elegant', u'second', u'sooth', u'shrugging', u'abundantly', u'errors', u'forgetting', u'contributed', u'fingers', u'increasing', u'exclamations', u'hero', u'leaning', u'Truth', u'here', u'china', u'hers', u'natured', u'substance', u'unwillingness...]

我完全复制了教程,但我一定做错了什么。

这是教程的链接:

http://www.nltk.org/book/ch01.html#sec-computing-with-language-texts-and-words http://www.nltk.org/book/ch01.html#sec-computing-with-language-texts-and-words

该示例位于“图 1.3:计算文本中出现的单词数(频率分布)”标题下

有谁知道我该如何解决这个问题?


From NLTK 的 GitHub https://github.com/nltk/nltk/issues/390#issuecomment-53171900:

NLTK3中的FreqDist是collections.Counter的包装器;专柜提供most_common()方法按顺序返回项目。FreqDist.keys()方法由标准库提供;它没有被覆盖。我认为我们与 stdlib 变得更加兼容是件好事。

googlecode 上的文档非常旧,是 2011 年的。更多最新文档可以在http://nltk.org http://nltk.org网站。

因此对于 NLKT 版本 3,而不是fdist1.keys()[:50], use fdist1.most_common(50).

The tutorial http://www.nltk.org/book/ch01.html#frequency-distributions也已更新:

fdist1 = FreqDist(text1)
>>> print(fdist1)
<FreqDist with 19317 samples and 260819 outcomes>
>>> fdist1.most_common(50)
[(',', 18713), ('the', 13721), ('.', 6862), ('of', 6536), ('and', 6024),
('a', 4569), ('to', 4542), (';', 4072), ('in', 3916), ('that', 2982),
("'", 2684), ('-', 2552), ('his', 2459), ('it', 2209), ('I', 2124),
('s', 1739), ('is', 1695), ('he', 1661), ('with', 1659), ('was', 1632),
('as', 1620), ('"', 1478), ('all', 1462), ('for', 1414), ('this', 1280),
('!', 1269), ('at', 1231), ('by', 1137), ('but', 1113), ('not', 1103),
('--', 1070), ('him', 1058), ('from', 1052), ('be', 1030), ('on', 1005),
('so', 918), ('whale', 906), ('one', 889), ('you', 841), ('had', 767),
('have', 760), ('there', 715), ('But', 705), ('or', 697), ('were', 680),
('now', 646), ('which', 640), ('?', 637), ('me', 627), ('like', 624)]
>>> fdist1['whale']
906
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

NLTK 中的 FreqDist 未对输出进行排序 的相关文章

随机推荐