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.
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