Fixed subtitle word-count splitting logic for more accurate segmentation

This commit is contained in:
Deniz Şafak
2025-10-22 18:27:26 +03:00
parent f80c86b00d
commit 0d0d3b7871
2 changed files with 24 additions and 11 deletions
+1
View File
@@ -1,5 +1,6 @@
# 1.2.1 (pre-release) # 1.2.1 (pre-release)
- Added loading gif animation to book handler window. - Added loading gif animation to book handler window.
- Fixed subtitle word-count splitting logic for more accurate segmentation.
# 1.2.0 # 1.2.0
- Added `Line` option to subtitle generation modes, allowing subtitles to be generated based on line breaks in the text, by @mleg in #94. - Added `Line` option to subtitle generation modes, allowing subtitles to be generated based on line breaks in the text, by @mleg in #94.
+23 -11
View File
@@ -1382,23 +1382,35 @@ class ConversionThread(QThread):
subtitle_entries[-1] = (start, fallback_end_time, text) subtitle_entries[-1] = (start, fallback_end_time, text)
else: else:
# Word count-based grouping # Word count-based grouping - simply count spaces and split after N spaces
try: try:
word_count = int(self.subtitle_mode.split()[0]) word_count = int(self.subtitle_mode.split()[0])
word_count = min(word_count, max_subtitle_words) word_count = min(word_count, max_subtitle_words)
except (ValueError, IndexError): except (ValueError, IndexError):
word_count = 1 word_count = 1
# Group words into subtitle entries (processed_tokens already has punctuation combined) current_group = []
for i in range(0, len(processed_tokens), word_count): space_count = 0
group = processed_tokens[i : i + word_count]
if group: for token in processed_tokens:
text = "".join( current_group.append(token)
t["text"] + (t.get("whitespace", "") or "") for t in group
) # Count spaces after tokens (in the whitespace field)
subtitle_entries.append( if token.get("whitespace", "") == " ":
(group[0]["start"], group[-1]["end"], text.strip()) space_count += 1
)
# Split after counting N spaces
if space_count >= word_count:
text = "".join(t["text"] + (t.get("whitespace", "") or "") for t in current_group)
subtitle_entries.append((current_group[0]["start"], current_group[-1]["end"], text.strip()))
current_group = []
space_count = 0
# Add any remaining tokens
if current_group:
text = "".join(t["text"] + (t.get("whitespace", "") or "") for t in current_group)
subtitle_entries.append((current_group[0]["start"], current_group[-1]["end"], text.strip()))
# Fallback for last entry # Fallback for last entry
if subtitle_entries and fallback_end_time is not None: if subtitle_entries and fallback_end_time is not None:
last_entry = subtitle_entries[-1] last_entry = subtitle_entries[-1]