【发布时间】:2014-04-13 12:23:56
【问题描述】:
我是 Python 新手,我正在尝试自学语言处理。 python 中的 NLTK 有一个名为 FreqDist 的函数,它给出了文本中单词的频率,但由于某种原因它不能正常工作。
这是教程让我写的:
fdist1 = FreqDist(text1)
vocabulary1 = fdist1.keys()
vocabulary1[:50]
所以基本上它应该给我一个文本中最常见的 50 个单词的列表。但是,当我运行代码时,结果是 50 个最不 出现频率的词,按照最不频繁到最频繁的顺序排列,而不是相反。我得到的输出如下:
[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
该示例位于标题“图 1.3:计算文本中出现的单词(频率分布)”的正下方
有人知道我该如何解决这个问题吗?
【问题讨论】:
-
这是你的输出:
['wonderingly', 'wonderments', 'wondrousness', 'wonst', 'woodcock', 'wooded', 'woodland', 'woodpecker', 'woody', 'wooing', 'woracious', 'wordless', 'worker', 'workers', 'workmen', 'worldly', 'worming', 'worried', 'worryings', 'wounding', 'wounds', 'wrangling', 'wrap', 'wrapall', 'wrapping', 'wreak', 'wreath', 'wrestling', 'wrestlings', 'wretchedly', 'wriggles', 'wring', 'wrinkling', 'writhed', 'wrung', 'yawed', 'yawing', 'yawingly', 'yearly', 'yokes', 'yoking', 'youngest', 'youngish', 'yourselbs', 'zag', 'zay', 'zephyr', 'zig', 'zoned', 'zoology']? -
还是顺序颠倒了?还是你完全得到了别的东西?
-
我得到了这个:[u'succour', u'four', u'woods', u'hanging', u'woody', u'conjure', u'looking', u'eligible', u'scold', u'unsuitable', 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'耸肩', u'大量', u'错误', u'忘记', u'贡献', u'手指', u'增加', u'感叹', u'英雄', u'倾斜', u'Truth',u'here',u'china',u'hers',u'natured',u'substance',u'unwillingness...]
-
我认为你的是按字母顺序排列的最后五十个单词。我可能是错的,但看起来我得到的是文本中出现次数最少的单词
-
您可能需要检查您的
text1。你如何定义text1?当我在您的帖子中运行代码时,我确实在您最近的评论(您正在寻找的输出)中获得了您的输出。我的第一条评论的输出是相同文本的最后五十个单词(按FreqDist排序)。