Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
620 views
in Technique[技术] by (71.8m points)

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

I am a beginner in Python and NLTK. I am trying to run the following code from a tutorial:

from nltk.corpus import gutenberg
from nltk import FreqDist

fd = FreqDist()

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

If I run this I get the following error:

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

Any idea what I am doing wrong?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You should do it like so:

fd[word] += 1

But usually FreqDist is used like this:

fd = FreqDist(my_text)

Also look at the examples here:

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...