File size: 9,171 Bytes
d5062c8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
# -*- coding: utf-8 -*-
"""
Created on Mon Mar  1 15:33:54 2021

@author: luol2
"""
# from BIO format to entity
def BIO_tag(tokens):
    gold_entity={}
    pre_entity={}
    gold_start,gold_end=0,0
    pre_start,pre_end=0,0
    for i in range(0,len(tokens)):
        segs=tokens[i].split('\t')
        
        # generate gold entity
        if segs[1].startswith('B-')>0:
            gold_start=i
            gold_type=segs[1][2:]
            if i+1>=len(tokens): # the last word
                gold_end=i
                if gold_type in gold_entity.keys():
                    gold_entity[gold_type].append([gold_start,gold_end])
                else:
                    gold_entity[gold_type]=[[gold_start,gold_end]]
            else: # non last word
                next_seg=tokens[i+1].split('\t')
                if next_seg[1].startswith('B-')>0 or next_seg[1]=='O':
                    gold_end=i
                    if gold_type in gold_entity.keys():
                        gold_entity[gold_type].append([gold_start,gold_end])
                    else:
                        gold_entity[gold_type]=[[gold_start,gold_end]]
                elif next_seg[1].startswith('I-')>0:
                    pass
        elif segs[1].startswith('I-')>0:
            if i+1>=len(tokens): # the last word
                gold_end=i
                if gold_type in gold_entity.keys():
                    gold_entity[gold_type].append([gold_start,gold_end])
                else:
                    gold_entity[gold_type]=[[gold_start,gold_end]]
            else: # non last word
                next_seg=tokens[i+1].split('\t')
                if next_seg[1].startswith('B-')>0 or next_seg[1]=='O':
                    gold_end=i
                    if gold_type in gold_entity.keys():
                        gold_entity[gold_type].append([gold_start,gold_end])
                    else:
                        gold_entity[gold_type]=[[gold_start,gold_end]]
                elif next_seg[1].startswith('I-')>0:
                    pass
        elif segs[1]=='O':
            pass
        
        # generate prediction entity            
        if segs[2].startswith('B-')>0:
            pre_start=i
            pre_type=segs[2][2:]
            if i+1>=len(tokens): # the last word
                pre_end=i
                if pre_type in pre_entity.keys():
                    pre_entity[pre_type].append([pre_start,pre_end])
                else:
                    pre_entity[pre_type]=[[pre_start,pre_end]]
            else: # non last word
                next_seg=tokens[i+1].split('\t')
                if next_seg[2].startswith('B-')>0 or next_seg[2]=='O':
                    pre_end=i
                    if pre_type in pre_entity.keys():
                        pre_entity[pre_type].append([pre_start,pre_end])
                    else:
                        pre_entity[pre_type]=[[pre_start,pre_end]]
                elif next_seg[2].startswith('I-')>0:
                    pass
        elif segs[2].startswith('I-')>0:
            if i==0 and i+1<len(tokens): # the first word and not only a word
                pre_start=i
                pre_type=segs[2][2:]
                next_seg=tokens[i+1].split('\t')
                if next_seg[2].startswith('B-')>0 or next_seg[2]=='O':
                    pre_end=i
                    if pre_type in pre_entity.keys():
                        pre_entity[pre_type].append([pre_start,pre_end])
                    else:
                        pre_entity[pre_type]=[[pre_start,pre_end]]
                elif next_seg[2].startswith('I-')>0:
                    pass
            elif i==0 and i+1==len(tokens):# only one word:
                pre_start=i
                pre_type=segs[2][2:]
                pre_end=i
                if pre_type in pre_entity.keys():
                    pre_entity[pre_type].append([pre_start,pre_end])
                else:
                    pre_entity[pre_type]=[[pre_start,pre_end]]
            elif i+1>=len(tokens): # the last word
                last_seg=tokens[i-1].split('\t')
                if last_seg[2]=='O':
                    pre_start=i
                    pre_type=segs[2][2:]                
                pre_end=i
                if pre_type in pre_entity.keys():
                    pre_entity[pre_type].append([pre_start,pre_end])
                else:
                    pre_entity[pre_type]=[[pre_start,pre_end]]
            elif i+1< len(tokens): # non last word
                next_seg=tokens[i+1].split('\t')
                last_seg=tokens[i-1].split('\t')
                if last_seg[2]=='O':
                    pre_start=i
                    pre_type=segs[2][2:]  
                if next_seg[2].startswith('B-')>0 or next_seg[2]=='O':
                    pre_end=i
                    if pre_type in pre_entity.keys():
                        pre_entity[pre_type].append([pre_start,pre_end])
                    else:
                        pre_entity[pre_type]=[[pre_start,pre_end]]
                elif next_seg[2].startswith('I-')>0:
                    pass
        elif segs[2]=='O':
            pass        
    # print(tokens)
    # print(gold_entity)
    # print(pre_entity)
    return gold_entity,pre_entity
        
# input: token \t Gold \t Prediction\n, sentence is split "\n"
def NER_Evaluation():
    path='//panfs/pan1/bionlp/lulab/luoling/OpenBioIE_project/models/Kfold/BiLSTM-CRF/'
    fin=open(path+'dev_pre.conll_all','r',encoding='utf-8')
    all_sentence=fin.read().strip().split('\n\n')
    fin.close()
    Metrics={} #{'entity_type':[TP,gold_num,pre_num]}

    for sentence in all_sentence:
        tokens=sentence.split('\n')
        gold_entity,pre_entity=BIO_tag(tokens)
        # print(tokens)
        for entity_type in gold_entity.keys():
            if entity_type not in Metrics.keys():
                Metrics[entity_type]=[0,len(gold_entity[entity_type]),0]
            else:
                Metrics[entity_type][1]+=len(gold_entity[entity_type])
        for entity_type in pre_entity.keys():
            if entity_type not in Metrics.keys():
                Metrics[entity_type]=[0,0,len(pre_entity[entity_type])]
            else:
                Metrics[entity_type][2]+=len(pre_entity[entity_type])
                for mention in pre_entity[entity_type]:
                    if entity_type in gold_entity.keys():
                        if mention in gold_entity[entity_type]:
                            Metrics[entity_type][0]+=1
    print(Metrics)
    TP,Gold_num,Pre_num=0,0,0
    for ele in Metrics.keys():
        if Metrics[ele][2]==0:
            p=0
        else:
            p=Metrics[ele][0]/Metrics[ele][2]
        if Metrics[ele][1]==0:
            r=0
        else:
            r=Metrics[ele][0]/Metrics[ele][1]
        if p+r==0:
            f1=0
        else:
            f1=2*p*r/(p+r)
        TP+=Metrics[ele][0]
        Gold_num+=Metrics[ele][1]
        Pre_num+=Metrics[ele][2]
        print(ele+': P=%.5f, R=%.5f, F1=%.5f' % (p,r,f1))
        # break
    if Pre_num==0:
        P=0
    else:
        P=TP/Pre_num
    R=TP/Gold_num
    F1=2*P*R/(P+R)
    print("Overall: P=%.5f, R=%.5f, F1=%.5f"% (P,R,F1))
    
def NER_Evaluation_fn(file):
    
    fin=open(file,'r',encoding='utf-8')
    all_sentence=fin.read().strip().split('\n\n')
    fin.close()
    Metrics={} #{'entity_type':[TP,gold_num,pre_num]}
    breai=0
    for sentence in all_sentence:
        breai+=1
        if breai>5000:
            break
        tokens=sentence.split('\n')
        gold_entity,pre_entity=BIO_tag(tokens)
        # print(tokens)
        for entity_type in gold_entity.keys():
            if entity_type not in Metrics.keys():
                Metrics[entity_type]=[0,len(gold_entity[entity_type]),0]
            else:
                Metrics[entity_type][1]+=len(gold_entity[entity_type])
        for entity_type in pre_entity.keys():
            if entity_type not in Metrics.keys():
                Metrics[entity_type]=[0,0,len(pre_entity[entity_type])]
            else:
                Metrics[entity_type][2]+=len(pre_entity[entity_type])
                for mention in pre_entity[entity_type]:
                    if entity_type in gold_entity.keys():
                        if mention in gold_entity[entity_type]:
                            Metrics[entity_type][0]+=1
    print(Metrics)
    TP,Gold_num,Pre_num=0,0,0
    for ele in Metrics.keys():
        if Metrics[ele][2]==0:
            p=0
        else:
            p=Metrics[ele][0]/Metrics[ele][2]
        if Metrics[ele][1]==0:
            r=0
        else:
            r=Metrics[ele][0]/Metrics[ele][1]
        if p+r==0:
            f1=0
        else:
            f1=2*p*r/(p+r)
        TP+=Metrics[ele][0]
        Gold_num+=Metrics[ele][1]
        Pre_num+=Metrics[ele][2]
        print(ele+': P=%.5f, R=%.5f, F1=%.5f' % (p,r,f1))
        # break
    if Pre_num==0:
        P=0
    else:
        P=TP/Pre_num
    R=TP/Gold_num
    if P+R==0:
        F1=0
    else:
        F1=2*P*R/(P+R)
    print("Overall: P=%.5f, R=%.5f, F1=%.5f"% (P,R,F1))
    return F1
            
if __name__=='__main__':
    NER_Evaluation()