Skip to main content
 首页 » 编程设计

python之nltk PlaintextCorpusReader 发送和参数函数不起作用

2025年05月04日24linjiqin

我无法使 PlaintextCorpusReader 中的 paras 和 sents 函数正常工作。这是我的代码:

import nltk 
from nltk.corpus import PlaintextCorpusReader 
 
corpus_root = './dir_root' 
newcorpus = PlaintextCorpusReader(corpus_root, '.*') # Files you want to add 
 
word_list = newcorpus.words('file1.txt') 
sentence_list = newcorpus.sents('file1.txt') 
paragraph_list = newcorpus.paras('file1.txt') 
 
print(word_list) 
print(sentence_list) 
print(paragraph_list) 

word_list 结果很好。

['__________________________________________________________________', 'Title', ...] 

但是,paragraph_list 和 sentence_list 都给出了这个错误:

    Traceback (most recent call last): 
  File "corpus.py", line 13, in <module> 
    print(sentence_list) 
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/collections.py", line 225, in __repr__ 
    for elt in self: 
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/util.py", line 296, in iterate_from 
    tokens = self.read_block(self._stream) 
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/corpus/reader/plaintext.py", line 129, in _read_sent_block 
    for sent in self._sent_tokenizer.tokenize(para)]) 
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 956, in __getattr__ 
    self.__load() 
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 948, in __load 
    resource = load(self._path) 
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 808, in load 
    opened_resource = _open(resource_url) 
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 926, in _open 
    return find(path_, path + ['']).open() 
  File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/site-packages/nltk/data.py", line 648, in find 
    raise LookupError(resource_not_found) 
LookupError: 
********************************************************************** 
  Resource 'tokenizers/punkt/PY3/english.pickle' not found. 
  Please use the NLTK Downloader to obtain the resource:  >>> 
  nltk.download() 
  Searched in: 
    - '/Users/username/nltk_data' 
    - '/usr/share/nltk_data' 
    - '/usr/local/share/nltk_data' 
    - '/usr/lib/nltk_data' 
    - '/usr/local/lib/nltk_data' 
    - '' 
********************************************************************** 

我尝试使用 nltk.download() 将文件下载到语料库中,但这也不起作用。另外,它看起来不像它应该的工作方式,因为 PlaintextCorpusReader 已经这样做了。 paras 和 sents 函数是 PlaintextCorpusReader 的一部分。我需要输入特定的字段吗?或者,是否需要某种正则表达式参数来查找句子或段落? documentation和 source code似乎没有说它比 words 功能需要更多。

请您参考如下方法:

您缺少句子分词器所需的数据文件(“资源”)。通过在交互式下载器中的“模型”下下载“punkt”资源来解决问题,或者通过一次非交互式地运行此代码来解决问题:

nltk.download("punkt") 

为了避免在探索 nltk 时反复遇到此类问题,我建议现在下载“book”包。它包含您可能需要一段时间的所有内容。