属性错误:'FreqDist'对象没有'inc'属性

2024-06-08 22:51:25 发布

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

我是Python和NLTK的初学者。我试图从教程中运行以下代码:

from nltk.corpus import gutenberg
from nltk import FreqDist

fd = FreqDist()

for word in gutenberg.words('austen-sense.txt'):
    fd.inc(word)

如果运行此命令,将出现以下错误:

AttributeError: 'FreqDist' object has no attribute 'inc'

知道我做错了什么吗?


Tags: 代码infromimportfor教程corpusword
3条回答

有些功能已被弃用。

所讨论的代码在nltk 2.0.4版本上确实有效

https://pypi.python.org/pypi/nltk/2.0.4

要安装版本2.0.4,请执行以下操作:

wget https://pypi.python.org/packages/source/n/nltk/nltk-2.0.4.zip#md5=cbd04d8635f1358a69a38c4774be029c

7z x nltk-2.0.4.zip

cd nltk-2.0.4/

python setup.py install

要检查安装的版本,请运行以下命令:

pip search nltk

enter image description here

你应该这样做:

fd[word] += 1

但通常FreqDist的用法如下:

fd = FreqDist(my_text)

还请看下面的示例:

http://www.nltk.org/book/ch01.html

对于希望将图书示例更改为NLTK 3.0的用户:

import nltk
from nltk.corpus import brown

suffix_fdist = nltk.FreqDist()
for word in brown.words():
    word = word.lower()
    suffix_fdist[word[-1:]] +=1
    suffix_fdist[word[-2:]] +=1
    suffix_fdist[word[-3:]] +=1
common_suffixes = []
for suffix in suffix_fdist.most_common(100):
    common_suffixes.append(str(suffix.__getitem__(0)))
print common_suffixes

相关问题 更多 >