我是Python新手,我正在尝试自学语言处理.python中的NLTK有一个名为FreqDist的函数,它给出了文本中单词的频率,但由于某种原因,它无法正常工作.
这是教程给我写的内容:
fdist1 = FreqDist(text1)
vocabulary1 = fdist1.keys()
vocabulary1[:50]
Run Code Online (Sandbox Code Playgroud)
所以基本上它应该给我一个文本中最常用的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...]
Run Code Online (Sandbox Code Playgroud)
我正在复制教程,但我一定是做错了.
以下是教程的链接:
http://www.nltk.org/book/ch01.html#sec-computing-with-language-texts-and-words
该示例位于"图1.3:计算文本中出现的单词(频率分布)"标题下
有谁知道我怎么解决这个问题?
我想计算文本语料库中单词的术语 - 频率.我一直在使用NLTK的word_tokenize,然后是probability.FreqDist一段时间才能完成.word_tokenize返回一个列表,该列表由FreqDist转换为频率分布.但是,我最近在集合(collections.Counter)中遇到了Counter函数,它似乎做了完全相同的事情.FreqDist和Counter都有一个most_common(n)函数,它返回n个最常用的单词.有谁知道这两者之间是否存在差异?一个比另一个快吗?是否存在可以工作而另一个不工作的情况?
也许这是一个愚蠢的问题,但是我在使用Python从语料库中提取十个最常见的单词时遇到了问题。这就是到目前为止。(顺便说一句,我与NLTK一起阅读一个带有两个子类别的语料库,每个子类别有10个.txt文件)
import re
import string
from nltk.corpus import stopwords
stoplist = stopwords.words('dutch')
from collections import defaultdict
from operator import itemgetter
def toptenwords(mycorpus):
words = mycorpus.words()
no_capitals = set([word.lower() for word in words])
filtered = [word for word in no_capitals if word not in stoplist]
no_punct = [s.translate(None, string.punctuation) for s in filtered]
wordcounter = {}
for word in no_punct:
if word in wordcounter:
wordcounter[word] += 1
else:
wordcounter[word] = 1
sorting = sorted(wordcounter.iteritems(), key = itemgetter, reverse = True)
return …Run Code Online (Sandbox Code Playgroud)