Python LookupError:Resource';语料库/stopwords';找不到
我正在尝试使用Flask在Heroku上运行Web应用程序。webapp是用Python和NLTK(自然语言工具包库)编程的 其中一个文件具有以下标头:Python LookupError:Resource';语料库/stopwords';找不到,python,python-2.7,heroku,flask,nltk,Python,Python 2.7,Heroku,Flask,Nltk,我正在尝试使用Flask在Heroku上运行Web应用程序。webapp是用Python和NLTK(自然语言工具包库)编程的 其中一个文件具有以下标头: import nltk, json, operator from nltk.corpus import stopwords from nltk.tokenize import RegexpTokenizer 调用带有stopwords代码的网页时,会产生以下错误: LookupError: ***********************
import nltk, json, operator
from nltk.corpus import stopwords
from nltk.tokenize import RegexpTokenizer
调用带有stopwords代码的网页时,会产生以下错误:
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()]
当我在计算机上测试代码时,Heroku提供的虚拟环境正在运行
我还尝试了由两个不同来源提供的NLTK,但是LookupError
仍然存在。我使用的两个来源是:问题在于语料库(“本例中的stopwords”)没有上传到Heroku。您的代码在本地机器上工作,因为它已经有NLTK语料库。请按照以下步骤解决此问题
nltk.data.path.append('path_to_nltk_data')
添加到实际使用nltk的Python文件中nltk.txt
文件添加到根目录,并在其中列出您的语料库。有关详细信息,请参阅
原始答案 这里有一个更干净的解决方案,允许您直接在Heroku上安装NLTK数据,而无需将其添加到git repo中 我使用类似的步骤在Heroku上安装,Heroku使用NLTK作为依赖项。在步骤3和步骤4中,我对我的原始代码做了一些小的调整,这些调整应该只适用于NLTK安装 默认heroku buildpack包括在所有默认构建步骤完成后运行的:
# 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
如您所见,它在您的项目目录中查找bin
目录中您自己的post\u compile
文件,如果它存在,它将运行它。您可以使用这个钩子来安装nltk数据
bin
目录
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"
# 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"
install_nltk_data
文件添加到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"
# 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"
nltk
添加到您的requirements.txt
文件中(或textblob
如果您使用的是textblob)
$ heroku config:set NLTK_DATA='/app/nltk_data'
post_compile
step触发器,然后是nltk下载
我希望这对你有帮助!享受吧 如果希望在Heroku上安装NLTK数据而不将其添加到git repo中,请参阅下面的我的解决方案:重要说明:Heroku python build pack v97改变了行为,导致忽略NLTK_数据目录。请参阅以获得修复。请尝试使用此选项