From 9e9561988db7084dd4a872e987dc27bc0c627465 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deniz=20=C5=9Eafak?= Date: Wed, 17 Sep 2025 20:14:26 +0300 Subject: [PATCH] Fixed subtitle splitting before commas --- CHANGELOG.md | 1 + abogen/conversion.py | 44 +++++++++++++++++++++++--------------------- 2 files changed, 24 insertions(+), 21 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ac7fdb..d976597 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ - Added new option `Configure silence between chapters` that lets you configure the silence between chapters, mentioned by @lfperez1982 in #79 - Better indicators while displaying and managing the input and processing files. - Improved the markdown logic to better handle various markdown structures and cases. +- Fixed subtitle splitting before commas by combining punctuation with preceding words. - Fixed save options not working correctly in queue mode, mentioned by @jborza in #78 - Fixed `No Qt platform plugin could be initialized` error, mentioned by @sunrainxyz in #59 - Fixed ordered list numbers not being included in EPUB content conversion. The numbers are now properly included in the converted content, mentioned by @jefro108 in #47 diff --git a/abogen/conversion.py b/abogen/conversion.py index d980b2a..8aa0530 100644 --- a/abogen/conversion.py +++ b/abogen/conversion.py @@ -1189,13 +1189,32 @@ class ConversionThread(QThread): if not tokens_with_timestamps: return + # Always combine punctuation with preceding words for consistent handling across modes + processed_tokens = [] + i = 0 + while i < len(tokens_with_timestamps): + token = tokens_with_timestamps[i].copy() + + # Look ahead for punctuation + while i + 1 < len(tokens_with_timestamps) and re.match( + r"^[^\w\s]+$", tokens_with_timestamps[i + 1]["text"] + ): + token["text"] += tokens_with_timestamps[i + 1]["text"] + token["end"] = tokens_with_timestamps[i + 1]["end"] + token["whitespace"] = tokens_with_timestamps[i + 1]["whitespace"] + i += 1 + + processed_tokens.append(token) + i += 1 + + # Use processed_tokens instead of tokens_with_timestamps for the rest of the method if self.subtitle_mode == "Sentence + Highlighting": # Sentence-based processing with karaoke highlighting separator = r"[.!?]" current_sentence = [] word_count = 0 - for token in tokens_with_timestamps: + for token in processed_tokens: # Updated to use processed_tokens current_sentence.append(token) word_count += 1 @@ -1249,7 +1268,7 @@ class ConversionThread(QThread): current_sentence = [] word_count = 0 - for token in tokens_with_timestamps: + for token in processed_tokens: # Updated to use processed_tokens current_sentence.append(token) word_count += 1 @@ -1299,25 +1318,7 @@ class ConversionThread(QThread): except (ValueError, IndexError): word_count = 1 - # Combine punctuation with preceding words - processed_tokens = [] - i = 0 - while i < len(tokens_with_timestamps): - token = tokens_with_timestamps[i].copy() - - # Look ahead for punctuation - while i + 1 < len(tokens_with_timestamps) and re.match( - r"^[^\w\s]+$", tokens_with_timestamps[i + 1]["text"] - ): - token["text"] += tokens_with_timestamps[i + 1]["text"] - token["end"] = tokens_with_timestamps[i + 1]["end"] - token["whitespace"] = tokens_with_timestamps[i + 1]["whitespace"] - i += 1 - - processed_tokens.append(token) - i += 1 - - # Group words into subtitle entries + # Group words into subtitle entries (processed_tokens already has punctuation combined) for i in range(0, len(processed_tokens), word_count): group = processed_tokens[i : i + word_count] if group: @@ -1334,6 +1335,7 @@ class ConversionThread(QThread): if end is None or end <= start or end <= 0: subtitle_entries[-1] = (start, fallback_end_time, text) + def cancel(self): self.cancel_requested = True self.should_cancel = True