【问题标题】:NLTK reuters datasets not foundNLTK 路透社数据集未找到
【发布时间】:2019-04-06 21:47:41
【问题描述】:

我使用以下命令从 nltk 下载了路透社数据集:

import nltk
nltk.download('reuters')

我确认已下载数据集,并且可以在“C:/Users/username/AppData/Roaming/nltk_data”下看到它。

但是,当我想读取数据集时,python 看不到它!我收到以下错误:

C:\Users\username\python\Python37-32\Lib\site-packages\sklearn\externals\joblib\externals\cloudpickle\cloudpickle.py:47: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses
  import imp
Traceback (most recent call last):
  File "C:\Users\username\python\Python37-32\Lib\site-packages\nltk\corpus\util.py", line 80, in __load
    try: root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
  File "C:\Users\username\python\Python37-32\Lib\site-packages\nltk\data.py", line 675, in find
    raise LookupError(resource_not_found)
LookupError: 
**********************************************************************
  Resource [93mreuters[0m not found.
  Please use the NLTK Downloader to obtain the resource:

  [31m>>> import nltk
  >>> nltk.download('reuters')
  [0m
  Searched in:
    - 'C:\\Users\\username/nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - 'C:\\Users\\username\\python\\Python37-32\\nltk_data'
    - 'C:\\Users\\username\\python\\Python37-32\\share\\nltk_data'
    - 'C:\\Users\\username\\python\\Python37-32\\lib\\nltk_data'
    - 'C:\\Users\\username\\AppData\\Roaming\\nltk_data'
*******
During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
      File "C:\Users\username\eclipse-workspace\ML\src\PAs\pa2\Test.py", line 17, in <module>
        from commons import util, datasets, runClassifier, mlGraphics
      File "C:\Users\username\eclipse-workspace\ML\src\commons\datasets.py", line 258, in <module>
        class Reuters:
      File "C:\Users\username\eclipse-workspace\ML\src\commons\datasets.py", line 259, in Reuters
        documents = reuters.fileids()
      File "C:\Users\username\python\Python37-32\Lib\site-packages\nltk\corpus\util.py", line 116, in __getattr__
        self.__load()
      File "C:\Users\username\python\Python37-32\Lib\site-packages\nltk\corpus\util.py", line 81, in __load
        except LookupError: raise e
      File "C:\Users\username\python\Python37-32\Lib\site-packages\nltk\corpus\util.py", line 78, in __load
        root = nltk.data.find('{}/{}'.format(self.subdir, self.__name))
      File "C:\Users\username\python\Python37-32\Lib\site-packages\nltk\data.py", line 675, in find
        raise LookupError(resource_not_found)
    LookupError: 
    *********
      Resource [93mreuters[0m not found.
      Please use the NLTK Downloader to obtain the resource:
      [31m>>> import nltk
      >>> nltk.download('reuters')
      [0m
      Searched in:
        - 'C:\\Users\\username/nltk_data'
        - 'C:\\nltk_data'
        - 'D:\\nltk_data'
        - 'E:\\nltk_data'
        - 'C:\\Users\\username\\python\\Python37-32\\nltk_data'
        - 'C:\\Users\\username\\python\\Python37-32\\share\\nltk_data'
        - 'C:\\Users\\username\\python\\Python37-32\\lib\\nltk_data'
        -C:\\Users\\username\\AppData\\Roaming\\nltk_data' 

我尝试手动创建一个目录“C:/Users/username/nltk_data”并将 reuters.zip 粘贴到那里,但这没有帮助! 当我使用 nltk.download() 再次下载它时,它会显示以下内容:

[nltk_data] Downloading package reuters to C:\Users\username/nltk_data...
[nltk_data]   Package reuters is already up-to-date!

有什么提示吗? 我也想知道为什么python打印的路径同时包含斜杠/和反斜杠\

【问题讨论】:

  • 你是用nltk.downloader还是手动下载语料库的?
  • 我用文章开头提到的 nltk.downloader 下载了它
  • 你在用vim吗?
  • 它看起来像 python 3.7。版本“错误”,您可以在旧版本的 python 上运行相同的代码,例如。 3.6.5.?
  • 您可以将此作为答案发布,以便我将其标记为解决方案吗?

标签: python nltk reuters


【解决方案1】:

这是我的代码。你可以得到相应的帮助

import nltk
#nltk.download('punkt')
#nltk.download('averaged_perceptron_tagger')
var = open("e:\Assignment\my_file.txt","r") #open file
lines = var.read() #read all lines
sentences = nltk.sent_tokenize(lines) #tokenize sentences
nouns = [] #empty to array to hold all nouns

for sentence in sentences:
     for word,pos in nltk.pos_tag(nltk.word_tokenize(str(sentence))):
         if (pos == 'NN' or pos == 'NNP' or pos == 'NNS' or pos == 'NNPS'):
             nouns.append(word)


print (nouns)

【讨论】:

  • 这里我将使用 NLTK 提取名词
  • 这如何解决他的问题?
  • 他可以用他的文件名代替我的文件.. NLTK 可以工作
  • 实际上他必须使用这里没有的路透社数据集
【解决方案2】:

由于imp 模块在使用nltkpython 3.7 时已弃用,因此请使用import importlib 而不是import imp,或者尝试使用旧版本的python 运行代码。

【讨论】:

    【解决方案3】:

    在我的情况下,我只是去一个下载语料库的文件夹,然后解压缩一个存档。 要查看语料库的下载位置:

    nltk.download('reuters')

    [nltk_data] 正在将软件包 reuters 下载到 /home/denys/nltk_data...
    [nltk_data] 包裹 reuters 已经是最新的了!

    【讨论】:

      猜你喜欢
      • 2011-09-03
      • 2017-12-21
      • 2018-04-02
      • 2023-04-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-20
      • 2021-03-04
      • 1970-01-01
      相关资源
      最近更新 更多