AttributeError:'FreqDist'对象没有属性'inc'

use*_*487 13 python attributes nltk python-2.7

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

from nltk.corpus import gutenberg
from nltk import FreqDist

fd = FreqDist()

for word in gutenberg.words('austen-sense.txt'):
    fd.inc(word)
Run Code Online (Sandbox Code Playgroud)

如果我运行这个我收到以下错误:

AttributeError: 'FreqDist' object has no attribute 'inc'
Run Code Online (Sandbox Code Playgroud)

知道我做错了什么吗?

Rai*_*iny 16

你应该这样做:

fd[word] += 1
Run Code Online (Sandbox Code Playgroud)

但通常FreqDist使用如下:

fd = FreqDist(my_text)
Run Code Online (Sandbox Code Playgroud)

另请查看以下示例:

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


dra*_*226 5

对于寻求如何将图书示例更改为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
Run Code Online (Sandbox Code Playgroud)