欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

安装nltk语料库

程序员文章站 2022-05-18 17:14:56
...

在jupyter notebook上运行代码时:

import nltk
paragraph = "i am a good boy ! are you ok? hahaha i am fine"
words_list = nltk.word_tokenize(paragraph)
print(words_list)

出现错误:

ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-4-55bf564de021> in <module>
----> 1 import nltk
      2 paragraph = "i am a good boy ! are you ok? hahaha i am fine"
      3 words_list = nltk.word_tokenize(paragraph)
      4 print(words_list)

ModuleNotFoundError: No module named 'nltk'

显示没有nltk这个模块。

然后在cmd和conda里分别运行pip list和conda list,发现nltk都已经安装好。之后搜了一个博客,才知道还要下载nltk语料包。

尝试了一下自动下载:

在idle 3.7(按照自己电脑上的版本)中运行代码:

>>> import nltk
>>> nltk.download()

跳出来NlTK Downloder框,然后出现了getaddrinfo failed错误,貌似是服务器的问题。

之后看博客说是把NlTK Downloder里的Server Index的内容:

https://raw.githubusercontent.com/nltk/nltk_data/gh-pages/index.xml

换成:http://www.nltk.org/nltk_data/

点击下载时又出现错误相同错误:getaddrinfo failed

搜了一堆博客尝试后无果,没办法只能乖乖手动安装nltk。

手动安装有点麻烦,但也没办法。

不过有看到一个大佬写了个代码装的,看起来很厉害:

手动安装Python NLTK语言包

我自己是手动下载,然后解压。

在github上下载语料库:https://github.com/nltk/nltk_data

下载之后把里面的packages文件名改成nltk_data(里面的压缩包都要解压),然后放在该放的路径下。
查看该放的路径:可以先运行一段代码(在idle中运行),错误提示里会给出路径,比如下面:

>>> import nltk
>>> paragraph = "i am a good boy ! are you ok? hahaha i am fine"
>>> words_list = nltk.word_tokenize(paragraph)
Traceback (most recent call last):
  File "<pyshell#5>", line 1, in <module>
    words_list = nltk.word_tokenize(paragraph)
  File "C:\Program Files\Python37\lib\site-packages\nltk\tokenize\__init__.py", line 144, in word_tokenize
    sentences = [text] if preserve_line else sent_tokenize(text, language)
  File "C:\Program Files\Python37\lib\site-packages\nltk\tokenize\__init__.py", line 105, in sent_tokenize
    tokenizer = load('tokenizers/punkt/{0}.pickle'.format(language))
  File "C:\Program Files\Python37\lib\site-packages\nltk\data.py", line 868, in load
    opened_resource = _open(resource_url)
  File "C:\Program Files\Python37\lib\site-packages\nltk\data.py", line 993, in _open
    return find(path_, path + ['']).open()
  File "C:\Program Files\Python37\lib\site-packages\nltk\data.py", line 701, in find
    raise LookupError(resource_not_found)
LookupError: 
**********************************************************************
  Resource [93mpunkt[0m not found.
  Please use the NLTK Downloader to obtain the resource:

  [31m>>> import nltk
  >>> nltk.download('punkt')
  [0m
  For more information see: https://www.nltk.org/data.html

  Attempted to load [93mtokenizers/punkt/english.pickle[0m

  Searched in:
    - 'C:\\Users\\马静静/nltk_data'
    - 'C:\\Program Files\\Python37\\nltk_data'
    - 'C:\\Program Files\\Python37\\share\\nltk_data'
    - 'C:\\Program Files\\Python37\\lib\\nltk_data'
    - 'C:\\Users\\马静静\\AppData\\Roaming\\nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - ''
**********************************************************************

这一部分就是可以放nltk_data的路径:

Searched in:
    - 'C:\\Users\\马静静/nltk_data'
    - 'C:\\Program Files\\Python37\\nltk_data'
    - 'C:\\Program Files\\Python37\\share\\nltk_data'
    - 'C:\\Program Files\\Python37\\lib\\nltk_data'
    - 'C:\\Users\\马静静\\AppData\\Roaming\\nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - ''

我解压完后,直接把文件夹 ( packages文件名改成nltk_data的文件夹 ) 放在C:\\Users\\马静静\\下。
再运行代码就ok了。

>>> import nltk
>>> paragraph = "i am a good boy ! are you ok? hahaha i am fine"
>>> words_list = nltk.word_tokenize(paragraph)
>>> print(words_list)
['i', 'am', 'a', 'good', 'boy', '!', 'are', 'you', 'ok', '?', 'hahaha', 'i', 'am', 'fine']

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

相关标签: Python nltk