How to config nltk data directory from code?

2019-01-03 09:46发布

How to config nltk data directory from code?

5条回答
相关推荐>>
2楼-- · 2019-01-03 10:10

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")
查看更多
对你真心纯属浪费
3楼-- · 2019-01-03 10:15

I use append, example

nltk.data.path.append('/libs/nltk_data/')
查看更多
再贱就再见
4楼-- · 2019-01-03 10:15

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

查看更多
不美不萌又怎样
5楼-- · 2019-01-03 10:28

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.

查看更多
我命由我不由天
6楼-- · 2019-01-03 10:31

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

查看更多
登录 后发表回答