File size: 3,107 Bytes
901f964 | 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 | """pes2o_split.py SHARD.zst IDX (10-27) - peS2o v3 s2ag shard (title + abstract, shards 0000-0029) -> one passage per paper,
split by Semantic Scholar field of study into papers_fin / papers_hum / papers_sci (research/{finance,humanities,stem}.md: the
"academic literature" slice is 22% of Finance, 27% of Humanities, ~20% of STEM AA items). Passage = "Title (year): abstract",
capped at WORDS words. Medicine is left out (the health table has PubMed). Year >= 1970 (peS2o drops older anyway). Reads the
shard through zstdcat (no zstandard module in the venv). Writes tables/<t>/passages/000_<IDX>.parquet for each table.
ODC-By: local retrieval only, do not redistribute the tables."""
import sys, os, json, ast, subprocess
import pyarrow as pa, pyarrow.parquet as pq
E = os.environ.get("ENGRAM_ROOT", "."); WORDS = int(os.environ.get("WORDS", 220))
src, idx = sys.argv[1], int(sys.argv[2])
G = {"papers_fin": {"Economics", "Business"},
"papers_hum": {"History", "Philosophy", "Art", "Linguistics", "Political Science", "Sociology", "Law", "Geography"},
"papers_sci": {"Physics", "Chemistry", "Materials Science", "Geology", "Mathematics"}} # research/stem.md rank 4; Engineering /
# Environmental / Biology would ~double it (rank 5) - add once disk allows. Education left out of hum (no AA items, large).
G["papers_eng"] = {"Engineering", "Environmental Science", "Agricultural And Food Sciences", "Agricultural and Food Sciences"} # 10-27 batch 4
G["papers_bio"] = {"Biology"} # non-medical biology only (see the Medicine check below)
G = {t: v for t, v in G.items() if t in os.environ.get("ONLY", "papers_fin,papers_hum,papers_sci").split(",")}
cols = {t: {"pid": [], "page_id": [], "title": [], "section": [], "text": []} for t in G}; n = {t: 0 for t in G}; docs = 0
p = subprocess.Popen(["zstdcat", src], stdout=subprocess.PIPE, text=True, bufsize=1 << 20)
for line in p.stdout:
try:
d = json.loads(line)
except ValueError:
continue
docs += 1; m = d.get("metadata") or {}
m = ast.literal_eval(m) if isinstance(m, str) else m
y = m.get("year") or 0; f = set(m.get("s2fieldsofstudy") or [])
if y and y < 1970 or not f:
continue
title = (m.get("title") or "").strip(); ab = (m.get("abstract") or "").split()
if not title or len(ab) < 30:
continue
for t, want in G.items():
if f & want and not (t in ("papers_sci", "papers_bio") and "Medicine" in f | set(m.get("extfieldsofstudy") or [])):
c = cols[t]; c["pid"].append((idx << 32) | n[t]); c["page_id"].append(int(d["id"]) if str(d["id"]).isdigit() else docs)
c["title"].append(title[:200]); c["section"].append(str(y)); c["text"].append(f"{title[:200]} ({y}): " + " ".join(ab[:WORDS])); n[t] += 1
p.wait()
for t, c in cols.items():
os.makedirs(f"{E}/tables/{t}/passages", exist_ok=True); dst = f"{E}/tables/{t}/passages/000_{idx:05d}.parquet"
pq.write_table(pa.table(c), dst + ".tmp", compression="zstd", row_group_size=4096); os.replace(dst + ".tmp", dst)
print(src, "docs", docs, " ".join(f"{t} {n[t]}" for t in G), flush=True)
|