OpenAI math manuscripts: LaTeX sources + OCR reasoning summaries (openai/math@adc7f12)
c6e5669 verified Download scripts/build_reasoning.py from ituperceptron/openai-math: direct link, hf CLI and curl.
- Browser
- Download file 2.97 kB
-
https://huggingface.co/datasets/ituperceptron/openai-math/resolve/main/scripts/build_reasoning.py
- Command line
-
hf download hf://datasets/ituperceptron/openai-math/scripts/build_reasoning.py
-
curl -L -o build_reasoning.py https://huggingface.co/datasets/ituperceptron/openai-math/resolve/main/scripts/build_reasoning.py
2.97 kB
| """`ocr_reasoning.py` sayfa çıktılarını belge başına birleştirip parquet üretir. | |
| python3 build_reasoning.py # -> data/reasoning_summaries.parquet | |
| """ | |
| import json | |
| import re | |
| from pathlib import Path | |
| import fitz | |
| import pandas as pd | |
| from build_papers import BLOB, SRC, OUT, parse_subjects, strip_comments, collapse | |
| TRACES = SRC / "reasoning_traces" | |
| OCR = Path(__file__).parent / "raw" / "ocr" | |
| SENTENCE_END = re.compile(r"[.!?:;)\]$`*]\s*$") | |
| BLOCK_START = re.compile(r"^(#|>|```|[-*+] |\d+\. |\||\$\$)") | |
| def readme_table() -> dict: | |
| """README 'Reasoning summaries' tablosu: slug -> (family, title).""" | |
| text = (SRC / "README.md").read_text(encoding="utf-8") | |
| rows = re.findall(r"^\| (\d{3}) \| \[(.+?)\]\(reasoning_traces/([^)]+)\.pdf\) \|$", text, re.M) | |
| return {slug: (fam, title) for fam, title, slug in rows} | |
| def join_pages(pages: list) -> str: | |
| out = pages[0] | |
| for page in pages[1:]: | |
| # Sayfa sonunda kapanıp sonraki sayfada yeniden açılan aynı kod bloğunu birleştir. | |
| if out.endswith("```") and page.startswith("```text"): | |
| out = out[:-3].rstrip("\n") + "\n" + page[len("```text"):].lstrip("\n") | |
| # Sayfa cümle ortasında bitiyorsa paragrafı sürdür (satır sonu tirelerinde boşluksuz). | |
| elif not SENTENCE_END.search(out) and not BLOCK_START.match(page): | |
| out = out.rstrip() + ("" if out.rstrip().endswith(("–", "-")) else " ") + page | |
| else: | |
| out = out.rstrip() + "\n\n" + page | |
| return re.sub(r"\n{3,}", "\n\n", out).strip() + "\n" | |
| def main(): | |
| table = readme_table() | |
| subjects = parse_subjects((SRC / "overview.tex").read_text(encoding="utf-8")) | |
| rows = [] | |
| for pdf in sorted(TRACES.glob("*.pdf")): | |
| with fitz.open(pdf) as doc: | |
| n = doc.page_count | |
| files = [OCR / pdf.stem / f"{i:03d}.json" for i in range(1, n + 1)] | |
| missing = [f.name for f in files if not f.exists()] | |
| if missing: | |
| raise SystemExit(f"{pdf.stem}: OCR eksik sayfalar {missing}") | |
| pages = [json.loads(f.read_text()) for f in files] | |
| tex = pdf.with_suffix(".tex") | |
| fam, title = table[pdf.stem] | |
| rows.append({ | |
| "family": fam, | |
| "subject": subjects[fam], | |
| "title": title, | |
| "slug": pdf.stem, | |
| "markdown": join_pages([p["markdown"] for p in pages]), | |
| "latex": collapse(strip_comments(tex.read_text(encoding="utf-8"))) if tex.exists() else None, | |
| "num_pages": n, | |
| "ocr_model": ",".join(sorted({p["model"] for p in pages})), | |
| "pdf_url": BLOB + f"reasoning_traces/{pdf.name}", | |
| }) | |
| df = pd.DataFrame(rows).sort_values("family").reset_index(drop=True) | |
| OUT.mkdir(exist_ok=True) | |
| df.to_parquet(OUT / "reasoning_summaries.parquet", index=False) | |
| print(df[["family", "slug", "num_pages"]].assign(chars=df.markdown.str.len()).to_string()) | |
| if __name__ == "__main__": | |
| main() | |