-
Notifications
You must be signed in to change notification settings - Fork 275
/
build_glove.py
43 lines (34 loc) · 1.32 KB
/
build_glove.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
"""Build an np.array from some glove file and some vocab file
You need to download `glove.840B.300d.txt` from
https://nlp.stanford.edu/projects/glove/ and you need to have built
your vocabulary first (Maybe using `build_vocab.py`)
"""
__author__ = "Guillaume Genthial"
from pathlib import Path
import numpy as np
if __name__ == '__main__':
# Load vocab
with Path('vocab.words.txt').open() as f:
word_to_idx = {line.strip(): idx for idx, line in enumerate(f)}
size_vocab = len(word_to_idx)
# Array of zeros
embeddings = np.zeros((size_vocab, 300))
# Get relevant glove vectors
found = 0
print('Reading GloVe file (may take a while)')
with Path('glove.840B.300d.txt').open() as f:
for line_idx, line in enumerate(f):
if line_idx % 100000 == 0:
print('- At line {}'.format(line_idx))
line = line.strip().split()
if len(line) != 300 + 1:
continue
word = line[0]
embedding = line[1:]
if word in word_to_idx:
found += 1
word_idx = word_to_idx[word]
embeddings[word_idx] = embedding
print('- done. Found {} vectors for {} words'.format(found, size_vocab))
# Save np.array to file
np.savez_compressed('glove.npz', embeddings=embeddings)