NameError: name 'FreqDist' is not defined

1,859 views
Skip to first unread message

typetoken

unread,
Sep 30, 2015, 11:53:03 PM9/30/15
to nltk-users
Dear All,

When I follow the instruction here  http://www.nltk.org/book/ch01.html  to use  fdist5 = FreqDist(text5)


IDE interpreter always pops up the following error:

>>> fdist5 = FreqDist(text5)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    fdist5 = FreqDist(text5)
NameError: name 'FreqDist' is not defined


As a matter of fact, I have input the following commands at the very beginning:

>>> import nltk
>>> from nltk.book import *

Seems the above command are not sufficient.

In order to solve the above Error "name 'FreqDist' is not defined", I have to import it again everytime as follows:
>>> from nltk import FreqDist


Why can't the following two commands work enough to avoid the error?
>>> import nltk
>>> from nltk.book import *

By the way, I am using Python 3.4.2, nltk took kit 3.0.

Any hints? Many thanks.

Sincerely
Typetoken

typetoken

unread,
Oct 1, 2015, 12:56:27 AM10/1/15
to nltk-users
The same happens to bigrams()

>>> list(bigrams(['more', 'is', 'said', 'than', 'done']))
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    list(bigrams(['more', 'is', 'said', 'than', 'done']))
NameError: name 'bigrams' is not defined
Message has been deleted

Maciej Pastuszka

unread,
Oct 1, 2015, 4:43:44 AM10/1/15
to nltk-users
Hi,

Regarding your first question, it seems that in NLTK 3, you need to import FreqDist from the nltk.probability module:

>>> from nltk.probability import FreqDist

Then run:

>>> fdist5 = FreqDist(text5)

and see if it works.

As far as bigrams are concerned, please import the relevant class first:

>>> from nltk.util import bigrams

Then run the example from the NLTK book:

>>> list(bigrams(['more', 'is', 'said', 'than', 'done']))

For further reference, please refer to the documentation of the NLTK package:


Best Regards,

Maciej.
Reply all
Reply to author
Forward
0 new messages