-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathscore.py
35 lines (28 loc) · 1.1 KB
/
score.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
import json
import numpy as np
import argparse
from tqdm import tqdm
from sklearn.metrics import f1_score, classification_report
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description = 'calculate F1 score from scoring files')
parser.add_argument('--input_file_path', type=str, help="path of the input scoring file")
parser.add_argument('--template_file_path', type=str, help="path of the template file")
args = parser.parse_args()
with open(args.input_file_path) as f:
data = json.load(f)
with open(args.template_file_path) as f:
templates = json.load(f)
relation_map = {rel:i for i, rel in enumerate(list(templates.keys()))}
relation_num = len(relation_map)
trues = []
preds = []
for pred, na_score, true in tqdm(data):
trues.append(relation_map[true])
if na_score > pred[1]:
preds.append(relation_map["no_relation"])
else:
preds.append(relation_map[pred[0]])
f1 = f1_score(trues, preds, labels=range(1, relation_num), average="micro")
print("F1 score:", f1)
print("Classification report:", classification_report(trues, preds, labels=range(1, relation_num), digits=4))