diff --git a/abogen/domain/normalization.py b/abogen/domain/normalization.py new file mode 100644 index 0000000..4bd34fd --- /dev/null +++ b/abogen/domain/normalization.py @@ -0,0 +1,30 @@ +"""Text normalization convenience helpers.""" + +from __future__ import annotations + +from typing import Any, Mapping, Optional + +from abogen.kokoro_text_normalization import ( + ApostropheConfig, + normalize_for_pipeline as _normalize_for_pipeline, +) +from abogen.normalization_settings import ( + build_apostrophe_config, + get_runtime_settings, + apply_overrides as _apply_overrides, +) + +_BASE_APOSTROPHE_CONFIG = ApostropheConfig() + + +def normalize_text_for_pipeline( + text: str, + *, + normalization_overrides: Optional[Mapping[str, Any]] = None, +) -> str: + """Normalize text using runtime settings with optional overrides.""" + runtime_settings = get_runtime_settings() + if normalization_overrides: + runtime_settings = _apply_overrides(runtime_settings, normalization_overrides) + apostrophe_config = build_apostrophe_config(settings=runtime_settings, base=_BASE_APOSTROPHE_CONFIG) + return _normalize_for_pipeline(text, config=apostrophe_config, settings=runtime_settings) diff --git a/abogen/webui/conversion_runner.py b/abogen/webui/conversion_runner.py index 9cc008f..5b19954 100644 --- a/abogen/webui/conversion_runner.py +++ b/abogen/webui/conversion_runner.py @@ -139,20 +139,6 @@ class AudioSink: _APOSTROPHE_CONFIG = ApostropheConfig() -def _normalize_for_pipeline( - text: str, - *, - normalization_overrides: Optional[Mapping[str, Any]] = None, -) -> str: - """Normalize text for tests or utilities with optional overrides.""" - - runtime_settings = get_runtime_settings() - if normalization_overrides: - runtime_settings = apply_normalization_overrides(runtime_settings, normalization_overrides) - apostrophe_config = build_apostrophe_config(settings=runtime_settings, base=_APOSTROPHE_CONFIG) - return normalize_for_pipeline(text, config=apostrophe_config, settings=runtime_settings) - - def _apply_m4b_chapters_with_mutagen( audio_path: Path, chapters: List[Dict[str, Any]], diff --git a/tests/test_chapter_merge_normalize.py b/tests/test_chapter_merge_normalize.py index 89054a8..9bfda73 100644 --- a/tests/test_chapter_merge_normalize.py +++ b/tests/test_chapter_merge_normalize.py @@ -148,20 +148,20 @@ class TestNormalizeForPipeline: """normalize_for_pipeline normalizes text with runtime settings.""" def test_basic_normalize(self): - from abogen.webui.conversion_runner import _normalize_for_pipeline + from abogen.domain.normalization import normalize_text_for_pipeline - result = _normalize_for_pipeline("Hello World") + result = normalize_text_for_pipeline("Hello World") assert isinstance(result, str) assert len(result) > 0 def test_empty_string(self): - from abogen.webui.conversion_runner import _normalize_for_pipeline + from abogen.domain.normalization import normalize_text_for_pipeline - result = _normalize_for_pipeline("") + result = normalize_text_for_pipeline("") assert result == "" def test_with_overrides(self): - from abogen.webui.conversion_runner import _normalize_for_pipeline + from abogen.domain.normalization import normalize_text_for_pipeline - result = _normalize_for_pipeline("test", normalization_overrides={"number_format": "words"}) + result = normalize_text_for_pipeline("test", normalization_overrides={"number_format": "words"}) assert isinstance(result, str)