首页 > 解决方案 > 在 Heroku 上将自然语言工具包与 Django 结合使用 - - 错误:找不到“nltk.txt”

问题描述

我有一个基本的 Django 项目。我正在研究的一项功能是计算 .txt 文件中最常出现的单词的数量,例如一本大型公共领域书籍。我使用 Python Natural Language Tool Kit 过滤掉“停用词”(在 SEO 语言中,这意味着冗余词,例如“the”、“you”等)。

无论如何,当 Django 提供模板时,我得到了这个调试回溯:

资源 [93mstopwords[0m 未找到。请使用 NLTK 下载器获取资源: [31m <<< import nltk nltk.download('stopwords') [0m 更多信息参见:https://www.nltk.org/data.html

所以我需要下载停用词库。为了解决这个问题,我只需在我的远程服务器上打开一个 Python REPL 并调用这两个简单的行:

<<< import nltk
<<< nltk.download('stopwords')

在 SO 的其他地方对此进行了详细介绍。这解决了问题,但只是暂时的。一旦 REPL 会话在我的远程服务器上终止,错误就会返回,因为停用词文件刚刚消失。

当我使用 git 将更改推送到 Heroku 上的远程服务器时,我注意到一些奇怪的事情。检查这个:

remote: -----> Python app detected
remote: -----> No change in requirements detected, installing from cache
remote: -----> Installing pip 20.1.1, setuptools 47.1.1 and wheel 0.34.2
remote: -----> Installing SQLite3
remote: -----> Installing requirements with pip
remote: -----> Downloading NLTK corpora…
remote:  !     'nltk.txt' not found, not downloading any corpora
remote:  !     Learn more: https://devcenter.heroku.com/articles/python-nltk 
remote: -----> $ python manage.py collectstatic --noinput
remote:        122 static files copied to '/tmp/build_f2f9d10f/staticfiles', 388 post-processed.

开发中心链接有点像存根,这意味着它不是很详细。充其量是稀疏的。文章说要使用 Python nltk,您需要nltk.txt在项目目录中添加一个文件,该文件指定 Heroku 要下载的对象列表。所以我继续创建了一个 nltk 文本文件,其中包含:

语料库

这是当前位于我的项目目录中的活动 nltk.txt。除了 coprora,我还尝试将以下三个条目的各种组合添加到 nltk.txt:

语料库

停止列表

英语

我尝试添加所有四个,只有两个和一个。例如,这是我尝试逐字逐句的替代 nltk.txt。我的感觉是,我真正需要的主要是 just corpora,所以这是我现在正在使用的 nltk.txt 中的唯一条目。在corpora那里,当我推送更改并且 Heroku 构建环境时,我看到了这个错误并回溯:

remote: -----> Downloading NLTK corpora…
remote: -----> Downloading NLTK packages: corpora english stopwords corpus
remote: /app/.heroku/python/lib/python3.6/runpy.py:125: RuntimeWarning: 'nltk.downloader' found in sys.modules after import of package 'nltk', but prior to execution of 'nltk.downloader'; this may result in unpredictable behaviour
remote:   warn(RuntimeWarning(msg))
remote: [nltk_data] Error loading corpora: Package 'corpora' not found in
remote: [nltk_data]     index
remote: Error installing package. Retry? [n/y/e]
remote: Traceback (most recent call last):
remote:   File "/app/.heroku/python/lib/python3.6/runpy.py", line 193, in _run_module_as_main
remote:     "__main__", mod_spec)
remote:   File "/app/.heroku/python/lib/python3.6/runpy.py", line 85, in _run_code
remote:     exec(code, run_globals)
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/nltk/downloader.py", line 2538, in <module>
remote:     halt_on_error=options.halt_on_error,
remote:   File "/app/.heroku/python/lib/python3.6/site-packages/nltk/downloader.py", line 790, in download

我显然没有正确使用 nltk.txt,因为它没有找到corpora包。我可以安装 nltk 并让它在我的本地开发服务器上正常运行,但我剩下的问题是:如何让 Heroku 在这种情况下远程正确处理 nltk

用户Michael Godshall对多个 Stack Overflow 问题提供了相同的答案,解释说bin您可以在项目根目录中创建一个目录并添加post_compilebash 脚本和install_nltk_data脚本。然而,这不再是必要的,因为 heroku-buildpack-python 上游维护者Kenneth Reitz 实现了一个简单的解决方案。现在所需要做的就是添加一个 nltk.txt ,其中包含您需要的库。但我这样做了,我仍然收到上面的错误。

nltk 官方网站记录了如何使用该库以及如何安装它,这对 Heroku 没有帮助,因为 Heroku 似乎以不同的方式处理 nltk。

标签: pythondjangogitheroku

解决方案


是的,您需要正确地与nltk.txt文件类似的requirements.txt文件。请参阅此处的官方文档。如果您仍然面临同样的情况,请在nltk.txt此处发布文件,这将为我们提供一些找到解决方案的方法

也许也会帮助你


推荐阅读