NLTK v3.2:无法nltk.pos_tag()

嗨文本挖掘冠军,

我在Windows 10上使用NLTK v3.2的Anaconda。(客户端环境)

当我尝试POS标记时,我不断收到一个URLLIB2错误:

URLError: <urlopen error unknown url type: c> 

看来urllib2无法识别Windowspath? 我该如何解决这个问题?

该命令很简单,如下所示:

nltk.pos_tag(nltk.word_tokenize("Hello World"))

编辑:有一个重复的问题,但我认为在这里获得的答案马南和阿尔瓦斯是一个更好的解决办法。

EDITED

这个问题已经从NLTK v3.2.1中解决了。 升级你的NLTK版本可以解决这个问题,例如pip install -U nltk


我面临同样的问题,遇到的错误如下:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\tag\__init__.py", line 110, in pos_tag tagger = PerceptronTagger() File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\tag\perceptron.py", line 141, in __init__ self.load(AP_MODEL_LOC) File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\tag\perceptron.py", line 209, in load self.model.weights, self.tagdict, self.classes = load(loc) File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\data.py", line 801, in load opened_resource = _open(resource_url) File "C:\Python27\lib\site-packages\nltk-3.2-py2.7.egg\nltk\data.py", line 924, in _open return urlopen(resource_url) File "C:\Python27\lib\urllib2.py", line 126, in urlopen return _opener.open(url, data, timeout) File "C:\Python27\lib\urllib2.py", line 391, in open response = self._open(req, data) File "C:\Python27\lib\urllib2.py", line 414, in _open 'unknown_open', req) File "C:\Python27\lib\urllib2.py", line 369, in _call_chain result = func(*args) File "C:\Python27\lib\urllib2.py", line 1206, in unknown_open raise URLError('unknown url type: %s' % type) urllib2.URLError: <urlopen error unknown url type: c> 

您提到的URLError是由于NLTK库for Windows中的perceptron.py文件中存在一个错误。 在我的机器上,文件在这个位置

 C:\Python27\Lib\site-packages\nltk-3.2-py2.7.egg\nltk\tag\perceptron.py 

(基本上看你在哪里有你的Python27文件夹中的等价位置)

该错误基本上是在代码中为您的机器中的averaged_perceptron_tagger找到相应的位置。 可以看一下data.py文件中提到的801和924行。

我认为NLTK开发者社区最近在代码中修正了这个bug。 看看这个提交给他们的代码在几天前。

https://github.com/nltk/nltk/commit/d3de14e58215beebdccc7b76c044109f6197d1d9#diff-26b258372e0d13c2543de8dbb1841252

改变的片段如下:

 self.tagdict = {} self.classes = set() if load: AP_MODEL_LOC = 'file:'+str(find('taggers/averaged_perceptron_tagger/'+PICKLE)) self.load(AP_MODEL_LOC) # Initially it was:AP_MODEL_LOC = str(find('taggers/averaged_perceptron_tagger/'+PICKLE)) def tag(self, tokens): 

更新文件到最近的提交为我工作,并能够使用nltk.pos_tag命令。 我相信这也可以解决你的问题(假设你已经设置了所有的东西)。

EDITED

这个问题已经从NLTK v3.2.1中解决了。 请升级您的NLTK!


首先阅读@MananVyas回答为什么:

https://stackoverflow.com/a/35902494/610569


下面是如何在不降级到NLTK v3.1的情况下使用NLTK 3.2,你可以使用这个“hack”:

 >>> from nltk.tag import PerceptronTagger >>> from nltk.data import find >>> PICKLE = "averaged_perceptron_tagger.pickle" >>> AP_MODEL_LOC = 'file:'+str(find('taggers/averaged_perceptron_tagger/'+PICKLE)) >>> tagger = PerceptronTagger(load=False) >>> tagger.load(AP_MODEL_LOC) >>> pos_tag = tagger.tag >>> pos_tag('The quick brown fox jumps over the lazy dog'.split()) [('The', 'DT'), ('quick', 'JJ'), ('brown', 'NN'), ('fox', 'NN'), ('jumps', 'VBZ'), ('over', 'IN'), ('the', 'DT'), ('lazy', 'JJ'), ('dog', 'NN')] 

我以前面临同样的问题。 解:

 nltk.download('averaged_perceptron_tagger')