-
Notifications
You must be signed in to change notification settings - Fork 11
/
generate_embeddings.py
78 lines (57 loc) · 2.18 KB
/
generate_embeddings.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import numpy as np
import pickle
import argparse
import re
"""
Convert pretrained GloVe embeddings into npy file
Run using:
python3 embeddings.py -d data/glove.42B.300d.txt --npy_output data/embeddings.npy --dict_output data/vocab.pckl --dict_whitelist data/aclImdb/imdb.vocab
"""
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--dataset', '-d', type=str, required=True)
parser.add_argument('--npy_output', type=str, required=True)
parser.add_argument('--dict_output', type=str, required=True)
parser.add_argument('--dict_whitelist', type=str, required=True)
parser.add_argument('--dump_frequency', type=int, default=10000)
return parser.parse_args()
def main():
args = parse_args()
# reserve 0 for unknown words
data = {
'': 0
}
embeddings = [
np.zeros((300), dtype=np.float32)
]
float_re = re.compile(' [-+]?[0-9]*\.?[0-9]+([eE][-+]?[0-9]+)?')
with open(args.dict_whitelist) as wfile:
whitelist = [line.strip() for line in wfile]
print("Building vocabulary ...")
with open(args.dataset) as ofile, \
open(args.dict_output, 'wb') as dfile, \
open(args.npy_output, 'wb') as nfile:
idx = 1
for line in ofile:
pos = next(re.finditer(float_re, line)).start()
word, vector = line[:pos], line[pos+1:].split()
if word not in whitelist:
continue
if word in data:
print('Possible duplicate at {} in {}'.format(idx, line))
continue
embedding = np.fromiter([float(d) for d in vector], np.float32)
if embedding.shape != (300,):
print('Shape is {}'.format(embedding.shape))
print(line)
embeddings.append(embedding)
data[word] = idx
idx += 1
if not idx % args.dump_frequency:
np.save(nfile, np.array(embeddings))
embeddings.clear()
np.save(nfile, np.array(embeddings))
pickle.dump(data, dfile)
print("Vocabulary saved, size is {} words".format(idx))
if __name__ == '__main__':
main()