-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathartifact_evaluation.py
291 lines (237 loc) · 9.91 KB
/
artifact_evaluation.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
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
import argparse
import pickle
import re
import logging
import spacy
from src.deeplearning.entity.infer.utils import get_series_bio
from src.deeplearning.entity.infer.wrapper import ActorWrapper, \
IntentionWrapper, ActorCombinedWrapper
from src.deeplearning.entity.utils.utils_metrics import classification_report
from src.deeplearning.relation.code.tasks.infer import infer_from_trained
from src.rules.config import intention_plugins
from src.rules.entity.dispatch import get_rule_fixes
from test.rules.utils.load_dataset import load_dataset
logging.disable(logging.CRITICAL)
def test_measure_bert_actor_prec():
data = list(
load_dataset("pretrained_data/2022_Kfold/actor/10/9/split_dev.jsonl")
)
sents = [d[1] for d in data]
labels = [d[2] for d in data]
wrapper = ActorWrapper()
results = wrapper.process(sents, labels)
pred_entities, true_entities = get_series_bio(results)
print(classification_report(true_entities, pred_entities))
def test_measure_bert_actor_rules_prec():
data = list(
load_dataset("pretrained_data/2022_Kfold/actor/10/0/split_dev.jsonl")
)
sents = [d[1] for d in data]
labels = [d[2] for d in data]
wrapper = ActorWrapper()
results = wrapper.process(sents, labels)
pred_entities, true_entities = get_series_bio(results)
new_pred_entities = list()
for sent, result in zip(sents, results):
res = get_rule_fixes(sent, result)
new_pred_entities.append(res)
pred_entities, true_entities = get_series_bio(new_pred_entities)
print(classification_report(true_entities, pred_entities))
def test_measure_bert_actor_combined_prec():
data = list(
load_dataset("pretrained_data/2022/actor/combined/split_dev.jsonl")
)
sents = [d[1] for d in data]
labels = [d[2] for d in data]
wrapper = ActorCombinedWrapper()
results = wrapper.process(sents, labels)
pred_entities, true_entities = get_series_bio(results)
print(classification_report(true_entities, pred_entities))
def test_measure_bert_actor_combined_rules_prec():
data = list(
load_dataset("pretrained_data/2022/actor/combined/split_dev.jsonl")
)
sents = [d[1] for d in data]
labels = [d[2] for d in data]
wrapper = ActorCombinedWrapper()
results = wrapper.process(sents, labels)
pred_entities, true_entities = get_series_bio(results)
new_pred_entities = list()
for sent, result in zip(sents, results):
res = get_rule_fixes(sent, result)
new_pred_entities.append(res)
pred_entities, true_entities = get_series_bio(new_pred_entities)
print(classification_report(true_entities, pred_entities))
def test_measure_bert_intention_verb_prec():
data = list(load_dataset("pretrained_data/2022/task/verb/split_dev.jsonl"))
sents = [d[1] for d in data]
labels = [d[2] for d in data]
wrapper = IntentionWrapper()
results = wrapper.process(sents, labels)
pred_entities, true_entities = get_series_bio(results)
print(classification_report(true_entities, pred_entities))
def test_measure_bert_intention_rules_prec():
data = list(load_dataset("pretrained_data/2022/task/verb/split_dev.jsonl"))
sents = [d[1] for d in data]
labels = [d[2] for d in data]
wrapper = IntentionWrapper()
results = wrapper.process(sents, labels)
new_pred_entities = list()
for sent, result in zip(sents, results):
res = get_rule_fixes(sent, result, intention_plugins)
new_pred_entities.append(res)
pred_entities, true_entities = get_series_bio(new_pred_entities)
print(classification_report(true_entities, pred_entities))
def test_measure_bert_relation_prec():
args = argparse.Namespace(**dict(task='istar', train_data='./pretrained_data/2022/relation/admin.jsonl', use_pretrained_blanks=0, num_classes=4, batch_size=32, gradient_acc_steps=1, max_norm=1.0, fp16=0, num_epochs=25, lr=7e-05, model_no=0, model_size='bert-base-uncased', train=0, infer=1))
inferer = infer_from_trained(args, detect_entities=False)
tp, fp, tn, fn = 0, 0, 0, 0
with open("pretrained_data/2022/relation/df_test.pkl", 'rb') as pkl_file:
test = pickle.load(pkl_file)
for index, row in test.iterrows():
sents = row["sents"]
relations = row["relations"]
trues = row["relations_id"] # no: 1; dependency: 0; isa: 2
preds = inferer.infer_sentence(sents, detect_entities=False)
if trues == 1:
if trues == preds:
tn += 1
else:
fp += 1
elif preds == 1:
# trues != 1
fn += 1
else:
if trues == preds:
tp += 1
else:
fp += 1
headers = ["precision", "recall", "f1-score", "support"]
head_fmt = "{:>{width}s} " + " {:>9}" * len(headers)
report = head_fmt.format("", *headers, width=5)
report += "\n\n"
row_fmt = "{:>{width}s} " + " {:>9.{digits}f}" * 3 + " {:>9}\n"
p = tp / (tp + fp)
r = tp / (tp + fn)
f1 = 2 * p * r / (p + r)
report += row_fmt.format(
*["Relation", r, p, f1, tp + fn], width=5, digits=5
)
print(report)
def find_children(token, dep=None, pos=None, tag=None, text=None):
children_list = list()
if not list(token.children):
return children_list
for t in token.children:
flag = True
if dep and t.dep_ != dep:
flag = False
if pos and t.pos != pos:
flag = False
if tag and t.tag != tag:
flag = False
if text and t.lower_ != text:
flag = False
if flag:
children_list.append(t)
return children_list
def match(stat, e11, e22):
nsubj = list()
obj = None
for token in stat:
if token.dep_ == 'nsubj' or token.dep_ == 'nsubjpass':
nsubj.append(token)
if token.dep_ in ['xcomp', 'ccomp'] and nsubj: # and token.head == nsubj.head:
obj = find_children(token.head, dep='dobj')
obj.extend(find_children(token, dep='dobj'))
obj.extend(find_children(token, dep='pobj'))
obj.extend(find_children(token.head, dep='pobj'))
if nsubj and obj:
for n in nsubj:
for o in obj:
if n.text in e11 and o.text in e22:
return True
if n.text in e22 and o.text in e11:
return True
def test_measure_bert_relation_rules_prec():
args = argparse.Namespace(**dict(task='istar', train_data='./pretrained_data/2022/relation/admin.jsonl', use_pretrained_blanks=0, num_classes=4, batch_size=32, gradient_acc_steps=1, max_norm=1.0, fp16=0, num_epochs=25, lr=7e-05, model_no=0, model_size='bert-base-uncased', train=0, infer=1))
inferer = infer_from_trained(args, detect_entities=False)
tp, fp, tn, fn = 0, 0, 0, 0
nlp = spacy.load('en_core_web_trf')
with open("pretrained_data/2022/relation/df_test.pkl", 'rb') as pkl_file:
test = pickle.load(pkl_file)
for index, row in test.iterrows():
sents = row["sents"]
relations = row["relations"]
trues = row["relations_id"] # no: 1; dependency: 0; isa: 2
preds = inferer.infer_sentence(sents, detect_entities=False)
e1 = re.search(r'\[E1](.*)\[/E1]', sents)
if not e1:
raise "Illegal: No e1!"
e1 = e1.group(1)
e2 = re.search(r'\[E2](.*)\[/E2]', sents)
if not e2:
raise "Illegal: No e2!"
e2 = e2.group(1)
raw_sent = sents.replace('[E1]', '').replace('[/E1]', '').replace('[E2]', '').replace('[/E2]', '')
text = nlp(raw_sent)
if match(text, e1, e2):
preds = 0
if trues == 1:
if trues == preds:
tn += 1
else:
fp += 1
elif preds == 1:
# trues != 1
fn += 1
else:
if trues == preds:
tp += 1
else:
fp += 1
headers = ["precision", "recall", "f1-score", "support"]
head_fmt = "{:>{width}s} " + " {:>9}" * len(headers)
report = head_fmt.format("", *headers, width=5)
report += "\n\n"
row_fmt = "{:>{width}s} " + " {:>9.{digits}f}" * 3 + " {:>9}\n"
p = tp / (tp + fp)
r = tp / (tp + fn)
f1 = 2 * p * r / (p + r)
report += row_fmt.format(
*["Relation", r, p, f1, tp + fn], width=5, digits=5
)
print(report)
if __name__ == '__main__':
print("Table 2: Hybrid Method")
print("--------------------------------------")
print("Actor Entity")
test_measure_bert_actor_rules_prec()
print("--------------------------------------")
print("Intention Entity")
test_measure_bert_intention_rules_prec()
print("--------------------------------------")
print("Actor Relation")
test_measure_bert_relation_rules_prec()
print("--------------------------------------")
print()
print("Table 3: Pure BERT Method")
print("--------------------------------------")
print("Actor Entity")
test_measure_bert_actor_prec()
print("--------------------------------------")
print("Intention Entity")
test_measure_bert_intention_verb_prec()
print("--------------------------------------")
print("Actor Relation")
test_measure_bert_relation_prec()
print("--------------------------------------")
print()
print("Table 4: Combined Actor Entities")
print("--------------------------------------")
print("Actor Entity - BERT")
test_measure_bert_actor_combined_prec()
print("--------------------------------------")
print("Actor Entity - Hybrid Method")
test_measure_bert_actor_combined_rules_prec()
print("--------------------------------------")