Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -20,44 +20,63 @@ def format_time_ms(milliseconds):
|
|
| 20 |
return f"{hrs:02}:{mins:02}:{secs:02},{ms:03}"
|
| 21 |
|
| 22 |
def smart_text_split(text, words_per_line, lines_per_segment):
|
| 23 |
-
# First split by major punctuation
|
| 24 |
-
sentences =
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
segments = []
|
| 30 |
current_segment = []
|
| 31 |
current_line = []
|
| 32 |
|
| 33 |
for sentence in sentences:
|
| 34 |
-
# Split sentence into words
|
| 35 |
words = sentence.strip().split()
|
|
|
|
| 36 |
|
| 37 |
-
|
| 38 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 39 |
|
| 40 |
-
#
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
| 42 |
current_segment.append(' '.join(current_line))
|
| 43 |
current_line = []
|
| 44 |
|
| 45 |
-
# Check if
|
| 46 |
if len(current_segment) >= lines_per_segment:
|
| 47 |
segments.append('\n'.join(current_segment))
|
| 48 |
current_segment = []
|
| 49 |
-
|
| 50 |
-
# If there are words in current_line, add them as a line
|
| 51 |
-
if current_line:
|
| 52 |
-
current_segment.append(' '.join(current_line))
|
| 53 |
-
current_line = []
|
| 54 |
|
| 55 |
-
|
| 56 |
-
if len(current_segment) >= lines_per_segment:
|
| 57 |
-
segments.append('\n'.join(current_segment))
|
| 58 |
-
current_segment = []
|
| 59 |
|
| 60 |
-
#
|
|
|
|
|
|
|
| 61 |
if current_segment:
|
| 62 |
segments.append('\n'.join(current_segment))
|
| 63 |
|
|
|
|
| 20 |
return f"{hrs:02}:{mins:02}:{secs:02},{ms:03}"
|
| 21 |
|
| 22 |
def smart_text_split(text, words_per_line, lines_per_segment):
|
| 23 |
+
# First split by major punctuation with more granular control
|
| 24 |
+
sentences = []
|
| 25 |
+
current = ""
|
| 26 |
+
|
| 27 |
+
# Split text into meaningful chunks using various punctuation marks
|
| 28 |
+
for char in text:
|
| 29 |
+
current += char
|
| 30 |
+
if char in '.!?': # Strong break
|
| 31 |
+
sentences.append(current.strip())
|
| 32 |
+
current = ""
|
| 33 |
+
elif char in ',;:': # Soft break - only break if it makes a meaningful chunk
|
| 34 |
+
if len(current.split()) >= words_per_line:
|
| 35 |
+
sentences.append(current.strip())
|
| 36 |
+
current = ""
|
| 37 |
+
|
| 38 |
+
if current:
|
| 39 |
+
sentences.append(current.strip())
|
| 40 |
+
|
| 41 |
+
# Process sentences into lines and segments
|
| 42 |
segments = []
|
| 43 |
current_segment = []
|
| 44 |
current_line = []
|
| 45 |
|
| 46 |
for sentence in sentences:
|
|
|
|
| 47 |
words = sentence.strip().split()
|
| 48 |
+
i = 0
|
| 49 |
|
| 50 |
+
while i < len(words):
|
| 51 |
+
# Look ahead to find the best breaking point
|
| 52 |
+
look_ahead = min(words_per_line, len(words) - i)
|
| 53 |
+
best_break = look_ahead
|
| 54 |
+
|
| 55 |
+
# Find natural breaking points
|
| 56 |
+
for j in range(look_ahead - 1, 0, -1):
|
| 57 |
+
if any(words[i + j - 1].endswith(p) for p in ',;:.!?') or \
|
| 58 |
+
any(words[i + j].startswith(p) for p in '([{'):
|
| 59 |
+
best_break = j
|
| 60 |
+
break
|
| 61 |
|
| 62 |
+
# Add words to current line
|
| 63 |
+
current_line.extend(words[i:i + best_break])
|
| 64 |
+
|
| 65 |
+
if len(current_line) >= words_per_line or i + best_break >= len(words):
|
| 66 |
+
# Add line to current segment
|
| 67 |
current_segment.append(' '.join(current_line))
|
| 68 |
current_line = []
|
| 69 |
|
| 70 |
+
# Check if segment is complete
|
| 71 |
if len(current_segment) >= lines_per_segment:
|
| 72 |
segments.append('\n'.join(current_segment))
|
| 73 |
current_segment = []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 74 |
|
| 75 |
+
i += best_break
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
+
# Handle remaining content
|
| 78 |
+
if current_line:
|
| 79 |
+
current_segment.append(' '.join(current_line))
|
| 80 |
if current_segment:
|
| 81 |
segments.append('\n'.join(current_segment))
|
| 82 |
|