ELLSA2_data / preprocess /common.py
Siyin519's picture
Add files using upload-large-folder tool
de254b2 verified
Raw History Blame Contribute Delete
1.3 kB
"""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")