File size: 2,949 Bytes
869146a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3

import argparse
import csv
import re
from pathlib import Path


def parse_args():
    parser = argparse.ArgumentParser(
        description="Create metadata file (audio|text) for QuEsT, optionally filtered by language."
    )
    parser.add_argument(
        "--root",
        type=Path,
        default=Path(__file__).resolve().parent,
        help="Root directory containing train/test folders and transcripts.txt",
    )
    parser.add_argument(
        "--split",
        choices=["train", "test"],
        default="train",
        help="Dataset split to scan for audio files.",
    )
    parser.add_argument(
        "--lang",
        choices=["qu", "es", "both"],
        default="qu",
        help="Language to include based on filename suffix.",
    )
    parser.add_argument(
        "--output",
        type=Path,
        help="Path to write metadata file. Defaults to <split>_metadata_<lang>.txt under root.",
    )
    return parser.parse_args()


def load_transcripts(transcripts_path: Path):
    id2pair = {}
    with transcripts_path.open(encoding="utf-8") as f:
        reader = csv.reader(f, delimiter="|", quotechar='"')
        for row in reader:
            if len(row) < 3:
                continue
            rid, qu, es = row[0].strip(), row[1].strip(), row[2].strip()
            id2pair[(rid, "qu")] = qu
            id2pair[(rid, "es")] = es
            rid_nz = rid.lstrip("0") or "0"
            id2pair[(rid_nz, "qu")] = qu
            id2pair[(rid_nz, "es")] = es
    return id2pair


def main():
    args = parse_args()
    root = args.root.resolve()
    transcripts = root / "transcripts.txt"
    if not transcripts.exists():
        raise FileNotFoundError(f"transcripts.txt not found at {transcripts}")

    lang_filter = {"qu", "es"} if args.lang == "both" else {args.lang}
    output = args.output or root / f"{args.split}_metadata_{args.lang}.txt"

    id2text = load_transcripts(transcripts)
    pattern = re.compile(r"(?:.*_)?(qu|es)_(\d+)\.wav$", re.IGNORECASE)

    audio_root = root / args.split
    if not audio_root.exists():
        raise FileNotFoundError(f"Split directory not found: {audio_root}")

    count = 0
    skipped = 0
    with output.open("w", encoding="utf-8") as fout:
        for wav in audio_root.rglob("*.wav"):
            match = pattern.search(wav.name)
            if not match:
                continue
            lang, idx = match.group(1).lower(), match.group(2)
            if lang not in lang_filter:
                continue
            idx_norm = idx.lstrip("0") or "0"
            text = id2text.get((idx_norm, lang))
            if not text:
                skipped += 1
                continue
            rel = wav.relative_to(root).as_posix()
            fout.write(f"{rel}|{text}\n")
            count += 1

    print(f"[OK] wrote {output} with {count} rows (skipped {skipped} lacking transcripts).")


if __name__ == "__main__":
    main()