问题描述
最近在学习《Python自然语言处理》一书中需要使用到NLTK模块,但是在导入数据时出现了问题。即输入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 "D:\Anaconda3\lib\site-packages\nltk\corpus\util.py", line 80, in __load
try: root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
File "D:\Anaconda3\lib\site-packages\nltk\data.py", line 653, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource 'corpora/gutenberg.zip/gutenberg/' not found. Please
use the NLTK Downloader to obtain the resource: >>>
nltk.download()
Searched in:
- 'F:\\Demo_files\\PCB/nltk_data'
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- 'D:\\Anaconda3\\nltk_data'
- 'D:\\Anaconda3\\lib\\nltk_data'
- 'C:\\Users\\Spico\\AppData\\Roaming\\nltk_data'
**********************************************************************
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "D:\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2862, in run_code
exec(code_obj, self.user_global_ns, self.user_ns)
File "<ipython-input-2-2a5ed6d8b2c3>", line 1, in <module>
from nltk.book import *
File "D:\PyCharm\PyCharm 2017.3.1\helpers\pydev\_pydev_bundle\pydev_import_hook.py", line 20, in do_import
module = self._system_import(name, *args, **kwargs)
File "D:\Anaconda3\lib\site-packages\nltk\book.py", line 20, in <module>
text1 = Text(gutenberg.words('melville-moby_dick.txt'))
File "D:\Anaconda3\lib\site-packages\nltk\corpus\util.py", line 116, in __getattr__
self.__load()
File "D:\Anaconda3\lib\site-packages\nltk\corpus\util.py", line 81, in __load
except LookupError: raise e
File "D:\Anaconda3\lib\site-packages\nltk\corpus\util.py", line 78, in __load
root = nltk.data.find('{}/{}'.format(self.subdir, self.__name))
File "D:\Anaconda3\lib\site-packages\nltk\data.py", line 653, in find
raise LookupError(resource_not_found)
LookupError:
**********************************************************************
Resource 'corpora/gutenberg' not found. Please use the NLTK
Downloader to obtain the resource: >>> nltk.download()
Searched in:
- 'F:\\Demo_files\\PCB/nltk_data'
- 'C:\\nltk_data'
- 'D:\\nltk_data'
- 'E:\\nltk_data'
- 'D:\\Anaconda3\\nltk_data'
- 'D:\\Anaconda3\\lib\\nltk_data'
- 'C:\\Users\\Spico\\AppData\\Roaming\\nltk_data'
**********************************************************************
之前曾经使用过nltk库,并且通过nltk.download()
工具下载过相关的语料,加之错误信息的提示,所以考虑到要从路径方面解决。
解决办法
在导入nltk.book
前,先使用如下指令将data路径导入:
from nltk import data
data.path.append(r"F:\nltk_data") # 这里的路径需要换成自己数据文件下载的路径
问题即可解决。网上查到的资料还有修改系统环境变量的方法,但是本人尝试后并未发现有效。