forked from SVAIGBA/WMSeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wmseg_eval.py
72 lines (63 loc) · 2.09 KB
/
wmseg_eval.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
69
70
71
72
def eval_sentence(y_pred, y, sentence, word2id):
words = sentence.split(' ')
seg_true = []
seg_pred = []
word_true = ''
word_pred = ''
for i in range(len(y)):
word_true += words[i]
word_pred += words[i]
if y[i] in ['S', 'E']:
if word_true not in word2id:
word_true = '*' + word_true + '*'
seg_true.append(word_true)
word_true = ''
if y_pred[i] in ['S', 'E']:
seg_pred.append(word_pred)
word_pred = ''
seg_true_str = ' '.join(seg_true)
seg_pred_str = ' '.join(seg_pred)
return seg_true_str, seg_pred_str
def cws_evaluate_word_PRF(y_pred, y):
#dict = {'E': 2, 'S': 3, 'B':0, 'I':1}
cor_num = 0
yp_wordnum = y_pred.count('E')+y_pred.count('S')
yt_wordnum = y.count('E')+y.count('S')
start = 0
for i in range(len(y)):
if y[i] == 'E' or y[i] == 'S':
flag = True
for j in range(start, i+1):
if y[j] != y_pred[j]:
flag = False
if flag:
cor_num += 1
start = i+1
P = cor_num / float(yp_wordnum) if yp_wordnum > 0 else -1
R = cor_num / float(yt_wordnum) if yt_wordnum > 0 else -1
F = 2 * P * R / (P + R)
print('P: ', P)
print('R: ', R)
print('F: ', F)
return P, R, F
def cws_evaluate_OOV(y_pred_list, y_list, sentence_list, word2id):
cor_num = 0
yt_wordnum = 0
for y_pred, y, sentence in zip(y_pred_list, y_list, sentence_list):
start = 0
for i in range(len(y)):
if y[i] == 'E' or y[i] == 'S':
word = ''.join(sentence[start:i+1])
if word in word2id:
start = i + 1
continue
flag = True
yt_wordnum += 1
for j in range(start, i+1):
if y[j] != y_pred[j]:
flag = False
if flag:
cor_num += 1
start = i + 1
OOV = cor_num / float(yt_wordnum) if yt_wordnum > 0 else -1
return OOV