-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreevaluate_normalized.py
More file actions
221 lines (171 loc) · 6.51 KB
/
Copy pathreevaluate_normalized.py
File metadata and controls
221 lines (171 loc) · 6.51 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
import argparse
import json
from collections import Counter, defaultdict
def normalize_label(label):
if label is None:
return "PARSE_ERROR"
x = str(label).strip().upper().replace("_", " ")
if x in {"SUPPORT", "SUPPORTS", "SUPPORTED", "ENTAILMENT", "TRUE"}:
return "SUPPORT"
if x in {
"REFUTE", "REFUTES", "REFUTED",
"CONTRADICT", "CONTRADICTS", "CONTRADICTION", "FALSE"
}:
return "REFUTE"
if x in {
"NOT ENOUGH INFO",
"NEI",
"NOT ENOUGH INFORMATION",
"INSUFFICIENT INFO"
}:
return "NOT ENOUGH INFO"
return "PARSE_ERROR"
def read_jsonl(path):
rows = []
with open(path, "r", encoding="utf-8") as f:
for line in f:
line = line.strip()
if line:
rows.append(json.loads(line))
return rows
def get_pred(row, method):
if method == "direct":
return row["direct"].get("label")
return row[method].get("refined_label")
def evaluate(rows, method):
correct = 0
total = 0
pred_counter = Counter()
gold_counter = Counter()
confusion = defaultdict(Counter)
for row in rows:
gold = normalize_label(row.get("gold_label"))
pred = normalize_label(get_pred(row, method))
gold_counter[gold] += 1
pred_counter[pred] += 1
confusion[gold][pred] += 1
if pred == gold:
correct += 1
total += 1
acc = correct / total if total else 0.0
return acc, pred_counter, gold_counter, confusion
def print_confusion(confusion):
labels = ["SUPPORT", "REFUTE", "NOT ENOUGH INFO", "PARSE_ERROR"]
print("\nConfusion matrix")
print("gold \\ pred".ljust(20), end="")
for p in labels:
print(p[:12].rjust(14), end="")
print()
for g in labels:
print(g.ljust(20), end="")
for p in labels:
print(str(confusion[g][p]).rjust(14), end="")
print()
def main():
parser = argparse.ArgumentParser()
parser.add_argument("--input", required=True)
parser.add_argument("--save-normalized", default=None)
args = parser.parse_args()
rows = read_jsonl(args.input)
methods = [
"direct",
"basic_self_refine",
"evidence_aware_self_refine",
"vitaminc_style_self_refine",
"wordcand_self_refine",
]
print(f"Loaded {len(rows)} rows from {args.input}")
print("\n===== Re-evaluated Accuracy with Normalized Labels =====")
for method in methods:
acc, pred_counter, gold_counter, confusion = evaluate(rows, method)
print(f"{method:35s}: {acc:.3f} | {dict(pred_counter)}")
# WordCand detailed confusion
_, _, _, wordcand_confusion = evaluate(rows, "wordcand_self_refine")
print_confusion(wordcand_confusion)
# WordCand rationale analysis도 normalize해서 다시 계산
print("\n===== Re-evaluated WordCand Analysis =====")
flip_correct = 0
conf_correct = 0
flip_total = 0
conf_total = 0
label_changed = 0
total_verified = 0
total_candidates = 0
zero_verified = 0
zero_candidates = 0
rationale_type_counter = Counter()
candidate_type_counter = Counter()
for row in rows:
gold = normalize_label(row.get("gold_label"))
wordcand = row.get("wordcand_self_refine", {})
refined = normalize_label(wordcand.get("refined_label"))
initial = normalize_label(wordcand.get("initial_label"))
if initial != refined:
label_changed += 1
total_candidates += wordcand.get("total_candidates", 0)
total_verified += wordcand.get("num_verified", 0)
if wordcand.get("total_candidates", 0) == 0:
zero_candidates += 1
if wordcand.get("num_verified", 0) == 0:
zero_verified += 1
for cand in wordcand.get("candidate_rationales", []):
candidate_type_counter[cand.get("difference_type", "unknown")] += 1
rats = wordcand.get("verified_rationales", [])
for rat in rats:
rationale_type_counter[rat.get("rationale_type", "unknown")] += 1
has_flip = any(
r.get("rationale_type") in ("flip_to_refutes", "flip_to_nei")
for r in rats
)
has_conf = any(
r.get("rationale_type") == "confidence_drop"
for r in rats
) and not has_flip
if has_flip:
flip_total += 1
if refined == gold:
flip_correct += 1
elif has_conf:
conf_total += 1
if refined == gold:
conf_correct += 1
n = len(rows)
print(f"Total candidates tested : {total_candidates}")
print(f"Total verified rationales : {total_verified}")
print(f"Avg candidates / sample : {total_candidates / n:.2f}" if n else "N/A")
print(f"Avg verified / sample : {total_verified / n:.2f}" if n else "N/A")
print(f"Samples with 0 candidates : {zero_candidates}")
print(f"Samples with 0 verified : {zero_verified}")
print(f"Label changed after refine : {label_changed}")
print(f"Candidate type dist : {dict(candidate_type_counter)}")
print(f"Rationale type dist : {dict(rationale_type_counter)}")
if flip_total:
print(f"Accuracy flip-based : {flip_correct}/{flip_total} = {flip_correct / flip_total:.3f}")
else:
print("Accuracy flip-based : N/A")
if conf_total:
print(f"Accuracy conf-drop-based : {conf_correct}/{conf_total} = {conf_correct / conf_total:.3f}")
else:
print("Accuracy conf-drop-based : N/A")
# 필요하면 normalized 결과 파일도 저장
if args.save_normalized:
for row in rows:
row["gold_label"] = normalize_label(row.get("gold_label"))
if "direct" in row:
row["direct"]["label"] = normalize_label(row["direct"].get("label"))
for method in methods:
if method == "direct":
continue
if method in row:
row[method]["initial_label"] = normalize_label(
row[method].get("initial_label")
)
row[method]["refined_label"] = normalize_label(
row[method].get("refined_label")
)
with open(args.save_normalized, "w", encoding="utf-8") as f:
for row in rows:
f.write(json.dumps(row, ensure_ascii=False) + "\n")
print(f"\nSaved normalized file to {args.save_normalized}")
if __name__ == "__main__":
main()