diff --git a/common/chat-tts-norm.cpp b/common/chat-tts-norm.cpp
index 2ec93a204..b828ed9b4 100644
--- a/common/chat-tts-norm.cpp
+++ b/common/chat-tts-norm.cpp
@@ -871,7 +871,7 @@ bool common_tts_dict_find(const common_tts_dict & dict, const std::string & key,
return true;
}
-void common_chat_tts_norm_add_builtins(context & ctx, std::shared_ptr dicts) {
+void common_chat_tts_norm_add_builtins(context & ctx, std::shared_ptr dicts, common_tts_g2p g2p) {
ctx.set_val("tts_norm_scan", mk_val("tts_norm_scan", tts_norm_builtin_scan));
ctx.set_val("tts_norm_sub", mk_val("tts_norm_sub", tts_norm_builtin_sub));
ctx.set_val("tts_dict", mk_val("tts_dict", [dicts](const func_args & args) -> value {
@@ -888,5 +888,15 @@ void common_chat_tts_norm_add_builtins(context & ctx, std::shared_ptr("tts_g2p", [g2p](const func_args & args) -> value {
+ args.ensure_count(1, 1);
+ args.ensure_vals();
+ const std::string out = g2p ? g2p(args.get_pos(0)->as_string().str()) : std::string();
+ if (out.empty()) {
+ return mk_val();
+ }
+ return mk_val(out);
+ }));
}
diff --git a/common/chat-tts-norm.h b/common/chat-tts-norm.h
index 84b16018c..b96486078 100644
--- a/common/chat-tts-norm.h
+++ b/common/chat-tts-norm.h
@@ -1,6 +1,7 @@
#pragma once
#include
+#include
#include
", "", "Vec". The "<" and ">" that are left may compare two things -#}
+{%- macro without_angles(text) -%}
+{%- set a = namespace(s=text, done=false) -%}
+{%- for _ in range(8) -%}
+ {%- if not a.done and '<' in a.s and '>' in a.s -%}
+ {%- set marked = tts_norm_sub(a.s, '(?(){}+*;|]*)>|(?<=[%w_⟧⟡])<([^<>(){}+*;|]{1,80})>(?![%w_$])|<(/?[A-Za-z][%w_%-]+/?)>', '\x01%1%2%3\x01') -%}
+ {%- set a.done = marked == a.s -%}
+ {%- set a.s = marked -%}
+ {%- endif -%}
+{%- endfor -%}
+{{ a.s }}
+{%- endmacro -%}
+
+{#- the words for a comparison sign, after "is" where the mark for it stands before the sign -#}
+{%- macro compared(marked, words) -%}{{ ' is ' if marked else ' ' }}{{ words }}{{ ' ' }}{%- endmacro -%}
+
+{#- put the mark for "is" after the first value of each comparison between two values: "67 > 66", "b>a", "f(x) >= 0".
+ Not where a word that opens no clause stands before that value: "for n > 4", "any q > 0" -#}
+{%- macro nz_mark_comparisons(text) -%}
+{%- set plain = without_angles(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, [['spaced', ' (>=|<=|!=|[<>≥≤≠]) '], ['joined', '(>=|<=|!=|[<>≥≤≠])']]) -%}
+ {%- set mark = namespace(v=false) -%}
+ {%- if m.rule -%}
+ {%- set at = o.pos + (1 if m.rule == 'spaced' else 0) -%}
+ {%- set before = plain[:o.pos] -%}
+ {%- set value = tts_norm_scan(before, "(?:(? 0 and value[-1].rule
+ and starts(plain[o.pos + m.text | length:], "[-+]?[$£€¥₹]?%d+(?:[.,%%]|(?![%w_]))|%.%d|[A-Za-z](?![%w_'’])|%(") -%}
+ {%- if value[-1].text == ')' -%}
+ {#- "f(x) > 0": the whole of it is the value -#}
+ {%- set cs = tts_norm_scan(before, '.') | map(attribute='text') | list -%}
+ {%- set st = namespace(i=cs | length - 1, depth=0, done=false) -%}
+ {%- for _ in cs -%}
+ {%- if not st.done and st.i >= 0 -%}
+ {%- set st.depth = st.depth + (1 if cs[st.i] == ')' else 0) - (1 if cs[st.i] == '(' else 0) -%}
+ {%- if st.depth == 0 -%}{%- set st.done = true -%}{%- else -%}{%- set st.i = st.i - 1 -%}{%- endif -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- for _ in cs -%}
+ {%- if st.i > 0 and cs[st.i - 1] in 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.' -%}{%- set st.i = st.i - 1 -%}{%- endif -%}
+ {%- endfor -%}
+ {%- set head = cs[:[st.i, 0] | max] | join('') -%}
+ {%- else -%}{%- set head = before[:before | length - value[-1].text | length] -%}
+ {%- endif -%}
+ {%- set opener = tts_norm_scan(head | trim, "[A-Za-z']+$") -%}
+ {%- set mark.v = not (opener | length > 0 and opener[-1].rule and opener[-1].text | lower not in CLAUSE_OPENERS) -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- set o.s = o.s ~ ('⟡' if mark.v else '') ~ m.text -%}
+ {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- "b>a", "x<5", "p<.05": the words for "<" and ">" written with no spaces. A comparison when a figure stands on one
+ side, or a single letter or a bracket on both; "File>Open" is none -#}
+{%- macro nz_joined_comparisons(text) -%}
+{%- set plain = without_angles(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(?<=[%w_)⟧⟡])[<>](?=[%w_(⟦]|%.%d| minus %d)') -%}
+ {%- set read = namespace(v=false) -%}
+ {%- if m.rule and plain[o.pos:o.pos + 1] == m.text -%}
+ {%- set before = tts_norm_sub(text[:o.pos], '⟡+$', '') -%}
+ {%- set after = text[o.pos + 1:] -%}
+ {%- set read.v = ends(before, '(?a", "(k)". "A", "a" and the small letters
+ chat writes for a word ("k", "u", "r") are read as words elsewhere -#}
+{%- set LETTER_SIGN = '[<>+*/^≤≥≠≈×÷±]' -%}
+{%- set LETTER_SPACED = ' [<>+*≤≥≠≈×÷±] ' -%}
+{#- after "=" or a spaced sign the letter must not open a phrase ("mode = a set of notes") -#}
+{%- set NO_PHRASE = "(?![%w_'’%-]|%s+(?!(?:and|or|then|if|when|where|while|for|is|are|was|but|so|in|to)(?![%w_]))[A-Za-z])" -%}
+{%- set LETTER_PLACES = [
+ ['plain', "(?!=]=|=))"],
+ ['plain', "(?:[<>!=]=|=)%s?[Aakur]" ~ NO_PHRASE],
+ ['plain', "(?+*/^()≤≥≠≈×÷±]') -%}
+ {%- set plain = without_angles(text) -%}
+ {%- set found = namespace(at=[]) -%}
+ {%- for place in LETTER_PLACES -%}
+ {%- set sc = namespace(pos=0) -%}
+ {%- for m in tts_norm_scan(plain, place[1]) -%}
+ {%- set sc.pos = sc.pos + m.text | length -%}
+ {%- if m.rule -%}
+ {#- the letter is the last character matched -#}
+ {%- set before = plain[:sc.pos - 1] -%}
+ {%- if (place[0] == 'plain' or (place[0] == 'after_sign' and ends(before, '[%w_)⟡]' ~ LETTER_SIGN) and not ends(before, '[Ww]/'))
+ or (place[0] == 'after_comma' and before.endswith(', '))) and sc.pos - 1 not in found.at -%}
+ {%- set found.at = found.at + [sc.pos - 1] -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- endfor -%}
+ {%- set o = namespace(s='', pos=0) -%}
+ {%- for at in found.at | sort -%}
+ {%- set o.s = o.s ~ text[o.pos:at] ~ spell_mark(text[at:at + 1] | upper) -%}
+ {%- set o.pos = at + 1 -%}
+ {%- endfor -%}
+ {{ o.s ~ text[o.pos:] }}
+{%- else -%}{{ text }}
+{%- endif -%}
+{%- endmacro -%}
+
+{#- U.S., e.g., p.m.: letters with full stops -#}
+{%- macro nz_dotted(text) -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '(? 0 and previous[-1].rule and previous[-1].groups[0] | lower in NUMBERED -%}
+ {%- set use.v = stop and (number_next or (key == 'no' and numbered)) -%}
+ {%- elif not stop or (when == 'small' and is_upper(token)) -%}{%- set use.v = false -%}
+ {%- elif key in ['mar', 'sun', 'sat', 'jan', 'dec', 'max', 'min', 'est', 'no'] and token[:1] not in UPPER and not number_next -%}
+ {#- also words; "max. load" and "est. time" are the abbreviation, "he sat." is not -#}
+ {%- set use.v = key in ['max', 'min', 'est'] and next_word and next_word[:1] in LOWER -%}
+ {%- endif -%}
+ {%- if use.v -%}
+ {#- "Est. arrival"; "est. 1885" is "established" -#}
+ {%- set words = 'estimated' if key == 'est' and not number_next else entry_[1] -%}
+ {%- set closes = when != 'title' and (not following | trim or (capital_next and next_word | lower in SENTENCE_STARTERS)) -%}
+ {%- set said.v = ' ' ~ words ~ (' .' if closes else '') ~ ' ' -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- set o.s = o.s ~ said.v -%}
+ {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- letters and digits written together: B12, 14C, X7K, PS5. A single letter is always named -#}
+{%- macro nz_glued(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, "(? 2 -%}
+ {%- set kind = identifier(text, o.pos) -%}
+ {%- set limit = 3 if kind in ['phone', 'pin'] else (4 if kind in ['strong', 'enter'] else (5 if kind == 'weak' else 7)) -%}
+ {#- a reference, not an amount -#}
+ {%- if plain | length >= limit and not (not kind and plain.endswith('000'))
+ and not (kind == 'enter' and plain | length == 4 and plain | int >= 1900 and plain | int <= 2099)
+ and not (kind in ['', 'phone', 'weak'] and counted(after)) -%}
+ {%- set said.v = digits(plain, 'oh' if kind == 'phone' else 'zero') -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- if not said.v and whole and plain | length == 4 -%}
+ {%- set number = plain | int -%}
+ {%- set before = text[:o.pos] -%}
+ {%- set near = namespace(last='') -%}
+ {%- for w in tts_norm_scan(before[[0, before | length - 24] | max:], '[A-Za-z]+') -%}{%- if w.rule -%}{%- set near.last = w.text | lower -%}{%- endif -%}{%- endfor -%}
+ {%- set next_word = at_start(after, '%s*([A-Za-z]+)', 0) | lower -%}
+ {%- set many = next_word and ((next_word.endswith('s') and not next_word.endswith('ss') and next_word | length > 1)
+ or next_word.endswith('people') or next_word.endswith('men') or next_word.endswith('children')
+ or next_word.endswith('feet') or next_word.endswith('miles')) -%}
+ {%- set dated = near.last and (near.last in YEAR_CONTEXT or near.last[:3] in MONTH_NUMBER) -%}
+ {%- set model = tts_norm_scan(before, '(? 0 and model[-1].rule and model[-1].groups[0] not in CAPITAL_WORDS and not many -%}{%- set said.v = pairs(number) -%}
+ {%- elif (number >= 1100 and number <= 1999) or (number >= 2000 and number <= 2099 and (dated or not many)) -%}{%- set said.v = year(number) -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- set o.s = o.s ~ ' ' ~ (said.v if said.v else number_words(value)) ~ ' ' -%}
+ {%- else -%}{%- set o.s = o.s ~ m.text -%}
+ {%- endif -%}
+ {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{%- macro nz_signs(text) -%}
+{%- set text = nz_named_letters(text) -%}
+{%- set text = nz_joined_comparisons(text) if '<' in text or '>' in text else text -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, '(?=|<=|!=)(?=[%w_%s⟦])') -%}
+ {%- set o.s = o.s ~ (compared(ends(text[:o.pos], '⟡ *'), COMPARISONS[m.text]) if m.rule else m.text) -%}
+ {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{#- RTX 4070 Ti -#}
+{%- set text = tts_norm_sub(o.s, '(?<=%d) Ti' ~ NOT_WORD, ' ⟦TI⟧') -%}
+{%- set text = tts_norm_sub(text, '(?]%s?(?=%d)|(?<=%s)[<>](?=%s)') -%}
+ {%- set o.s = o.s ~ (compared(m.text.startswith('⟡') or ends(text[:o.pos], '⟡ *'), 'less than' if '<' in m.text else 'greater than') if m.rule else m.text) -%}
+ {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{%- set text = o.s -%}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, '[&@+=%%×÷±©§°≥≤≠≈∞√π]') -%}
+ {%- if m.rule -%}
+ {%- set o.s = o.s ~ (compared(ends(text[:o.pos], '⟡ *'), SYMBOL_WORDS[m.text]) if m.text in '≥≤≠' else (' ' ~ SYMBOL_WORDS[m.text] ~ ' ')) -%}
+ {%- else -%}{%- set o.s = o.s ~ m.text -%}
+ {%- endif -%}
+ {%- set o.pos = o.pos + m.text | length -%}
+{%- endfor -%}
+{{ o.s | replace('⟡', '') }}
+{%- endmacro -%}
+
+{#- ---------- web addresses and file names ---------- -#}
+{#- one run of letters inside an address: a word, or letters to name -#}
+{%- macro address_part(part) -%}
+{%- set lower = part | lower -%}
+{%- if part | length > 1 and is_word(lower) and (part | length > 2 or lower in SHORT_WORDS) -%}{{ part }}
+{%- else -%}{{ spell_mark(part) if part | length <= 3 else part }}
+{%- endif -%}
+{%- endmacro -%}
+
+{%- macro spoken_address(core) -%}
+{%- set pieces = tts_norm_scan(core, [['l', '[A-Za-z]+'], ['d', '%d+'], ['o', '.']]) -%}
+{%- set mail = '@' in core -%}
+{%- set a = namespace(out=[], host=false) -%}
+{%- for p in pieces -%}
+ {%- set piece = p.text -%}
+ {%- set following = pieces[loop.index0 + 1].text if not loop.last else '' -%}
+ {%- if piece == '@' -%}{%- set a.host = true -%}{%- endif -%}
+ {%- if p.rule == 'd' -%}{%- set a.out = a.out + [address_digits(piece)] -%}
+ {%- elif p.rule != 'l' -%}{%- set a.out = a.out + [SEPARATORS[piece] if piece in SEPARATORS else ''] -%}
+ {%- elif loop.index0 > 0 and pieces[loop.index0 - 1].text == '.' and (a.host or not mail)
+ and (following in ['', '/', ':', '?', '#'] or (piece | length <= 3 and (tts_dict('endings', piece | lower) is not none or not is_word(piece | lower)))) -%}
+ {#- the ending of a file name or an address: a word if the table says so, otherwise letters -#}
+ {%- set a.out = a.out + [piece | lower if tts_dict('endings', piece | lower) == 'word' else spell_mark(piece)] -%}
+ {%- else -%}
+ {%- set run = namespace(letters='') -%}
+ {%- for part in tts_norm_scan(piece, '[A-Z]?[a-z]+|(?:[A-Z](?![a-z]))+') -%}
+ {%- if part.rule -%}
+ {%- set said = address_part(part.text) -%}
+ {%- if said.startswith('⟦') -%}{%- set run.letters = run.letters ~ part.text -%}
+ {%- else -%}
+ {%- if run.letters -%}{%- set a.out = a.out + [spell_mark(run.letters)] -%}{%- set run.letters = '' -%}{%- endif -%}
+ {%- set a.out = a.out + [said] -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- if run.letters -%}{%- set a.out = a.out + [spell_mark(run.letters)] -%}{%- endif -%}
+ {%- endif -%}
+{%- endfor -%}
+{%- set kept = namespace(p=[]) -%}
+{%- for item in a.out -%}{%- if item -%}{%- set kept.p = kept.p + [item] -%}{%- endif -%}{%- endfor -%}
+{{ kept.p | join(' ') }}
+{%- endmacro -%}
+
+{#- web and mail addresses, file names and paths: the dots, slashes, underscores and @ are spoken. The ending is a
+ word when the table says so ("dot com") and letters otherwise ("dot P D F") -#}
+{#- the ending of a file name: a dot, a letter, up to nine letters or figures; a part of the name that leaves an
+ ending after it. So the name stops before its last possible ending, as in dizione, and a name with no possible
+ ending is no match at all (the search goes on inside it: "1.0bpmhttp://...") -#}
+{%- set FILE_ENDING = '%.[A-Za-z][A-Za-z0-9]{0,9}(?![%w_%-])' -%}
+{%- set FILE_PART = '%.[%w_%-]+(?=(?:(?!' ~ FILE_ENDING ~ ')%.[%w_%-]+)*' ~ FILE_ENDING ~ ')' -%}
+{%- macro nz_address(text) -%}
+{%- set o = namespace(s='') -%}
+{%- for m in tts_norm_scan(text, [
+ ['url', "(?:https?://|www%.)[^%s<>\"']+"],
+ ['mail', "[%w_.+%-]+@[%w_%-]+(?:%.[%w_%-]+)+"],
+ ['file', "(?\"']*)?"],
+]) -%}
+ {%- set said = namespace(v=m.text) -%}
+ {%- if m.rule -%}
+ {%- set ok = namespace(v=true) -%}
+ {%- if m.rule == 'file' -%}
+ {%- set stem = m.groups[1] -%}{%- set ending = m.groups[2] -%}
+ {%- set listed = tts_dict('endings', ending | lower) is not none -%}
+ {%- set marked = m.groups[0] or m.groups[3] or '_' in stem -%}
+ {#- "the end.Then": a missing space, not a file -#}
+ {%- set ok.v = listed or (marked and ending | length <= 5 and (ending == ending | lower or ending == ending | upper) and not is_word(ending | lower)) -%}
+ {%- endif -%}
+ {%- if ok.v -%}
+ {#- punctuation of the sentence, not of the address -#}
+ {%- set body = tts_norm_sub(m.text, '[.,;:!?)]+$', '') -%}
+ {%- set core = tts_norm_sub(tts_norm_sub(tts_norm_sub(body, '^https?://', ''), '^www%.', ''), '/+$', '') -%}
+ {%- set said.v = ' ' ~ spoken_address(core) ~ ' ' ~ m.text[body | length:] -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- set o.s = o.s ~ said.v -%}
+{%- endfor -%}
+{{ o.s }}
+{%- endmacro -%}
+
+{#- the text of one sentence with every non-word written out, marks included (normalize.py: rewrite). The text has
+ had its accents, quotation marks, brackets and inline pronunciations dealt with -#}
+{%- macro nz_rewrite(text) -%}
+{%- set text = nz_address(text) -%}
+{%- set text = tts_norm_sub(text | replace('→', ' to '), '[🀀-☀-➿←-⇿⬀-⯿️⃣]+', ' ') -%}
+{#- faces are not read and make no pause: ":)", ";-)", "<3", "^_^"; a heart joined to
+ a word of two letters or more: "you<3" -#}
+{%- set o = namespace(s='', pos=0) -%}
+{%- for m in tts_norm_scan(text, [
+ ['mouth', "(?<]+)"],
+ ['face', "(?_<|T_T|\\o/|:'[()])(?![A-Za-z0-9])|[:;]['^%-]?[)(]+|(?≥≤≠]|!=') else text -%}
+{#- most text has no digit and skips these passes -#}
+{%- set text = nz_figures(text) if has(text, '%d') else text -%}
+{{ nz_signs(text) }}
+{%- endmacro -%}
+
+{#- ---------- sentences (tiny-voca infer.py: sentences): each one is read by itself ----------
+ no break after a title or an initial, and only before a capital or a figure -#}
+{%- set whole = tts_norm_sub(messages[0].content if messages is defined and messages else '', '%s+', ' ') | trim -%}
+{#- a text that ends without a mark gets a full stop: without one the audio breaks off (infer.py: finished).
+ A face or another sign after the last word is no mark ("see you :)"), a comma or a colon is one -#}
+{%- set last = namespace(w='') -%}
+{%- for seg in tts_norm_scan(whole, '%S+') -%}{%- if seg.rule -%}{%- set last.w = seg.text -%}{%- endif -%}{%- endfor -%}
+{%- if last.w -%}
+ {%- set lw = namespace(word=false, stop=false) -%}
+ {%- for seg in tts_norm_scan(last.w, [['word', '[%w²³¹¼½¾⅐-⅞]'], ['stop', '[.!?…]']]) -%}
+ {%- if seg.rule == 'word' -%}{%- set lw.word = true -%}{%- elif seg.rule == 'stop' -%}{%- set lw.stop = true -%}{%- endif -%}
+ {%- endfor -%}
+ {%- set closed = tts_norm_scan(last.w, '[.!?…,;:]["\'”’)%]»]*$') -%}
+ {%- set marked = (closed | length > 0 and closed[-1].rule is not none) if lw.word else (lw.stop or last.w in [',', ';', ':']) -%}
+ {%- if not marked -%}{%- set whole = whole ~ '.' -%}{%- endif -%}
+{%- endif -%}
+{%- set sp = namespace(parts=[], cur='') -%}
+{%- for seg in tts_norm_scan(whole,
+ [['end', '([.!?…]["\')%]]?)%s+(?=["\'(%[]?[A-Z0-9])']]) -%}
+ {%- if seg.rule -%}
+ {%- set sp.cur = sp.cur ~ seg.groups[0] -%}
+ {%- set held = tts_norm_scan(sp.cur, '(?i)(?= 3 and 5 * caps.up >= 3 * caps.n -%}
+
+{%- set segs = tts_norm_scan(text, [
+ ['spelled', '⟦([A-Za-z ]+)⟧'],
+ ['inline', '⟪(.)⟫'],
+ ['aword', "(?i)'(?:em|tis|twas|til|cause|bout|round|n)(?![A-Za-z]|'[A-Za-z])"],
+ ['word', "[A-Za-z]+(?:'[A-Za-z]+)*"],
+ ['number', '%d+'],
+ ['join', '(?<=%S)-(?=%w)'],
+ ['hyphen', '-'],
+ ['inside', "(?<=%w)'(?=%w)(?![A-Za-z])"],
+ ['punct', '[,.?!;:"\'()—…]'],
+]) -%}
+{#- attach: a hyphen joins the next word; previous: the word before as written; fresh: a sentence starts here;
+ short: where "Ss" or "Zz" stands apart (perhaps a hiss); replies: where a chat spelling read only as a reply stands -#}
+{%- set it = namespace(items=[], attach=false, previous='', fresh=true, short=[], replies=[]) -%}
+{%- for seg in segs -%}
+ {%- set k = loop.index0 -%}
+ {%- set t = namespace(new=[], word='', done=false) -%}
+ {%- if seg.rule == 'join' -%}
+ {%- set it.attach = it.items | length > 0 and it.items[-1].w is defined -%}
+ {%- elif seg.rule == 'hyphen' -%}
+ {%- set it.attach = false -%}
+ {%- elif seg.rule == 'punct' -%}
+ {%- set it.items = it.items + [{'p': seg.text}] -%}
+ {%- if seg.text in '.!?;…' -%}{%- set it.fresh = true -%}{%- endif -%}
+ {%- set it.attach = false -%}
+ {%- elif seg.rule -%}
+ {%- if seg.rule == 'word' -%}{%- set t.word = seg.text -%}
+ {%- elif seg.rule == 'aword' -%}{%- set t.word = seg.text if is_word(seg.text | lower) else seg.text[1:] -%}
+ {%- elif seg.rule == 'inline' -%}
+ {%- for key in INLINE_KEYS -%}{%- if key == seg.groups[0] and loop.index0 < il.list | length -%}{%- set t.new = [il.list[loop.index0]] -%}{%- endif -%}{%- endfor -%}
+ {%- elif seg.rule == 'spelled' -%}
+ {%- set t.new = [{'w': seg.groups[0] | replace(' ', '') | lower, 'r': seg.groups[0], 'a': false, 'spell': true, 'ps': ''}] -%}
+ {%- elif seg.rule == 'number' -%}
+ {%- for x in number_words(seg.text).split(' ') -%}{%- if x -%}{%- set t.new = t.new + [{'w': x, 'r': seg.text, 'a': false, 'spell': false, 'ps': ''}] -%}{%- endif -%}{%- endfor -%}
+ {%- endif -%}
+ {%- if t.word -%}
+ {#- one run of letters and apostrophes as written -#}
+ {%- set raw = t.word -%}
+ {%- set lower = raw | lower -%}
+ {%- set chat = tts_dict('chat', lower) -%}
+ {%- set possessive = lower.endswith("'s") -%}
+ {%- set stem = raw[:-2] if possessive else raw -%}
+ {%- set n = stem | length -%}
+ {%- if chat is not none and (raw | length > 1 or raw == lower) and not chat[1] -%}
+ {%- if chat[0] != '-' -%}
+ {%- for part in chat[0].split(' ') -%}
+ {%- set t.new = t.new + [{'w': part[1:] | lower, 'r': raw, 'a': false, 'spell': true, 'ps': ''} if part.startswith('*')
+ else {'w': part | lower, 'r': part, 'a': false, 'spell': false, 'ps': ''}] -%}
+ {%- endfor -%}
+ {%- endif -%}
+ {%- elif raw in ROMAN and it.previous and it.previous[:1] in UPPER -%}
+ {#- "World War II", "Henry VIII" -#}
+ {%- set said = cardinal(ROMAN[raw]) if it.previous | lower in CARDINAL_AFTER else 'the ' ~ ordinal(ROMAN[raw] | string) -%}
+ {%- for x in said.split(' ') -%}{%- set t.new = t.new + [{'w': x, 'r': raw, 'a': false, 'spell': false, 'ps': ''}] -%}{%- endfor -%}
+ {%- elif n == 1 -%}
+ {#- "A" is the article unless it labels something: "Plan A.", "vitamin A" -#}
+ {%- set label = stem == 'A' and it.previous and (not ahead(segs, k, '%s*[A-Za-z]') or (not it.fresh and it.previous | lower in LABELLED)) -%}
+ {%- set t.new = [{'w': lower, 'r': raw, 'a': false, 'spell': not (stem in ['a', 'A', 'I', 'i'] and not possessive and not label), 'ps': ''}] -%}
+ {%- elif n > 2 and stem[:1] in 'SsZz' and lower | replace(lower[:1], '') == '' and (all_caps or not is_upper(stem)) -%}
+ {#- "Sss", "Zzzz": a hiss, a snore -#}
+ {%- set t.new = [{'w': lower, 'r': raw, 'a': false, 'spell': false, 'ps': lower[:1]}] -%}
+ {%- else -%}
+ {%- set known_stem = is_word(stem | lower) -%}
+ {%- if is_upper(stem) and "'" not in stem and n >= 2 and n <= 5 and (not all_caps or not known_stem) and (stem in INITIALISMS or not known_stem) -%}
+ {%- set t.new = [{'w': lower, 'r': raw, 'a': false, 'spell': true, 'ps': ''}] -%}{%- set t.done = true -%}
+ {%- endif -%}
+ {#- iOS, macOS, eSIM: small letters, then capitals that are named. The dictionary's "ios" is another word -#}
+ {%- set capitals = fullmatch(raw, "[a-z]+(?:[A-Z][a-z]+)*[A-Z]{2,}(?:'s)?") -%}
+ {%- if not t.done and (capitals or not is_word(lower)) -%}
+ {%- if not capitals -%}
+ {%- for candidate in unstretched(lower).split(' ') -%}
+ {%- if not t.done and candidate and is_word(candidate) -%}
+ {%- set t.new = [{'w': candidate, 'r': raw, 'a': false, 'spell': false, 'ps': ''}] -%}{%- set t.done = true -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- endif -%}
+ {%- if not t.done -%}
+ {#- iPhone, YouTube, JavaScript -#}
+ {%- set parts = tts_norm_scan(raw, "[A-Z]?[a-z']+|(?:[A-Z](?![a-z]))+") -%}
+ {%- set whole = namespace(v=true) -%}
+ {%- for part in parts -%}{%- if not part.rule -%}{%- set whole.v = false -%}{%- endif -%}{%- endfor -%}
+ {%- if whole.v and parts | length > 1 -%}
+ {%- for p in parts -%}
+ {%- set part = p.text -%}
+ {%- set named = capitals and is_upper(part) and part in INITIALISMS -%}
+ {%- set spelled = part | length == 1 or named or (is_upper(part) and not is_word(part | lower)) -%}
+ {%- set spelled = true if spelled else false -%}
+ {%- if capitals and t.new and t.new[-1].spell and (spelled or part == "'s") -%}
+ {%- set before = t.new[-1] -%}
+ {%- set t.new = t.new[:-1] + [{'w': before.w ~ part | lower, 'r': before.r ~ part, 'a': before.a, 'spell': true, 'ps': ''}] -%}
+ {%- else -%}
+ {%- set t.new = t.new + [{'w': part | lower, 'r': part, 'a': t.new | length > 0, 'spell': spelled, 'ps': ''}] -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- set t.done = true -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- if not t.done -%}{%- set t.new = [{'w': lower, 'r': raw, 'a': false, 'spell': false, 'ps': ''}] -%}{%- endif -%}
+ {%- endif -%}
+ {%- set it.previous = raw -%}
+ {%- set kb = (k - 2) if seg.rule == 'word' and k > 0 and segs[k - 1].rule == 'punct' and segs[k - 1].text == "'" else (k - 1) -%}
+ {%- set before = segs[kb].text[-1:] if kb >= 0 else ' ' -%}
+ {%- set apart = before in [' ', '\t', '\n', '\r', '"', "'"] -%}
+ {%- if raw in ['Ss', 'ss', 'Zz', 'zz'] and t.new | length == 1 and (apart or before == '(') and ahead(segs, k, '[.!?…,;:—"\')]*%s') -%}
+ {%- set it.short = it.short + [it.items | length] -%}
+ {%- endif -%}
+ {%- if chat is not none and chat[1] and (raw | length > 1 or raw == lower) and t.new | length == 1 and apart
+ and ahead(segs, k, '[.?…,;—]*["\']?%s|!+["\']?%s*$') -%}
+ {%- set it.replies = it.replies + [it.items | length] -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- if t.new and it.attach -%}
+ {%- set y = t.new[0] -%}
+ {%- set t.new = [{'w': y.w, 'r': y.r, 'a': true, 'spell': y.spell, 'ps': y.ps}] + t.new[1:] -%}
+ {%- endif -%}
+ {%- set it.items = it.items + t.new -%}
+ {%- set it.attach = false -%}
+ {%- set it.fresh = it.fresh and not t.new -%}
+ {%- endif -%}
+{%- endfor -%}
+{#- "Ss!" and "Sh, ss, tsk" are a hiss. Beside an ordinary word the two letters stay letters ("the ss", "mm:ss");
+ quotation marks and brackets do not part them from that word -#}
+{%- for place in it.short -%}
+ {%- set r = namespace(first=place, last=place + 1, sounds=true) -%}
+ {%- for _ in range(place) -%}
+ {%- if r.first > 0 and (it.items[r.first - 1].w is defined or it.items[r.first - 1].p in ['"', "'", '(', ')']) -%}{%- set r.first = r.first - 1 -%}{%- endif -%}
+ {%- endfor -%}
+ {%- for _ in range(it.items | length - place) -%}
+ {%- if r.last < it.items | length and (it.items[r.last].w is defined or it.items[r.last].p in ['"', "'", '(', ')']) -%}{%- set r.last = r.last + 1 -%}{%- endif -%}
+ {%- endfor -%}
+ {%- for x in it.items[r.first:r.last] -%}
+ {%- if x.w is defined and not (x.w in SOUNDS or (x.ps and x.w and x.w | replace(x.w[:1], '') == '')) -%}{%- set r.sounds = false -%}{%- endif -%}
+ {%- endfor -%}
+ {%- if r.sounds -%}
+ {%- set y = it.items[place] -%}
+ {%- set it.items = it.items[:place] + [{'w': y.w, 'r': y.r, 'a': y.a, 'spell': false, 'ps': y.w[:1]}] + it.items[place + 1:] -%}
+ {%- endif -%}
+{%- endfor -%}
+{#- "k" is "okay" as a reply by itself ("k", "k, thanks"); beside a word or a figure it is the letter, and between two
+ commas, or with a single letter next to it across a comma, it is one of a list ("i, j, k") -#}
+{%- for place in it.replies | reverse -%}
+ {%- set r = namespace(reply=true, marks=[]) -%}
+ {%- for step in [-1, 1] -%}
+ {%- set at = namespace(i=place + step) -%}
+ {%- for _ in it.items -%}
+ {%- if at.i >= 0 and at.i < it.items | length and it.items[at.i].p is defined and it.items[at.i].p in ['"', "'", '(', ')'] -%}{%- set at.i = at.i + step -%}{%- endif -%}
+ {%- endfor -%}
+ {%- if at.i < 0 or at.i >= it.items | length -%}{%- set r.marks = r.marks + [''] -%}
+ {%- elif it.items[at.i].w is defined -%}{%- set r.reply = false -%}
+ {%- else -%}
+ {%- set r.marks = r.marks + [it.items[at.i].p] -%}
+ {%- set j = at.i + step -%}
+ {%- if it.items[at.i].p == ',' and j >= 0 and j < it.items | length and it.items[j].w is defined and it.items[j].w | length == 1 and it.items[j].w != 'i' -%}
+ {%- set r.reply = false -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- endfor -%}
+ {%- if r.reply and r.marks != [',', ','] -%}
+ {%- set y = it.items[place] -%}
+ {%- set said = namespace(new=[]) -%}
+ {%- set chat = tts_dict('chat', y.r | lower) -%}
+ {%- if chat[0] != '-' -%}
+ {%- for part in chat[0].split(' ') -%}
+ {%- set said.new = said.new + [{'w': part[1:] | lower, 'r': y.r, 'a': false, 'spell': true, 'ps': ''} if part.startswith('*')
+ else {'w': part | lower, 'r': part, 'a': false, 'spell': false, 'ps': ''}] -%}
+ {%- endfor -%}
+ {%- endif -%}
+ {%- set it.items = it.items[:place] + said.new + it.items[place + 1:] -%}
+ {%- endif -%}
+{%- endfor -%}
+
+{#- ---------- clauses and marks -> symbols; repeated marks are collapsed ---------- -#}
+{#- the text as the homograph rules see it -#}
+{%- set wr = namespace(toks=[]) -%}
+{%- for x in it.items -%}{%- set wr.toks = wr.toks + [x.p if x.p is defined else (x.r if x.r else 'x')] -%}{%- endfor -%}
+{%- set out = namespace(s='', words=[], places=[], last='', phones=false) -%}
+{%- for x in it.items + [{'p': ''}] -%}
+ {%- if x.p is defined -%}
+ {%- if out.words -%}
+ {%- set said = clause(out.words, wr.toks, out.places) -%}
+ {%- set out.words = [] -%}{%- set out.places = [] -%}
+ {%- if said -%}
+ {%- set out.s = out.s ~ (' ' if out.s else '') ~ said -%}
+ {%- set out.last = '' -%}{%- set out.phones = true -%}
+ {%- endif -%}
+ {%- endif -%}
+ {%- if x.p and x.p != out.last -%}
+ {%- set out.s = out.s ~ (' ' if out.s else '') ~ x.p -%}
+ {%- set out.last = x.p -%}
+ {%- endif -%}
+ {%- else -%}
+ {%- set out.words = out.words + [x] -%}{%- set out.places = out.places + [loop.index0] -%}
+ {%- endif -%}
+{%- endfor -%}
+{#- : the end of an utterance -#}
+{%- if out.s and out.phones -%}{%- set spoken_text.s = spoken_text.s ~ (' ' if spoken_text.s else '') ~ out.s ~ ' ' -%}{%- endif -%}
+{%- endif -%}
+{%- endfor -%}
+{{ spoken_text.s }}
diff --git a/src/llama-arch.cpp b/src/llama-arch.cpp
index 530fc8e12..feee62bd4 100644
--- a/src/llama-arch.cpp
+++ b/src/llama-arch.cpp
@@ -177,6 +177,7 @@ static const std::map LLM_ARCH_NAMES = {
{ LLM_ARCH_POCKETTTS, "pockettts" },
{ LLM_ARCH_ZONOS3, "zonos3" },
{ LLM_ARCH_KOKORO, "kokoro" },
+ { LLM_ARCH_TINY_VOCA, "tiny-voca" },
{ LLM_ARCH_K2_HORIZON, "k2-horizon" },
{ LLM_ARCH_BEAM, "beam" },
{ LLM_ARCH_KOLIBRI1, "kolibri1" },
diff --git a/src/llama-arch.h b/src/llama-arch.h
index 05a00c3ac..4187a0d08 100644
--- a/src/llama-arch.h
+++ b/src/llama-arch.h
@@ -178,6 +178,7 @@ enum llm_arch {
LLM_ARCH_POCKETTTS,
LLM_ARCH_ZONOS3,
LLM_ARCH_KOKORO,
+ LLM_ARCH_TINY_VOCA,
LLM_ARCH_MINIMAX_01,
LLM_ARCH_HRM_TEXT,
LLM_ARCH_K2_HORIZON,
diff --git a/src/llama-model.cpp b/src/llama-model.cpp
index 9adb96364..728473fff 100644
--- a/src/llama-model.cpp
+++ b/src/llama-model.cpp
@@ -130,6 +130,8 @@ static llama_model * llama_model_mapping(llm_arch arch, const llama_model_params
return new llama_model_zonos3(params);
case LLM_ARCH_KOKORO:
return new llama_model_kokoro(params);
+ case LLM_ARCH_TINY_VOCA:
+ return new llama_model_tiny_voca(params);
case LLM_ARCH_PHI2:
return new llama_model_phi2(params);
case LLM_ARCH_PHI3:
@@ -2990,6 +2992,7 @@ llama_memory_i * llama_model::create_memory(const llama_memory_params & params,
case LLM_ARCH_RND1:
case LLM_ARCH_CLEF:
case LLM_ARCH_KOKORO:
+ case LLM_ARCH_TINY_VOCA:
{
res = nullptr;
} break;
@@ -3774,6 +3777,7 @@ llama_rope_type llama_model_rope_type(const llama_model * model) {
case LLM_ARCH_POCKETTTS:
case LLM_ARCH_ZONOS3:
case LLM_ARCH_LIMITE:
+ case LLM_ARCH_TINY_VOCA:
return LLAMA_ROPE_TYPE_NORM;
case LLM_ARCH_BAILINGMOE3:
// VL files carry mrope sections; text-only files keep NORM rope
diff --git a/src/models/models.h b/src/models/models.h
index ccbd1be62..8da3bcf29 100644
--- a/src/models/models.h
+++ b/src/models/models.h
@@ -785,6 +785,18 @@ struct llama_model_kokoro : public llama_model_base {
std::unique_ptr build_arch_graph(const llm_graph_params & params) const override;
};
+struct llama_model_tiny_voca : public llama_model_base {
+ llama_model_tiny_voca(const struct llama_model_params & params) : llama_model_base(params) {}
+ void load_arch_hparams(llama_model_loader & ml) override;
+ void load_arch_tensors(llama_model_loader & ml) override;
+
+ struct graph : public llm_graph_context {
+ graph(const llm_graph_params & params);
+ };
+
+ std::unique_ptr build_arch_graph(const llm_graph_params & params) const override;
+};
+
struct llama_model_zonos3 : public llama_model_base {
llama_model_zonos3(const struct llama_model_params & params) : llama_model_base(params) {}
void load_arch_hparams(llama_model_loader & ml) override;
diff --git a/src/models/tiny-voca.cpp b/src/models/tiny-voca.cpp
new file mode 100644
index 000000000..c7541f5e9
--- /dev/null
+++ b/src/models/tiny-voca.cpp
@@ -0,0 +1,171 @@
+#include "models.h"
+
+#include