File size: 2,301 Bytes
24c50f3
 
 
 
 
 
 
 
 
37aba78
24c50f3
 
 
37aba78
 
 
24c50f3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.11"
# dependencies = []
# ///
"""Generate fictional messages with the release's scenario vocabulary."""
import argparse
from collections import defaultdict
from pathlib import Path
import sys
import json
import random

HERE=Path(__file__).absolute().parent
sys.path.insert(0, str(HERE))
from slot_values import slots,render

def main():
    parser=argparse.ArgumentParser()
    parser.add_argument("--output",type=Path,required=True)
    parser.add_argument("--seed",type=int,help="Defaults to the released study seed")
    parser.add_argument("--per-template",type=int,help="Override split-specific sample counts")
    args=parser.parse_args()
    if args.output.exists() and any(args.output.iterdir()):
        parser.error("Choose an empty output directory")
    if args.per_template is not None and args.per_template<1:
        parser.error("Sample count must be positive")
    config=json.loads((HERE/"generation.json").read_text())
    rng=random.Random(config["seed"] if args.seed is None else args.seed)
    seen=defaultdict(set)
    splits=defaultdict(list)
    # Traverse the shared study catalog in its original order to replay RNG draws.
    for line in (HERE/"templates.tsv").read_text().splitlines():
        if line.startswith("#"): continue
        task,split,scenario,*alternates=line.split("\t")
        for number,template in enumerate(alternates,1):
            key=f"{scenario}-{number}"
            for i in range(args.per_template or config["generation"][split]):
                for _ in range(1000):
                    row=render(task,template,slots(rng,split))
                    text_key=" ".join(row["text"].casefold().split())
                    if text_key not in seen[task]: break
                else: raise ValueError("Template value pool exhausted: "+key)
                seen[task].add(text_key)
                if task==config["task"]:
                    splits[split].append({"id":f"{task}-{key}-{i:03d}","scenario":scenario,"template":key,**row})
    args.output.mkdir(parents=True,exist_ok=True)
    for split,rows in splits.items():
        (args.output/(split+".jsonl")).write_text("".join(json.dumps(row,ensure_ascii=False)+"\n" for row in rows))
if __name__=="__main__":
    main()