NLTK POS标注器要我下载什么?

32

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

我使用以下内容开始进行词性标注:

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(),但它没有起作用。


这样,我已经为您整理好了。请将此作为格式化未来问题的示例。 - Björn Pollex
谢谢,问题现在已经解决了。 - Pearl
10
如果您已经解决了问题,请随意发布解决方案并接受它,以便其他遇到相同问题的人可以找到它。 - Björn Pollex
7个回答

37

NLTK版本高于v3.2之后,请使用:

>>> 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


31
当你在Python中输入 nltk.download() 时,会自动显示NLTK下载器界面。
点击“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'), ('.', '.')]

17
如果你指定标记器名称nltk.download('maxent_treebank_pos_tagger'),你甚至可以直接在代码中下载它。请参阅此帖子https://dev59.com/sUzSa4cB1Zd3GeqPjQ8S#5208563。 - ForceMagic

5

从shell/终端中,您可以使用以下命令:

python -m nltk.downloader maxent_treebank_pos_tagger

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

这将安装 maxent_treebank_pos_tagger (即 NLTK 中的标准树库 POS 标记器)并解决您的问题。


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'), ('delivers', 'NNS'), ('his', 'PRP$'), ('first', 'JJ'), ('speech', 'NN'), ('.', '.')]

(我刚刚问了另一个问题,在那里使用了这个代码)


2
值得注意的是,这个解析是不正确的--词性标注器将“delivers”标记为复数名词... - simon

1
nltk.download()

点击“模型”,选择“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'), ('.', '.')]

0

我正在使用 Google Colab,当今的 NLTK 版本是 3.2.5

这是对我有效的方法。

import nltk
nltk.download('punkt')
nltk.download('averaged_perceptron_tagger')
from nltk.tokenize import word_tokenize

text = word_tokenize("Hello welcome to the world of to learn Categorizing and POS Tagging with NLTK and Python")
nltk.pos_tag(text)

0
如果nltk版本是3.4.5,则执行以下操作:
import nltk
nltk.download('averaged_perceptron_tagger')

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

print (nltk.__version__)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接