File size: 894 Bytes
2e818da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
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,
    }