forked from tttr222/autumn_ner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
66 lines (50 loc) · 2.23 KB
/
utility.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
import os, random, pickle, re
def load_dataset(fname, shuffle=False):
dataset = []
with open(fname,'r') as f:
dataset = [ x.split('\n') for x in f.read().split('\n\n') if x ]
vocab = []
output = []
for x in dataset:
tokens, labels = zip(*[ z.split(' ') for z in x if z ])
for t in tokens:
t = t.lower()
if t not in vocab:
vocab.append(t)
output.append((tokens, labels))
return output, vocab
def load_embeddings(fname, vocab, dim=200):
cached = 'scratch/embeddings_{}.npy'.format(abs(hash(' '.join(vocab))))
if not os.path.exists(cached):
weight_matrix = np.random.uniform(-0.05, 0.05, (len(vocab),dim)).astype(np.float32)
ct = 0
with codecs.open(fname, encoding='utf-8') as f:
for line in f:
word, vec = line.split(u' ', 1)
if word not in vocab:
continue
idx = vocab.index(word)
vec = np.array(vec.split(), dtype=np.float32)
weight_matrix[idx,:dim] = vec[:dim]
ct += 1
if ct % 33 == 0:
sys.stdout.write('Loading embeddings {}/{} \r'.format(ct, len(vocab)))
print "Loaded {}/{} embedding vectors".format(ct, len(vocab))
np.save(cached,weight_matrix)
else:
weight_matrix = np.load(cached)
print "Loaded weight matrix {}..".format(weight_matrix.shape)
return weight_matrix
def report_performance(model, X_test,y_test, outname):
micro_evaluation = model.evaluate(X_test,y_test,macro=False)
macro_evaluation = model.evaluate(X_test,y_test,macro=True)
print "Micro Test Eval: F={:.4f} P={:.4f} R={:.4f}".format(*micro_evaluation)
print "Macro Test Eval: F={:.4f} P={:.4f} R={:.4f}".format(*macro_evaluation)
pred_test = model.predict(X_test)
with open(outname,'w') as f:
for (x,y,z) in zip(X_test,y_test,pred_test):
for token, y_true, y_pred in zip(x,y,z):
print >> f, token, y_true, y_pred
print >> f, ''
with open(outname,'r') as f:
conlleval.report(conlleval.evaluate(f))