study-buddy / app /services /transcript_diagnostics.py
GitHub Actions
deploy b3d187d69e5df10bb2ec396e89f539f853465a06
14184e3
Raw
History Blame Contribute Delete
894 Bytes
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,
}