我想嘗試,並在nltk
包的Python 3.5使用PerceptronTagger
,但我得到的錯誤TypeError: 'LazySubsequence' object does not support item assignment
NLTK感知惡搞「類型錯誤:‘LazySubsequence’對象不支持項目分配」
我想用帶有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
看起來你在寫我的時候寫了一個答案。我得出了同樣的結論,所以我會將你的評價標記爲正確,因爲我很欣賞這一努力。 –
很好,你自己找到了解決方案!這些NLTK容器可能非常棘手,有時候... – lenz