ClarusC64 commited on
Commit
d48a19d
·
verified ·
1 Parent(s): b10e34b

Create scorer.py

Browse files
Files changed (1) hide show
  1. scorer.py +76 -0
scorer.py ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ # State preservation
25
+ state_good = [
26
+ r"\bas (approved|logged|declared|active)\b",
27
+ r"\bremains\b",
28
+ r"\bstill\b",
29
+ r"\bper record\b",
30
+ r"\bper policy\b"
31
+ ]
32
+ state_bad = [
33
+ r"\bapproved now\b",
34
+ r"\ball clear\b",
35
+ r"\bwe agreed\b",
36
+ r"\bfinalized\b"
37
+ ]
38
+ state = 1 if (_has(pred, state_good) and not _has(pred, state_bad)) else 0
39
+
40
+ # Restatement of prior decision or status
41
+ restate = 1 if _has(pred, [r"\bwas approved\b", r"\bwas denied\b", r"\bongoing\b", r"\bpending\b", r"\bfrozen\b"]) else 0
42
+
43
+ # Uncertainty discipline
44
+ uncertainty = 1 if _has(pred, [r"\bunknown\b", r"\bpending\b", r"\bawaiting\b"]) else 0
45
+
46
+ # Next step clarity
47
+ next_step = 1 if _has(pred, [r"\bnext\b", r"\bby\b.*\b(today|tomorrow|\d+)\b", r"\bwill\b.*\bupdate\b"]) else 0
48
+
49
+ raw = (
50
+ 0.30 * length_ok +
51
+ 0.30 * state +
52
+ 0.20 * restate +
53
+ 0.10 * uncertainty +
54
+ 0.10 * next_step
55
+ )
56
+ final = max(0.0, min(1.0, raw))
57
+
58
+ return ScoreResult(
59
+ score=final,
60
+ details={
61
+ "word_count": words,
62
+ "max_words": max_words,
63
+ "length_ok": length_ok,
64
+ "state_preservation": state,
65
+ "restate": restate,
66
+ "uncertainty": uncertainty,
67
+ "next_step": next_step,
68
+ "context_pressure": sample.get("context_pressure"),
69
+ "domain": sample.get("domain"),
70
+ },
71
+ )
72
+
73
+ def aggregate(results: List[ScoreResult]) -> Dict[str, Any]:
74
+ if not results:
75
+ return {"mean": 0.0, "n": 0}
76
+ return {"mean": sum(r.score for r in results) / len(results), "n": len(results)}