2016-05-08 14 views
7

Tôi sẽ sử dụng nltk.tokenize.word_tokenize trên một cụm nơi tài khoản của tôi bị giới hạn bởi dung lượng bộ nhớ. Ở nhà, tôi đã tải xuống tất cả các tài nguyên nltk bởi nltk.download() nhưng, như tôi đã phát hiện ra, phải mất ~ 2.5GB.Điều gì cần tải xuống để thực hiện công việc nltk.tokenize.word_tokenize?

Điều này có vẻ hơi quá mức đối với tôi. Bạn có thể đề xuất phụ thuộc tối thiểu (hoặc gần như tối thiểu) cho nltk.tokenize.word_tokenize không? Cho đến nay, tôi đã thấy nltk.download('punkt') nhưng tôi không chắc liệu nó có đủ hay không và kích cỡ là bao nhiêu. Chính xác thì tôi nên chạy cái gì để nó hoạt động?

+0

Hơi không liên quan, nhưng bạn có thể muốn [xem spaCy] (https://spacy.io) thay thế cho NLTK. – ChrisP

Trả lời

11

Bạn nói đúng. Bạn cần các mô hình Tokenizer Punkt. Nó có 13 MB và nltk.download('punkt') nên làm các trick.

+0

Ngoài ra, nếu bạn chạy 'nltk.download()', NLTK Downloader sẽ mở (ứng dụng GUI), vì vậy bạn có thể duyệt tất cả các gói. –

+4

hoặc sử dụng thiết bị đầu cuối: 'python -m nltk.downloader 'punkt''. Cũng lưu ý rằng 13 MB là tập tin nén, điều cuối cùng là ~ 36 MB. – patrick

4

Nói tóm lại:

nltk.download('punkt') 

sẽ đủ.


Trong dài:

Bạn làm nhu cầu không cần thiết phải tải về tất cả các mô hình và corpora sẵn trong NLTK nếu bạn chỉ sẽ sử dụng NLTK cho tokenization.

Thực ra, nếu bạn chỉ sử dụng word_tokenize(), thì bạn sẽ không thực sự cần bất kỳ tài nguyên nào từ nltk.download(). Nếu chúng ta nhìn vào đoạn code, mặc định word_tokenize() rằng về cơ bản là TreebankWordTokenizer không nên sử dụng bất kỳ nguồn lực bổ sung:

[email protected]:~$ ls nltk_data/ 
chunkers corpora grammars help models stemmers taggers tokenizers 
[email protected]:~$ mv nltk_data/ tmp_move_nltk_data/ 
[email protected]:~$ python 
Python 2.7.11+ (default, Apr 17 2016, 14:00:29) 
[GCC 5.3.1 20160413] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from nltk import word_tokenize 
>>> from nltk.tokenize import TreebankWordTokenizer 
>>> tokenizer = TreebankWordTokenizer() 
>>> tokenizer.tokenize('This is a sentence.') 
['This', 'is', 'a', 'sentence', '.'] 

Nhưng:

[email protected]:~$ ls nltk_data/ 
chunkers corpora grammars help models stemmers taggers tokenizers 
[email protected]:~$ mv nltk_data/ tmp_move_nltk_data 
[email protected]:~$ python 
Python 2.7.11+ (default, Apr 17 2016, 14:00:29) 
[GCC 5.3.1 20160413] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> from nltk import sent_tokenize 
>>> sent_tokenize('This is a sentence. This is another.') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/nltk/tokenize/__init__.py", line 90, in sent_tokenize 
    tokenizer = load('tokenizers/punkt/{0}.pickle'.format(language)) 
    File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 801, in load 
    opened_resource = _open(resource_url) 
    File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 919, in _open 
    return find(path_, path + ['']).open() 
    File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 641, in find 
    raise LookupError(resource_not_found) 
LookupError: 
********************************************************************** 
    Resource u'tokenizers/punkt/english.pickle' not found. Please 
    use the NLTK Downloader to obtain the resource: >>> 
    nltk.download() 
    Searched in: 
    - '/home/alvas/nltk_data' 
    - '/usr/share/nltk_data' 
    - '/usr/local/share/nltk_data' 
    - '/usr/lib/nltk_data' 
    - '/usr/local/lib/nltk_data' 
    - u'' 
********************************************************************** 

>>> from nltk import word_tokenize 
>>> word_tokenize('This is a sentence.') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/usr/local/lib/python2.7/dist-packages/nltk/tokenize/__init__.py", line 106, in word_tokenize 
    return [token for sent in sent_tokenize(text, language) 
    File "/usr/local/lib/python2.7/dist-packages/nltk/tokenize/__init__.py", line 90, in sent_tokenize 
    tokenizer = load('tokenizers/punkt/{0}.pickle'.format(language)) 
    File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 801, in load 
    opened_resource = _open(resource_url) 
    File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 919, in _open 
    return find(path_, path + ['']).open() 
    File "/usr/local/lib/python2.7/dist-packages/nltk/data.py", line 641, in find 
    raise LookupError(resource_not_found) 
LookupError: 
********************************************************************** 
    Resource u'tokenizers/punkt/english.pickle' not found. Please 
    use the NLTK Downloader to obtain the resource: >>> 
    nltk.download() 
    Searched in: 
    - '/home/alvas/nltk_data' 
    - '/usr/share/nltk_data' 
    - '/usr/local/share/nltk_data' 
    - '/usr/lib/nltk_data' 
    - '/usr/local/lib/nltk_data' 
    - u'' 
********************************************************************** 

Nhưng có vẻ như đó không phải là trường hợp, nếu chúng ta xem https://github.com/nltk/nltk/blob/develop/nltk/tokenize/init.py#L93. Có vẻ như word_tokenize được gọi ngầm là sent_tokenize() yêu cầu mô hình punkt.

Tôi không chắc chắn cho dù đây là một lỗi hoặc một tính năng nhưng nó có vẻ như thành ngữ cũ thể là lỗi thời cho các mã hiện tại:

>>> from nltk import sent_tokenize, word_tokenize 
>>> sentences = 'This is a foo bar sentence. This is another sentence.' 
>>> tokenized_sents = [word_tokenize(sent) for sent in sent_tokenize(sentences)] 
>>> tokenized_sents 
[['This', 'is', 'a', 'foo', 'bar', 'sentence', '.'], ['This', 'is', 'another', 'sentence', '.']] 

Nó có thể chỉ đơn giản là:

>>> word_tokenize(sentences) 
['This', 'is', 'a', 'foo', 'bar', 'sentence', '.', 'This', 'is', 'another', 'sentence', '.'] 

Nhưng chúng tôi thấy rằng word_tokenize() làm phẳng danh sách chuỗi danh sách thành một danh sách chuỗi duy nhất.


Ngoài ra, bạn có thể thử sử dụng một tokenizer mới sẽ được thêm vào NLTK toktok.py dựa trên https://github.com/jonsafari/tok-tok mà không đòi hỏi mô hình pre-đào tạo.

Các vấn đề liên quan