refactor: unify PUNCTUATION constants in domain/split_pattern.py

This commit is contained in:
Artem Akymenko
2026-07-24 19:17:39 +03:00
parent 654c395943
commit cfc7de7abf
3 changed files with 12 additions and 18 deletions
+6 -2
View File
@@ -5,8 +5,12 @@ import re
from abogen.domain.enums import Language, SubtitleMode
PUNCTUATION_SENTENCE = r".!?。!?"
PUNCTUATION_SENTENCE_COMMA = r".!?,。!?、,"
# Canonical punctuation sets covering all supported scripts:
# ASCII (. ! ?), Arabic ؟, CJK (。!?), Devanagari ।
PUNCTUATION_SENTENCE = r".!?؟。!?।"
# Commas: ASCII , CJK fullwidth CJK ideographic 、
PUNCTUATION_SENTENCE_COMMA = r".!?,?。!?،,、।"
PUNCTUATION_COMMAS = ",,、"
def get_split_pattern(language: str, subtitle_mode: str) -> str:
+4 -10
View File
@@ -11,11 +11,7 @@ import re
from typing import List, Optional, Tuple
from abogen.domain.enums import Language, SubtitleMode
# Punctuation constants for sentence splitting
PUNCTUATION_SENTENCE = ".!?\u061f\u3002\uff01\uff1f" # .!? .?. ??
PUNCTUATION_SENTENCE_COMMA = ".!?,\u3001\u061f\u3002\uff01\uff0c\uff1f" # .!?, ,. ??
from abogen.domain.split_pattern import PUNCTUATION_SENTENCE, PUNCTUATION_SENTENCE_COMMA
def process_subtitle_tokens(
@@ -87,7 +83,7 @@ def _process_karaoke_highlighting(
fallback_end_time: Optional[float],
) -> None:
"""Process tokens for Sentence + Highlighting mode (karaoke effect)."""
separator = rf"[{re.escape(PUNCTUATION_SENTENCE)}]"
separator = rf"[{PUNCTUATION_SENTENCE}]"
current_sentence = []
word_count = 0
@@ -247,11 +243,9 @@ def _process_regex_sentences(
if subtitle_mode == SubtitleMode.LINE:
separator = r"\n"
elif subtitle_mode == SubtitleMode.SENTENCE:
# Use punctuation without comma
separator = rf"[{re.escape(PUNCTUATION_SENTENCE)}]"
separator = rf"[{PUNCTUATION_SENTENCE}]"
else: # Sentence + Comma
# Use punctuation with comma
separator = rf"[{re.escape(PUNCTUATION_SENTENCE_COMMA)}]"
separator = rf"[{PUNCTUATION_SENTENCE_COMMA}]"
current_sentence = []
word_count = 0
+2 -6
View File
@@ -78,6 +78,7 @@ from abogen.subtitle_utils import (
sanitize_name_for_os,
split_text_by_voice_markers
)
from abogen.domain.split_pattern import PUNCTUATION_SENTENCE, PUNCTUATION_SENTENCE_COMMA, PUNCTUATION_COMMAS
class CountdownDialog(QDialog):
"""Base dialog with auto-accept countdown functionality"""
@@ -241,11 +242,6 @@ class ConversionThread(QThread):
log_updated = pyqtSignal(object) # Updated signal for log updates
chapters_detected = pyqtSignal(int) # Signal for chapter detection
# Punctuation constants for unified handling across languages
PUNCTUATION_SENTENCE = ".!?।。!?"
PUNCTUATION_SENTENCE_COMMA = ".!?,।。!?、,"
PUNCTUATION_COMMAS = ",,、"
def __init__(
self,
file_name,
@@ -932,7 +928,7 @@ class ConversionThread(QThread):
# For Sentence + Comma mode, still split on commas within spaCy sentences
if self.subtitle_mode == "Sentence + Comma":
active_split_pattern = r"(?<=[{}]){}|\n+".format(
self.PUNCTUATION_COMMAS, spacing_pattern
PUNCTUATION_COMMAS, spacing_pattern
)
else:
active_split_pattern = (