-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.py
68 lines (49 loc) · 2.16 KB
/
validation.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
67
68
import numpy as np
#Method to compute the accruarcy. Call predict_labels to get the labels for the dataset
def compute_f1(predictions, correct, idx2Label):
label_pred = []
for sentence in predictions:
label_pred.append([idx2Label[element] for element in sentence])
label_correct = []
for sentence in correct:
label_correct.append([idx2Label[element] for element in sentence])
#print label_pred
#print label_correct
prec = compute_precision(label_pred, label_correct)
rec = compute_precision(label_correct, label_pred)
f1 = 0
if (rec+prec) > 0:
f1 = 2.0 * prec * rec / (prec + rec);
return prec, rec, f1
def compute_precision(guessed_sentences, correct_sentences):
assert(len(guessed_sentences) == len(correct_sentences))
correctCount = 0
count = 0
for sentenceIdx in range(len(guessed_sentences)):
guessed = guessed_sentences[sentenceIdx]
correct = correct_sentences[sentenceIdx]
assert(len(guessed) == len(correct))
idx = 0
while idx < len(guessed):
if guessed[idx][0] == 'B': #A new chunk starts
count += 1
if guessed[idx] == correct[idx]:
idx += 1
correctlyFound = True
while idx < len(guessed) and guessed[idx][0] == 'I': #Scan until it no longer starts with I
if guessed[idx] != correct[idx]:
correctlyFound = False
idx += 1
if idx < len(guessed):
if correct[idx][0] == 'I': #The chunk in correct was longer
correctlyFound = False
if correctlyFound:
correctCount += 1
else:
idx += 1
else:
idx += 1
precision = 0
if count > 0:
precision = float(correctCount) / count
return precision