如何从代码中配置nltk数据目录?

发布于 2024-09-15 02:08:53 字数 24 浏览 8 评论 0原文

如何从代码中配置nltk数据目录?

How to config nltk data directory from code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

凉城已无爱 2024-09-22 02:08:53

只需更改nltk.data.path的项目即可,这是一个简单的列表。

Just change items of nltk.data.path, it's a simple list.

人心善变 2024-09-22 02:08:53

从代码中,http://www.nltk.org/_modules/nltk/data.html

``nltk:path``:指定NLTK数据中存储的文件
 包位于*路径*。 NLTK 将在以下位置搜索这些文件
 由“nltk.data.path”指定的目录。

然后在代码中:

######################################################################
# Search Path
######################################################################

path = []
"""A list of directories where the NLTK data package might reside.
   These directories will be checked in order when looking for a
   resource in the data package.  Note that this allows users to
   substitute in their own versions of resources, if they have them
   (e.g., in their home directory under ~/nltk_data)."""

# User-specified locations:
path += [d for d in os.environ.get('NLTK_DATA', str('')).split(os.pathsep) if d]
if os.path.expanduser('~/') != '~/':
    path.append(os.path.expanduser(str('~/nltk_data')))

if sys.platform.startswith('win'):
    # Common locations on Windows:
    path += [
        str(r'C:\nltk_data'), str(r'D:\nltk_data'), str(r'E:\nltk_data'),
        os.path.join(sys.prefix, str('nltk_data')),
        os.path.join(sys.prefix, str('lib'), str('nltk_data')),
        os.path.join(os.environ.get(str('APPDATA'), str('C:\\')), str('nltk_data'))
    ]
else:
    # Common locations on UNIX & OS X:
    path += [
        str('/usr/share/nltk_data'),
        str('/usr/local/share/nltk_data'),
        str('/usr/lib/nltk_data'),
        str('/usr/local/lib/nltk_data')
    ]

要修改路径,只需附加到可能路径的列表中:

import nltk
nltk.data.path.append("/home/yourusername/whateverpath/")

或者在 Windows 中:

import nltk
nltk.data.path.append("C:\somewhere\farfar\away\path")

From the code, http://www.nltk.org/_modules/nltk/data.html:

``nltk:path``: Specifies the file stored in the NLTK data
 package at *path*.  NLTK will search for these files in the
 directories specified by ``nltk.data.path``.

Then within the code:

######################################################################
# Search Path
######################################################################

path = []
"""A list of directories where the NLTK data package might reside.
   These directories will be checked in order when looking for a
   resource in the data package.  Note that this allows users to
   substitute in their own versions of resources, if they have them
   (e.g., in their home directory under ~/nltk_data)."""

# User-specified locations:
path += [d for d in os.environ.get('NLTK_DATA', str('')).split(os.pathsep) if d]
if os.path.expanduser('~/') != '~/':
    path.append(os.path.expanduser(str('~/nltk_data')))

if sys.platform.startswith('win'):
    # Common locations on Windows:
    path += [
        str(r'C:\nltk_data'), str(r'D:\nltk_data'), str(r'E:\nltk_data'),
        os.path.join(sys.prefix, str('nltk_data')),
        os.path.join(sys.prefix, str('lib'), str('nltk_data')),
        os.path.join(os.environ.get(str('APPDATA'), str('C:\\')), str('nltk_data'))
    ]
else:
    # Common locations on UNIX & OS X:
    path += [
        str('/usr/share/nltk_data'),
        str('/usr/local/share/nltk_data'),
        str('/usr/lib/nltk_data'),
        str('/usr/local/lib/nltk_data')
    ]

To modify the path, simply append to the list of possible paths:

import nltk
nltk.data.path.append("/home/yourusername/whateverpath/")

Or in windows:

import nltk
nltk.data.path.append("C:\somewhere\farfar\away\path")
悍妇囚夫 2024-09-22 02:08:53

我使用附加,例如

nltk.data.path.append('/libs/nltk_data/')

I use append, example

nltk.data.path.append('/libs/nltk_data/')
高冷爸爸 2024-09-22 02:08:53

NLTK 接受 NLTK_DATA 环境变量,而不是将 nltk.data.path.append('your/path/to/nltk_data') 添加到每个脚本。 (代码链接

打开~/. bashrc(或~/.profile),使用文本编辑器(例如nanovimgedit ),并添加以下行:

export NLTK_DATA="your/path/to/nltk_data"

执行 source 加载环境变量

source ~/.bashrc

测试

打开 python 并执行以下行

import nltk
nltk.data.path

您可以看到 nltk 数据路径已在其中。

参考:@alvations 的回答
nltk/nltk #1997

Instead of adding nltk.data.path.append('your/path/to/nltk_data') to every script, NLTK accepts NLTK_DATA environment variable. (code link)

Open ~/.bashrc (or ~/.profile) with text editor (e.g. nano, vim, gedit), and add following line:

export NLTK_DATA="your/path/to/nltk_data"

Execute source to load environmental variable

source ~/.bashrc

Test

Open python and execute following lines

import nltk
nltk.data.path

Your can see your nltk data path already in there.

Reference: @alvations's answer on
nltk/nltk #1997

输什么也不输骨气 2024-09-22 02:08:53

使用 fnjn 上面关于打印路径的建议:

print(nltk.data.path)

我在 Windows 上看到了这种格式的路径字符串:

C:\\Users\\my_user_name\\AppData\\Roaming\\SPB_Data

所以当我使用路径时,我将路径从 python 类型正斜杠“/”切换为双反斜杠“\\” .append:

nltk.data.path.append("C:\\workspace\\my_project\\data\\nltk_books")

异常消失了。

Using fnjn's advice above on printing out the path:

print(nltk.data.path)

I saw the path strings in this type of format on windows:

C:\\Users\\my_user_name\\AppData\\Roaming\\SPB_Data

So I switched my path from the python type forward slash '/', to a double backslash '\\' when I used path.append:

nltk.data.path.append("C:\\workspace\\my_project\\data\\nltk_books")

The exception went away.

独夜无伴 2024-09-22 02:08:53

对于那些使用 uwsgi 的人:

我遇到了麻烦,因为我想要一个 uwsgi 应用程序(以与我不同的用户身份运行)来访问我之前下载的 nltk 数据。对我有用的是将以下行添加到 myapp_uwsgi.ini

env = NLTK_DATA=/home/myuser/nltk_data/

这会设置环境变量 NLTK_DATA,如 @schemacs 所建议的。
进行此更改后,您可能需要重新启动 uwsgi 进程。

For those using uwsgi:

I was having trouble because I wanted a uwsgi app (running as a different user than myself) to have access to nltk data that I had previously downloaded. What worked for me was adding the following line to myapp_uwsgi.ini:

env = NLTK_DATA=/home/myuser/nltk_data/

This sets the environment variable NLTK_DATA, as suggested by @schemacs.
You may need to restart your uwsgi process after making this change.

橘味果▽酱 2024-09-22 02:08:53

另一个解决方案是抢先一步。

尝试
导入nltk
nltk.download()

当弹出窗口询问是否要下载语料库时,可以指定下载到哪个目录。

Another solution is to get ahead of it.

try
import nltk
nltk.download()

When the window box pops up asking if you want to download the corpus , you can specify there which directory it is to be downloaded to.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文