forked from catcd/LSTM-CNN-SUD
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
64 lines (49 loc) · 1.4 KB
/
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
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
import time
import numpy as np
def get_trimmed_w2v_vectors(filename):
"""
Args:
filename: path to the npz file
Returns:
matrix of embeddings (np array)
"""
with np.load(filename) as data:
return data["embeddings"]
def load_vocab(filename):
"""
Args:
filename: file with a word per line
Returns:
d: dict[word] = index
"""
d = dict()
with open(filename) as f:
for idx, word in enumerate(f):
word = word.strip()
d[word] = idx + 1 # preserve idx 0 for pad_tok
return d
class Timer:
def __init__(self):
self.start_time = {}
self.running_jobs = []
def start(self, job):
if job is None:
return None
self.start_time[job] = time.time()
print("[INFO] {job} started.".format(job=job))
self.running_jobs.append(job)
def stop(self, job=None):
if job is None:
job = self.running_jobs[-1]
if job is None or job not in self.start_time:
return None
elapsed_time = time.time() - self.start_time[job]
print("[INFO] {job} finished in {elapsed_time:0.3f}s.".format(job=job, elapsed_time=elapsed_time))
del self.start_time[job]
self.running_jobs.remove(job)
class Log:
verbose = True
@staticmethod
def log(text):
if Log.verbose:
print(text)