File size: 3,749 Bytes
10a8395
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
93
94
95
#!/usr/bin/env python3
"""Build the frozen clean task pool sized for roughly 3,800 unique comparisons."""

from __future__ import annotations

import argparse
import json
import random
import re
from collections import Counter
from pathlib import Path

from build_pilot_pool import SELECTION


CORRUPTION = re.compile(r"\bsintense(?:er|ing)?\b", re.IGNORECASE)
TARGETS = {"emotion": 30, "acting_challenge": 30, "voicenet": 24}


def sentence_count(text: str) -> int:
    return len(re.findall(r"[.!?]+(?:[\"”’']+)?(?=\s|$)", text))


def clean(task: dict) -> bool:
    return not CORRUPTION.search(json.dumps(task)) and sentence_count(
        task["script"]["text"]
    ) == int(task["script"]["sentence_count"])


def choose(tasks: list[dict], track: str, count: int, rng: random.Random) -> list[dict]:
    candidates = [task for task in tasks if task["track"] == track and clean(task)]
    required_ids = set(SELECTION) & {task["id"] for task in candidates}
    selected = [task for task in candidates if task["id"] in required_ids]
    remaining = [task for task in candidates if task["id"] not in required_ids]
    rng.shuffle(remaining)

    # Vocal events are scarce and central to the benchmark, so retain all that fit.
    burst_first = [task for task in remaining if task.get("instruction", {}).get("vocal_burst")]
    ordinary = [task for task in remaining if task not in burst_first]
    selected.extend(burst_first[: max(0, count - len(selected))])

    # Greedily diversify target labels and difficulty tags for the remaining slots.
    label_counts = Counter(task.get("target", {}).get("label") for task in selected)
    tag_counts = Counter(tag for task in selected for tag in task.get("difficulty_tags", []))
    while len(selected) < count:
        candidate = min(
            ordinary,
            key=lambda task: (
                label_counts[task.get("target", {}).get("label")],
                sum(tag_counts[tag] for tag in task.get("difficulty_tags", [])),
                task["id"],
            ),
        )
        ordinary.remove(candidate)
        selected.append(candidate)
        label_counts[candidate.get("target", {}).get("label")] += 1
        tag_counts.update(candidate.get("difficulty_tags", []))
    return selected


def main() -> None:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("benchmark", type=Path)
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--seed", type=int, default=20260912)
    args = parser.parse_args()
    source = json.loads(args.benchmark.read_text(encoding="utf-8"))
    rng = random.Random(args.seed)
    selected = []
    for track, count in TARGETS.items():
        selected.extend(choose(source["tasks"], track, count, rng))
    selected.sort(key=lambda task: task["id"])
    systems = 10
    output = {
        "meta": {
            "name": "Speech Performance Arena evaluation pool",
            "version": "0.1.0",
            "source_benchmark_version": source.get("meta", {}).get("version", "unknown"),
            "selection_seed": args.seed,
            "selection_policy": "clean deterministic coverage; pilot anchors and vocal bursts prioritized",
            "task_count": len(selected),
            "track_counts": dict(Counter(task["track"] for task in selected)),
            "planned_systems": systems,
            "possible_task_system_pairs": len(selected) * systems * (systems - 1) // 2,
        },
        "tasks": selected,
    }
    args.output.parent.mkdir(parents=True, exist_ok=True)
    args.output.write_text(json.dumps(output, ensure_ascii=False, indent=2) + "\n", encoding="utf-8")
    print(json.dumps(output["meta"], indent=2))


if __name__ == "__main__":
    main()