#!/usr/bin/env python3 """Generate Cipher-17 train/test JSONL under ``data/``. Run from repo root:: python data/export_cipher17.py Writes:: data/cipher_train.jsonl # 1_000_000 rows data/cipher_test.jsonl # 5_000 rows Rules: ``anchored_global_dependency.py`` (same folder). See ``cipher_pipeline.md``. """ from __future__ import annotations import importlib.util import json import random from pathlib import Path DATA_DIR = Path(__file__).resolve().parent GENERATOR = DATA_DIR / "anchored_global_dependency.py" SEED = 42 N = 17 K_OFFSET = 5 POS_CONST = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9, 3, 2] NUM_TRAIN = 1_000_000 NUM_TEST = 5_000 OUT_TRAIN = DATA_DIR / "cipher_train.jsonl" OUT_TEST = DATA_DIR / "cipher_test.jsonl" def _load_generator(): if not GENERATOR.is_file(): raise FileNotFoundError(f"Missing generator: {GENERATOR}") spec = importlib.util.spec_from_file_location("cipher_anchored_global", GENERATOR) if spec is None or spec.loader is None: raise ImportError(f"Cannot load {GENERATOR}") mod = importlib.util.module_from_spec(spec) spec.loader.exec_module(mod) return mod def write_jsonl(path: Path, samples: list[dict[str, str]]) -> None: with path.open("w", encoding="utf-8") as f: for row in samples: f.write(json.dumps(row, ensure_ascii=False) + "\n") def main() -> None: mod = _load_generator() print(f"generator: {GENERATOR}") print(f"out_dir: {DATA_DIR}") print(f"train={NUM_TRAIN} test={NUM_TEST} seed={SEED} n={N}") rng = random.Random(SEED) train = mod.generate_samples_anchored_global( num_samples=NUM_TRAIN, n=N, k_offset=K_OFFSET, pos_const=POS_CONST, rng=rng, ) test = mod.generate_samples_anchored_global( num_samples=NUM_TEST, n=N, k_offset=K_OFFSET, pos_const=POS_CONST, rng=rng, ) write_jsonl(OUT_TRAIN, train) write_jsonl(OUT_TEST, test) ok, _, _ = mod.verify_one_sample( sample=test[0], n=N, k_offset=K_OFFSET, pos_const=POS_CONST ) status = "SUCCESS" if ok else "FAILED" print(f"wrote {OUT_TRAIN} ({len(train)} rows)") print(f"wrote {OUT_TEST} ({len(test)} rows)") print(f"verify first test sample: {status}") if not ok: raise SystemExit("verification failed") print("DONE") if __name__ == "__main__": main()