File size: 1,241 Bytes
39a78f4 | 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 | #!/usr/bin/env python3
import gzip, json, os, shutil
import pyarrow as pa
import pyarrow.parquet as pq
ROOT = os.environ.get("UNSC_CORPUS_ROOT", ".")
D = f"{ROOT}/publication/data"
src = f"{D}/fulltext.jsonl"
schema = pa.schema([("record_id", pa.int64()), ("symbol", pa.string()),
("family", pa.string()), ("date", pa.string()),
("n_chars", pa.int64()), ("text", pa.string())])
writer = pq.ParquetWriter(f"{D}/fulltext.parquet", schema, compression="zstd")
batch, N = [], 0
def flush():
global batch
if not batch:
return
cols = {f.name: [r.get(f.name) for r in batch] for f in schema}
cols["record_id"] = [int(x) if x is not None else None for x in cols["record_id"]]
writer.write_table(pa.table(cols, schema=schema))
batch = []
for line in open(src):
batch.append(json.loads(line)); N += 1
if len(batch) >= 2000:
flush()
flush(); writer.close()
print(f"wrote fulltext.parquet ({N} rows)", flush=True)
with open(src, "rb") as f_in, gzip.open(f"{src}.gz", "wb", compresslevel=6) as f_out:
shutil.copyfileobj(f_in, f_out, length=1024*1024)
os.remove(src)
print("wrote fulltext.jsonl.gz; removed uncompressed", flush=True)
print("DONE", flush=True)
|