forked from qhduan/just_another_seq2seq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_vector.py
61 lines (49 loc) · 1.56 KB
/
read_vector.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
"""
读取一个文本格式的,保存预训练好的embedding的文件
wiki.zh.vec
它的第一行会被忽略
第二行开始,每行是 词 + 空格 + 词向量维度0 + 空格 + 词向量维度1 + ...
参考fasttext的文本格式
https://github.com/facebookresearch/fastText/blob/master/pretrained-vectors.md
"""
import pickle
import numpy as np
from tqdm import tqdm
def read_vector(path='wiki.zh.vec', output_path='word_vec.pkl'):
"""
读取文本文件 path 中的数据,并且生成一个 dict 写入到 output_path
格式:
word_vec = {
'word_1': np.array(vec_of_word_1),
'word_2': np.array(vec_of_word_2),
...
}
"""
fp = open(path, 'r')
word_vec = {}
first_skip = False
dim = None
for line in tqdm(fp):
if not first_skip:
first_skip = True
else:
line = line.strip()
line = line.split(' ')
if len(line) >= 2:
word = line[0]
vec_text = line[1:]
vec = np.array([float(v) for v in vec_text])
word_vec[word] = vec
if dim is None:
dim = vec.shape
# PAD_TAG = '<pad>'
# UNK_TAG = '<unk>'
# START_TAG = '<s>'
# END_TAG = '</s>'
np.random.seed(0)
word_vec['<pad>'] = np.random.random(size=(300,)) - 0.5
word_vec['<s>'] = np.random.random(size=(300,)) - 0.5
word_vec['<unk>'] = np.random.random(size=(300,)) - 0.5
pickle.dump(word_vec, open(output_path, 'wb'))
if __name__ == '__main__':
read_vector()