Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NLTK pos_tag module returns LookupError [duplicate]

enter image description here

enter image description here

The details are on the above. I run it on Jupiter notebook, and get the error message.

like image 797
xixixixi Avatar asked Sep 05 '25 03:09

xixixixi


1 Answers

TL;DR

On the terminal:

python -m nltk.downloader averaged_perceptron_tagger

or in Python

import nltk
nltk.download('averaged_perceptron_tagger')

In Long

Firstly, please update your NLTK version to version 3.2.5, on the command line (Use sudo if necessary):

pip install -U nltk

Now you can try using the pos_tag function again and you should see a more helpful error message:

>>> from nltk import pos_tag
>>> pos_tag(['foo', 'bar'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/nltk/tag/__init__.py", line 133, in pos_tag
    tagger = _get_tagger(lang)
  File "/usr/local/lib/python2.7/site-packages/nltk/tag/__init__.py", line 97, in _get_tagger
    tagger = PerceptronTagger()
  File "/usr/local/lib/python2.7/site-packages/nltk/tag/perceptron.py", line 140, in __init__
    AP_MODEL_LOC = 'file:'+str(find('taggers/averaged_perceptron_tagger/'+PICKLE))
  File "/usr/local/lib/python2.7/site-packages/nltk/data.py", line 673, in find
    raise LookupError(resource_not_found)
LookupError: 
**********************************************************************
  Resource averaged_perceptron_tagger not found.
  Please use the NLTK Downloader to obtain the resource:

  >>> import nltk
  >>> nltk.download('averaged_perceptron_tagger')

  Searched in:
    - '/Users/alvas/nltk_data'
    - '/usr/share/nltk_data'
    - '/usr/local/share/nltk_data'
    - '/usr/lib/nltk_data'
    - '/usr/local/lib/nltk_data'

**********************************************************************

Note that the punkt resource is used for word_tokenize() but the pos_tag() function requires the averaged_perceptron_tagger model.

So, on the terminal, do:

python -m nltk.downloader averaged_perceptron_tagger

or in Python

import nltk
nltk.download('averaged_perceptron_tagger')
like image 141
alvas Avatar answered Sep 10 '25 22:09

alvas