26 lines · 845 B
python
| 1 | """Attach Hydra speakers onto Whisper cues. |
| 2 | |
| 3 | For each Whisper cue [start, end, text], pick the Hydra region that covers |
| 4 | the midpoint. That is the whole diarizer: you are reading who X thought was |
| 5 | unmuted, not guessing voices. |
| 6 | """ |
| 7 | |
| 8 | from __future__ import annotations |
| 9 | |
| 10 | |
| 11 | def attach_speakers(cues: list[dict], regions: list[dict]) -> list[dict]: |
| 12 | out: list[dict] = [] |
| 13 | j = 0 |
| 14 | n = len(regions) |
| 15 | for cue in cues: |
| 16 | start = float(cue.get("start") or 0) |
| 17 | end = float(cue.get("end") or start) |
| 18 | mid = (start + end) / 2 |
| 19 | while j + 1 < n and regions[j]["end"] < mid: |
| 20 | j += 1 |
| 21 | speaker = regions[j]["speaker"] if j < n else "unknown" |
| 22 | if speaker in ("silence", None, ""): |
| 23 | speaker = "unknown" |
| 24 | out.append({**cue, "start": start, "end": end, "speaker": speaker}) |
| 25 | return out |