0

IndexError: list index out of range当调用tag_sents()NLTK SennaTagger( http://www.nltk.org/_modules/nltk/tag/senna.html ) 的方法时。

给出一个句子列表作为tag_sents方法的输入。

运行标记器需要一个 senna 可执行文件。可在此处找到 SENNA 工具包的安装指南。http://ronan.collobert.com/senna/

代码:

from nltk.tag import SennaTagger

SENNA_EXECUTABLE_DIR = '../../tools/senna'

pos_tagger = SennaTagger(SENNA_EXECUTABLE_DIR)

tagged = pos_tagger.tag_sents(["All the banks are closed", "Today is Sunday"])

输出:

Traceback (most recent call last):

  File "<ipython-input-90-886051c3d91d>", line 1, in <module>
    tagged = pos_tagger.tag_sents(["All the banks are closed", "Today is Sunday"])

  File "F:\Programs\Anaconda3\lib\site-packages\nltk\tag\senna.py", line 55, in tag_sents
    tagged_sents = super(SennaTagger, self).tag_sents(sentences)

  File "F:\Programs\Anaconda3\lib\site-packages\nltk\classify\senna.py", line 161, in tag_sents
    result[tag] = tags[map_[tag]].strip()

IndexError: list index out of rangeenter code here
4

1 回答 1

1

senna.tag_sents 的输入是字符串列表,可以通过[word_tokenize(sent) for sent in sents]

>>> from nltk import word_tokenize
>>> from nltk.tag import SennaTagger
>>> senna = SennaTagger('/home/alvas/senna/')
>>> sents = ["All the banks are closed", "Today is Sunday"]

>>> tokenized_sents = [word_tokenize(sent) for sent in sents]
>>> senna.tag_sents(tokenized_sents)
[[('All', u'PDT'), ('the', u'DT'), ('banks', u'NNS'), ('are', u'VBP'), ('closed', u'VBN')], [('Today', u'NN'), ('is', u'VBZ'), ('Sunday', u'NNP')]]

或者map如果您不想tokenized_sents在标记之前实现,请使用:

>>> tokenized_sents = map(word_tokenize, sents)
>>> senna.tag_sents(tokenized_sents)
[[('All', u'PDT'), ('the', u'DT'), ('banks', u'NNS'), ('are', u'VBP'), ('closed', u'VBN')], [('Today', u'NN'), ('is', u'VBZ'), ('Sunday', u'NNP')]]
于 2017-05-14T05:09:37.800 回答