【发布时间】:2017-01-30 00:47:05
【问题描述】:
我想尝试在 Python 3.5 的 nltk 包中使用 PerceptronTagger,但我收到错误 TypeError: 'LazySubsequence' object does not support item assignment
我想用来自带有universal 标签集的棕色语料库的数据来训练它。
这是我遇到问题时正在运行的代码。
import nltk,math
tagged_sentences = nltk.corpus.brown.tagged_sents(categories='news',tagset='universal')
i = math.floor(len(tagged_sentences)*0.2)
testing_sentences = tagged_sentences[0:i]
training_sentences = tagged_sentences[i:]
perceptron_tagger = nltk.tag.perceptron.PerceptronTagger(load=False)
perceptron_tagger.train(training_sentences)
它不会正确训练,并给出以下堆栈跟踪。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-10-61332d63d2c3> in <module>()
1 perceptron_tagger = nltk.tag.perceptron.PerceptronTagger(load=False)
----> 2 perceptron_tagger.train(training_sentences)
/home/nathan/anaconda3/lib/python3.5/site-packages/nltk/tag/perceptron.py in train(self, sentences, save_loc, nr_iter)
192 c += guess == tags[i]
193 n += 1
--> 194 random.shuffle(sentences)
195 logging.info("Iter {0}: {1}/{2}={3}".format(iter_, c, n, _pc(c, n)))
196 self.model.average_weights()
/home/nathan/anaconda3/lib/python3.5/random.py in shuffle(self, x, random)
270 # pick an element in x[:i+1] with which to exchange x[i]
271 j = randbelow(i+1)
--> 272 x[i], x[j] = x[j], x[i]
273 else:
274 _int = int
TypeError: 'LazySubsequence' object does not support item assignment
它似乎来自random 模块中的shuffle 函数,但这似乎并不正确。
还有其他可能导致问题的原因吗? 有人遇到过这个问题吗?
我在 Ubuntu 16.04.1 上使用 Anaconda Python 3.5 运行它。 nltk 版本是3.2.1
【问题讨论】:
标签: classification nltk anaconda python-3.5 perceptron