【问题标题】:NLTK: Package Errors? punkt and pickle?NLTK:包错误?朋克和泡菜?
【发布时间】:2021-06-02 16:39:41
【问题描述】:

基本上,我不知道为什么会出现此错误。

不仅仅是一张图片,这里有一个类似的代码格式的消息。由于它是最近的,消息中已经提到了这个线程的答案:

Preprocessing raw texts ...

---------------------------------------------------------------------------

LookupError                               Traceback (most recent call last)

<ipython-input-38-263240bbee7e> in <module>()
----> 1 main()

7 frames

<ipython-input-32-62fa346501e8> in main()
     32     data = data.fillna('')  # only the comments has NaN's
     33     rws = data.abstract
---> 34     sentences, token_lists, idx_in = preprocess(rws, samp_size=samp_size)
     35     # Define the topic model object
     36     #tm = Topic_Model(k = 10), method = TFIDF)

<ipython-input-31-f75213289788> in preprocess(docs, samp_size)
     25     for i, idx in enumerate(samp):
     26         sentence = preprocess_sent(docs[idx])
---> 27         token_list = preprocess_word(sentence)
     28         if token_list:
     29             idx_in.append(idx)

<ipython-input-29-eddacbfa6443> in preprocess_word(s)
    179     if not s:
    180         return None
--> 181     w_list = word_tokenize(s)
    182     w_list = f_punct(w_list)
    183     w_list = f_noun(w_list)

/usr/local/lib/python3.7/dist-packages/nltk/tokenize/__init__.py in word_tokenize(text, language, preserve_line)
    126     :type preserver_line: bool
    127     """
--> 128     sentences = [text] if preserve_line else sent_tokenize(text, language)
    129     return [token for sent in sentences
    130             for token in _treebank_word_tokenizer.tokenize(sent)]

/usr/local/lib/python3.7/dist-packages/nltk/tokenize/__init__.py in sent_tokenize(text, language)
     92     :param language: the model name in the Punkt corpus
     93     """
---> 94     tokenizer = load('tokenizers/punkt/{0}.pickle'.format(language))
     95     return tokenizer.tokenize(text)
     96 

/usr/local/lib/python3.7/dist-packages/nltk/data.py in load(resource_url, format, cache, verbose, logic_parser, fstruct_reader, encoding)
    832 
    833     # Load the resource.
--> 834     opened_resource = _open(resource_url)
    835 
    836     if format == 'raw':

/usr/local/lib/python3.7/dist-packages/nltk/data.py in _open(resource_url)
    950 
    951     if protocol is None or protocol.lower() == 'nltk':
--> 952         return find(path_, path + ['']).open()
    953     elif protocol.lower() == 'file':
    954         # urllib might not use mode='rb', so handle this one ourselves:

/usr/local/lib/python3.7/dist-packages/nltk/data.py in find(resource_name, paths)
    671     sep = '*' * 70
    672     resource_not_found = '\n%s\n%s\n%s\n' % (sep, msg, sep)
--> 673     raise LookupError(resource_not_found)
    674 
    675 

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

  >>> import nltk
  >>> nltk.download('punkt')
  
  Searched in:
    - '/root/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'
    - '/usr/nltk_data'
    - '/usr/lib/nltk_data'
    - ''
**********************************************************************

【问题讨论】:

  • 阅读********************s 之间的内容。打开 Python shell import nltk,然后运行 ​​nltk.download()
  • 图片应该已经格式化为代码了。

标签: python command-line package nlp nltk


【解决方案1】:

执行以下操作:

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

然后当您收到一个弹出窗口时,在位于Module 选项卡中的identifier 列下选择punkt

【讨论】:

  • 所以我在 python shell 中试过这个,但它说“没有模块 nltk”,我知道我确实安装了
  • import nltk之后是不是这么说的?
  • 确实如此,但我发现需要先更新 nltk,然后其余的工作。谢谢!
  • 生产环境呢?我的意思是,在本地运行良好,但如果我需要使用 CICD 的 GitHub 操作部署到 AWS,该怎么办?需要向工作流文件 (.yml) 或 Dockerfile 添加一些内容吗?
【解决方案2】:

>>> import nltk
>>> nltk.download('punkt')
>>> from nltk import sent_tokenize

下载所有数据集和模型

>>> nltk.download('all')

确保您拥有最新版本的 NLTK,因为它一直在改进并不断维护:

$ pip install --upgrade nltk

Windows/Linux 上的类似问题,但上面的代码 sn-p 无济于事

Windows 上的类似问题

Linux 平台上的类似问题

OSX 上的类似问题

类似的问题,但有一些授权/身份验证错误

尝试在 python 解释器之外安装 NLTK 数据的类似问题

【讨论】:

    【解决方案3】:

    使用单个命令行 python -c 'import nltk; nltk.download("punkt")' 预安装 punkt 包。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      • 2016-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多