最为小菜,最近初次了解NLTK,因此想学一下代码如下
nltk.tokenize import sent_tokenize mytext = "Hello Mr. Adam, how are you? I hope everything is going well. Today is a good day, see you dude." sent_tokenize(mytext)
出现报错, Resource punkt not found. Please use the NLTK Downloader to obtain the resource:
import nltk nltk.download('punkt')
但实际上是存在的
仔细一看, 错误中Search在C盘下C:\Users\Hu/nltk_data
Searched in: - 'C:\Users\Hu/nltk_data' - 'D:\ProgramData\Miniconda3\envs\tensorflow\nltk_data' - 'D:\ProgramData\Miniconda3\envs\tensorflow\share\nltk_data' - 'D:\ProgramData\Miniconda3\envs\tensorflow\lib\nltk_data' - 'C:\Users\Hu\AppData\Roaming\nltk_data' - 'C:\nltk_data' - 'D:\nltk_data' - 'E:\nltk_data' - ''
查看源码可以看见
tokenizer=load('tokenizers/punkt/{0}.pickle'.format(language))
在看load来自nltk下的data
def load( resource_url, format='auto', cache=True, verbose=False, logic_parser=None, fstruct_reader=None, encoding=None,
从这里,我的第一反应是resource_url,因此迅速找到下文发现
resource_url = normalize_resource_url(resource_url) resource_url = add_py3_data(resource_url)
先看到被屏蔽的说明文字windows = sys.platform.startswith('win') 于是我找到前文
path += [ os.path.join(sys.prefix, str('nltk_data')), os.path.join(sys.prefix, str('share'), str('nltk_data')), os.path.join(sys.prefix, str('lib'), str('nltk_data')), os.path.join(os.environ.get(str('APPDATA'), str('C:\')), str('nltk_data')), str(r'C:\nltk_data'), str(r'D:\nltk_data'), str(r'E:\nltk_data'),
所以我改了nltk.data.path = ['D:\Users/Hu/AppData/Roaming/nltk_data'] OK运行成功
['Hello Mr. Adam, how are you?', 'I hope everything is going well.', 'Today is a good day, see you dude.']
Process finished with exit code 0