NLTK v3.2:无法使用nltk.pos_tag() [英] NLTK v3.2: Unable to nltk.pos_tag()

查看:95
本文介绍了NLTK v3.2:无法使用nltk.pos_tag()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文本挖掘冠军,

我正在Windows 10(客户端环境)上将Anaconda与NLTK v3.2一起使用

I'm using Anaconda with NLTK v3.2 on Windows 10.(client's environment)

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

When I try to POS tag, I keep getting a URLLIB2 error:

URLError: <urlopen error unknown url type: c>

似乎urllib2无法识别Windows路径?我该如何解决?

It seems urllib2 is unable to recognize windows paths? How can I work around this?

命令很简单:

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

有一个重复的问题,但是我认为manan和alvas在这里获得的答案是更好的解决方法.

edit: There is a duplicate question, however I think the answers obtained here by manan and alvas are a better fix.

推荐答案

已编辑

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

我遇到了同样的问题,遇到的错误如下;

I faced the same issue and the error encountered was as follows;

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是由于Windows NLTK库中的perceptron.py文件中的错误所致. 在我的机器上,文件位于此位置

The URLError that you mentioned was due to a bug in the perceptron.py file within the NLTK library for Windows. In my machine, the file is at this location

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

(基本上在您拥有Python27文件夹的地方查看同一个位置)

(Basically look at an equivalent location within yours wherever you have the Python27 folder)

该错误基本上在代码中,该错误用于查找计算机中averaged_perceptron_tagger的对应位置.可以看看data.py文件中提到的第801和924行.

The bug was basically in the code to find the corresponding location for the averaged_perceptron_tagger within your machine. One can have a look at the line 801 and 924 mentioned in the data.py file regarding this.

我认为NLTK开发人员社区最近在代码中修复了此错误.几天前看一下对他们的代码的提交.

I think the NLTK developer community recently fixed this bug in the code. Have a look at this commit made to their code a few days back.

https://github.com/nltk/nltk1109d10911099 diff-26b258372e0d13c2543de8dbb1841252

进行更改的代码段如下;

The snippet where the change was made is as follows;

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命令.我相信这也可以解决您的问题(假设您已经设置了所有其他功能).

Updating the file to the most recent commit worked for me and was able to use the nltk.pos_tag command. I believe this would resolve your problem as well (assuming you have everything else set up).

这篇关于NLTK v3.2:无法使用nltk.pos_tag()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆