NLTK data路径设置

来源:互联网 发布:java web 反编译 编辑:程序博客网 时间:2024/05/17 12:21
安装nltk后用这个命令:from nltk.book import *


按照正常的,应该出现以下:
>>> from nltk.book import *
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: *texts()* or *sents()* to list the materials.
text1: Moby Dick by Herman Melville 1851
text2: Sense and Sensibility by Jane Austen 1811
text3: The Book of Genesis
text4: Inaugural Address Corpus
text5: Chat Corpus
text6: Monty Python and the Holy Grail
text7: Wall Street Journal
text8: Personals Corpus
text9: The Man Who Was Thursday by G . K . Chesterton 1908
>>>



但是我出现的是这个:


>>> from nltk.book import *
*** Introductory Examples for the NLTK Book ***
Loading text1, ..., text9 and sent1, ..., sent9
Type the name of the text or sentence to view it.
Type: *texts()* or *sents()* to list the materials.


Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
from nltk.book import *
File "C:\Python26-B\lib\site-packages\nltk\book.py", line 22, in <module>
text1 = Text(gutenberg.words(*melville-moby_dick.txt*))
File "C:\Python26-B\lib\site-packages\nltk\corpus\util.py", line 99, in __getattr__
self.__load()
File "C:\Python26-B\lib\site-packages\nltk\corpus\util.py", line 64, in __load
except LookupError: raise e
LookupError:
**********************************************************************
Resource u*corpora/gutenberg* not found. Please use the NLTK
Downloader to obtain the resource: >>> nltk.download()
Searched in:
- *C:\\Users\\XXXXXX/nltk_data*
- *C:\\nltk_data*
- *D:\\nltk_data*
- *E:\\nltk_data*
- *C:\\Python26-B\\nltk_data*
- *C:\\Python26-B\\lib\\nltk_data*
- *C:\\Users\\XXXXX\\AppData\\Roaming\\nltk_data*
**********************************************************************

>>>


看下面的目录就明白了,下载nltk_data时我更改了路径,而默认的搜索路径中没有我的nltk_data路径。

在 http://www.datatang.com/data/16932 中找了如下内容:

If you did not install the data to one of the above central locations, you will need to set the NLTK_DATA environment variable to specify the location of the data.  (On a Windows machine, do right click on "My Computer", select Properties > Advanced > Environment Variables > User Variables > New...)


所以在电脑属性环境变量中增加 NLTK_DATA  ,设置自己的路径后,重新打开Python 问题即解决。




0 0
原创粉丝点击