-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjudge_parser.py
More file actions
79 lines (72 loc) · 2.8 KB
/
Copy pathjudge_parser.py
File metadata and controls
79 lines (72 loc) · 2.8 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
import re
import json
from typing import Dict, Tuple
# canonical metric keys we expect (extend if judge returns more)
METRICS = [
"Confusion Recognition",
"Adaptive Response",
"Learning Facilitation",
"Strategic Decision-Making",
"Engagement & Emotional Intelligence",
"Knowledge Pillar",
"Error Analysis",
"Adaptive Capability",
"Curriculum Awareness",
"Explanation Ability",
"Overall"
]
def parse_scores(text: str) -> Dict[str, float]:
"""
Attempts to parse scores from judge LLM output. Handles:
- markdown table rows
- bullet / hyphen lists like "- Confusion Recognition: 8/10"
- summary lines like "**Overall Effectiveness Score**: 8.7/10"
- also extracts **Winner** and **Margin** info if present
Returns a dict containing:
{
"scores": {metric -> score},
"winner": <winner model name or None>,
"margin": <margin text or None>
}
"""
scores = {}
# --- METRIC SCORE PARSING ---
for metric in METRICS:
# build flexible pattern: metric name then some chars then number
key_pattern = re.sub(r'&', r'&', re.escape(metric))
pattern = rf"{key_pattern}.*?(\d+(?:\.\d+)?)\s*/\s*10"
m = re.search(pattern, text, re.IGNORECASE | re.DOTALL)
if m:
scores[metric] = float(m.group(1))
# fallback: try to parse lines like "| Confusion Recognition | 8 |"
lines = text.splitlines()
for line in lines:
if '|' in line:
parts = [p.strip() for p in line.split('|') if p.strip()]
if parts:
last = parts[-1]
num_m = re.match(r'^(\d+(?:\.\d+)?)$', last)
if num_m:
metric_name = parts[0]
for mname in METRICS:
if metric_name.lower().startswith(mname.split()[0].lower()):
if mname not in scores:
scores[mname] = float(num_m.group(1))
# Extract overall score if present
overall_pat = re.search(r'Overall.*?Score.*?(\d+(?:\.\d+)?)\s*/\s*10', text, re.IGNORECASE)
if overall_pat:
scores["Overall"] = float(overall_pat.group(1))
# --- WINNER & MARGIN PARSING ---
# Example patterns:
# **Winner**: Response C (google/gemini-2.5-pro)
# **Margin**: Clear (highest overall score at 9.2/10 ...)
winner_match = re.search(r'\*\*Winner\*\*:\s*Response\s*[A-Z]\s*\(([^)]+)\)', text, re.IGNORECASE)
margin_match = re.search(r'\*\*Margin\*\*:\s*(.+)', text, re.IGNORECASE)
winner = winner_match.group(1).strip() if winner_match else None
margin = margin_match.group(1).strip() if margin_match else None
# Return combined dictionary
return {
"scores": scores,
"winner": winner,
"margin": margin
}