Wenn Sie nicht die $ NLTK_DATA einstellen möchten, bevor Ihre Skripte ausgeführt wird, können Sie es innerhalb der Python-Skripte mit tun:
import nltk
nltk.path.append('/home/alvas/some_path/nltk_data/')
Z.B. lassen Sie uns die zu einem Nicht-Standard-Pfad der nltk_data
bewegen, dass NLTK wird es nicht automatisch finden: Jetzt
[email protected]:~$ ls nltk_data/
chunkers corpora grammars help misc models stemmers taggers tokenizers
[email protected]:~$ mkdir some_path
[email protected]:~$ mv nltk_data/ some_path/
[email protected]:~$ ls nltk_data/
ls: cannot access nltk_data/: No such file or directory
[email protected]:~$ ls some_path/nltk_data/
chunkers corpora grammars help misc models stemmers taggers tokenizers
, verwenden wir den nltk.path.append()
Hack:
[email protected]:~$ python
>>> import os
>>> import nltk
>>> nltk.path.append('/home/alvas/some_path/nltk_data/')
>>> nltk.pos_tag('this is a foo bar'.split())
[('this', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('foo', 'JJ'), ('bar', 'NN')]
>>> nltk.data
<module 'nltk.data' from '/usr/local/lib/python2.7/dist-packages/nltk/data.pyc'>
>>> nltk.data.path
['/home/alvas/some_path/nltk_data/', '/home/alvas/nltk_data', '/usr/share/nltk_data', '/usr/local/share/nltk_data', '/usr/lib/nltk_data', '/usr/local/lib/nltk_data']
>>> exit()
Sagen wir es zurückgehen und sehen, ob es funktioniert:
[email protected]:~$ ls nltk_data
ls: cannot access nltk_data: No such file or directory
[email protected]:~$ mv some_path/nltk_data/ .
[email protected]:~$ python
>>> import nltk
>>> nltk.data.path
['/home/alvas/nltk_data', '/usr/share/nltk_data', '/usr/local/share/nltk_data', '/usr/lib/nltk_data', '/usr/local/lib/nltk_data']
>>> nltk.pos_tag('this is a foo bar'.split())
[('this', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('foo', 'JJ'), ('bar', 'NN')]
Wenn Sie wirklich wirklich finden wollen nltk_data automagically, Verwendung so etwas wie:
import scandir
import os, sys
import time
import nltk
def find(name, path):
for root, dirs, files in scandir.walk(path):
if root.endswith(name):
return root
def find_nltk_data():
start = time.time()
path_to_nltk_data = find('nltk_data', '/')
print >> sys.stderr, 'Finding nltk_data took', time.time() - start
print >> sys.stderr, 'nltk_data at', path_to_nltk_data
with open('where_is_nltk_data.txt', 'w') as fout:
fout.write(path_to_nltk_data)
return path_to_nltk_data
def magically_find_nltk_data():
if os.path.exists('where_is_nltk_data.txt'):
with open('where_is_nltk_data.txt') as fin:
path_to_nltk_data = fin.read().strip()
if os.path.exists(path_to_nltk_data):
nltk.data.path.append(path_to_nltk_data)
else:
nltk.data.path.append(find_nltk_data())
else:
path_to_nltk_data = find_nltk_data()
nltk.data.path.append(path_to_nltk_data)
magically_find_nltk_data()
print nltk.pos_tag('this is a foo bar'.split())
Lassen Sie uns, dass die Python-Skript aufrufen, test.py
:
[email protected]:~$ ls nltk_data/
chunkers corpora grammars help misc models stemmers taggers tokenizers
[email protected]:~$ python test.py
Finding nltk_data took 4.27330780029
nltk_data at /home/alvas/nltk_data
[('this', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('foo', 'JJ'), ('bar', 'NN')]
[email protected]:~$ mv nltk_data/ some_path/
[email protected]:~$ python test.py
Finding nltk_data took 4.75850391388
nltk_data at /home/alvas/some_path/nltk_data
[('this', 'DT'), ('is', 'VBZ'), ('a', 'DT'), ('foo', 'JJ'), ('bar', 'NN')]
Da das 'nltk_data' Verzeichnis ist statisch, warum brauchen Sie den Pfad automatisch zu finden? – alvas
Standardmäßig findet NLTK automatisch das Verzeichnis 'nltk_data' in diesen Verzeichnissen. '/ Home/user/nltk_data' '/ usr/share/nltk_data', '/ usr/local/share/nltk_data', '/ usr/lib/nltk_data ','/usr/local/lib/nltk_data ' – alvas
Ich habe nltk.download() angegeben, um Daten nach $ NLTK_DATA herunterzuladen, wenn $ NLTK_DATA nicht zum Durchsuchen des Verzeichnisses hinzugefügt wird, scheint es, dass die heruntergeladenen Daten' t von Skripten verwendet werden (aber ein einfacher Befehl kann nicht verwendet werden). –