mirror of
https://github.com/denizsafak/abogen.git
synced 2026-07-18 13:40:27 +02:00
Fixed subtitle word-count splitting logic for more accurate segmentation
This commit is contained in:
@@ -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
@@ -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]
|
||||||
|
|||||||
Reference in New Issue
Block a user