Chinese_Debate_Documents / scripts /reparse_metadata.py
DBWBD's picture
updated
6342c6d
"""Re-parse filename metadata over an existing JSONL without re-opening Word.
Reads `data/_train_raw.jsonl`, applies the current `parse_filename` from
`extract_transcripts.py` to each record's `source_file`, overwrites the four
metadata fields, and writes back atomically. Then prints coverage statistics.
"""
from __future__ import annotations
import json
import sys
from pathlib import Path
REPO = Path(__file__).resolve().parent.parent
JSONL = REPO / "data" / "_train_raw.jsonl"
TMP = REPO / "data" / "_train_raw.jsonl.tmp"
sys.path.insert(0, str(REPO / "scripts"))
from extract_transcripts import parse_filename # noqa: E402
def main() -> int:
if not JSONL.exists():
print(f"missing {JSONL}", file=sys.stderr)
return 1
updated = 0
total = 0
parsed_before = 0
parsed_after = 0
with open(JSONL, "r", encoding="utf-8") as src, open(
TMP, "w", encoding="utf-8"
) as dst:
for line in src:
line = line.strip()
if not line:
continue
rec = json.loads(line)
total += 1
had = rec.get("topic") is not None
parsed_before += int(had)
stem = Path(rec["source_file"]).stem
meta = parse_filename(stem)
for k in ("topic", "round", "team_a", "team_b"):
if rec.get(k) != meta[k]:
updated += 1 if k == "topic" else 0
rec[k] = meta[k]
parsed_after += int(rec.get("topic") is not None)
dst.write(json.dumps(rec, ensure_ascii=False) + "\n")
TMP.replace(JSONL)
print(f"records: {total}")
print(f"topic parsed before: {parsed_before} after: {parsed_after}")
print(f"coverage gain: +{parsed_after - parsed_before}")
return 0
if __name__ == "__main__":
sys.exit(main())