Python NLTK pos_tag抛出URLError

时间:2016-03-06 14:08:33

标签: python-3.x nltk

我试图在NLTK 3中使用pos_tag函数(在Windows上),但是这个错误突然出现了:

>>> 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> 

所有软件包都已成功安装(包括maxent_treebank_pos_tagger),我也安装了Numpy。

我做错了吗?

1 个答案:

答案 0 :(得分:11)

EDITED

此问题已从NLTK v3.2.1解决。升级您的NLTK可以解决问题,例如pip install -U nltk

您可能正在使用nltk verion 3.2。将其降级到版本3.1,它将正常工作。我自己现在使用下面提到的方法,URL错误消失了。似乎是nltk_version-3.2的问题

浏览计算机上的此目录

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

基本上,目标是进入site-packages目录,该目录包含已安装的软件包。

搜索并删除这些文件和目录:

nltk-3.1.dist-info
nltk

删除强制安装旧版本后: -

pip install nltk==3.1