在 Google Colab 中加载某些 NLTK 模块时出错

Error loading certain NLTK modules in Google Colab

我正在尝试将 comtrans 模块从 NLTK 加载到 Google Colab 笔记本中,但出现以下错误:

[nltk_data] Downloading package comtrans to /root/nltk_data...
[nltk_data]   Package comtrans is already up-to-date!

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

LookupError                               Traceback (most recent call last)

/usr/local/lib/python3.7/dist-packages/nltk/corpus/util.py in __load(self)
     79             except LookupError as e:
---> 80                 try: root = nltk.data.find('{}/{}'.format(self.subdir, zip_name))
     81                 except LookupError: raise e

5 frames

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

  >>> import nltk
  >>> nltk.download('comtrans')
  
  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'
**********************************************************************


During handling of the above exception, another exception occurred:

LookupError                               Traceback (most recent call last)

/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 comtrans not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('comtrans')
  
  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'
**********************************************************************

这是我使用的代码:

import nltk
nltk.download('comtrans')

data = nltk.corpus.comtrans.aligned_sents('alignment-en-fr.txt')
print(data[0])
print(len(data))

在我看到的其他问题中,大多数人都提到停用词有问题。但就我而言,停用词按预期工作。

import nltk
nltk.download('stopwords')

words = nltk.corpus.stopwords.words('english')
print(words[10])
print(len(words))

''' output:
[nltk_data] Downloading package stopwords to /root/nltk_data...
[nltk_data]   Package stopwords is already up-to-date!
you've
179
'''

当 运行 在我的本地计算机上时,停用词和 comtrans 都可以正常工作。这只是 colab,comtrans 无法加载数据。这些是我希望从 comtrans 打印语句中看到的值:

<AlignedSent: 'Resumption of the se...' -> 'Reprise de la sessio...'>
33334

是否有其他方法可以尝试通过 NLTK 加载此数据,或者我是否一直在做一些事情,例如从我的机器上载文件本身并通过其他方式加载它。如果我需要直接上传文件,什么代码会将其从文本文件解析为 NLTK 返回的 AlignedSent 对象?

看起来 Colab 正在正确下载包,就像它声称的那样。但是 NLTK 模块都是作为 zip 文件下载的,停用词和 comtrans 都是这种情况。对于停用词,它会在下载后解压缩,而 comtrans 会跳过解压缩步骤。这里的区别在于,本地 NLTK 愿意直接从 zip 文件中获取 comtrans 数据,但在 Colab 中则不然。因此,由于该数据仅以 zip 形式提供,因此它拒绝了带有“未找到资源”的操作。

我检查过的所有 NLTK 压缩文件都在根级别包含一个文件夹,其中包含模块的所有特定文件。该文件夹需要解压缩到与 zip 文件相同的位置。

在这种情况下解压缩只需要手动完成。

import nltk
nltk.download('comtrans')
# Data is downloaded to /root/nltk_data/corpora/comtrans.zip

from zipfile import ZipFile
file_loc = '/root/nltk_data/corpora/comtrans.zip'
with ZipFile(file_loc, 'r') as z:
  z.extractall('/root/nltk_data/corpora/')

data = nltk.corpus.comtrans.aligned_sents('alignment-en-fr.txt')
print(data[0]) # <AlignedSent: 'Resumption of the se...' -> 'Reprise de la sessio...'>