什么是NLTK POS tagger要我下载?

时间:2011-12-21 13:14:02

标签: python nlp nltk pos-tagger

我刚刚开始使用词性标注器,我遇到了很多问题。

我开始使用以下内容进行POS标记:

import nltk
text=nltk.word_tokenize("We are going out.Just you and me.")

当我想要打印'text'时,会发生以下情况:

print nltk.pos_tag(text)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "F:\Python26\lib\site-packages\nltk\tag\__init__.py", line 63, in pos_tag
tagger = nltk.data.load(_POS_TAGGER)
File "F:\Python26\lib\site-packages\nltk\data.py", line 594, in load
resource_val = pickle.load(_open(resource_url))
File "F:\Python26\lib\site-packages\nltk\data.py", line 673, in _open
 return find(path).open()
 File "F:\Python26\lib\site-packages\nltk\data.py", line 455, in find
   raise LookupError(resource_not_found)`  
LookupError:
 Resource 'taggers/maxent_treebank_pos_tagger/english.pickle' not
 found.  Please use the NLTK Downloader to obtain the resource:

>>> nltk.download().

 Searched in:
    - 'C:\\Documents and Settings\\Administrator/nltk_data'
    - 'C:\\nltk_data'
    - 'D:\\nltk_data'
    - 'E:\\nltk_data'
    - 'F:\\Python26\\nltk_data'
    - 'F:\\Python26\\lib\\nltk_data'
    - 'C:\\Documents and Settings\\Administrator\\Application Data\\nltk_data'

我使用了nltk.download(),但它没有用。

6 个答案:

答案 0 :(得分:30)

在Python中键入nltk.download()时,会自动显示NLTK Downloader界面 单击Models并选择maxent_treebank_pos_。它会自动安装。

import nltk 
text=nltk.word_tokenize("We are going out.Just you and me.")
print nltk.pos_tag(text)
[('We', 'PRP'), ('are', 'VBP'), ('going', 'VBG'), ('out.Just', 'JJ'),
 ('you', 'PRP'), ('and', 'CC'), ('me', 'PRP'), ('.', '.')]

答案 1 :(得分:28)

从高于v3.2的NLTK版本开始,请使用:

>>> import nltk
>>> nltk.__version__
'3.2.1'
>>> nltk.download('averaged_perceptron_tagger')
[nltk_data] Downloading package averaged_perceptron_tagger to
[nltk_data]     /home/alvas/nltk_data...
[nltk_data]   Package averaged_perceptron_tagger is already up-to-date!
True

对于使用旧版MaxEnt模型的NLTK版本,即v3.1及更低版本,请使用:

>>> import nltk
>>> nltk.download('maxent_treebank_pos_tagger')
[nltk_data] Downloading package maxent_treebank_pos_tagger to
[nltk_data]     /home/alvas/nltk_data...
[nltk_data]   Package maxent_treebank_pos_tagger is already up-to-date!
True

有关默认pos_tag更改的详细信息,请参阅https://github.com/nltk/nltk/pull/1143

答案 2 :(得分:5)

从shell /终端,您可以使用:

python -m nltk.downloader maxent_treebank_pos_tagger

(可能需要在Linux上使用sudo)

它将安装maxent_treebank_pos_tagger(即NLTK中的标准树库POS标记器)并修复您的问题。

答案 3 :(得分:1)

import nltk
text = "Obama delivers his first speech."

sent  =  nltk.sent_tokenize(text)


loftags = []
for s in sent:
    d = nltk.word_tokenize(s)   

    print nltk.pos_tag(d)

结果:

  

akshayy @ ubuntu:〜/ summ $ python nn1.py [('Obama','NNP'),('deliver',   'NNS'),('他'','PRP $'),('first','JJ'),('speech','NN'),('。',   '')]

(我刚刚问过另一个使用此代码的问题)

答案 4 :(得分:1)

nltk.download()

单击Models并选择maxent_treebank_pos_。它会自动安装。

import nltk 
text=nltk.word_tokenize("We are going out.Just you and me.")
print nltk.pos_tag(text)
[('We', 'PRP'), ('are', 'VBP'), ('going', 'VBG'), ('out.Just', 'JJ'),
 ('you', 'PRP'), ('and', 'CC'), ('me', 'PRP'), ('.', '.')]

答案 5 :(得分:0)

如果nltk版本是3.4.5,请执行以下操作:

import nltk
nltk.download('averaged_perceptron_tagger')

要检查您的nltk版本,请执行以下操作:

print (nltk.__version__)