# TTS normalization patterns Text-to-speech models often need their input normalized before tokenization: numbers, currencies, dates and symbols spelled out, punctuation mapped, spacing fixed. The exact rules are model-specific (they must match what the model was trained on), so they live in the model's `tts` task template rather than in C++. Jinja has no regex support, and exposing `std::regex` / Python `re` would not help: the two differ in many details (`\d`, `\w`, `\b`, lookbehind, case folding, backtracking limits), so a template could give different results in llama.cpp and in Python. Instead, task templates get two functions with a small matching language whose semantics are defined here: - `tts_norm_scan(text, rules)`: split text into matched and unmatched segments - `tts_norm_sub(text, pattern, repl)`: replace every match Implementations: - C++: [common/chat-tts-norm.cpp](../common/chat-tts-norm.cpp), injected into task templates by `common_chat_template_render()` in [common/chat.cpp](../common/chat.cpp). They are not available to chat templates. - Python reference: [scripts/chat_tts_norm.py](../scripts/chat_tts_norm.py), for use with `jinja2` (`env.globals["tts_norm_scan"] = chat_tts_norm.tts_norm_scan`) - Both are checked against each other by `tests/test-jinja.cpp` (`test-jinja -py "tts norm patterns.*"`) ## Example ```jinja {%- for s in tts_norm_scan(text, [ ['money', '%$(%d+)(?:%.(%d%d))?'], ['percent', '(%d+)%%'], ['time', '(%d%d?):(%d%d)(?!%d)'], ['neg', '(?