【发布时间】:2014-08-29 20:44:41
【问题描述】:
我一直在尝试使用 gensim 在 Python 中使用 word2vec。当我使用 try/except 来检查不在 Google 模型中的单词时,我遇到了问题。每次出现异常时,我都可以打印单词,但模型会停止并且不会计算列表中剩余的单词。
在代码之后,我显示了词汇表的内容,模型停在单词 travellers 之后,没有转换单词 travellers 之后的其余单词。我真的被困住了,我可以在这个问题上使用一些帮助。有什么想法吗?
for x in range(0,len(data)):
titles.append(data[x]['title'])
paragraphs.append(data[x]['paragraphs'])
model = gensim.models.Word2Vec.load('/tmp/models/google2')
for y in range(95,96):
vocabulary.append(titles[y])
vocabulary.append(paragraphs[y][0])
vocabulary.append(paragraphs[y+1][0])
print vocabulary
for entry in vocabulary:
try:
row = tokenizer.tokenize(entry)
row = [word for word in row if word not in stopwords.words('english')]
row = [model[item] for item in row]
row = [np.sum(item) for item in row]
last.append(row)
except KeyError,e:
print "There is a word that does not exist in the vocabulary: ", e
词汇表中不存在一个词:u'travellers'
词汇[0]:亚洲的全球旅游热潮
词汇表[1]:随着越来越多的亚洲人,尤其是中国人出国旅游,大陆地区的旅游、旅游和消费能力正在发生变化。
词汇[2]:这是最近记忆中中亚最贫穷国家发生的最激动人心的事情。
提前谢谢你。
【问题讨论】: