Download code/dolma_chunk.py from tablinum/beam-engram-tables: direct link, hf CLI and curl.
- Browser
- Download file 3.4 kB
-
https://huggingface.co/datasets/tablinum/beam-engram-tables/resolve/main/code/dolma_chunk.py
- Command line
-
hf download hf://datasets/tablinum/beam-engram-tables/code/dolma_chunk.py
-
curl -L -o dolma_chunk.py https://huggingface.co/datasets/tablinum/beam-engram-tables/resolve/main/code/dolma_chunk.py
3.4 kB
| """dolma_chunk.py MODE IN.json.gz OUT.parquet - Common Pile (dolma json.gz: id, text, metadata) -> engram passages (10-20, | |
| PLAN-verticals B2/B4-6). Same schema as chunk.py; file_idx from OUT's name AAA_BB. | |
| doc: OER books and pages (libretexts, pressbooks, oercommons, doab). Title = metadata.title; a short line with no end punctuation starts a new section; WORDS-word windows. | |
| cap: Caselaw Access Project opinions. Title "Case name (court, date)" from the caption lines; the caption/counsel block is | |
| skipped (body starts at the first line of >= 25 words); only the first CAP_WINDOWS windows are kept (the syllabus / | |
| statement of the holding), and opinions with a body under 120 words are dropped - 6.7M cases would otherwise be ~140M | |
| passages.""" | |
| import sys, os, re, json, gzip | |
| import pyarrow as pa, pyarrow.parquet as pq | |
| WORDS, MIN_WORDS, CAP_WINDOWS = 140, 12, int(os.environ.get("CAP_WINDOWS", 3)) | |
| mode, src, dst = sys.argv[1:4] | |
| a, b = re.search(r"(\d+)_(\d+)\.parquet$", dst).groups(); fidx = int(b) + 100 * int(a) | |
| cols = {"pid": [], "page_id": [], "title": [], "section": [], "text": []}; n = docs = 0 | |
| def emit(title, sec, words, doc, limit=None): | |
| global n | |
| k = 0 | |
| for i in range(0, len(words), WORDS): | |
| part = words[i:i + WORDS + 20] if len(words) - i <= WORDS + 20 else words[i:i + WORDS] | |
| if len(part) >= MIN_WORDS: | |
| cols["pid"].append((fidx << 32) | n); cols["page_id"].append(doc); cols["title"].append(title); cols["section"].append(sec) | |
| cols["text"].append(f"{title} — {sec}: " + " ".join(part) if sec else f"{title}: " + " ".join(part)); n += 1; k += 1 | |
| if len(words) - i <= WORDS + 20 or (limit and k >= limit): | |
| break | |
| for line in gzip.open(src, "rt"): | |
| d = json.loads(line); md = d.get("metadata") or {}; t = d["text"]; docs += 1 | |
| if mode == "cap": | |
| lines = [x.strip() for x in t.split("\n") if x.strip()] | |
| if not lines: | |
| continue | |
| start = next((i for i, x in enumerate(lines) if len(x.split()) >= 25), None) | |
| if start is None: | |
| continue | |
| body = " ".join(lines[start:]).split() | |
| if len(body) < 120: | |
| continue | |
| cap = [x for x in lines[:start] if not x.lower().startswith(("no.", "docket", "before", "argued", "submitted"))] | |
| court = next((x for x in cap[1:6] if re.search(r"court|supreme|appeals|circuit", x, re.I)), "") | |
| date = next((x for x in cap[1:8] if re.search(r"\b1[6-9]\d\d\b|\b20\d\d\b", x) and len(x) < 40), "") | |
| title = re.sub(r"\s+", " ", cap[0] if cap else lines[0])[:160] + (f" ({', '.join(x for x in (court, date) if x)})" if court or date else "") | |
| emit(title, "", body, docs, CAP_WINDOWS) | |
| else: | |
| title = (md.get("title") or t.split("\n", 1)[0])[:160].strip() | |
| sec, buf = "", [] | |
| for x in t.split("\n"): | |
| x = x.strip().strip("|").strip() | |
| if not x: | |
| continue | |
| if len(x.split()) <= 8 and not re.search(r"[.:;,?!)\]]$", x) and len(buf) >= MIN_WORDS: | |
| emit(title, sec, buf, docs); buf = []; sec = x[:100] | |
| else: | |
| buf += x.split() | |
| emit(title, sec, buf, docs) | |
| pq.write_table(pa.table(cols), dst + ".tmp", compression="zstd", row_group_size=4096); os.replace(dst + ".tmp", dst) | |
| print(src, mode, "docs", docs, "passages", n, flush=True) | |