chat: allow TTS model to provide custom text normalizer via jinja (#32053)

* chat: allow TTS model to provide custom text normalizer via jinja

* rm tts_norm_is_reserved_name
This commit is contained in:
Xuan-Son Nguyen authored and GitHub committed 2026-11-11 17:34:55 +01:00
1 parent 956591f457
commit f7b886fe6f
7 files changed
+1769 -11

No files matched your search

+157 -11
View File
@@ -3,6 +3,7 @@
#include <random>
#include <cstdlib>
#include "chat-tts-norm.h"
#include "json.h"
#include "subproc.h"
@@ -16,8 +17,8 @@
using json = common_json;
static void test_template(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect);
static void test_template_error(testing & t, const std::string & name, const std::string & tmpl, const std::string & expect);
static void test_template(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect, bool tts_norm = false);
static void test_template_error(testing & t, const std::string & name, const std::string & tmpl, const std::string & expect, bool tts_norm = false);
static void test_whitespace_control(testing & t);
static void test_conditionals(testing & t);
@@ -39,6 +40,7 @@ static void test_stats(testing & t);
static void test_caps(testing & t);
static void test_string_parts(testing & t);
static void test_fuzzing(testing & t);
static void test_tts_norm_patterns(testing & t);
static bool g_python_mode = false;
@@ -75,6 +77,7 @@ int main(int argc, char *argv[]) {
t.test("string methods", test_string_methods);
t.test("array methods", test_array_methods);
t.test("object methods", test_object_methods);
t.test("tts norm patterns", test_tts_norm_patterns);
if (!g_python_mode) {
t.test("hasher", test_hasher);
t.test("stats", test_stats);
@@ -2713,13 +2716,134 @@ static void test_string_parts(testing & t) {
}
static void test_template_error(testing & t, const std::string & name, const std::string & tmpl, const std::string & expect) {
t.test(name, [&tmpl, &expect](testing & t) {
// TTS normalization patterns: tts_norm_scan() and tts_norm_sub(), see docs/tts-norm-patterns.md
// these functions are only available to task templates (e.g. "tts"), so every test here passes tts_norm = true
static void test_tts_norm_patterns(testing & t) {
test_template(t, "not available without tts norm builtins",
"{{ tts_norm_scan is defined }}",
json::object(),
"False");
test_template(t, "tts_norm_scan splits text into matched and unmatched segments",
"{%- for s in tts_norm_scan(text, [['num', '%d+'], ['word', '%a+']]) -%}[{{ s.rule or '-' }}:{{ s.text }}]{%- endfor -%}",
{{"text", "abc 123, x9"}},
"[word:abc][-: ][num:123][-:, ][word:x][num:9]", true);
test_template(t, "tts_norm_scan groups, optional group that did not match is empty",
"{%- for s in tts_norm_scan(text, [['money', '%$(%d+)(?:%.(%d%d))?']]) -%}"
"{%- if s.rule -%}{%- set whole, cents = s.groups -%}{{ whole }}|{{ cents }}|{{ s.groups | length }};{%- endif -%}"
"{%- endfor -%}",
{{"text", "$3.50 and $7"}},
"3|50|2;7||2;", true);
test_template(t, "tts_norm_scan rules as dict, the first rule that matches wins",
"{%- for s in tts_norm_scan(text, {'ordinal': '%d+(?:st|nd|rd|th)%b', 'number': '%d+'}) -%}"
"{%- if s.rule -%}<{{ s.rule }}:{{ s.text }}>{%- else -%}{{ s.text }}{%- endif -%}"
"{%- endfor -%}",
{{"text", "the 3rd of 10 or 4three"}},
"the <ordinal:3rd> of <number:10> or <number:4>three", true);
// unlike regex, a failed sequence does not retry the other branches: (?:a|ab)c never matches "abc"
test_template(t, "ordered alternation commits to the first branch that matches",
"{%- for s in tts_norm_scan(text, '(?:a|ab)c') -%}[{{ s.rule or '-' }}:{{ s.text }}]{%- endfor -%}",
{{"text", "abc ac"}},
"[-:abc ][match:ac]", true);
// unlike regex, quantifiers never give back what they consumed: %d+0 never matches
test_template(t, "quantifiers are possessive",
"{{ tts_norm_sub(text, '%d+0', 'X') }}|{{ tts_norm_sub(text, '%d+', 'X') }}",
{{"text", "100 20"}},
"100 20|X X", true);
test_template(t, "quantifier ranges",
"{{ tts_norm_sub(text, '%d{3}-%d{2,}', '#') }}",
{{"text", "123-4 123-45 123-456789"}},
"123-4 # #", true);
test_template(t, "lookbehind: a minus sign is not part of a word",
"{{ tts_norm_sub(text, '(?<!%w)-(%d+)', 'minus %1') }}",
{{"text", "GPT-5 is -5, (-3)"}},
"GPT-5 is minus 5, (minus 3)", true);
test_template(t, "lookahead: clock time not followed by a digit",
"{{ tts_norm_sub(text, '(%d%d?):(%d%d)(?!%d)', '%1h%2') }}",
{{"text", "at 10:30 or 1:234"}},
"at 10h30 or 1:234", true);
test_template(t, "tts_norm_sub references: group, whole match and literal percent",
"{{ tts_norm_sub(text, '(%d+)%%', '%1 percent (%0) 100%%') }}",
{{"text", "up 50%"}},
"up 50 percent (50%) 100%", true);
test_template(t, "split letters and digits, non-ASCII letters included",
"{{ tts_norm_sub(tts_norm_sub(text, '(%a)(%d)', '%1 %2'), '(%d)(%a)', '%1 %2') }}",
{{"text", "R2D2 café2 4K"}},
"R 2 D 2 café 2 4 K", true);
test_template(t, "case-insensitive pattern and word boundary",
"{{ tts_norm_sub(text, '(?i)%bdr%.', 'doctor') }}",
{{"text", "Dr. Who and dr. No, not Mdr."}},
"doctor Who and doctor No, not Mdr.", true);
test_template(t, "sets with multi-byte codepoints, negated sets with classes",
"{{ tts_norm_sub(text, '[$€£]%s*(%d+)', '<%1>') }}|{{ tts_norm_sub(text, '[^%w%s]', '') }}",
{{"text", "€ 5 & $6!"}},
"<5> & <6>!| 5 6", true);
test_template(t, "remove spaces between Han characters",
"{{ tts_norm_sub(text, '(%h) +(%h)', '%1%2') }}",
{{"text", "你 好 world 世界"}},
"你好 world 世界", true);
test_template(t, "mini text normalizer",
"{%- set ones = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten',"
" 'eleven', 'twelve', 'thirteen', 'fourteen', 'fifteen', 'sixteen', 'seventeen', 'eighteen', 'nineteen'] -%}\n"
"{%- set tens = ['', '', 'twenty', 'thirty', 'forty', 'fifty', 'sixty', 'seventy', 'eighty', 'ninety'] -%}\n"
"{%- macro num(n) -%}{{ ones[n] if n < 20 else tens[(n / 10) | int] ~ ('-' ~ ones[n % 10] if n % 10 else '') }}{%- endmacro -%}\n"
"{%- for s in tts_norm_scan(text, [\n"
" ['money', '%$(%d+)(?:%.(%d%d))?'],\n"
" ['percent', '(%d+)%%'],\n"
" ['time', '(%d%d?):(%d%d)(?!%d)'],\n"
" ['neg', '(?<!%w)-(%d+)'],\n"
" ['number', '%d+'],\n"
"]) -%}\n"
"{%- if s.rule == 'money' -%}{%- set whole, cents = s.groups -%}{{ num(whole | int) }} dollars{{ ' and ' ~ num(cents | int) ~ ' cents' if cents else '' }}\n"
"{%- elif s.rule == 'percent' -%}{{ num(s.groups[0] | int) }} percent\n"
"{%- elif s.rule == 'time' -%}{%- set h, m = s.groups -%}{{ num(h | int) }} {{ num(m | int) }}\n"
"{%- elif s.rule == 'neg' -%}minus {{ num(s.groups[0] | int) }}\n"
"{%- elif s.rule == 'number' -%}{{ num(s.text | int) }}\n"
"{%- else -%}{{ s.text }}\n"
"{%- endif -%}\n"
"{%- endfor -%}",
{{"text", "It costs $3.50, 20% off, at 10:45, -5 degrees, 7 cats."}},
"It costs three dollars and fifty cents, twenty percent off, at ten forty-five, minus five degrees, seven cats.", true);
if (!g_python_mode) {
test_template_error(t, "error: unclosed group", "{{ tts_norm_scan('x', '(a') }}", "expected ')' to close group", true);
test_template_error(t, "error: unbalanced parenthesis", "{{ tts_norm_scan('x', 'a)') }}", "unbalanced ')'", true);
test_template_error(t, "error: nothing to repeat", "{{ tts_norm_scan('x', '*a') }}", "nothing to repeat", true);
test_template_error(t, "error: multiple quantifiers", "{{ tts_norm_scan('x', 'a+?') }}", "multiple quantifiers", true);
test_template_error(t, "error: bad quantifier range", "{{ tts_norm_scan('x', 'a{2,1}') }}", "quantifier max is smaller than min", true);
test_template_error(t, "error: repeated assertion", "{{ tts_norm_scan('x', '%b+') }}", "zero-width assertion cannot be repeated", true);
test_template_error(t, "error: unknown class", "{{ tts_norm_scan('x', '%q') }}", "unknown escape '%q'", true);
test_template_error(t, "error: unescaped bracket", "{{ tts_norm_scan('x', 'a]') }}", "unescaped ']'", true);
test_template_error(t, "error: no named groups", "{{ tts_norm_scan('x', '(?<n>a)') }}", "unknown group type", true);
test_template_error(t, "error: wide lookbehind", "{{ tts_norm_scan('x', '(?<=ab)c') }}", "lookbehind must contain exactly one codepoint matcher", true);
test_template_error(t, "error: missing group in replacement", "{{ tts_norm_sub('x', '(a)', '%2') }}", "group %2 does not exist", true);
test_template_error(t, "error: bad rules", "{{ tts_norm_scan('x', 1) }}", "rules must be a string, a dict or a list", true);
}
}
static void test_template_error(testing & t, const std::string & name, const std::string & tmpl, const std::string & expect, bool tts_norm) {
t.test(name, [&tmpl, &expect, tts_norm](testing & t) {
std::string error;
try {
jinja::lexer lexer;
jinja::program ast = jinja::parse_from_tokens(lexer.tokenize(tmpl));
jinja::context ctx(tmpl);
if (tts_norm) {
common_chat_tts_norm_add_builtins(ctx);
}
jinja::runtime runtime(ctx);
runtime.gather_string_parts(runtime.execute(ast));
} catch (const std::exception & e) {
@@ -2729,14 +2853,17 @@ static void test_template_error(testing & t, const std::string & name, const std
});
}
static void test_template_cpp(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect) {
t.test(name, [&tmpl, &vars, &expect](testing & t) {
static void test_template_cpp(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect, bool tts_norm) {
t.test(name, [&tmpl, &vars, &expect, tts_norm](testing & t) {
jinja::lexer lexer;
auto lexer_res = lexer.tokenize(tmpl);
jinja::program ast = jinja::parse_from_tokens(lexer_res);
jinja::context ctx(tmpl);
if (tts_norm) {
common_chat_tts_norm_add_builtins(ctx);
}
jinja::global_from_json(ctx, vars, true);
jinja::runtime runtime(ctx);
@@ -2788,17 +2915,36 @@ env.filters["tojson"] = lambda x, ensure_ascii=False, indent=None, separators=No
env.globals["strftime_now"] = lambda format: datetime.now().strftime(format)
env.globals["raise_exception"] = raise_exception
if merged_input.get("tts_norm_ref"):
import importlib.util
spec = importlib.util.spec_from_file_location("chat_tts_norm", merged_input["tts_norm_ref"])
chat_tts_norm = importlib.util.module_from_spec(spec)
sys.modules["chat_tts_norm"] = chat_tts_norm # required by dataclasses
spec.loader.exec_module(chat_tts_norm)
env.globals["tts_norm_scan"] = chat_tts_norm.tts_norm_scan
env.globals["tts_norm_sub"] = chat_tts_norm.tts_norm_sub
template = env.from_string(tmpl)
result = template.render(**vars_json)
sys.stdout.buffer.write(result.encode())
)";
static void test_template_py(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect) {
t.test(name, [&tmpl, &vars, &expect](testing & t) {
// python reference of the TTS normalization patterns, relative to this file
static std::string tts_norm_ref_path() {
std::string path = __FILE__;
const size_t pos = path.find_last_of("/\\");
return (pos == std::string::npos ? std::string(".") : path.substr(0, pos)) + "/../scripts/chat_tts_norm.py";
}
static void test_template_py(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect, bool tts_norm) {
t.test(name, [&tmpl, &vars, &expect, tts_norm](testing & t) {
// Prepare arguments
json merged;
merged["tmpl"] = json(tmpl);
merged["vars"] = vars;
if (tts_norm) {
merged["tts_norm_ref"] = tts_norm_ref_path();
}
#ifdef _WIN32
const char * python_executable = "python.exe";
@@ -2859,11 +3005,11 @@ static void test_template_py(testing & t, const std::string & name, const std::s
});
}
static void test_template(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect) {
static void test_template(testing & t, const std::string & name, const std::string & tmpl, const json & vars, const std::string & expect, bool tts_norm) {
if (g_python_mode) {
test_template_py(t, name, tmpl, vars, expect);
test_template_py(t, name, tmpl, vars, expect, tts_norm);
} else {
test_template_cpp(t, name, tmpl, vars, expect);
test_template_cpp(t, name, tmpl, vars, expect, tts_norm);
}
}