File size: 677 Bytes
1425afc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
def score_word(word):
    """
    Simple heuristic scoring:
    - longer words slightly more important
    - punctuation emphasis
    """

    score = len(word["text"]) * 0.1

    if any(p in word["text"] for p in ["!", "?", "."]):
        score += 1

    return score


def detect_highlights(words, threshold=0.8):
    """
    Groups words into highlight segments
    """

    highlights = []
    buffer = []

    for w in words:
        if score_word(w) > threshold:
            buffer.append(w)
        else:
            if buffer:
                highlights.append(buffer)
                buffer = []

    if buffer:
        highlights.append(buffer)

    return highlights