forked from michael-wzhu/PromptCBLUE
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_generate_process.py
More file actions
465 lines (387 loc) · 17.2 KB
/
post_generate_process.py
File metadata and controls
465 lines (387 loc) · 17.2 KB
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
# coding=utf-8
# Created by Michael Zhu
# ECNU, 2023
import json
import sys
def process_generated_results(pred_file):
structured_output = {
"CMeEE-V2": [],
"CMeIE": [],
"CHIP-CDN": [],
"CHIP-CDEE": [],
"CHIP-STS": [],
"CHIP-CTC": [],
"CHIP-MDCFNPC": [],
"KUAKE-IR": [],
"KUAKE-QIC": [],
"KUAKE-QQR": [],
"KUAKE-QTR": [],
"MedDG": [],
"IMCS-V2-MRG": [],
"IMCS-V2-NER": [],
"IMCS-V2-DAC": [],
"IMCS-V2-SR": [],
}
with open(pred_file, "r", encoding="utf-8") as f:
for line in f:
line = json.loads(line)
# print("line: ", line)
sample_id_ = line.get("sample_id", "xxxx")
input = line["input"]
gen_output = line["target"]
gen_output = gen_output.replace(":", ":", 100).replace(",", ",", 100).replace(";", ";", 100)
# gen_output = line["generated_output"]
task_dataset = line["task_dataset"]
task_type = line["task_type"]
# 选项:
answer_choices = line["answer_choices"]
if task_dataset == "CMeEE-V2":
# 答案格式:
# 第一行:引导词
# 实体每类占一行,每行格式为 "[类型名称]实体:实体名称1,实体名称2,实体名称3\n"
# 多个实体,用 , 符号分割
list_entities = []
assert isinstance(answer_choices, list)
for choice in answer_choices:
for piece in gen_output.split("\n"):
if piece.startswith(f"{choice}实体"):
mentions = piece.replace(f"{choice}实体:", "").split(",")
mentions = [w.strip() for w in mentions if len(w.strip()) > 0]
for ment in mentions:
list_entities.append(
{
"entity": ment,
"type": choice,
# "sample_id": sample_id_,
}
)
# print("line: ", line)
# print("gen_output: ", gen_output)
# print("list_entities: ", list_entities)
structured_output["CMeEE-V2"].append(
{
"sample_id": sample_id_,
"answer": list_entities,
}
)
elif task_dataset == "CMeIE":
# 答案格式:
# 每个关系类型占一行,格式为
# "具有{lab}关系的实体对如下:头实体:str,尾实体:str;头实体:str,尾实体:str;"
list_spos = []
assert isinstance(answer_choices, list)
list_answer_strs = gen_output.split("\n")
for line in list_answer_strs:
# print("line: ", line)
# 首先是解析出label:
predicate = line.split("关系的头尾实体对")[0][2: ].strip()
# print("predicate: ", predicate)
line = line.replace(f"具有{predicate}关系的头尾实体对如下:", "")
for spo_str in line.split("。"):
# print("spo_str: ", spo_str)
if len(spo_str.split(",尾实体为")) < 2:
continue
head_mention_str, tail_mention_str = spo_str.split(",尾实体为")[:2]
head_mention_str = head_mention_str.replace("头实体为", "").strip()
tail_mention_str = tail_mention_str.replace("尾实体为", "").strip()
# print("head_mention_str: ", head_mention_str)
# print("tail_mention_str: ", tail_mention_str)
list_spos.append(
{
"predicate": predicate,
"subject": head_mention_str,
"object": tail_mention_str,
}
)
# print("line: ", line)
# print("gen_output: ", gen_output)
# print("list_spos: ", list_spos)
structured_output[f"{task_dataset}"].append(
{
"sample_id": sample_id_,
"answer": list_spos,
}
)
elif task_dataset == "CHIP-CDN":
# 答案格式:
# 多个选中的标准化实体,用 , 符号分割
answer_str = gen_output.split("\n")[-1]
answers = answer_str.split(",")
answers = [w.strip() for w in answers if len(w.strip()) > 0]
#
answers = [w for w in answers if w in answer_choices]
answers = list(set(answers))
answers = [
{
"entity": w,
"type": "normalization",
# "sample_id": sample_id_,
}
for w in answers
]
# print("line: ", line)
# print("gen_output: ", gen_output)
# print("answers: ", answers)
structured_output["CHIP-CDN"].append(
{
"sample_id": sample_id_,
"answer": answers,
}
)
elif task_dataset == "CHIP-CDEE":
# 答案格式:
# 第一行:引导词
# 每个事件占一行,事件字段用 ; 分隔, 然后每个字段是 字段名:字段值的格式"
# 字段值有多个,则用 ,符号分隔
keys = ["主体词", "发生状态", "描述词", "解剖部位"]
list_answer_strs = gen_output.split("\n")[1: ]
list_events = []
for ans_str in list_answer_strs:
event_info = {}
ans_attrs = ans_str.split(";")
for a_attr in ans_attrs:
# print("a_attr: ", a_attr)
for key in keys:
if a_attr.startswith(f"{key}:"):
a_attr = a_attr.replace(f"{key}:", "").strip()
if key in ["描述词", "解剖部位"]:
a_attr_split = a_attr.split(",")
a_attr_split = [w.strip() for w in a_attr_split if len(w.strip()) > 0]
event_info[key] = a_attr_split
else:
event_info[key] = a_attr
for key in keys:
if key not in event_info:
if key in ["描述词", "解剖部位"]:
event_info[key] = []
else:
event_info[key] = ""
# event_info["sample_id"] = sample_id_
list_events.append(event_info)
# print("line: ", line)
# print("gen_output: ", gen_output)
# print("list_events: ", list_events)
structured_output["CHIP-CDEE"].append(
{
"sample_id": sample_id_,
"answer": list_events,
}
)
elif task_dataset == "CHIP-STS":
# 答案格式:直接回答"是","不是","相同", ”不同“
answer_str = gen_output.strip()
# if answer_str not in answer_choices:
# answer_str = "不是"
answer_choices = ["是的", "不是", ]
if answer_str == "相同":
answer_str = "是的"
elif answer_str == "不同":
answer_str = "不是"
# print(answer_str)
# if answer_str not in answer_choices:
# answer_str = "不是"
structured_output["CHIP-STS"].append(
{
"sample_id": sample_id_,
"answer": answer_str,
}
)
elif task_dataset == "CHIP-CTC":
# 答案格式:直接回答分类标签
answer_str = gen_output.strip()
# if not answer_str in answer_choices:
# answer_str = "非上述类型"
structured_output[task_dataset].append(
{
"sample_id": sample_id_,
"answer": answer_str,
}
)
elif task_dataset == "KUAKE-IR":
# 答案格式:直接回答 "相关", "不相关"
answer_str = gen_output.strip()
# if answer_str not in answer_choices:
# answer_str = "不相关"
structured_output[task_dataset].append(
{
"sample_id": sample_id_,
"answer": answer_str,
}
)
elif task_dataset == "KUAKE-QIC":
# 答案格式:直接回答分类标签
answer_str = gen_output.strip()
# if not answer_str in answer_choices:
# answer_str = "非上述类型"
structured_output[task_dataset].append(
{
"sample_id": sample_id_,
"answer": answer_str,
}
)
elif task_dataset == "KUAKE-QQR":
# 答案格式:直接回答分类标签
answer_str = gen_output.strip()
# if not answer_str in answer_choices:
# answer_str = "后者是前者的语义父集或语义毫无关联"
structured_output[task_dataset].append(
{
"sample_id": sample_id_,
"answer": answer_str,
}
)
elif task_dataset == "KUAKE-QTR":
# 答案格式:直接回答分类标签
answer_str = gen_output.strip()
# if not answer_str in answer_choices:
# answer_str = "完全不匹配"
structured_output[task_dataset].append(
{
"sample_id": sample_id_,
"answer": answer_str,
}
)
elif task_dataset == "CHIP-MDCFNPC":
# 答案格式:
# 第一行:引导词
# 每一行就是 "[症状词]:[阴阳性判断结果]"
list_answer_strs = gen_output.split("\n")[1:]
list_finding_attrs = []
for ans_str in list_answer_strs:
if not len(ans_str.split(":")) == 2:
continue
finding, conclusion = ans_str.split(":")
if conclusion not in answer_choices:
conclusion = "无实际意义的不标注或者和病人当前的状态独立不标注"
list_finding_attrs.append(
{
"entity": finding.strip(),
"attr": conclusion
}
)
structured_output[f"{task_dataset}"].append(
{
"sample_id": sample_id_,
"answer": list_finding_attrs,
}
)
elif task_dataset == "IMCS-V2-NER":
# 答案格式:
# 第一行:引导词
# 实体每类占一行,每行格式为 "[类型名称]实体:实体名称1,实体名称2,实体名称3\n"
# 多个实体,用 , 符号分割
list_entities = []
assert isinstance(answer_choices, list)
for choice in answer_choices:
for piece in gen_output.split("\n"):
if piece.startswith(f"{choice}实体"):
mentions = piece.replace(f"{choice}实体:", "").split(",")
mentions = [w.strip() for w in mentions if len(w.strip()) > 0]
for ment in mentions:
list_entities.append(
{
"entity": ment,
"type": choice,
}
)
structured_output["IMCS-V2-NER"].append(
{
"sample_id": sample_id_,
"answer": list_entities,
}
)
elif task_dataset == "IMCS-V2-DAC":
# 答案格式:直接回答分类标签
answer_str = gen_output.strip()
# if not answer_str in answer_choices:
# answer_str = "非上述类型"
# print("line: ", line)
# print("gen_output: ", gen_output)
# print("answer_str: ", answer_str)
structured_output[task_dataset].append(
{
"sample_id": sample_id_,
"answer": answer_str,
}
)
elif task_dataset == "IMCS-V2-SR":
# 答案格式:
# 第一行:引导词
# 每一行就是 "[症状词]:[阴阳性判断结果]"
list_answer_strs = gen_output.split("\n")[1:]
list_finding_attrs = []
for ans_str in list_answer_strs:
if not len(ans_str.split(":")) == 2:
continue
finding, conclusion = ans_str.split(":")
if conclusion not in answer_choices:
conclusion = "无法根据上下文确定病人是否患有该症状"
list_finding_attrs.append(
{
"entity": finding.strip(),
"attr": conclusion
}
)
structured_output[f"{task_dataset}"].append(
{
"sample_id": sample_id_,
"answer": list_finding_attrs,
}
)
elif task_dataset == "IMCS-V2-MRG":
# 答案格式:
# 1. 第一行是引导词;
# 第二行开始是 [section_name]:str的格式
keys = [
"主诉:",
"现病史:",
"辅助检查:",
"既往史:",
"诊断:",
"建议:"
]
answer_dict = {}
for key in keys:
for line in gen_output.strip().split("\n")[1: ]:
# print("line: ", line)
if not line.startswith(key):
continue
answer_str = line.strip().split(key)[-1].strip()
answer_dict[key[: -1]] = answer_str
structured_output[f"{task_dataset}"].append(
{
"sample_id": sample_id_,
"answer": answer_dict,
}
)
elif task_dataset == "MedDG":
# 答案格式:str
answer_str = gen_output.strip()
# print("line: ", line)
# print("gen_output: ", gen_output)
# print("answer_str: ", answer_str)
structured_output[f"{task_dataset}"].append(
{
"sample_id": sample_id_,
"answer": answer_str,
}
)
else:
# print("task_dataset: ", task_dataset)
# print("task_type: ", task_type)
raise ValueError
return structured_output
if __name__ == "__main__":
from_dir = sys.argv[1]
to_dir = sys.argv[2]
structured_outputs = process_generated_results(
from_dir
)
for key in structured_outputs.keys():
print(key, len(structured_outputs[key]))
json.dump(
structured_outputs,
open(to_dir, "w", encoding="utf-8"),
ensure_ascii=False,
indent=2
)