| #!/usr/bin/env python3 | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| from typing import Dict | |
| import orjson | |
| def load_jsonl(path: Path): | |
| with open(path, "r", encoding="utf-8") as f: | |
| for line in f: | |
| if line.strip(): | |
| yield json.loads(line) | |
| def main() -> None: | |
| parser = argparse.ArgumentParser(description="Merge hypothesis texts into base JSONL by id.") | |
| parser.add_argument("--base", required=True, help="Base JSONL with entries containing id and audio...") | |
| parser.add_argument("--hyp", required=True, help="Hypothesis JSONL with id,text") | |
| parser.add_argument("--out", required=True, help="Output JSONL (overwrites base or writes new)") | |
| args = parser.parse_args() | |
| hyp_map: Dict[str, str] = {} | |
| for rec in load_jsonl(Path(args.hyp)): | |
| txt = (rec.get("text") or "").strip() | |
| if txt: | |
| hyp_map[rec["id"]] = txt | |
| with open(args.out, "w", encoding="utf-8") as out_f: | |
| for base in load_jsonl(Path(args.base)): | |
| _id = base["id"] | |
| if _id in hyp_map: | |
| base["text"] = hyp_map[_id] | |
| out_f.write(orjson.dumps(base).decode("utf-8") + "\n") | |
| print(f"Merged transcripts -> {args.out} (updated {len(hyp_map)} items)") | |
| if __name__ == "__main__": | |
| main() | |