在python中查找文本文件中每个单词的频率

2024-09-30 14:15:49 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的文本文件中找到所有单词的频率,以便从中找出最常出现的单词。 有人能帮我一下这个命令吗。

import nltk
text1 = "hello he heloo hello hi " // example text
 fdist1 = FreqDist(text1) 

我用过上面的代码,但问题是它没有给出单词的频率,而是显示每个字符的频率。 我也想知道如何使用文本文件输入文本。


Tags: textimport命令helloexamplehi单词频率
3条回答

我看到你在用这个例子,看到了和你看到的一样的东西,为了让它正常工作,你必须用空格分开字符串。如果你不这样做,它似乎计数每个字符,这是你看到的。这将返回每个单词的正确计数,而不是字符。

import nltk

text1 = 'hello he heloo hello hi '
text1 = text1.split(' ')
fdist1 = nltk.FreqDist(text1)
print (fdist1.most_common(50))

如果要读取文件并获取字数,可以这样做:

input.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))

nltk book中的text1是标记(单词、标点符号)的集合,与代码示例中的text1是字符串(Unicode代码点的集合)不同:

>>> from nltk.book import text1
>>> text1
<Text: Moby Dick by Herman Melville 1851>
>>> text1[99] # 100th token in the text
','
>>> from nltk import FreqDist
>>> FreqDist(text1)
FreqDist({',': 18713, 'the': 13721, '.': 6862, 'of': 6536, 'and': 6024,
          'a': 4569, 'to': 4542, ';': 4072, 'in': 3916, 'that': 2982, ...})

如果您的输入确实是空格分隔的单词,那么要查找频率,请使用@Boa's answer

freq = Counter(text_with_space_separated_words.split())

注:FreqDist是一个Counter,但它还定义了其他方法,如.plot()

如果要改用nltk标记器:

#!/usr/bin/env python3
from itertools import chain
from nltk import FreqDist, sent_tokenize, word_tokenize # $ pip install nltk

with open('your_text.txt') as file:
    text = file.read()
words = chain.from_iterable(map(word_tokenize, sent_tokenize(text)))
freq = FreqDist(map(str.casefold, words))
freq.pprint()
# -> FreqDist({'hello': 2, 'hi': 1, 'heloo': 1, 'he': 1})

sent_tokenize()将文本标记为句子。然后word_tokenize将每个句子标记为单词。There are many ways to tokenize text in ^{}.

就它的价值而言,NLTK似乎对这项任务来说是过分的。下面将按从高到低的顺序为您提供单词频率。

from collections import Counter
input_string = [...] # get the input from a file
word_freqs = Counter(input_string.split())

相关问题 更多 >

    热门问题