2010-08-19 4 views

Trả lời

52

Chỉ cần thay đổi các mục của nltk.data.path, đây là danh sách đơn giản.

+25

hoặc đặt biến môi trường NLTK_DATA. – schemacs

+0

My nltk.data.path có ''/ home/aankney/nltk_data'' là phần tử đầu tiên của danh sách NHƯNG Tôi đang ở trên máy chủ và muốn' nltk_data' được chia sẻ bởi những người khác sử dụng máy chủ. Làm cách nào để ngăn không cho nltk sử dụng điều này làm một trong các đường dẫn tải xuống? –

15

tôi sử dụng thêm, ví dụ

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

Từ mã, 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``. 

Sau đó trong mã:

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

Để thay đổi con đường, chỉ đơn giản là appen d vào danh sách các đường dẫn có thể:

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

Hoặc trong cửa sổ:

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

Thư mục nào sẽ chứa tệp này? – hlin117

+0

là mã nguồn gốc của NLTK. Chuyển đến thư mục mà bạn lưu mã nguồn, sau đó đi tới 'nltk/nltk/data' – alvas

+0

xem 'magically_find_nltk_data()' từ http://stackoverflow.com/questions/36382937/nltk-doesnt-add-nltk -data-to-search-path/36383314 # 36383314 – alvas

1

Đối với những người sử dụng uwsgi:

tôi đã gặp khó khăn bởi vì tôi muốn có một ứng dụng uwsgi (chạy như một khác nhau người dùng hơn bản thân mình) để có quyền truy cập vào dữ liệu nltk mà tôi đã tải xuống trước đó. Có gì làm việc cho tôi là thêm dòng sau vào myapp_uwsgi.ini:

env = NLTK_DATA=/home/myuser/nltk_data/ 

này thiết lập các biến môi trường NLTK_DATA, theo đề nghị của @schemacs.
Bạn có thể cần phải khởi động lại quá trình uwsgi của mình sau khi thực hiện thay đổi này.