ClarusC64's picture
Create scorer.py
58e0144 verified
Raw
History Blame Contribute Delete
1.39 kB
from dataclasses import dataclass
from typing import Dict, Any, List
import re
@dataclass
class ScoreResult:
score: float
details: Dict[str, Any]
# Expect the model to output these fields in text
REQ = ["interface_coherence_score", "baseline_failure_margin", "signal_paths"]
_float_re = re.compile(r"(interface_coherence_score|baseline_failure_margin)\s*[:=]\s*(0(\.\d+)?|1(\.0+)?)", re.I)
def _has_paths(text: str) -> bool:
# Accept either "A:..|B:.." style or "A:..>..|B:..>.." style
t = text.replace(" ", "")
return ("A:" in t and "B:" in t and ">" in t) or ("signal_paths" in t and ">" in t)
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
p = (prediction or "").strip()
pl = p.lower()
words_ok = len(p.split()) <= 900
field_words = sum(1 for k in REQ if k in pl)
float_hits = len(_float_re.findall(p))
has_paths = _has_paths(p)
raw = (
0.25 * int(words_ok) +
0.35 * min(1.0, field_words / len(REQ)) +
0.30 * min(1.0, float_hits / 2) +
0.10 * int(has_paths)
)
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "field_word_hits": field_words})
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
if not results:
return {"mean": 0.0, "n": 0}
return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}