如何在nltk中下载punkt tokenizer?

问题描述 投票:0回答:2

最近我使用下载了NLTK库

“pip 安装 nltk”

并且在使用库时

“从nltk.tokenize导入sent_tokenize 发送令牌化(文本)”

我收到此错误

**********************************************************************
  Resource punkt not found.
  Please use the NLTK Downloader to obtain the resource:

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

  Attempted to load tokenizers/punkt/english.pickle

  Searched in:
    - 'C:\\Users\\adars/nltk_data'
    - 'C:\\Users\\adars\\AppData\\Local\\Programs\\Python\\Python310\\nltk_data'
    - 'C:\\Users\\adars\\AppData\\Local\\Programs\\Python\\Python310\\share\\nltk_data'
    - 'C:\\Users\\adars\\AppData\\Local\\Programs\\Python\\Python310\\lib\\nltk_data'
    - 'C:\\Users\\adars\\AppData\\Roaming\\nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - ''```
So in order to solve this error i tried
 

"import nltk
nltk.download('punkt')"

---


but then i am unable to download this package because everytime i run this i get error that says

[nltk_data] Error loading punkt: <urlopen error [WinError 10060] A
[nltk_data]     connection attempt failed because the connected party
[nltk_data]     did not properly respond after a period of time, or
[nltk_data]     established connection failed because connected host
[nltk_data]     has failed to respond>

please help me out here
machine-learning debugging nlp nltk
2个回答
0
投票

1)只需打开任何提示符或 anaconda 提示符并激活运行脚本的环境 2)输入python 3)按回车键 4)执行“导入nltk” 5)nltk.download('punkt') 下载后,您可以继续使用您的代码IDE


0
投票

这很可能是由于网络问题而发生的。您可以尝试以下任一解决方案:

1. 在脚本中添加以下内容:

import nltk
import ssl

try:
    _create_unverified_https_context = ssl._create_unverified_context
except AttributeError:
    pass
else:
    ssl._create_default_https_context = _create_unverified_https_context

nltk.download('punkt')

2. 如果上述解决方案没有帮助,那么:

  • 这里
    手动下载'punkt'
  • 解压文件,然后转到
    'C:/Users/adars/AppData/Local/Programs/Python/Python310/lib/'
    ,您需要在其中创建一个名为
    nltk_data
    的文件夹。
  • nltk_data
    下,创建另一个名为
    tokenizers
    的文件夹,并将提取的文件夹
    punkt
    放在那里,以便您获得一个新目录
    tokenizers/punkt
    ,其中包含所有
    .pickle
    文件。
  • 完成后,您无需再做
    nltk.download('punkt')
    再次,直接运行您的代码即可。
© www.soinside.com 2019 - 2024. All rights reserved.