File size: 1,295 Bytes
de254b2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Shared manifest I/O and participant identity for data preparation."""
import json
from pathlib import Path
import re

SPEAKER = re.compile(r"(?:^|_)P([^_/.]+)", re.IGNORECASE)


def normalize_speaker_id(value):
    return str(value).strip().upper().removeprefix("P")


def infer_speaker_id(sample):
    for key in ("speaker_id", "speaker", "person_id"):
        if sample.get(key) is not None and str(sample[key]).strip():
            return normalize_speaker_id(sample[key])
    for key in ("audio_file", "pose_file"):
        match = SPEAKER.search(Path(sample.get(key, "")).name)
        if match:
            return normalize_speaker_id(match[1])
    raise ValueError("Recording has no participant identity")


def load_manifest(path):
    value = json.loads(Path(path).read_text())
    rows = value["data"] if isinstance(value, dict) else value
    assert isinstance(rows, list) and all(isinstance(row, dict) for row in rows)
    return rows, value if isinstance(value, dict) else None


def write_manifest(path, rows, original=None, **metadata):
    value = {**original, "data": rows, **metadata} if original is not None else rows
    output = Path(path)
    output.parent.mkdir(parents=True, exist_ok=True)
    output.write_text(json.dumps(value, indent=2, ensure_ascii=False) + "\n")