我是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'
知道我做错了吗?
答案 0 :(得分:16)
你应该这样做:
fd[word] += 1
但通常使用FreqDist:
fd = FreqDist(my_text)
另见这里的例子:
答案 1 :(得分:4)
对于想要如何将书籍示例更改为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
答案 2 :(得分: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
要检查安装的版本,请运行以下命令:
点子搜索nltk
答案 3 :(得分:0)
最新版本的nltk没有公司。相反,我使用了更新。
from nltk.corpus import gutenberg
from nltk import FreqDist
fd = FreqDist()
for word in gutenberg.words('austen-sense.txt'):
fd.update([word])
更新采用可迭代项目。因此,请确保在更新函数中传递可迭代项。