File size: 3,098 Bytes
62831c3 | 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 | """Parse MetaMathQA rows into steps + final answer (no LLM)."""
from __future__ import annotations
import hashlib
import re
from typing import Any
CALC_RE = re.compile(r"<<.*?>>")
# MetaMath often ends with "The answer is: X" or GSM-style #### X
FINAL_PATTERNS = [
re.compile(r"The answer is:\s*(.+?)\s*$", re.IGNORECASE | re.MULTILINE),
re.compile(r"The answer is\s+(.+?)\s*$", re.IGNORECASE | re.MULTILINE),
re.compile(r"####\s*(.+)"),
]
def family_id_from_original(original_question: str) -> str:
h = hashlib.sha1(original_question.strip().encode("utf-8")).hexdigest()[:12]
return f"mm_{h}"
def extract_final_answer(response: str) -> str | None:
text = response.strip()
for pat in FINAL_PATTERNS:
m = pat.search(text)
if not m:
continue
ans = m.group(1).strip()
# Trim trailing punctuation / quotes
ans = ans.strip().rstrip(".")
ans = ans.strip("`\"'")
# Prefer last numeric token if the capture is noisy
num = re.search(r"[-+]?\d[\d,]*(?:\.\d+)?", ans)
if num and (ans == num.group(0) or len(ans) <= 32):
return num.group(0).replace(",", "")
if ans:
return ans
return None
def split_steps(response: str) -> list[str]:
"""Split CoT body (before final-answer line) into steps."""
body = response.strip()
for pat in FINAL_PATTERNS:
m = pat.search(body)
if m:
body = body[: m.start()].strip()
break
cleaned_lines: list[str] = []
for line in body.splitlines():
line = line.strip()
if not line:
continue
# Drop leading "Step N:" / "N." numbering noise into content
line = re.sub(r"^(?:step\s*)?\d+[\.:)\]]\s*", "", line, flags=re.IGNORECASE)
line = CALC_RE.sub("", line).strip()
line = re.sub(r"\s+", " ", line)
if line:
cleaned_lines.append(line)
if not cleaned_lines:
return []
if len(cleaned_lines) >= 2:
return cleaned_lines
text = cleaned_lines[0]
parts = [p.strip() for p in re.split(r"(?<=[.!?])\s+", text) if p.strip()]
return parts if len(parts) >= 2 else [text]
def parse_metamath_row(
row: dict[str, Any],
*,
instance_id: str,
split: str,
) -> dict[str, Any] | None:
query = (row.get("query") or "").strip()
response = (row.get("response") or "").strip()
original = (row.get("original_question") or query).strip()
typ = (row.get("type") or "").strip()
if not query or not response:
return None
steps = split_steps(response)
final = extract_final_answer(response)
if not final or not steps:
return None
return {
"family_id": family_id_from_original(original),
"instance_id": instance_id,
"split": split,
"source": "meta-math/MetaMathQA",
"type": typ,
"question": query,
"original_question": original,
"steps": steps,
"final_answer": final,
"answer_raw": response,
"n_steps": len(steps),
}
|