"""The text the Thai model reads: phonemes, not Thai script. The front-end's IPA (words in /slashes/, bare punctuation) becomes one plain string: สวัสดีครับ วันนี้อากาศดี -> sa˨˩.wat̚˨˩.diː˧ kʰrap̚˦˥, wan˧.niː˦˥ ʔaː˧.kaːt̚˨˩ diː˧ Slashes are dropped and punctuation is glued to the word before it, so every space-separated piece is a spoken word. Training and speaking must use the same string, so both go through this file: to_model_text(fe, text) speaking (ct.py, bench.py) word_pairs(fe, text) training: [(Thai word as written, its piece of the model text), ...] The Thai word is what the aligner finds in the audio; the piece replaces it afterwards (training/align_thai.py). " ".join(piece for _, piece in word_pairs(fe, text)) == to_model_text(fe, text), always (tests/test_modeltext.py). """ from __future__ import annotations from typing import List, Tuple from .normalize import ( _KEEP_FOR_G2P, _LATIN_WORD_RE, _PUNCT, _SYMBOL_RE, _THAI_BREAK_RE, _THAI_CONSONANT_RE, _THAI_END_RE, THAI_OVERRIDES, ThaiFrontend, _g2p_normalize, english_to_ipa, fallback_g2p, ) _MARKS = set(_PUNCT.values()) def _thai_pairs(fe: ThaiFrontend, text: str) -> List[Tuple[str, str]]: """ThaiFrontend._thai_ipa(text, "pause"), but keeping the written word next to its IPA. A punctuation mark comes back as ("", mark).""" if not text.strip(): return [] g = fe._g2p merged: List[str] = [] for t in g._tokenizer.tokenize(_g2p_normalize(text)): if not t.strip(): continue if t.strip() in _PUNCT: merged.append(_PUNCT[t.strip()]) elif _SYMBOL_RE.match(t): continue elif (fe.repair and merged and _THAI_CONSONANT_RE.match(t) and t not in g._ipa and _THAI_END_RE.search(merged[-1])): merged[-1] += t else: merged.append(t) out: List[Tuple[str, str]] = [] for t in merged: if t in _MARKS: if out and out[-1][1].startswith("/"): out.append(("", t)) else: out.append((t, (THAI_OVERRIDES.get(t) if fe.repair else None) or g._ipa.get(t) or fallback_g2p(t))) return out def _raw_pairs(fe: ThaiFrontend, text: str) -> List[Tuple[str, str]]: """ThaiFrontend.phonemize(text, "pause") as (written word, IPA or mark) pairs.""" text = _THAI_BREAK_RE.sub(", ", fe.normalize_text(text)) out: List[Tuple[str, str]] = [] pos = 0 for m in _LATIN_WORD_RE.finditer(text): word = m.group(0) if word.lower() in _KEEP_FOR_G2P: continue ipa = english_to_ipa(word, fe._g2p._ipa) if not ipa: continue out += _thai_pairs(fe, text[pos:m.start()]) out.append((word, ipa)) pos = m.end() out += _thai_pairs(fe, text[pos:]) return [(w, ipa) for w, ipa in out if ipa] def word_pairs(fe: ThaiFrontend, text: str) -> List[Tuple[str, str]]: """[(word as written, its piece of the model text)]. Punctuation is glued to the piece before it.""" out: List[Tuple[str, str]] = [] for word, ipa in _raw_pairs(fe, text): if not word: # a mark if out: out[-1] = (out[-1][0], out[-1][1] + ipa) continue piece = " ".join(ipa.replace("/", " ").split()) if piece: out.append((word, piece)) return out def to_model_text(fe: ThaiFrontend, text: str) -> str: """One chat line or sentence -> the string the Thai model reads.""" return " ".join(piece for _, piece in word_pairs(fe, text))