【发布时间】:2019-05-24 05:46:38
【问题描述】:
我想从预训练的 Glove 嵌入中提取特征。但我得到了某些单词的 Keyerror。这是单词标记的列表。
words1=['nuclear','described', 'according', 'called','physics', 'account','interesting','holes','theoretical','like','space','radiation','property','impulsed','darkfield']
我从“冲动”、“暗场”词中得到 Keyerror,因为这些词可能是看不见的词。我怎样才能避免这个错误? .
这是我的完整代码:
gloveFile = "glove.6B.50d.txt"
import numpy as np
def loadGloveModel(gloveFile):
print ("Loading Glove Model")
with open(gloveFile, encoding="utf8" ) as f:
content = f.readlines()
model = {}
for line in content:
splitLine = line.split()
word = splitLine[0]
embedding = np.array([float(val) for val in splitLine[1:]])
model[word] = embedding
print ("Done.",len(model)," words loaded!")
return model
model = loadGloveModel(gloveFile)
words1=['nuclear','described', 'according', 'called','physics', 'account','interesting','holes','theoretical','like','space','radiation','property','impulsed','darkfield']
import numpy as np
vector_2 = np.mean([model[word] for word in words1],axis=0) ## Got error message
“冲动”字的错误信息
有什么办法可以跳过这些看不见的单词?
【问题讨论】:
标签: python nlp word-embedding