100

How to config nltk data directory from code?

alvas
  • 115,346
  • 109
  • 446
  • 738
Juanjo Conti
  • 28,823
  • 42
  • 111
  • 133
  • Related: [Change nltk.download() path directory from default ~/ntlk_data](https://stackoverflow.com/questions/44857382/change-nltk-download-path-directory-from-default-ntlk-data) – smci Nov 02 '17 at 18:04

7 Answers7

83

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

Tim McNamara
  • 18,019
  • 4
  • 52
  • 83
  • 35
    or set NLTK_DATA environment variable. – schemacs Oct 16 '13 at 14:34
  • 1
    My nltk.data.path has `'/home/aankney/nltk_data'` as the first element of the list BUT I'm on a server and want `nltk_data` to be shared by other people using the server. How do I prevent nltk from using this as one of the download paths? – Austin A Jun 17 '15 at 16:09
54

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")
alvas
  • 115,346
  • 109
  • 446
  • 738
  • What directory would contain this file? – hlin117 Aug 10 '14 at 01:28
  • it's in the original source code of NLTK. Go to the directory where you save the source code then go to `nltk/nltk/data` – alvas Aug 10 '14 at 06:06
  • take a look at `magically_find_nltk_data()` from http://stackoverflow.com/questions/36382937/nltk-doesnt-add-nltk-data-to-search-path/36383314#36383314 – alvas Apr 03 '16 at 21:56
32

I use append, example

nltk.data.path.append('/libs/nltk_data/')
Tushar Gupta - curioustushar
  • 58,085
  • 24
  • 103
  • 107
bahlum
  • 421
  • 4
  • 5
21

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

fnjn
  • 1,201
  • 12
  • 9
1

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.

danyamachine
  • 1,848
  • 1
  • 18
  • 21
1

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.

0

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.

Steve
  • 4,388
  • 3
  • 17
  • 25