ClarusC64 commited on
Commit
3dea79e
·
verified ·
1 Parent(s): 3e09d5d

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +52 -0
scorer.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from dataclasses import dataclass
2
+ from typing import Dict, Any, List
3
+ import re
4
+
5
+ REQ = [
6
+ "failure_surface_type",
7
+ "subgroup_affected",
8
+ "intervention_route",
9
+ "recalibration_needed",
10
+ "protocol_reset_plan",
11
+ "escalation_priority",
12
+ "confidence_score",
13
+ ]
14
+
15
+ @dataclass
16
+ class ScoreResult:
17
+ score: float
18
+ details: Dict[str, Any]
19
+
20
+ def _yesno(text: str, key: str) -> bool:
21
+ return bool(re.search(rf"{key}\s*[:=]\s*(yes|no)\b", text))
22
+
23
+ def _float01(text: str, key: str) -> bool:
24
+ return bool(re.search(rf"{key}\s*[:=]\s*(0\.\d+|1\.0)\b", text))
25
+
26
+ def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
27
+ p = (prediction or "").lower()
28
+ words_ok = len(p.split()) <= 1200
29
+
30
+ hits = sum(1 for k in REQ if k in p)
31
+
32
+ recal_ok = int(_yesno(p, "recalibration_needed"))
33
+ conf_ok = int(_float01(p, "confidence_score"))
34
+
35
+ priority_ok = int("escalation_priority" in p and any(x in p for x in [
36
+ "low", "medium", "high", "critical"
37
+ ]))
38
+
39
+ raw = (
40
+ 0.20 * int(words_ok) +
41
+ 0.55 * (hits / len(REQ)) +
42
+ 0.10 * recal_ok +
43
+ 0.10 * conf_ok +
44
+ 0.05 * priority_ok
45
+ )
46
+
47
+ return ScoreResult(score=min(1.0, raw), details={"id": sample.get("id"), "hits": hits})
48
+
49
+ def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
50
+ if not results:
51
+ return {"mean": 0.0, "n": 0}
52
+ return {"mean": sum(r.score for r in results)/len(results), "n": len(results)}