【问题标题】:LookupError: Resource 'corpora/stopwords' not foundLookupError:找不到资源“语料库/停用词”
【发布时间】:2014-07-29 05:39:32
【问题描述】:

我正在尝试使用 Flask 在 Heroku 上运行 webapp。 webapp 是用 Python 编写的,带有 NLTK(自然语言工具包库)。

其中一个文件具有以下标题:

import nltk, json, operator
from nltk.corpus import stopwords 
from nltk.tokenize import RegexpTokenizer 

当调用带有停用词代码的网页时,会产生以下错误:

LookupError: 
**********************************************************************
  Resource 'corpora/stopwords' not found.  Please use the NLTK  
  Downloader to obtain the resource:  >>> nltk.download()  
  Searched in:  
    - '/app/nltk_data'  
    - '/usr/share/nltk_data'  
    - '/usr/local/share/nltk_data'  
    - '/usr/lib/nltk_data'  
    - '/usr/local/lib/nltk_data'  
**********************************************************************

使用的确切代码:

#remove punctuation  
toker = RegexpTokenizer(r'((?<=[^\w\s])\w(?=[^\w\s])|(\W))+', gaps=True) 
data = toker.tokenize(data)  

#remove stop words and digits 
stopword = stopwords.words('english')  
data = [w for w in data if w not in stopword and not w.isdigit()]  

stopword = stopwords.words('english') 被注释掉时,Heroku 上的 webapp 不会产生查找错误。

代码在我的本地计算机上正常运行。我已经使用

在我的计算机上安装了所需的库
pip install requirements.txt  

当我在我的电脑上测试代码时,Heroku 提供的虚拟环境正在运行。

我也尝试了两个不同来源提供的 NLTK,但 LookupError 仍然存在。我使用的两个来源是:
http://pypi.python.org/packages/source/n/nltk/nltk-2.0.1rc4.zip
https://github.com/nltk/nltk.git

【问题讨论】:

标签: python python-2.7 heroku flask nltk


【解决方案1】:

更新

As Kenneth Reitz pointed out,在 heroku-python-buildpack 中添加了一个更简单的解决方案。将nltk.txt 文件添加到您的根目录并在其中列出您的语料库。详情请见https://devcenter.heroku.com/articles/python-nltk


原答案

这是一个更简洁的解决方案,允许您直接在 Heroku 上安装 NLTK 数据,而无需将其添加到您的 git 存储库中。

我使用类似的步骤在 Heroku 上安装 Textblob,它使用 NLTK 作为依赖项。我在第 3 步和第 4 步中对我的原始代码进行了一些小调整,这些调整应该适用于仅 NLTK 的安装。

默认的 heroku buildpack 包含一个 post_compile step,它在所有默认构建步骤完成后运行:

# post_compile
#!/usr/bin/env bash

if [ -f bin/post_compile ]; then
    echo "-----> Running post-compile hook"
    chmod +x bin/post_compile
    sub-env bin/post_compile
fi

如您所见,它会在您的项目目录中查找您自己的post_compile 文件,该文件位于bin 目录中,如果存在则运行它。你可以使用这个钩子来安装 nltk 数据。

  1. 在本地项目的根目录中创建bin 目录。

  2. 将您自己的post_compile 文件添加到bin 目录。

    # bin/post_compile
    #!/usr/bin/env bash
    
    if [ -f bin/install_nltk_data ]; then
        echo "-----> Running install_nltk_data"
        chmod +x bin/install_nltk_data
        bin/install_nltk_data
    fi
    
    echo "-----> Post-compile done"
    
  3. 将您自己的install_nltk_data 文件添加到bin 目录。

    # bin/install_nltk_data
    #!/usr/bin/env bash
    
    source $BIN_DIR/utils
    
    echo "-----> Starting nltk data installation"
    
    # Assumes NLTK_DATA environment variable is already set
    # $ heroku config:set NLTK_DATA='/app/nltk_data'
    
    # Install the nltk data
    # NOTE: The following command installs the stopwords corpora, 
    # so you may want to change for your specific needs.  
    # See http://www.nltk.org/data.html
    python -m nltk.downloader stopwords
    
    # If using Textblob, use this instead:
    # python -m textblob.download_corpora lite
    
    # Open the NLTK_DATA directory
    cd ${NLTK_DATA}
    
    # Delete all of the zip files
    find . -name "*.zip" -type f -delete
    
    echo "-----> Finished nltk data installation"
    
  4. nltk 添加到您的requirements.txt 文件(如果您使用的是Textblob,则添加textblob)。

  5. 将所有这些更改提交到您的存储库。

  6. 在您的 heroku 应用上设置 NLTK_DATA 环境变量。

    $ heroku config:set NLTK_DATA='/app/nltk_data'
    
  7. 部署到 Heroku。您将在部署结束时看到 post_compile 步骤触发器,然后是 nltk 下载。

我希望这对您有所帮助!享受吧!

【讨论】:

【解决方案2】:

问题在于语料库(在本例中为“停用词”)没有上传到 Heroku。您的代码可以在您的本地机器上运行,因为它已经拥有 NLTK 语料库。请按照以下步骤解决问题。

  1. 在您的项目中创建一个新目录(我们称之为“nltk_data”)
  2. 下载该目录中的 NLTK 语料库。您必须在下载期间进行配置。
  3. 告诉 nltk 寻找这个特定的路径。只需将 nltk.data.path.append('path_to_nltk_data') 添加到实际使用 nltk 的 Python 文件即可。
  4. 现在将应用程序推送到 Heroku。

希望能解决问题。为我工作!

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2012-12-07
  • 2017-05-27
  • 2022-10-25
  • 2021-07-22
  • 2015-08-20
  • 2014-12-28
  • 2016-06-19
  • 1970-01-01
相关资源
最近更新 更多