File size: 1,848 Bytes
6342c6d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Re-parse filename metadata over an existing JSONL without re-opening Word.

Reads `data/_train_raw.jsonl`, applies the current `parse_filename` from
`extract_transcripts.py` to each record's `source_file`, overwrites the four
metadata fields, and writes back atomically. Then prints coverage statistics.
"""
from __future__ import annotations

import json
import sys
from pathlib import Path

REPO = Path(__file__).resolve().parent.parent
JSONL = REPO / "data" / "_train_raw.jsonl"
TMP = REPO / "data" / "_train_raw.jsonl.tmp"

sys.path.insert(0, str(REPO / "scripts"))
from extract_transcripts import parse_filename  # noqa: E402


def main() -> int:
    if not JSONL.exists():
        print(f"missing {JSONL}", file=sys.stderr)
        return 1

    updated = 0
    total = 0
    parsed_before = 0
    parsed_after = 0
    with open(JSONL, "r", encoding="utf-8") as src, open(
        TMP, "w", encoding="utf-8"
    ) as dst:
        for line in src:
            line = line.strip()
            if not line:
                continue
            rec = json.loads(line)
            total += 1
            had = rec.get("topic") is not None
            parsed_before += int(had)

            stem = Path(rec["source_file"]).stem
            meta = parse_filename(stem)
            for k in ("topic", "round", "team_a", "team_b"):
                if rec.get(k) != meta[k]:
                    updated += 1 if k == "topic" else 0
                    rec[k] = meta[k]
            parsed_after += int(rec.get("topic") is not None)
            dst.write(json.dumps(rec, ensure_ascii=False) + "\n")

    TMP.replace(JSONL)
    print(f"records: {total}")
    print(f"topic parsed before: {parsed_before}  after: {parsed_after}")
    print(f"coverage gain: +{parsed_after - parsed_before}")
    return 0


if __name__ == "__main__":
    sys.exit(main())