2010-08-19 9 views

risposta

52

Basta cambiare gli articoli di nltk.data.path, è un elenco semplice.

+25

o impostare la variabile di ambiente NLTK_DATA. – schemacs

+0

Il mio nltk.data.path ha ''/ home/aankney/nltk_data'' come il primo elemento della lista MA io sono su un server e voglio' nltk_data' essere condiviso da altre persone che usano il server. Come posso impedire a nltk di usarlo come uno dei percorsi di download? –

15

uso aggiungere, esempio

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

Dal codice, 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``. 

Poi nel codice:

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

Per modificare il percorso, semplicemente appen d per l'elenco dei possibili percorsi:

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

o in Windows:

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

Quale directory dovrebbe contenere questo file? – hlin117

+0

è nel codice sorgente originale di NLTK. Vai alla directory in cui salverai il codice sorgente, quindi vai a 'nltk/nltk/data' – alvas

+0

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

1

Per coloro che utilizzano uwsgi:

avevo problemi perché volevo un'applicazione uwsgi (in esecuzione come un diverso utente di me) per avere accesso ai dati nltk che avevo precedentemente scaricato. Che cosa ha funzionato per me era aggiunta la seguente riga al myapp_uwsgi.ini:

env = NLTK_DATA=/home/myuser/nltk_data/ 

Questo imposta la variabile d'ambiente NLTK_DATA, come suggerito da @schemacs.
Potrebbe essere necessario riavviare il processo uwsgi dopo aver apportato questa modifica.

Problemi correlati