extract normalize_text_for_pipeline to domain/normalization.py

This commit is contained in:
Artem Akymenko
2026-07-15 15:19:01 +00:00
parent 56cfd0810d
commit 7fef9c1d93
3 changed files with 36 additions and 20 deletions
+30
View File
@@ -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)
-14
View File
@@ -139,20 +139,6 @@ class AudioSink:
_APOSTROPHE_CONFIG = ApostropheConfig() _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( def _apply_m4b_chapters_with_mutagen(
audio_path: Path, audio_path: Path,
chapters: List[Dict[str, Any]], chapters: List[Dict[str, Any]],
+6 -6
View File
@@ -148,20 +148,20 @@ class TestNormalizeForPipeline:
"""normalize_for_pipeline normalizes text with runtime settings.""" """normalize_for_pipeline normalizes text with runtime settings."""
def test_basic_normalize(self): 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 isinstance(result, str)
assert len(result) > 0 assert len(result) > 0
def test_empty_string(self): 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 == "" assert result == ""
def test_with_overrides(self): 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) assert isinstance(result, str)