File size: 2,435 Bytes
97ecad4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#!/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()