Kurz:
nltk.download('punkt')
ausreichen würde.
In langen:
Sie müssen nicht notwendig Notwendigkeit, die Modelle und Corpora alle in NLTK zum Download, wenn Sie nur NLTK
für tokenization verwenden werden.
Eigentlich, wenn Sie nur word_tokenize()
verwenden, dann brauchen Sie wirklich keine der Ressourcen von nltk.download()
. Wenn wir den Code anschauen, die die Standard-word_tokenize()
ist im Grunde die TreebankWordTokenizer keine zusätzlichen Ressourcen verwenden sollte:
[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', '.']
Aber:
[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''
**********************************************************************
Aber es sieht aus wie das ist nicht der Fall, wenn wir Blick auf https://github.com/nltk/nltk/blob/develop/nltk/tokenize/init.py#L93. Es scheint wie word_tokenize
hat implizit sent_tokenize()
genannt, die das Modell punkt
erfordert.
Ich bin nicht sicher, ob das ein Bug oder ein Feature ist, aber es scheint wie das alte Idiom könnte den aktuellen Code gegeben veraltet sein:
>>> 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', '.']]
Es kann einfach sein:
>>> word_tokenize(sentences)
['This', 'is', 'a', 'foo', 'bar', 'sentence', '.', 'This', 'is', 'another', 'sentence', '.']
Aber wir sehen, dass die word_tokenize()
die Liste der Liste der Zeichenfolge zu einer einzigen Liste von Zeichenfolge abflacht.
Alternativ können Sie versuchen, eine neue tokenizer zu verwenden, die toktok.py
basierend auf https://github.com/jonsafari/tok-tok zu NLTK hinzugefügt werden, die keine vortrainiert Modelle erfordert.
Geringfügig nicht verwandt, aber Sie können [spycy] (https://spacy.io) als Alternative zu NLTK überprüfen. – ChrisP