from __future__ import annotations import re from collections import Counter from typing import Any def repetition_diagnostics(transcript: str, ngram_size: int = 6, threshold: int = 3) -> dict[str, Any]: words = re.findall(r"[a-z0-9']+", transcript.lower()) if len(words) < ngram_size: return { "possible_repetition_loop": False, "repeated_phrase_count": 0, "repeated_phrase": None, } phrases = [" ".join(words[index:index + ngram_size]) for index in range(0, len(words) - ngram_size + 1)] counts = Counter(phrases) phrase, count = counts.most_common(1)[0] repeated_phrase_count = count if count >= threshold else 0 return { "possible_repetition_loop": count >= threshold, "repeated_phrase_count": repeated_phrase_count, "repeated_phrase": phrase if count >= threshold else None, }