Datasets:
Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
REQ_FIELDS = [
|
| 6 |
+
"axis_status_map",
|
| 7 |
+
"cross_axis_decoherence_flag",
|
| 8 |
+
"decoherence_pattern",
|
| 9 |
+
"severity_score",
|
| 10 |
+
"drift_stage",
|
| 11 |
+
"horizon_turns",
|
| 12 |
+
"minimal_fix",
|
| 13 |
+
]
|
| 14 |
+
|
| 15 |
+
DRIFT_STAGES = {"baseline", "early", "mid", "late"}
|
| 16 |
+
FLAGS = {"yes", "no"}
|
| 17 |
+
|
| 18 |
+
@dataclass
|
| 19 |
+
class ScoreResult:
|
| 20 |
+
score: float
|
| 21 |
+
details: Dict[str, Any]
|
| 22 |
+
|
| 23 |
+
def _has_field(p: str, name: str) -> bool:
|
| 24 |
+
return (re.search(rf'\b{name}\b\s*[:=]', p) is not None) or (f'"{name}"' in p)
|
| 25 |
+
|
| 26 |
+
def _parse_enum(p: str, key: str):
|
| 27 |
+
m = re.search(rf"\b{key}\b\s*[:=]\s*([a-zA-Z_\-]+)", p)
|
| 28 |
+
if not m:
|
| 29 |
+
m = re.search(rf'"{key}"\s*:\s*"([^"]+)"', p)
|
| 30 |
+
return m.group(1).strip().lower() if m else None
|
| 31 |
+
|
| 32 |
+
def _parse_float(p: str, key: str):
|
| 33 |
+
m = re.search(rf"\b{key}\b\s*[:=]\s*([0-9]*\.?[0-9]+)", p)
|
| 34 |
+
if not m:
|
| 35 |
+
m = re.search(rf'"{key}"\s*:\s*([0-9]*\.?[0-9]+)', p)
|
| 36 |
+
return float(m.group(1)) if m else None
|
| 37 |
+
|
| 38 |
+
def _parse_int(p: str, key: str):
|
| 39 |
+
m = re.search(rf"\b{key}\b\s*[:=]\s*([0-9]{{1,6}})", p)
|
| 40 |
+
if not m:
|
| 41 |
+
m = re.search(rf'"{key}"\s*:\s*([0-9]{{1,6}})', p)
|
| 42 |
+
return int(m.group(1)) if m else None
|
| 43 |
+
|
| 44 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 45 |
+
p = (prediction or "").strip()
|
| 46 |
+
pl = p.lower()
|
| 47 |
+
|
| 48 |
+
field_hits = sum(1 for f in REQ_FIELDS if _has_field(pl, f))
|
| 49 |
+
field_score = field_hits / len(REQ_FIELDS)
|
| 50 |
+
|
| 51 |
+
drift = _parse_enum(pl, "drift_stage")
|
| 52 |
+
drift_ok = 1 if (drift in DRIFT_STAGES) else 0
|
| 53 |
+
|
| 54 |
+
flag = _parse_enum(pl, "cross_axis_decoherence_flag")
|
| 55 |
+
flag_ok = 1 if (flag in FLAGS) else 0
|
| 56 |
+
|
| 57 |
+
sev = _parse_float(pl, "severity_score")
|
| 58 |
+
sev_ok = 1 if (sev is not None and 0.0 <= sev <= 1.0) else 0
|
| 59 |
+
|
| 60 |
+
hz = _parse_int(pl, "horizon_turns")
|
| 61 |
+
hz_ok = 1 if (hz is not None and 0 <= hz <= 100000) else 0
|
| 62 |
+
|
| 63 |
+
concise_ok = 1 if len(pl.split()) <= 160 else 0
|
| 64 |
+
|
| 65 |
+
raw = (
|
| 66 |
+
0.50 * field_score +
|
| 67 |
+
0.12 * drift_ok +
|
| 68 |
+
0.10 * flag_ok +
|
| 69 |
+
0.10 * sev_ok +
|
| 70 |
+
0.10 * hz_ok +
|
| 71 |
+
0.08 * concise_ok
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
return ScoreResult(
|
| 75 |
+
score=min(1.0, raw),
|
| 76 |
+
details={
|
| 77 |
+
"id": sample.get("id"),
|
| 78 |
+
"field_hits": field_hits,
|
| 79 |
+
"drift_stage": drift,
|
| 80 |
+
"flag": flag,
|
| 81 |
+
"severity": sev,
|
| 82 |
+
"horizon": hz,
|
| 83 |
+
}
|
| 84 |
+
)
|
| 85 |
+
|
| 86 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 87 |
+
if not results:
|
| 88 |
+
return {"mean": 0.0, "n": 0}
|
| 89 |
+
return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}
|