2

I have been trying to use nltk.pos_tag in my code but I face an error when I do so. I have already downloaded Penn treebank and max_ent_treebank_pos. But the error persists. here is my code :

import nltk
from nltk import tag
from nltk import*



a = "Alan Shearer is the first player to score over a hundred Premier League goals."
a_sentences = nltk.sent_tokenize(a)
a_words     = [nltk.word_tokenize(sentence) for sentence in a_sentences]
a_pos       = [nltk.pos_tag(sentence) for sentence in a_words]
print(a_pos)

and this is the error I get :

"Traceback (most recent call last):
  File "<pyshell#9>", line 1, in <module>
  print (nltk.pos_tag(text))
  File "C:\Python34\lib\site-packages\nltk\tag\__init__.py", line 110, in  pos_tag
tagger = PerceptronTagger()
File "C:\Python34\lib\site-packages\nltk\tag\perceptron.py", line 140, in    __init__
AP_MODEL_LOC = 'file:'+str(find('taggers/averaged_perceptron_tagger/'+PICKLE))
File "C:\Python34\lib\site-packages\nltk\data.py", line 641, in find
raise LookupError(resource_not_found)
LookupError: 

 Resource 'taggers/averaged_perceptron_tagger/averaged_perceptron
 _tagger.pickle' not found.  Please use the NLTK Downloader to
 obtain the resource:  >>> nltk.download()
 Searched in:
   - 'C:\\Users\\T01142/nltk_data'
   - 'C:\\nltk_data'
   - 'D:\\nltk_data'
   - 'E:\\nltk_data'
   - 'C:\\Python34\\nltk_data'
   - 'C:\\Python34\\lib\\nltk_data'
   - 'C:\\Users\\T01142\\AppData\\Roaming\\nltk_data'
aqua
  • 41
  • 1
  • 6
  • Possible duplicate of [How to do POS tagging using the NLTK POS tagger in Python?](http://stackoverflow.com/questions/8590370/how-to-do-pos-tagging-using-the-nltk-pos-tagger-in-python) – alvas Jun 06 '16 at 06:55
  • See http://stackoverflow.com/a/37651321/610569 – alvas Jun 06 '16 at 07:02
  • 2
    Thanks! :) but I got by downloading 'averaged_perceptron_tagger' from nltk downloader – aqua Jun 06 '16 at 08:20

3 Answers3

4

Call this from python:

nltk.download('averaged_perceptron_tagger')

Abhishek
  • 3,337
  • 4
  • 32
  • 51
0

Had the same problem in a Flask server. nltk used a different path when in server config, so I recurred to adding nltk.data.path.append("/home/yourusername/whateverpath/") inside of the server code right before the pos_tag call

Note there is some replication of this question

How to config nltk data directory from code?

nltk doesn't add $NLTK_DATA to search path?

POS tagging with NLTK. Can't locate averaged_perceptron_tagger

Pythonic
  • 2,091
  • 3
  • 21
  • 34
0

To resolve this error run following command on python prompt:

import nltk
nltk.download('averaged_perceptron_tagger')
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81