Create scorer.py
Browse files
scorer.py
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import re
|
| 2 |
+
from dataclasses import dataclass
|
| 3 |
+
from typing import Dict, Any, List
|
| 4 |
+
|
| 5 |
+
LABELS = {
|
| 6 |
+
"consistent",
|
| 7 |
+
"inconsistent-duplication",
|
| 8 |
+
"inconsistent-teleport",
|
| 9 |
+
"inconsistent-stale-entity",
|
| 10 |
+
"inconsistent-wrong-identity",
|
| 11 |
+
"inconsistent-impossible-state",
|
| 12 |
+
"inconsistent-missing-object",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
@dataclass
|
| 16 |
+
class ScoreResult:
|
| 17 |
+
score: float
|
| 18 |
+
details: Dict[str, Any]
|
| 19 |
+
|
| 20 |
+
def _has(t: str, pats: List[str]) -> bool:
|
| 21 |
+
t = (t or "").lower()
|
| 22 |
+
return any(re.search(p, t) for p in pats)
|
| 23 |
+
|
| 24 |
+
def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
|
| 25 |
+
pred = (prediction or "").strip()
|
| 26 |
+
words_ok = len(pred.split()) <= 260
|
| 27 |
+
|
| 28 |
+
label_ok = 1 if any(lbl in pred for lbl in LABELS) else 0
|
| 29 |
+
compare_ref = 1 if _has(pred, [r"model", r"world model", r"observed", r"perception", r"memory"]) else 0
|
| 30 |
+
inconsistency_ref = 1 if _has(pred, [r"inconsist", r"dup", r"teleport", r"stale", r"missing", r"contradic", r"swap"]) else 0
|
| 31 |
+
|
| 32 |
+
raw = (
|
| 33 |
+
0.25 * int(words_ok) +
|
| 34 |
+
0.40 * label_ok +
|
| 35 |
+
0.20 * compare_ref +
|
| 36 |
+
0.15 * inconsistency_ref
|
| 37 |
+
)
|
| 38 |
+
final = max(0.0, min(1.0, raw))
|
| 39 |
+
|
| 40 |
+
return ScoreResult(
|
| 41 |
+
score=final,
|
| 42 |
+
details={
|
| 43 |
+
"words_ok": words_ok,
|
| 44 |
+
"label_ok": label_ok,
|
| 45 |
+
"compare_ref": compare_ref,
|
| 46 |
+
"inconsistency_ref": inconsistency_ref,
|
| 47 |
+
"consistency_pressure": sample.get("consistency_pressure"),
|
| 48 |
+
"scenario": sample.get("scenario"),
|
| 49 |
+
}
|
| 50 |
+
)
|
| 51 |
+
|
| 52 |
+
def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
|
| 53 |
+
if not results:
|
| 54 |
+
return {"mean": 0.0, "n": 0}
|
| 55 |
+
return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}
|