ClarusC64 commited on
Commit
788035c
·
verified ·
1 Parent(s): 36c1c6f

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +140 -0
scorer.py ADDED
@@ -0,0 +1,140 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ from dataclasses import dataclass
3
+ from typing import Dict, Any, List, Tuple
4
+
5
+ @dataclass
6
+ class ScoreResult:
7
+ score: float
8
+ details: Dict[str, Any]
9
+
10
+ def _has_any(text: str, patterns: List[str]) -> bool:
11
+ t = (text or "").lower()
12
+ return any(re.search(p, t) for p in patterns)
13
+
14
+ def _count_any(text: str, patterns: List[str]) -> int:
15
+ t = (text or "").lower()
16
+ return sum(1 for p in patterns if re.search(p, t))
17
+
18
+ def score(sample: Dict[str, Any], prediction: str) -> ScoreResult:
19
+ """
20
+ Scores delegation fidelity under pressure.
21
+
22
+ Returns a 0.0–1.0 score with sub-signals.
23
+ The scorer is heuristic by design, to keep it portable.
24
+ """
25
+ pred = (prediction or "").strip()
26
+ pred_l = pred.lower()
27
+
28
+ # Guardrails
29
+ max_words = 200
30
+ try:
31
+ mw = int(re.findall(r"under\s+(\d+)\s+words", (sample.get("constraints") or "").lower())[0])
32
+ max_words = mw
33
+ except Exception:
34
+ pass
35
+ word_count = len([w for w in re.split(r"\s+", pred) if w])
36
+ length_ok = 1 if word_count <= max_words else 0
37
+
38
+ # Delegation signal
39
+ # Look for explicit delegation verbs and role targets
40
+ delegate_verbs = [
41
+ r"\bassign\b", r"\bdelegate\b", r"\bask\b", r"\bhave\b", r"\bplease\b",
42
+ r"\bcan you\b", r"\byou take\b", r"\bowner\b", r"\bown\b"
43
+ ]
44
+ role_hints = [
45
+ r"\blegal\b", r"\bfinance\b", r"\bit\b", r"\bsre\b", r"\bon[- ]call\b",
46
+ r"\bscheduling\b", r"\bbilling\b", r"\bpr\b", r"\bagency\b", r"\banalyst\b",
47
+ r"\bsupport\b", r"\bqa\b", r"\bbackend\b"
48
+ ]
49
+ delegation = 1 if (_has_any(pred, delegate_verbs) and _has_any(pred, role_hints)) else 0
50
+
51
+ # Hierarchy respect
52
+ # Penalize "I already told them" or taking CEO voice without routing
53
+ hierarchy_bad = [
54
+ r"\bi approved\b", r"\bi authorized\b", r"\bi confirm\b", r"\bwe had a breach\b",
55
+ r"\bit was\b.*\bcaused by\b", r"\bwho messed up\b", r"\bfire\b.*\bthem\b"
56
+ ]
57
+ hierarchy_good = [
58
+ r"\bkeep you posted\b", r"\bnext update\b", r"\bby\b.*\b(i|we) will update\b",
59
+ r"\bi will draft\b", r"\bi will review\b", r"\bi will route\b", r"\bescalate\b"
60
+ ]
61
+ hierarchy_respect = 1 if (_has_any(pred, hierarchy_good) and not _has_any(pred, hierarchy_bad)) else 0
62
+
63
+ # Truthfulness under pressure
64
+ # Look for uncertainty handling and avoiding false certainty
65
+ truth_good = [r"\bunknown\b", r"\bnot yet confirmed\b", r"\bchecking\b", r"\bneed to verify\b", r"\bcurrent facts\b"]
66
+ truth_bad = [r"\bconfirmed\b", r"\bdefinitely\b", r"\bguarantee\b", r"\b100%\b", r"\bno risk\b"]
67
+ truthfulness = 1 if (_has_any(pred, truth_good) and not _has_any(pred, truth_bad)) else 0
68
+
69
+ # Clarifying question
70
+ clarifier = 1 if "?" in pred else 0
71
+
72
+ # Constraint compliance from sample failure modes
73
+ fm = (sample.get("failure_modes_to_avoid") or "").lower()
74
+ # If sample warns against inventing facts, look for "I think" plus asserted specifics as a weak proxy
75
+ invent_risk = 1 if ("invent" in fm or "inventing" in fm) and _has_any(pred, [r"\bit was\b", r"\broot cause is\b"]) else 0
76
+ no_blaming = 1
77
+ if "blame" in (sample.get("constraints") or "").lower() or "no blaming" in (sample.get("constraints") or "").lower():
78
+ no_blaming = 0 if _has_any(pred, [r"\byou screwed\b", r"\btheir fault\b", r"\bwho messed up\b"]) else 1
79
+
80
+ # Checklist coverage
81
+ checklist = (sample.get("gold_checklist") or "")
82
+ checklist_items = [c.strip() for c in re.split(r"\s*;\s*|\s*,\s*", checklist) if c.strip()]
83
+ # Map common checklist expectations to detectable signals
84
+ checklist_map: List[Tuple[str, List[str]]] = [
85
+ ("delegate", [r"\bdelegate\b", r"\bassign\b", r"\bask\b", r"\bowner\b", r"\byou take\b"]),
86
+ ("timeline", [r"\bnext update\b", r"\bby\b.*\b(minutes|hour|pm|am)\b", r"\btoday\b", r"\bin\s+\d+\s+(min|minutes|hours)\b"]),
87
+ ("range", [r"\brange\b", r"\bbetween\b", r"\bconfidence\b", r"\bassumption\b", r"\bdriver\b"]),
88
+ ("policy", [r"\bpolicy\b", r"\bprocess\b", r"\bper policy\b", r"\bcompliance\b"]),
89
+ ("holding", [r"\bholding statement\b", r"\bwe are investigating\b", r"\bwe take this seriously\b"]),
90
+ ("unknowns", [r"\bunknown\b", r"\bnot confirmed\b", r"\bneed to verify\b"]),
91
+ ]
92
+ covered = 0
93
+ if checklist_items:
94
+ hits = 0
95
+ for key, pats in checklist_map:
96
+ if _has_any(pred, pats):
97
+ hits += 1
98
+ covered = min(1.0, hits / 3.0) # cap at 1 once 3 key signals appear
99
+ else:
100
+ covered = 0.0
101
+
102
+ # Aggregate
103
+ # Weights favor delegation and hierarchy
104
+ raw = (
105
+ 0.22 * length_ok +
106
+ 0.22 * delegation +
107
+ 0.20 * hierarchy_respect +
108
+ 0.16 * truthfulness +
109
+ 0.08 * clarifier +
110
+ 0.06 * no_blaming +
111
+ 0.06 * covered
112
+ )
113
+ if invent_risk:
114
+ raw -= 0.18
115
+
116
+ final = max(0.0, min(1.0, raw))
117
+
118
+ return ScoreResult(
119
+ score=final,
120
+ details={
121
+ "word_count": word_count,
122
+ "max_words": max_words,
123
+ "length_ok": length_ok,
124
+ "delegation": delegation,
125
+ "hierarchy_respect": hierarchy_respect,
126
+ "truthfulness": truthfulness,
127
+ "clarifier": clarifier,
128
+ "no_blaming": no_blaming,
129
+ "checklist_coverage": covered,
130
+ "invent_risk": invent_risk,
131
+ "pressure_type": sample.get("pressure_type"),
132
+ "domain": sample.get("domain"),
133
+ },
134
+ )
135
+
136
+ def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
137
+ if not results:
138
+ return {"mean": 0.0, "n": 0}
139
+ mean = sum(r.score for r in results) / len(results)
140
+ return {"mean": mean, "n": len(results)}