infinitylogesh's picture
Upload folder using huggingface_hub
371b96b verified
Raw History Blame Contribute Delete
4.27 kB
"""Structural defect gate + question parser, extracted verbatim from annotate_v2.py so other
drivers can reuse them without duplicating the logic. Measured on the first 362 generated questions:
12% had near-duplicate options and 5% a gold option unsupported by the source material."""
import collections, json, re
_WORD = re.compile(r"[a-z0-9']{4,}")
_STOP = set("""that this with from into over under after before then than which what were was
been being have has had will would could should there here they them their your mine ours
first second third fourth i my me the and or of to in on at for a an it its is are be""".split())
def _content(s):
return {w for w in _WORD.findall(re.sub(r"^\s*[A-D][\.\)]\s*", "", s or "").lower())
if w not in _STOP}
def structural_issues(q, desc):
"""Shape defects a blind-attack filter does not reliably catch.
Measured on the first 362 questions: 12% had two near-duplicate options and 5% had a gold
option whose content never appeared in the description. Compound questions are the main
offender -- built as a factorial grid over only TWO values per slot, they collapse into a
2-way guess ("'Luyben Av' + minivan" vs "'Luyben Av' + van")."""
issues = []
opts = q.get("options") or []
gold = str(q.get("answer", "")).upper()[:1]
if len(opts) != 4 or gold not in "ABCD":
return ["malformed"]
bodies = [re.sub(r"^\s*[A-D][\.\)]\s*", "", o).strip() for o in opts]
sets = [_content(b) for b in bodies]
for i in range(4):
for j in range(i + 1, 4):
if sets[i] and sets[j]:
jac = len(sets[i] & sets[j]) / len(sets[i] | sets[j])
if jac > 0.8:
issues.append("dup_options")
break
if issues:
break
# a compound option set must not reuse the same component across most options
if len(bodies) == 4:
counts = collections.Counter()
for b in bodies:
for part in re.split(r"\s+and\s+|,\s+", b):
p = " ".join(sorted(_content(part)))
if p:
counts[p] += 1
if counts and max(counts.values()) >= 3:
issues.append("repeated_component")
gi = "ABCD".index(gold)
others = [len(b) for k, b in enumerate(bodies) if k != gi]
if abs(len(bodies[gi]) - (sum(others) / 3)) > 30:
issues.append("gold_len_outlier")
dw = _content(desc)
gw = sets[gi]
if gw and len(gw - dw) / len(gw) > 0.7:
issues.append("gold_unsupported_by_desc")
return issues
def parse_questions(txt):
"""Reasoning models often wrap the JSON in prose or a ```json fence despite response_format,
and sometimes run out of budget mid-object. Recover what we can instead of dropping the round."""
if not txt:
return []
qs = None
for cand in (txt,
(re.search(r"```(?:json)?\s*(.*?)```", txt, re.S) or [None, None])[1],
(re.search(r"\{.*\}", txt, re.S) or [None])[0]):
if not cand:
continue
try:
obj = json.loads(cand)
if isinstance(obj, dict):
qs = obj.get("questions")
elif isinstance(obj, list):
qs = obj
if qs:
break
except Exception:
continue
if not qs:
# last resort: pull individual question objects out of a truncated array
qs = []
for m in re.finditer(r'\{[^{}]*"question"\s*:.*?\}(?=\s*[,\]])', txt, re.S):
try:
qs.append(json.loads(m.group(0)))
except Exception:
pass
if not qs:
return []
out = []
for q in qs:
if not isinstance(q, dict) or not q.get("question"):
continue
opts = q.get("options") or []
if len(opts) != 4 or str(q.get("answer", "")).upper()[:1] not in "ABCD":
continue
out.append(q)
return out
BATCH_SYS = ("Answer each numbered multiple-choice question independently. "
'Reply with ONLY JSON mapping question number to letter, e.g. {"1":"A","2":"C"}. '
"Answer every question; if unsure, still give your best single letter.")