I get an error when trying FreqDist () in NLTK - NameError: name 'nltk' is not defined

I am learning NLTK and my mac works fine, except that I have problems with FreqDist (). (I saw another question about FreqDist (), but it got another error message. TypeError: unhashable type: 'list') Here is an example:

>>> from nltk.corpus import brown >>> news_text = brown.words(categories='news') >>> fdist = nltk.FreqDist([w.lower() for w in news_text]) Traceback (most recent call last): ` File "<stdin>", line 1, in <module>` `NameError: name 'nltk' is not defined` 

This error message is pretty consistent. I get this message every time I try to use FreqDist (). Other commands like → → brown.fileids () are fine.

Thanks for your help!

+7
source share
4 answers

Before you can use FreqDist, you need to import it.

Add the line as follows:

 import nltk 

or if you just want to use FreqDist, you should try the following:

 >>> from nltk.corpus import brown >>> from nltk import FreqDist >>> news_text = brown.words(categories='news') >>> fdist = FreqDist([w.lower() for w in news_text]) 
+14
source

which means you did not install nltk. follow these steps to install nltk:

1: follow this link https://pypi.python.org/pypi/setuptools at the end of the page where you will find setuptools-7.0.zip (md5), download it, then unzip it. you can find easy_install.py python script.

2: use the sudo easy_install pip command. By this time, pip will be installed ready to use (make sure you are in the directory where you can find the easy_install script file).

3: use this command sudo pip install -U nltk. successful execution ensures that nltk is now installed.

4: open IDLE, then enter the following:

import nltk

If nltk is installed correctly, you will be returned using the console.

+1
source

setuptools are required for older versions of Python. No need for the same, if you use 3.2+ you can easily download the same from https://pypi.python.org/pypi/nltk

For more information on http://www.nltk.org/install.html

0
source

NLTK requires data that you must download first. Then run the following code:

 import nltk nltk.download('stopwords') from nltk.corpus import stopwords stopwords.words("english") 
0
source

All Articles