mirror of
https://github.com/denizsafak/abogen.git
synced 2026-09-20 11:40:57 +02:00
- Create domain/text_utils.py with canonical calculate_text_length (strips chapter markers, voice markers, metadata tags) - Remove calculate_text_length from utils.py and subtitle_utils.py - Update all imports to use domain.text_utils directly - Create application/chapter_selection.py with build_chapter_payload() (orchestrates preselection + character count + safety net) - Replace manual chapter logic in form.py with build_chapter_payload() - 22 new tests for text_utils and chapter_selection (1550 total)
23 lines
748 B
Python
23 lines
748 B
Python
"""Text utility functions for the domain layer."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import re
|
|
|
|
# Pre-compiled patterns for calculate_text_length
|
|
_METADATA_TAG_PATTERN = re.compile(r"<<METADATA_[^:]+:[^>]*>>")
|
|
_CHAPTER_MARKER_PATTERN = re.compile(r"<<CHAPTER_MARKER:[^>]*>>")
|
|
_VOICE_MARKER_PATTERN = re.compile(r"<<VOICE:[^>]*>>")
|
|
|
|
|
|
def calculate_text_length(text: str) -> int:
|
|
"""Calculate character count, ignoring internal markers and newlines.
|
|
|
|
Strips chapter markers, voice markers, and metadata tags before counting.
|
|
"""
|
|
text = _CHAPTER_MARKER_PATTERN.sub("", text)
|
|
text = _VOICE_MARKER_PATTERN.sub("", text)
|
|
text = _METADATA_TAG_PATTERN.sub("", text)
|
|
text = text.replace("\n", "").strip()
|
|
return len(text)
|