gpt4 book ai didi

python - FreqDist Python ... 最后一期

转载 作者:行者123 更新时间:2023-11-28 18:44:11 24 4
gpt4 key购买 nike

当我尝试打印 FreqDist 对象时,我在打印结束时得到“...”?我尝试在 Internet 上查找它,但没有找到。

请让我知道哪里出错了。

代码:

for word in nltk.word_tokenize(lin):
fdist.inc(word)

print fdist

最佳答案

当您使用 fdist 时,它会返回一个键值对列表。您必须使用循环将它们打印出来。像下面这样的东西应该可以工作:

import nltk
from nltk.tokenize import word_tokenize

lin = "A frequency distribution for the outcomes of an experiment. A frequency distribution records the number of times each outcome of an experiment has occurred. For example, a frequency distribution could be used to record the frequency of each word type in a document. Formally, a frequency distribution can be defined as a function mapping from each sample to the number of times that sample occurred as an outcome."

fdist = nltk.FreqDist()

for word in word_tokenize(lin):
fdist.inc(word)

for f in fdist:
print f, fdist[f]

结果是:

frequency 5
of 5
a 4
distribution 4
the 4
an 3
each 3
, 2
A 2
as 2
be 2
number 2
outcome 2
sample 2
times 2
to 2
. 1
For 1
Formally 1
can 1
could 1
defined 1
document. 1
example 1
experiment 1
experiment. 1
for 1
from 1
function 1
has 1
in 1
mapping 1
occurred 1
occurred. 1
outcomes 1
record 1
records 1
that 1
type 1
used 1
word 1
[Finished in 1.5s]

如果这有帮助,请告诉我们。

编辑:

另一种方法:

import nltk
from nltk.tokenize import word_tokenize

lin = "A frequency distribution for the outcomes of an experiment. A frequency distribution records the number of times each outcome of an experiment has occurred. For example, a frequency distribution could be used to record the frequency of each word type in a document. Formally, a frequency distribution can be defined as a function mapping from each sample to the number of times that sample occurred as an outcome."

tokens = word_tokenize(lin)
fdist = nltk.FreqDist(tokens)

for f in fdist:
print f, fdist[f]

输出相同。

关于python - FreqDist Python ... 最后一期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22415915/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com