Python NLTK pos_tag引发URLError [英] Python NLTK pos_tag throws URLError
问题描述
我试图在NLTK 3(在Windows上)中使用pos_tag
函数,但此错误弹出:
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>
所有软件包都已成功安装(包括maxent_treebank_pos_tagger),并且我还安装了Numpy.
All the packages are installed succesfully (including the maxent_treebank_pos_tagger) and I also have Numpy installed.
我做错什么了吗?
推荐答案
已编辑
NLTK v3.2.1已解决此问题.升级您的NLTK可以解决此问题,例如pip install -U nltk
.
您可能正在使用nltk verion 3.2.将其降级到3.1版,它将可以正常工作.我本人现在使用下面提到的方法,URL错误消失了.似乎是nltk_version-3.2的问题
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\
主要目的是进入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
这篇关于Python NLTK pos_tag引发URLError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!