basyx commited on
Commit
77a4fb1
·
verified ·
1 Parent(s): 1317cf8

Create highlights.py

Browse files
Files changed (1) hide show
  1. utils/highlights.py +35 -0
utils/highlights.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ def score_word(word):
2
+ """
3
+ Simple heuristic scoring:
4
+ - longer words slightly more important
5
+ - punctuation emphasis
6
+ """
7
+
8
+ score = len(word["text"]) * 0.1
9
+
10
+ if any(p in word["text"] for p in ["!", "?", "."]):
11
+ score += 1
12
+
13
+ return score
14
+
15
+
16
+ def detect_highlights(words, threshold=0.8):
17
+ """
18
+ Groups words into highlight segments
19
+ """
20
+
21
+ highlights = []
22
+ buffer = []
23
+
24
+ for w in words:
25
+ if score_word(w) > threshold:
26
+ buffer.append(w)
27
+ else:
28
+ if buffer:
29
+ highlights.append(buffer)
30
+ buffer = []
31
+
32
+ if buffer:
33
+ highlights.append(buffer)
34
+
35
+ return highlights