-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.py
29 lines (21 loc) · 833 Bytes
/
utils.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
import numpy as np
import bcolz
import pickle
def save_array(fname, arr):
c = bcolz.carray(arr, rootdir=fname, mode='w')
c.flush()
def load_array(fname):
return bcolz.open(fname)[:]
def get_glove(path, res_path, name):
with open(f'{path}glove.{name}.txt', 'r') as f:
lines = [line.split() for line in f]
words = [d[0] for d in lines]
vecs = np.stack(np.array(d[1:], dtype=np.float32) for d in lines)
wordidx = {o:i for i,o in enumerate(words)}
save_array(f'{res_path}{name}.dat', vecs)
pickle.dump(words, open(f'{res_path}{name}_words.pkl', 'wb'))
pickle.dump(wordidx, open(f'{res_path}{name}_idx.pkl', 'wb'))
def load_glove(loc):
return (load_array(f'{loc}.dat'),
pickle.load(open(f'{loc}_words.pkl', 'rb')),
pickle.load(open(f'{loc}_idx.pkl', 'rb')))