2010-08-19 7 views

Respuesta

52

Simplemente cambie los artículos de nltk.data.path, es una lista simple.

+25

o establecer la variable de entorno NLTK_DATA. – schemacs

+0

Mi nltk.data.path tiene ''/ home/aankney/nltk_data'' como el primer elemento de la lista, PERO estoy en un servidor y quiero que' nltk_data' sea compartido por otras personas que usan el servidor. ¿Cómo evito que nltk use esto como una de las rutas de descarga? –

15

utilizo añadir, ejemplo

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

A partir del código, 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``. 

A continuación, dentro del código:

###################################################################### 
# 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') 
    ] 

que modificar la ruta, simplemente appen d para la lista de posibles caminos:

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

o en Windows:

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

¿Qué directorio contendría este archivo? – hlin117

+0

está en el código fuente original de NLTK. Vaya al directorio donde guarda el código fuente y luego vaya a 'nltk/nltk/data' – alvas

+0

eche un vistazo a' magically_find_nltk_data() 'desde http://stackoverflow.com/questions/36382937/nltk-doesnt-add-nltk -data-to-search-path/36383314 # 36383314 – alvas

1

Para aquellos que utilizan uwsgi:

que estaba teniendo problemas porque quería una aplicación uwsgi (que se ejecuta como un diferente usuario que yo) para tener acceso a los datos nltk que había descargado previamente. Lo que funcionó para mí fue añadiendo la siguiente línea a myapp_uwsgi.ini:

env = NLTK_DATA=/home/myuser/nltk_data/ 

Esto establece la variable de entorno NLTK_DATA, según lo sugerido por @schemacs.
Es posible que deba reiniciar su proceso uwsgi después de realizar este cambio.

Cuestiones relacionadas