KeyError:word2vec中的“单词'word'不在词汇表中" [英] KeyError: “word 'word' not in vocabulary” in word2vec
本文介绍了KeyError:word2vec中的“单词'word'不在词汇表中"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用我训练过的维基语料word2vec
,如果我输入的单词不在word2vec
的词汇表中怎么办?
I am using word2vec
, wiki corpus I trained, what can I do if the word I input not in vocabulary in word2vec
?
稍作测试:
model = word2vec.Word2Vec.load('model/' + 'wiki_chinese_word2vec.model')
model['boom']
错误:
KeyError(单词'%s'不在词汇中"%单词)
KeyError("word '%s' not in vocabulary" % word)
推荐答案
使用try
& except
用于处理Python中的异常. try
块正常执行.如果发生任何异常或错误,则将执行except
块.
Use try
& except
to handle exceptions in Python. try
block executes normally. If any exception or error occurs then except
block will be executed.
try:
c = model['boom']
except KeyError:
print "not in vocabulary"
c = 0
这篇关于KeyError:word2vec中的“单词'word'不在词汇表中"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文