-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathread_word2vec.py
48 lines (39 loc) · 1.3 KB
/
read_word2vec.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
# Read in a .bin file as saved in word2vec, which stores words in text but vectors in binary format
__author__ = 'gpfinley'
import struct
import time
import numpy as np
import logging
def get_dictionary(binfile):
f = open(binfile)
content = f.read()
f.close()
# Header of the file contains two integers: the number of words and the vector dimensionality
header = content[:content.find('\n')]
words, size = int(header.split()[0]), int(header.split()[1])
rawvecs = content[content.find('\n')+1:]
vectors = {}
print header
start = time.time()
# Move through the rest of it character by character
word = ''
i = 0
while True:
if i >= len(rawvecs):
break
c = rawvecs[i]
if c == ' ':
# Move the index past the space and unpack the vector, four bytes at a time
i += 1
vector = []
for x in range(size):
vector.append(struct.unpack('f', rawvecs[x*4+i:(x+1)*4+i])[0])
# Set i to the beginning of the next line
i += size*4 + 1
vectors[word] = np.array(vector)
word = ''
else:
word += c
i += 1
logging.info('Read', len(vectors), 'words in', time.time() - start, 'seconds')
return vectors