File size: 3,989 Bytes
4882451 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | """Deterministic grading for binary-reasoning-v1 items. Pure functions, no framework deps."""
import json
import re
JUDGE_FAMILIES = {"func_summary", "arch_semantics_qa", "isa_semantics_qa", "pe_import_reasoning", "pe_structure_qa"}
def norm_symbol(s: str) -> str:
s = s.strip().lower()
s = re.sub(r"^[*&]+", "", s)
s = re.sub(r"\(.*\)$", "", s).strip()
s = re.sub(r"\.+$", "", s) # sentence-final dot captured by token regex
return s
def _answer_symbol(answer) -> str:
if isinstance(answer, dict):
return answer.get("symbol", "")
return str(answer)
def grade_func_id_exact(reply: str, answer) -> float:
sym = norm_symbol(_answer_symbol(answer))
if not sym:
return 0.0
accepted = {sym}
if isinstance(answer, dict):
accepted |= {norm_symbol(a) for a in answer.get("aliases", []) if a}
toks = [norm_symbol(t) for t in re.findall(r"[A-Za-z_][A-Za-z0-9_.]+", reply)]
return 1.0 if accepted & set(toks) else 0.0
def grade_func_id_mc(reply: str, answer: dict) -> float:
correct = answer.get("correct", "")
opts = answer.get("options", [])
r = reply.strip()
m = re.search(r"\b([A-D])\b", r[:80], re.IGNORECASE)
if m:
idx = ord(m.group(1).upper()) - 65
if 0 <= idx < len(opts):
return float(norm_symbol(opts[idx]) == norm_symbol(correct))
names = [norm_symbol(o) for o in re.findall(r"[A-Za-z_][A-Za-z0-9_.]{3,}", r.lower())]
return 1.0 if norm_symbol(correct) in names else 0.0
def grade_tcfp(reply: str, answer: dict) -> float:
strs = answer.get("toolchain_strings", [])
if not strs:
return 0.0
hits = sum(1 for s in strs if s.lower() in reply.lower())
return hits / len(strs)
def needs_judge(family: str, answer) -> bool:
if family in JUDGE_FAMILIES:
return True
if family == "func_id_exact":
sym = _answer_symbol(answer)
# descriptive/inferred answers (contain prose) cannot be string-matched
return (" " in sym.strip()) or ("inferred" in sym.lower())
return False
def grade_agentic(reply: str, answer) -> float:
flag = _answer_symbol(answer)
return 1.0 if flag and flag in reply else 0.0
def grade_crossarch(reply: str, answer) -> float:
if isinstance(answer, str):
try:
answer = json.loads(answer)
except Exception:
answer = {"same": "true" in answer.lower(),
"symbol": re.sub(r'.*?"symbol"\s*:\s*"([^"]+)".*', r"\1", answer) if '"symbol"' in answer else ""}
want = bool(answer.get("same"))
r = reply.lower()
neg = bool(re.search(r"\bnot\s+(the\s+)?same\b|\bdifferent\b|\bno\b", r))
pos = not neg and bool(re.search(r"\bsame\b|\byes\b|\btrue\b|match", r))
if not (neg or pos):
return 0.0 # abstention / no clear stance
if pos != want:
return 0.0
if want:
sym = norm_symbol(answer.get("symbol", ""))
toks = [norm_symbol(t) for t in re.findall(r"[A-Za-z_][A-Za-z0-9_.]+", reply)]
if sym not in toks:
return 0.0
return 1.0
def grade_library_ident(reply: str, answer) -> float:
ans = _answer_symbol(answer)
toks = [t for t in re.split(r"\s+", ans) if len(t) >= 2]
r = reply.lower()
return 1.0 if all(t.lower() in r for t in toks) else 0.0
def grade_item(family: str, reply: str, answer) -> float:
if needs_judge(family, answer):
return 0.0 # handled by rubric judge in taskset.py
if family == "library_ident":
return grade_library_ident(reply, answer)
if family == "crossarch_equivalence":
return grade_crossarch(reply, answer)
if family == "agentic_flag_hunt":
return grade_agentic(reply, answer)
if family == "func_id_exact":
return grade_func_id_exact(reply, answer)
if family == "func_id_mc":
return grade_func_id_mc(reply, answer)
if family == "toolchain_fp":
return grade_tcfp(reply, answer)
return 0.0
|