Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

nltk

I'm learning about the NLTK and my mac is working fine except I have trouble with the FreqDist(). (I saw another question about FreqDist() but he was getting a different error message. TypeError: unhashable type: 'list') Here's 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 the FreqDist(). Other commands like - >>> brown.fileids() are fine.

Thanks for your help!

like image 638
user1839239 Avatar asked Nov 20 '12 15:11

user1839239


2 Answers

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

Add a line as follows:

import nltk

or if you just want to use FreqDist you should try this:

>>> from nltk.corpus import brown
>>> from nltk import FreqDist
>>> news_text = brown.words(categories='news')
>>> fdist = FreqDist([w.lower() for w in news_text])
like image 185
Spaceghost Avatar answered Sep 23 '22 19:09

Spaceghost


which means you haven't installed nltk. follow these steps to install nltk:

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

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

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

4:open the IDLE then you type the following:

import nltk

if nltk is installed properly then you will be returned with console.

like image 45
Manjunath N Avatar answered Sep 21 '22 19:09

Manjunath N