File size: 4,463 Bytes
46e27b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"""Corpus QA reporter — deterministic full re-verification of a committed dataset.

Single-label non-autoregressive speech disfluency contract:
1. Category must be CLEAN or DISFLUENCY.
2. All spans must have label "speech disfluency".
3. Zero targets in spans.
4. Clean speech emits spans: [].
5. Roundtrip slice-cut excision must exactly reconstruct clean_text.
6. Entity/Number conservation, pronoun safety, Tier-1 filler free.
"""

import argparse
import json
import re
import sys
from collections import Counter

sys.path.insert(0, "/opt/vox/sandbox/scripts")

from compiler import verify_roundtrip
from dedup import PROTECTED, TIER1, TIER1_ANY, near_dup, norm_tokens
from schema import DatasetExample

# Imported from dedup so the gate and the harvester can never drift apart.
SAFE_SPAN = PROTECTED


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("dataset")
    ap.add_argument("--near-dup-jaccard", type=float, default=0.85)
    ap.add_argument("--max-pairs", type=int, default=200000)
    ap.add_argument("--report", default=None)
    args = ap.parse_args()

    fails = Counter()
    examples = {}
    ids, raws, fps = set(), set(), []
    n = 0

    for ln, line in enumerate(open(args.dataset), 1):
        line = line.strip()
        if not line:
            continue
        n += 1
        try:
            ex = DatasetExample(**{k: v for k, v in json.loads(line).items()
                                   if k in DatasetExample.model_fields})
        except Exception as e:
            fails["1_schema"] += 1
            examples.setdefault("1_schema", (ln, str(e)[:80]))
            continue

        if ex.category not in {"DISFLUENCY", "CLEAN"}:
            fails["1_schema"] += 1
            examples.setdefault("1_schema", (ln, f"Invalid category: {ex.category}"))

        if ex.id in ids:
            fails["2_id_unique"] += 1
            examples.setdefault("2_id_unique", (ln, ex.id))
        ids.add(ex.id)

        key = ex.raw_text.strip().lower()
        if key in raws:
            fails["3_exact_unique"] += 1
        raws.add(key)
        fp = norm_tokens(ex.raw_text)
        if near_dup(fp, fps, args.near_dup_jaccard):
            fails["4_near_unique"] += 1
            examples.setdefault("4_near_unique", (ln, ex.raw_text[:70]))
        if len(fps) < args.max_pairs:
            fps.append(fp)

        if ex.raw_text.strip()[-1:] not in ".?!\u0964" or \
           ex.clean_text.strip()[-1:] not in ".?!\u0964":
            fails["10_boundary"] += 1

        if ex.category == "CLEAN":
            if ex.spans:
                fails["5_clean_has_spans"] += 1
            continue

        for s in ex.spans:
            if s.label != "speech disfluency":
                fails["1_schema"] += 1
                examples.setdefault("1_schema", (ln, f"Invalid span label: {s.label}"))

        ok, rec = verify_roundtrip(ex.raw_text, ex.spans, ex.clean_text)
        if not ok:
            fails["5_roundtrip"] += 1
            examples.setdefault("5_roundtrip", (ln, f"{rec!r} != {ex.clean_text!r}"))
            continue

        for s in ex.spans:
            t = s.text.strip()
            if TIER1.match(t) or TIER1_ANY.search(t):
                fails["8_tier1_free"] += 1
                examples.setdefault("8_tier1_free", (ln, t[:40]))
            if t.lower().strip(",. ") in SAFE_SPAN:
                fails["9_pronoun_safe"] += 1
                examples.setdefault("9_pronoun_safe", (ln, t[:40]))

        if TIER1_ANY.search(ex.clean_text):
            fails["11_clean_not_tier1"] += 1

    cats = Counter()
    for line in open(args.dataset):
        try:
            cats[json.loads(line).get("category", "?")] += 1
        except Exception:
            pass

    total = sum(fails.values())
    print(f"dataset: {args.dataset}")
    print(f"examples: {n}  categories: {dict(cats)}")
    if not fails:
        print("QA REPORT: CLEAN — all deterministic checks pass")
    else:
        print(f"QA REPORT: {total} violation(s)")
        for k in sorted(fails):
            print(f"  {k}: {fails[k]}  e.g. {examples.get(k)}")

    if args.report:
        json.dump({"dataset": args.dataset, "examples": n,
                   "categories": dict(cats), "violations": dict(fails),
                   "examples_of_violation": {k: list(v) for k, v in examples.items()}},
                  open(args.report, "w"), indent=2)
    sys.exit(1 if fails else 0)


if __name__ == "__main__":
    main()