2016-03-07 79 views
4

喜文本挖掘冠軍,NLTK V3.2:無法nltk.pos_tag()

我在Windows 10(客戶端環境)使用蟒蛇與NLTK V3.2

當我嘗試POS標記,我不斷收到一個URLLIB2錯誤:

URLError: <urlopen error unknown url type: c> 

看來urllib2無法識別Windows路徑?我該如何解決這個問題?

的命令也很簡單:

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

編輯: 有一個重複的問題,但是我認爲馬南這裏獲得的答案和alvas是更好的修復。

+0

的可能的複製[Python的NLTK POS \ _tag拋出URLError](http://stackoverflow.com/questions/35827859/python-nltk-pos-tag-throws-urlerror) – alvas

+0

看起來像耶。我之前讀過那篇文章。 – Max

回答

10

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庫在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命令。我相信這也可以解決您的問題(假設您已經設置了其他所有設置)。

+0

像夢一樣工作。謝謝@MananVyas – Max

+0

FWIW我在Win10 python 3.4(64Bit)上使用nltk時通過pip安裝了相同的錯誤,並且截至4月2日爲止。找到percepthon.py文件並在上面的代碼片段中進行更改後,重新啓動後才能正常工作。希望我在4個小時前看過這篇文章,但因爲我認爲這是我的代幣問題 – mobcdi

+0

對不起,將編輯添加到您的答案,這是爲了避免跨平臺溝通和NLTK用戶開始在github回購上的新問題這個解決的問題。 – alvas

6

EDITED

這個問題已經從NLTK V3.2.1解決。請升級您的NLTK!


首先閱讀@MananVyas回答的原因:

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


這裏是怎麼了,不降級到NLTK V3.1,使用NLTK 3.2,你可以用這個「黑客「:

>>> 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')] 
+0

我運行了上面的代碼,它運行良好,但是當我嘗試運行我的nltk例程時,它仍然給出<< raise URLError('unknown url type:%s'%type)>>我使用的代碼是在http:/ /stackoverflow.com/questions/36255291/extract-city-names-from-text-using-python/36255377?noredirect=1#comment60196241_36255377我也成功運行了Sarim Hussain的建議,但沒有運氣。 – GeorgeC

+0

嘗試升級你的nltk,'pip install -U nltk' – alvas

+0

剛試過。同樣的錯誤。在pip命令中,我得到<< 將dependency_links寫入nltk.egg-info \ dependency_links.txt 警告:manifest_maker:標準文件'-c'未找到 閱讀清單模板'MANIFEST.in' 警告:沒有找到匹配的文件' Makefile'在目錄'* .txt'下 警告:在分發的任何地方找不到與'*〜'匹配的以前包含的文件 編寫清單文件'nltk.egg-info \ SOURCES.txt' 成功安裝nltk-3.2 >> – GeorgeC

1

我以前遇到過同樣的問題。 解決方案:

nltk.download('averaged_perceptron_tagger')