| |
|
|
| 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() |
|
|