32

I just started using a part-of-speech tagger, and I am facing many problems.

I started POS tagging with the following:

import nltk
text=nltk.word_tokenize("We are going out.Just you and me.")

When I want to print 'text', the following happens:

print nltk.pos_tag(text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "F:\Python26\lib\site-packages\nltk\tag\__init__.py", line 63, in pos_tag
tagger = nltk.data.load(_POS_TAGGER)
File "F:\Python26\lib\site-packages\nltk\data.py", line 594, in load
resource_val = pickle.load(_open(resource_url))
File "F:\Python26\lib\site-packages\nltk\data.py", line 673, in _open
 return find(path).open()
 File "F:\Python26\lib\site-packages\nltk\data.py", line 455, in find
   raise LookupError(resource_not_found)`  
LookupError:
 Resource 'taggers/maxent_treebank_pos_tagger/english.pickle' not
 found.  Please use the NLTK Downloader to obtain the resource:

>>> nltk.download().

 Searched in:
    - 'C:\\Documents and Settings\\Administrator/nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - 'F:\\Python26\\nltk_data'
    - 'F:\\Python26\\lib\\nltk_data'
    - 'C:\\Documents and Settings\\Administrator\\Application Data\\nltk_data'

I used nltk.download() but it did not work.

alvas
  • 115,346
  • 109
  • 446
  • 738
Pearl
  • 759
  • 1
  • 6
  • 7

7 Answers7

37

From NLTK versions higher than v3.2, please use:

>>> import nltk
>>> nltk.__version__
'3.2.1'
>>> nltk.download('averaged_perceptron_tagger')
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     /home/alvas/nltk_data...
[nltk_data]   Package averaged_perceptron_tagger is already up-to-date!
True

For NLTK versions using the old MaxEnt model, i.e. v3.1 and below, please use:

>>> import nltk
>>> nltk.download('maxent_treebank_pos_tagger')
[nltk_data] Downloading package maxent_treebank_pos_tagger to
[nltk_data]     /home/alvas/nltk_data...
[nltk_data]   Package maxent_treebank_pos_tagger is already up-to-date!
True

For more details on the change in the default pos_tag, please see https://github.com/nltk/nltk/pull/1143

alvas
  • 115,346
  • 109
  • 446
  • 738
31

When you type nltk.download() in Python, an NLTK Downloader interface gets displayed automatically.
Click on Models and choose maxent_treebank_pos_. It gets installed automatically.

import nltk 
text=nltk.word_tokenize("We are going out.Just you and me.")
print nltk.pos_tag(text)
[('We', 'PRP'), ('are', 'VBP'), ('going', 'VBG'), ('out.Just', 'JJ'),
 ('you', 'PRP'), ('and', 'CC'), ('me', 'PRP'), ('.', '.')]
Jonathan Spooner
  • 7,682
  • 2
  • 34
  • 41
Pearl
  • 759
  • 1
  • 6
  • 7
  • 17
    Even more, you can download it directly in the code if you specify the tagger name `nltk.download('maxent_treebank_pos_tagger');`. See this post http://stackoverflow.com/a/5208563/62921 – ForceMagic Mar 27 '13 at 21:23
5

From the shell/terminal, you can use:

python -m nltk.downloader maxent_treebank_pos_tagger

(might need to be sudo on Linux)

It will install maxent_treebank_pos_tagger (i.e. the standard treebank POS tagger in NLTK) and fix your issue.

Franck Dernoncourt
  • 77,520
  • 72
  • 342
  • 501
1
import nltk
text = "Obama delivers his first speech."

sent  =  nltk.sent_tokenize(text)


loftags = []
for s in sent:
    d = nltk.word_tokenize(s)   

    print nltk.pos_tag(d)

Result :

akshayy@ubuntu:~/summ$ python nn1.py [('Obama', 'NNP'), ('delivers', 'NNS'), ('his', 'PRP$'), ('first', 'JJ'), ('speech', 'NN'), ('.', '.')]

( I just asked another question where used this code )

Raj
  • 22,346
  • 14
  • 99
  • 142
akshayb
  • 1,219
  • 2
  • 18
  • 44
  • 2
    It's worth noting that this parse is **incorrect** -- the POS tagger has marked "delivers" as a plural noun... – simon Jul 16 '15 at 22:15
1
nltk.download()

Click on Models and choose maxent_treebank_pos_. It gets installed automatically.

import nltk 
text=nltk.word_tokenize("We are going out.Just you and me.")
print nltk.pos_tag(text)
[('We', 'PRP'), ('are', 'VBP'), ('going', 'VBG'), ('out.Just', 'JJ'),
 ('you', 'PRP'), ('and', 'CC'), ('me', 'PRP'), ('.', '.')]
Supun Dharmarathne
  • 1,138
  • 1
  • 10
  • 19
0

If nltk version is 3.4.5, do the below:

import nltk
nltk.download('averaged_perceptron_tagger')

To check you nltk version, do the below:

print (nltk.__version__)
slfan
  • 8,950
  • 115
  • 65
  • 78
R Shriya
  • 43
  • 6
0

I am using Google Colab and the current version of NLTK is 3.2.5 as of today.

This is what worked for me.

import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
from nltk.tokenize import word_tokenize

text = word_tokenize("Hello welcome to the world of to learn Categorizing and POS Tagging with NLTK and Python")
nltk.pos_tag(text)
tarmas99
  • 359
  • 1
  • 11