python - 从默认 ~/ntlk_data 更改 nltk.download() 路径目录

标签 python python-2.7 path nltk default

我试图在计算服务器上下载/更新 python nltk 包,它返回了这个 [Errno 122] Disk quota exceeded: 错误。

具体来说:

[nltk_data] Downloading package stop words to /home/sh2264/nltk_data...
[nltk_data] Error downloading u'stopwords' from
[nltk_data] <https://raw.githubusercontent.com/nltk/nltk_data/gh-
[nltk_data] pages/packages/corpora/stopwords.zip>: [Errno 122]
[nltk_data] Disk quota exceeded:
[nltk_data] u'/home/sh2264/nltk_data/corpora/stopwords.zip
False

如何更改 nltk 包的整个路径,以及我应该进行哪些其他更改以确保正确加载 nltk

最佳答案

这可以通过命令行( nltk.download(..., download_dir=) 或 GUI 进行配置。奇怪的是 nltk 似乎完全忽略了它自己的环境变量 NLTK_DATA 并将其下载目录默认为一组标准的五个路径,无论 NLTK_DATA 是否被定义以及它在哪里点,并且不管 nltk 的五个默认目录是否甚至存在于机器或架构上(!)。其中一些记录在 Installing NLTK Data 中,尽管它不完整并且有点被掩埋;在下面以更清晰的格式转载:

Command line installation

The downloader will search for an existing nltk_data directory to install NLTK data. If one does not exist it will attempt to create one in a central location (when using an administrator account) or otherwise in the user’s filespace. If necessary, run the download command from an administrator account, or using sudo. The recommended system location is:

  • C:\nltk_data (Windows) ;
  • /usr/local/share/nltk_data (Mac) and
  • /usr/share/nltk_data (Unix).

You can use the -d flag to specify a different location (but if you do this, be sure to set the NLTK_DATA environment variable accordingly).

  • Run the command python -m nltk.downloader all

  • To ensure central installation, run the command: sudo python -m nltk.downloader -d /usr/local/share/nltk_data all

  • But really they should say: sudo python -m nltk.downloader -d $NLTK_DATA all

现在关于NLTK_DATA 应该使用什么推荐路径,nltk 并没有真正给出任何正确的指导,但它应该是一个通用的独立路径,不在任何安装树下(所以不在 <python-install-directory>/lib/site-packages 下)或任何用户目录。因此, /usr/local/share/opt/share 或类似的。在 MacOS 10.7+ 上,/usr/usr/local/ 这些天默认隐藏,所以 /opt/share 可能是更好的选择。或者执行 chflags nohidden /usr/local/share

关于python - 从默认 ~/ntlk_data 更改 nltk.download() 路径目录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44857382/

相关文章:

Python Seaborn堆叠条形图多列

python - 在 PyCharm 中的远程解释器上运行远程脚本

Python请求创建Cookie失败

multithreading - 使用 Python 2.7 的 GAE 上的线程抛出 "FrontendsNotSupported"错误

flutter - 保存的图像不会在图库中显示

QMimeData 中的 Python 对象

python - 多个列表的字典

java - 从 VBA 代码启动 jar 文件

javascript - 拉斐尔的相交路径

python - 获取每行的三个最小值并返回对应的列名