Python NLTK pos_tag throws URLError
Asked Answered
H

1

7

I was trying to use the pos_tag function in NLTK 3 (on Windows), but this error popped up:

>>> import nltk
>>> tokens = nltk.word_tokenize("This is a sentence!")
>>> tokens
['This', 'is', 'a', 'sentence', '!']
>>> tags = nltk.pos_tag(tokens)
Traceback (most recent call last):
  File "<pyshell#24>", line 1, in <module>
    tags = nltk.pos_tag(tokens)
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\site-packages\nltk\tag\__init__.py", line 110, in pos_tag
    tagger = PerceptronTagger()
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\site-packages\nltk\tag\perceptron.py", line 141, in __init__
    self.load(AP_MODEL_LOC)
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\site-packages\nltk\tag\perceptron.py", line 209, in load
    self.model.weights, self.tagdict, self.classes = load(loc)
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\site-packages\nltk\data.py", line 801, in load
    opened_resource = _open(resource_url)
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\site-packages\nltk\data.py", line 924, in _open
    return urlopen(resource_url)
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 162, in urlopen
    return opener.open(url, data, timeout)
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 465, in open
    response = self._open(req, data)
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 488, in _open
    'unknown_open', req)
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 443, in _call_chain
    result = func(*args)
  File "C:\Users\Gebruiker\AppData\Local\Programs\Python\Python35-32\lib\urllib\request.py", line 1310, in unknown_open
    raise URLError('unknown url type: %s' % type)
urllib.error.URLError: <urlopen error unknown url type: c> 

All the packages are installed succesfully (including the maxent_treebank_pos_tagger) and I also have Numpy installed.

Am I doing something wrong?

Humpbacked answered 6/3, 2016 at 14:8 Comment(11)
try pip install -U nltk; python -m nltk.downloader averaged_perceptron_taggerNotional
I tried that, but I still received the error.Humpbacked
What is the error after that? Did you install NLTK with the .exe?Notional
It is the URLError again and yes I did install it with the windows exeHumpbacked
IMHO, easiest and best way to install is to Install it using powershell and pip, use conda.Notional
See gist.github.com/alvations/0ed8641d7d2e1941b9f9Notional
Use pip install -U https://github.com/nltk/nltk/archive/develop.zipNotional
ok, so I tried to reinstall Python and I built NLTK from source but I'm still receiving the same URLError.Humpbacked
How did you reinstall Python? Did you use pip? What do you mean by built from source?Notional
I ran python setup.py install from a windows command prompt.Humpbacked
Did you use github.com/nltk/nltk/archive/develop.zip ?Notional
B
11

EDITED

This issue has been resolved from NLTK v3.2.1. Upgrading your NLTK would resolve the issue, e.g. pip install -U nltk.


You might be using nltk verion 3.2 . Downgrade it to version 3.1 and it will work fine. I myself used the undermentioned method right now and the URL error is gone. Seems like an issue with nltk_version-3.2

Browse to this directory on your computer

C:\Users\USERNAME\AppData\Local\Continuum\Anaconda2\Lib\site-packages
         OR
C:\python2.7\site-packages\

Basically the aim is to go into site-packages directory, which holds the installed packages.

Search and delete these files and directories :

nltk-3.1.dist-info
nltk

After deleting force install older version as :-

pip install nltk==3.1
Bonnette answered 7/3, 2016 at 18:59 Comment(4)
Thanks! It worked perfectly! But I had to use py -m pip install nltk==3.1. Or did you mean that?Humpbacked
:-) whatever work for you (you might be on soem other distro I guess , as Im on Windows) Thanks for accepting the answer . Cheers!!!!Poteat
See also: #35837407Notional
Sorry for adding the edit to your answer, this is to avoid cross-platform communication and NLTK users starting new issues on the github repo on this resolved issue.Notional

© 2022 - 2024 — McMap. All rights reserved.