Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Dict, Any, List
|
| 4 |
+
|
| 5 |
+
@dataclass
|
| 6 |
+
class ScoreResult:
|
| 7 |
+
score: float
|
| 8 |
+
details: Dict[str, Any]
|
| 9 |
+
|
| 10 |
+
def _has(text: str, pats: List[str]) -> bool:
|
| 11 |
+
t = (text or "").lower()
|
| 12 |
+
return any(re.search(p, t) for p in pats)
|
| 13 |
+
|
| 14 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 15 |
+
pred = (prediction or "").strip()
|
| 16 |
+
words = len([w for w in re.split(r"\s+", pred) if w])
|
| 17 |
+
|
| 18 |
+
max_words = 200
|
| 19 |
+
m = re.search(r"under\s+(\d+)\s+words", (sample.get("constraints") or "").lower())
|
| 20 |
+
if m:
|
| 21 |
+
max_words = int(m.group(1))
|
| 22 |
+
length_ok = 1 if words <= max_words else 0
|
| 23 |
+
|
| 24 |
+
# Intent preservation
|
| 25 |
+
intent_good = [
|
| 26 |
+
r"\bfirst\b", r"\bthe intent is\b", r"\bwhat you asked\b",
|
| 27 |
+
r"\bkeeping to\b", r"\banswering your\b"
|
| 28 |
+
]
|
| 29 |
+
intent_bad = [
|
| 30 |
+
r"\blet me also\b", r"\bby the way\b", r"\bseparately\b",
|
| 31 |
+
r"\bhere's a draft\b", r"\bwe could also\b"
|
| 32 |
+
]
|
| 33 |
+
intent = 1 if (_has(pred, intent_good) and not _has(pred, intent_bad)) else 0
|
| 34 |
+
|
| 35 |
+
# Scope control
|
| 36 |
+
scope = 1 if _has(pred, [r"\bpark\b", r"\bcan take that next\b", r"\bseparate task\b", r"\bafter this\b"]) else 0
|
| 37 |
+
|
| 38 |
+
# Precision discipline
|
| 39 |
+
precision = 1 if _has(pred, [r"\brange\b", r"\bconditions\b", r"\bcriteria\b"]) else 0
|
| 40 |
+
|
| 41 |
+
# Action focus
|
| 42 |
+
action = 1 if _has(pred, [r"\bnext\b", r"\bdecision\b", r"\bapprove\b", r"\bdeny\b", r"\btest\b"]) else 0
|
| 43 |
+
|
| 44 |
+
raw = (
|
| 45 |
+
0.30 * length_ok +
|
| 46 |
+
0.30 * intent +
|
| 47 |
+
0.15 * scope +
|
| 48 |
+
0.15 * precision +
|
| 49 |
+
0.10 * action
|
| 50 |
+
)
|
| 51 |
+
final = max(0.0, min(1.0, raw))
|
| 52 |
+
|
| 53 |
+
return ScoreResult(
|
| 54 |
+
score=final,
|
| 55 |
+
details={
|
| 56 |
+
"word_count": words,
|
| 57 |
+
"max_words": max_words,
|
| 58 |
+
"length_ok": length_ok,
|
| 59 |
+
"intent_preservation": intent,
|
| 60 |
+
"scope_control": scope,
|
| 61 |
+
"precision": precision,
|
| 62 |
+
"action_focus": action,
|
| 63 |
+
"intent_pressure": sample.get("intent_pressure"),
|
| 64 |
+
"domain": sample.get("domain"),
|
| 65 |
+
},
|
| 66 |
+
)
|
| 67 |
+
|
| 68 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 69 |
+
if not results:
|
| 70 |
+
return {"mean": 0.0, "n": 0}
|
| 71 |
+
return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}
|