import math import numpy as np def _build_chunk(segs: list[dict], embs: list[np.ndarray]) -> dict: total = sum(max(s["end"] - s["start"], 0.1) for s in segs) logp = sum(s.get("logprob", 0.0) * max(s["end"] - s["start"], 0.1) for s in segs) / total stacked = np.stack(embs) return { "text": " ".join(s["text"] for s in segs), "start": segs[0]["start"], "end": segs[-1]["end"], "logprob": round(logp, 3), "emb": np.mean(stacked, axis=0), "seg_embs": stacked, # per-segment embeddings for MaxSim retrieval "seg_texts": [s["text"] for s in segs], # per-segment texts for cross-encoder scoring } def _cosine(a: np.ndarray, b: np.ndarray) -> float: return float(np.dot(a, b) / (np.linalg.norm(a) * np.linalg.norm(b) + 1e-9)) def _sigmoid(x: float) -> float: return 1.0 / (1.0 + math.exp(-float(x))) def fmt_time(s: float) -> str: """m:ss, or h:mm:ss past an hour. The old m:ss.cc (with minutes running past 59) clashed with the [m:ss] the prompts ask for, so cited times came back reformatted.""" total = max(0, int(round(float(s or 0.0)))) h, rem = divmod(total, 3600) m, sec = divmod(rem, 60) return f"{h}:{m:02d}:{sec:02d}" if h else f"{m}:{sec:02d}" def conf_bar(c: float) -> str: f = round(c * 10) return "█" * f + "░" * (10 - f)