File size: 1,910 Bytes
209c437 | 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 43 44 45 46 | from __future__ import annotations
import argparse
import json
from pathlib import Path
def main() -> None:
parser = argparse.ArgumentParser()
parser.add_argument("paraphrased", type=Path)
parser.add_argument("challenge", type=Path)
parser.add_argument("output", type=Path)
parser.add_argument("--audit", type=Path, help="Optional paraphrase audit JSONL; disputed paraphrases are excluded.")
args = parser.parse_args()
paraphrased = [json.loads(line) for line in args.paraphrased.open(encoding="utf-8")]
challenge = [json.loads(line) for line in args.challenge.open(encoding="utf-8")]
if any(row["split"] == "challenge" for row in paraphrased):
raise ValueError("Challenge rows found in the paraphrased input")
if not challenge or any(row["split"] != "challenge" for row in challenge):
raise ValueError("Unexpected challenge split")
excluded = set()
if args.audit:
for line in args.audit.open(encoding="utf-8"):
row = json.loads(line)
if not row["equivalent"]:
excluded.add(row["id"])
before = len(paraphrased)
paraphrased = [row for row in paraphrased if row["id"] not in excluded]
print(f"Excluded {before - len(paraphrased)} disputed paraphrases")
rows = paraphrased + challenge
ids = [row["id"] for row in rows]
if len(ids) != len(set(ids)):
raise ValueError("Duplicate row IDs")
rows.sort(key=lambda row: row["id"])
args.output.parent.mkdir(parents=True, exist_ok=True)
temporary = args.output.with_suffix(args.output.suffix + ".tmp")
with temporary.open("w", encoding="utf-8") as handle:
for row in rows:
handle.write(json.dumps(row, ensure_ascii=False, separators=(",", ":")) + "\n")
temporary.replace(args.output)
print(f"Wrote {len(rows)} frozen rows to {args.output}")
if __name__ == "__main__":
main()
|