2016-05-08 37 views
7

Ho intenzione di utilizzare nltk.tokenize.word_tokenize in un cluster in cui il mio account è molto limitato dalla quota dello spazio. A casa, ho scaricato tutte le risorse nltk entro il nltk.download() ma, come ho scoperto, occorrono circa 2,5 GB.Cosa scaricare per rendere nltk.tokenize.word_tokenize funzionante?

Questo sembra un po 'eccessivo per me. Potresti suggerire quali sono le dipendenze minime (o quasi minime) per nltk.tokenize.word_tokenize? Finora, ho visto nltk.download('punkt') ma non sono sicuro se sia sufficiente e quale sia la dimensione. Che cosa dovrei esattamente correre per farlo funzionare?

+0

Leggermente non correlato, ma è possibile che si desideri [controllare spaCy] (https://spacy.io) in alternativa a NLTK. – ChrisP

risposta

11

Hai ragione. Hai bisogno di modelli Punkt Tokenizer. Ha 13 MB e nltk.download('punkt') dovrebbe fare il trucco.

+0

Inoltre, se si esegue 'nltk.download()', è necessario aprire NLTK Downloader (un'applicazione GUI), in modo da poter sfogliare tutti i pacchetti. –

+4

o utilizzare il terminale: 'python -m nltk.downloader 'punkt''. Si noti inoltre che il 13 MB è il file zippato, l'ultima cosa è ~ 36 MB. – patrick

4

In breve:

nltk.download('punkt') 

sarebbe sufficiente.


a Long:

che fai non è necessario bisogno di scaricare tutti i modelli e corpora disponibili in NLTK se si sta solo andando a utilizzare NLTK per tokenizzazione.

In realtà, se si utilizza solo lo standard word_tokenize(), non è necessario disporre di alcuna delle risorse da nltk.download(). Se guardiamo il codice, il valore predefinito word_tokenize() che è fondamentalmente la TreebankWordTokenizer non dovrebbero utilizzare le risorse aggiuntive:

[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', '.'] 

Ma:

[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'' 
********************************************************************** 

Ma sembra che non è il caso, se noi guarda https://github.com/nltk/nltk/blob/develop/nltk/tokenize/init.py#L93. Sembra che word_tokenize abbia implicitamente chiamato sent_tokenize() che richiede il modello punkt.

Non sono sicuro se questo è un bug o di una funzione, ma sembra che il vecchio linguaggio possono essere superate dato il codice corrente:

>>> 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', '.']] 

Può essere semplicemente:

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

Ma vediamo che lo word_tokenize() appiattisce la lista di stringhe in un unico elenco di stringhe.


In alternativa, si può provare a utilizzare un nuovo tokenizzatore che verrà aggiunto al NLTK toktok.py sulla base di https://github.com/jonsafari/tok-tok che non necessita di modelli pre-addestrato.