如果您不想在运行脚本之前设置$NLTK_DATA,您可以在Python脚本中执行以下操作:
import nltk
nltk.path.append('/home/alvas/some_path/nltk_data/')
例如,让我们将
nltk_data
移动到非标准路径,NLTK不会自动找到它:
alvas@ubi:~$ ls nltk_data/
chunkers corpora grammars help misc models stemmers taggers tokenizers
alvas@ubi:~$ mkdir some_path
alvas@ubi:~$ mv nltk_data/ some_path/
alvas@ubi:~$ ls nltk_data/
ls: cannot access nltk_data/: No such file or directory
alvas@ubi:~$ ls some_path/nltk_data/
chunkers corpora grammars help misc models stemmers taggers tokenizers
现在,我们使用
nltk.path.append()
的方法:
alvas@ubi:~$ 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()
我们将其移回并查看是否有效:
alvas@ubi:~$ ls nltk_data
ls: cannot access nltk_data: No such file or directory
alvas@ubi:~$ mv some_path/nltk_data/ .
alvas@ubi:~$ 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')]
如果您真的非常想自动查找nltk_data,请使用以下方法:
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())
让我们称之为Python脚本,
test.py
:
alvas@ubi:~$ ls nltk_data/
chunkers corpora grammars help misc models stemmers taggers tokenizers
alvas@ubi:~$ 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')]
alvas@ubi:~$ mv nltk_data/ some_path/
alvas@ubi:~$ 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')]
nltk_data
目录是静态的,为什么需要自动查找路径? - alvasnltk_data
目录:'/home/user/nltk_data'、'/usr/share/nltk_data'、'/usr/local/share/nltk_data'、'/usr/lib/nltk_data'和'/usr/local/lib/nltk_data'。 - alvasnltk.path.append()
,就无需担心os.environ
中的数据目录问题。请查看更新的答案。 - alvasNLTK_DATA
路径添加到数据搜索路径中。问题必须由其他原因引起:第二个Python脚本没有继承环境?路径不正确(或可能是相对路径,仅在某些目录中有效)?谁知道呢。但解决方案(适用于任何遇到相同问题的人)是检查和修复变量设置。当nltk看到该变量时,它会自动处理。 - alexis