| """Convert data/_train_raw.jsonl -> data/train-00000-of-00001.parquet |
| with a typed Arrow schema that HuggingFace `datasets` can load directly. |
| """ |
| from __future__ import annotations |
|
|
| import json |
| import sys |
| from pathlib import Path |
|
|
| import pyarrow as pa |
| import pyarrow.parquet as pq |
|
|
| REPO = Path(__file__).resolve().parent.parent |
| SRC = REPO / "data" / "_train_raw.jsonl" |
| DST = REPO / "data" / "train-00000-of-00001.parquet" |
|
|
| SEGMENT_TYPE = pa.struct([ |
| pa.field("speaker_id", pa.int32()), |
| pa.field("timestamp", pa.string()), |
| pa.field("text", pa.string()), |
| ]) |
|
|
| SCHEMA = pa.schema([ |
| pa.field("id", pa.string()), |
| pa.field("source_collection", pa.string()), |
| pa.field("source_file", pa.string()), |
| pa.field("source_format", pa.string()), |
| pa.field("topic", pa.string()), |
| pa.field("round", pa.string()), |
| pa.field("team_a", pa.string()), |
| pa.field("team_b", pa.string()), |
| pa.field("num_segments", pa.int32()), |
| pa.field("num_chars", pa.int32()), |
| pa.field("transcript", pa.string()), |
| pa.field("segments", pa.list_(SEGMENT_TYPE)), |
| ]) |
|
|
|
|
| def main() -> int: |
| rows = [] |
| with open(SRC, "r", encoding="utf-8") as f: |
| for line in f: |
| line = line.strip() |
| if not line: |
| continue |
| rows.append(json.loads(line)) |
|
|
| if not rows: |
| print("no rows in input", file=sys.stderr) |
| return 1 |
|
|
| columns = {field.name: [] for field in SCHEMA} |
| for r in rows: |
| for field in SCHEMA: |
| columns[field.name].append(r.get(field.name)) |
|
|
| table = pa.table(columns, schema=SCHEMA) |
| pq.write_table(table, DST, compression="snappy") |
|
|
| size = DST.stat().st_size |
| print(f"wrote {table.num_rows} rows ({size:,} bytes) -> {DST}") |
| print(f"schema:\n{table.schema}") |
| return 0 |
|
|
|
|
| if __name__ == "__main__": |
| sys.exit(main()) |
|
|