Tim*_*ara 62

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

  • 或设置NLTK_DATA环境变量. (27认同)

alv*_*vas 39

从代码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``.
Run Code Online (Sandbox Code Playgroud)

然后在代码中:

######################################################################
# 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')
    ]
Run Code Online (Sandbox Code Playgroud)

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

import nltk
nltk.data.path.append("/home/yourusername/whateverpath/")
Run Code Online (Sandbox Code Playgroud)

或者在windows中:

import nltk
nltk.data.path.append("C:\somewhere\farfar\away\path")
Run Code Online (Sandbox Code Playgroud)


小智 24

我用append,例子

nltk.data.path.append('/libs/nltk_data/')
Run Code Online (Sandbox Code Playgroud)


fnj*_*njn 6

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

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

export NLTK_DATA="your/path/to/nltk_data"
Run Code Online (Sandbox Code Playgroud)

执行source加载环境变量

source ~/.bashrc
Run Code Online (Sandbox Code Playgroud)


测试

打开python并执行以下几行

import nltk
nltk.data.path
Run Code Online (Sandbox Code Playgroud)

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

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