Files
Athena-Deck/tts_text_enhancer.py

43 lines
2.0 KiB
Python

"""Optional deterministic German speech preparation shared by API TTS backends.
Changes speech input only, never the displayed answer. No model or network calls.
This is normalization, not a guarantee of faithful audio synthesis.
"""
import re
from speech_text import expand_german_abbreviations, prepare_technical_german
def prepare(text, language='German'):
if not isinstance(text, str):
raise ValueError('TTS-Eingabetext muss Text sein.')
if str(language).strip().lower() not in ('german', 'de', 'de-de', 'deutsch'):
return text
# Protect HTTP URLs from the Unix-path rule. Speak link labels, not targets.
text = re.sub(r'!\[([^\]]*)\]\([^\n)]*\)', r'\1', text)
text = re.sub(r'\[([^\]]+)\]\([^\n)]*\)', r'\1', text)
protected = []
def protect(match):
protected.append(match.group(0))
return f'\x00URL{len(protected)-1}\x00'
text = re.sub(r'https?://[^\s`<>]+', protect, text)
text = prepare_technical_german(text)
def path(match):
return re.sub(r'[/_.-]', lambda m: {
'/': ' Schrägstrich ', '_': ' Unterstrich ',
'-': ' Bindestrich ', '.': ' Punkt '
}[m.group()], match.group(0))
text = re.sub(r'(?<![\w:/])/[A-Za-z0-9_.-]+(?:/[A-Za-z0-9_.-]+)*/?', path, text)
text = re.sub(r'^\s*[-*+]\s+', '', text, flags=re.M)
text = re.sub(r'\*\*(.*?)\*\*', r'\1', text, flags=re.S)
text = text.replace('`', '').replace('≠', ' ist nicht gleich ')
# Speak inline log fields too, after their markup has been removed.
text = prepare_technical_german(text)
text = re.sub(r'\bKI\b', 'Künstliche Intelligenz', text)
text = re.sub(r'\bHTTP\b', 'Ha Te Te Pe', text)
text = re.sub(r'\b403\b', 'vierhundertdrei', text)
text = text.replace('Labeling', 'Kennzeichnung').replace('User-Agent', 'User Agent')
text = expand_german_abbreviations(text)
for i, value in enumerate(protected):
text = text.replace(f'\x00URL{i}\x00', value)
return re.sub(r'\s+', ' ', text).strip()