Datasets:
Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from dataclasses import dataclass
|
| 2 |
+
from typing import Dict, Any, List
|
| 3 |
+
import re
|
| 4 |
+
|
| 5 |
+
@dataclass
|
| 6 |
+
class ScoreResult:
|
| 7 |
+
score: float
|
| 8 |
+
details: Dict[str, Any]
|
| 9 |
+
|
| 10 |
+
# Expect the model to output these fields in text
|
| 11 |
+
REQ = ["interface_coherence_score", "baseline_failure_margin", "signal_paths"]
|
| 12 |
+
|
| 13 |
+
_float_re = re.compile(r"(interface_coherence_score|baseline_failure_margin)\s*[:=]\s*(0(\.\d+)?|1(\.0+)?)", re.I)
|
| 14 |
+
|
| 15 |
+
def _has_paths(text: str) -> bool:
|
| 16 |
+
# Accept either "A:..|B:.." style or "A:..>..|B:..>.." style
|
| 17 |
+
t = text.replace(" ", "")
|
| 18 |
+
return ("A:" in t and "B:" in t and ">" in t) or ("signal_paths" in t and ">" in t)
|
| 19 |
+
|
| 20 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 21 |
+
p = (prediction or "").strip()
|
| 22 |
+
pl = p.lower()
|
| 23 |
+
words_ok = len(p.split()) <= 900
|
| 24 |
+
|
| 25 |
+
field_words = sum(1 for k in REQ if k in pl)
|
| 26 |
+
float_hits = len(_float_re.findall(p))
|
| 27 |
+
has_paths = _has_paths(p)
|
| 28 |
+
|
| 29 |
+
raw = (
|
| 30 |
+
0.25 * int(words_ok) +
|
| 31 |
+
0.35 * min(1.0, field_words / len(REQ)) +
|
| 32 |
+
0.30 * min(1.0, float_hits / 2) +
|
| 33 |
+
0.10 * int(has_paths)
|
| 34 |
+
)
|
| 35 |
+
return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "field_word_hits": field_words})
|
| 36 |
+
|
| 37 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 38 |
+
if not results:
|
| 39 |
+
return {"mean": 0.0, "n": 0}
|
| 40 |
+
return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}
|