def get_glove_k(self, K):
assert hasattr(self, 'glove_path'), 'warning : \
you need to set_glove_path(glove_path)'
# create word_vec with k first glove vectors
k = 0
word_vec = {}
with io.open(self.glove_path) as f:
for line in f:
word, vec = line.split(' ', 1)
if k <= K:
word_vec[word] = np.fromstring(vec, sep=' ')
k += 1
if k > K:
if word in ['<s>', '</s>']:
word_vec[word] = np.fromstring(vec, sep=' ')
if k>K and all([w in word_vec for w in ['<s>', '</s>']]):
break
return word_vec
评论列表
文章目录