返回经过训练的三元标记时出错:UnpicklingError:无效的加载键“\x0a”

2024-03-29 12:01:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我在取消勾选.pkl文件并对其进行编辑时遇到问题。我尝试了以下代码,但显示错误。我怎样才能继续

def trained_tagger(existing=False):
print('checkpoint 8')
"""Returns a trained trigram tagger
existing : set to True if already trained tagger has been pickled
"""
if existing:
    trigram_tagger =  pickle.load(open('D:\\Knowledge Base\\trained_tagger.pkl', 'rb'))

    return trigram_tagger

# Aggregate trained sentences for N-Gram Taggers
train_sents = nltk.corpus.brown.tagged_sents()
train_sents += nltk.corpus.conll2000.tagged_sents()
train_sents += nltk.corpus.treebank.tagged_sents()

# Create instance of SubjectTrigramTagger and persist instance of it
trigram_tagger = SubjectTrigramTagger(train_sents)
pickle.dump(trigram_tagger, open('D:\\Knowledge Base\\trained_tagger.pkl', 'wb'))


return trigram_tagger

它将错误显示为:

UnpicklingError: invalid load key, '\x0a'.

Tags: if错误loadtraincorpusopentrigrampickle