-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuman_filtering.py
93 lines (86 loc) · 3.28 KB
/
human_filtering.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
import re
import json
import glob
from collections import defaultdict
from statsmodels.stats.inter_rater import fleiss_kappa
YEAR = 2023
if __name__ == "__main__":
with open(f'human_filtering/questions_{YEAR}.json', 'r', encoding='utf-8') as f:
all = json.load(f)
history = {}
for i in all:
history[i['name']] = i
count = defaultdict(lambda: [0, 0])
table = defaultdict(lambda: [0, 0, 0, 0, 0, 0])
multi = defaultdict(int)
none = defaultdict(int)
error = defaultdict(int)
files = glob.glob(f'benchmark_{YEAR}/filtering/*.json')
to_del = []
for p in files:
with open(p, 'r', encoding='utf8') as f:
obj = json.load(f)
for it, value in obj.items():
match = re.match(r"([a-z]+)([0-9]+)", it, re.I)
items = match.groups()[0]
if '-Comment' in it:
continue
if value == 'none':
table[it][-1] += 1
elif value not in history[it]["choices"]:
table[it][-2] += 1
else:
table[it][history[it]["choices"].index(value)] += 1
count[items][0] += int(history[it]['correctAnswer'] == value)
count[items][1] += 1
if history[it]['correctAnswer'] != value:
to_del.append(history[it]['title'])
if value == 'none':
none[items] += 1
elif value not in history[it]["choices"]:
multi[items] += 1
else:
error[items] += 1
tables = []
count2 = 0
count3 = 0
for k, v in table.items():
if 2 in v:
count2 += 1
elif 3 in v:
count3 += 1
tables.append(v)
print(f"Fleiss Kappa: {fleiss_kappa(tables)}")
print(count2, count3)
a = b = 0
for k in count.keys():
print('Dataset:', k)
print('Correct Rate:', count[k][0] / count[k][1])
print('Multi.:', multi[k], ', None:', none[k], ', Wrong:', error[k])
a += count[k][0]
b += count[k][1]
print()
print('Total Correct Rate:', a / b)
for data in ['COMA', 'CSJ']:
with open(f"benchmark_{YEAR}/{data}.jsonl", 'r', encoding='utf-8') as f,\
open(f"benchmark_{YEAR}/{data}_clean.jsonl", 'w', encoding='utf-8') as w:
for cnt, line in enumerate(f):
js = json.loads(line)
flag = False
for k in to_del:
if js['term'] in k and js['meaning'] in k and js['question'] in k:
flag = True
break
if not flag:
w.write(line)
with open(f"benchmark_{YEAR}/COST.jsonl", 'r', encoding='utf-8') as f,\
open(f"benchmark_{YEAR}/COST_clean.jsonl", 'w', encoding='utf-8') as w:
for cnt, line in enumerate(f):
js = json.loads(line)
flag = False
for k in to_del:
if js['term'] in k and js['meaning'] in k and js['question'].replace('_', '<u>        </u>') in k:
flag = True
break
if not flag:
w.write(line)