我想找到我的文本文件中所有单词的频率,以便我可以从中找出最常出现的单词。 有人可以帮我提供用于该命令的命令吗?
import nltk
text1 = "hello he heloo hello hi " // example text
fdist1 = FreqDist(text1)
我用过上面的代码,但问题是它没有给出词频,而是显示每个字符的频率。 我也想知道如何使用文本文件输入文本。
最佳答案
我看到你在使用这个例子并且看到了你看到的同样的东西,为了让它正常工作,你必须用空格分割字符串。如果您不这样做,它似乎会计算每个字符,这就是您所看到的。这会返回每个单词的正确计数,而不是字符。
import nltk
text1 = 'hello he heloo hello hi '
text1 = text1.split(' ')
fdist1 = nltk.FreqDist(text1)
print (fdist1.most_common(50))
如果你想从文件中读取并获取字数,你可以这样做:
输入.txt
hello he heloo hello hi
my username is heinst
your username is frooty
python代码
import nltk
with open ("input.txt", "r") as myfile:
data=myfile.read().replace('\n', ' ')
data = data.split(' ')
fdist1 = nltk.FreqDist(data)
print (fdist1.most_common(50))
关于python - 在python中查找文本文件中每个单词的频率,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29052393/