嘿,我想在我的 Heroku 服务器上安装 NLTK pos_tag。我怎么能这样做。请给我作为 Heroku 服务器系统新手的步骤。
4 回答
我刚刚为 buildpack 添加了官方nltk
支持!
只需添加一个nltk.txt
包含您要安装的语料库列表的文件,一切都应该按预期工作。
更新
正如 Kenneth Reitz 指出的那样,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
步骤完成后运行的步骤:
# 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 数据。
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"
将您自己的
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 averaged_perceptron_tagger corpora, # so you may want to change for your specific needs. # See http://www.nltk.org/data.html python -m nltk.downloader averaged_perceptron_tagger # 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 应用程序上设置 NLTK_DATA 环境变量。
$ heroku config:set NLTK_DATA='/app/nltk_data'
部署到 Heroku。您将
post_compile
在部署结束时看到 step trigger,然后是 nltk 下载。
我希望你觉得这很有帮助!享受!
如果您想使用简单的功能,如 pos_tag、tokenizer、stemming 等,那么您可以执行以下步骤
- 在 requirements.txt 中提到 nltk
- 在 nltk.txt 中提及以下模块
- 词网
- 优点缺点
- 路透社
- hmm_treebank_pos_tagger
- maxent_treebank_pos_tagger
- 通用标签集
- 朋克
- averaged_perceptron_tagger_ru
- averaged_perceptron_tagger
- 雪球数据
- 回复
- 搬运工测试
- vader_lexicon
- 树库
- 依赖关系树库
您需要按照以下步骤操作。
- nltk.txt 需要出现在根文件夹中
- 将您要下载的模块(如 punkt、停用词)添加为单独的行项目
- 将行结尾从 windows 更改为 UNIX
更改行尾是非常重要的一步。可以通过 Sublime Text 或 Notepad++ 轻松完成。在 Sublime Text 中,它可以从 View 菜单中完成,然后是 Line Endings。
希望这可以帮助