from __future__ import annotations import re import sys from common import TTS_UZ sys.path.insert(0, str(TTS_UZ)) from text_to_phoneme.phoneme_converter import uzbek_to_ipa_final from text_to_phoneme.pipelene_text_to_phoneme import is_punctuation_or_symbol, tokenize_text from text_to_phoneme.text_normalization_for_tts import text_normalization_for_tts from text_to_phoneme.uzbek_orthography import normalize_uzbek_apostrophes ALT_TAG_RE = re.compile(r"<[a-z][a-z -]*>") SUFFIXES = ("gacha", "larda", "dan", "da", "ga", "ni", "ning", "lar") SUFFIX_RE = "|".join(SUFFIXES) BAD_AFTER = re.compile(r"\d|\b(ayiruv|minus|bo'luv|kuchukcha|nuqta|dablyu)\b|\bnol nol\b|[@/\\#*_=<>{}\[\]|~^]") NUMBER_WORDS = ("nol bir ikki uch to'rt besh olti yetti sakkiz to'qqiz o'n yigirma o'ttiz qirq ellik oltmish " "yetmish sakson to'qson yuz ming million").split() STRAY_U = re.compile(r"\b(" + "|".join(map(re.escape, NUMBER_WORDS)) + r") u\b") ACRONYM = re.compile(r"\b[A-Z]{2,}\b|(? str: return text_normalization_for_tts(str(n)).strip() def attach(words: str, suffix: str) -> str: if not suffix: return words last = words[-1] if suffix.startswith("g") and last in "kq": suffix = last + suffix[1:] return words + suffix def say_time(hour: int, minute: int) -> str: if not (0 <= hour <= 24 and 0 <= minute <= 59): raise ValueError(f"bad time {hour}:{minute}") if minute == 0: return say_number(hour) if minute < 10: raise ValueError("minutes 01-09 have no settled spoken form") return f"{say_number(hour)} {say_number(minute)}" def prenorm(text: str) -> str: text = normalize_uzbek_apostrophes(" ".join(text.split())) if re.search(r"https?://|www\.|@|\.uz\b|\.com\b", text): raise ValueError("links and e-mails are not read out") if ACRONYM.search(text): raise ValueError("acronyms and letter codes are read inconsistently") text = re.sub(r"\b(\d{1,2}):(\d{2})\s*[-–]\s*(\d{1,2}):(\d{2})\b", lambda m: f"{attach(say_time(int(m[1]), int(m[2])), 'dan')} " f"{attach(say_time(int(m[3]), int(m[4])), 'gacha')}", text) text = re.sub(rf"\b(\d{{1,2}}):(\d{{2}})(?:\s?-?({SUFFIX_RE})\b)?", lambda m: attach(say_time(int(m[1]), int(m[2])), m[3] or ""), text) text = re.sub(r"\b(\d+)[.,]5\b(?!\d)", lambda m: f"{say_number(int(m[1]))} yarim", text) if re.search(r"\d[.,]\d", text) and not re.search(r"\d{1,3}(?:[ .,]\d{3})+", text): raise ValueError("decimal fraction") def rng(m): a, b = int(m[1]), int(m[2]) if not a < b <= 999: raise ValueError(f"hyphenated digits {m[0]!r} look like a code") return f"{say_number(a)}-{attach(say_number(b), m[3] or '')}" text = re.sub(rf"\b(\d{{1,3}})\s?[-–]\s?(\d{{1,3}})(?:-?({SUFFIX_RE})\b)?(?=\s|[,.]|$)", rng, text) if re.search(r"\d\s?[-–]\s?\d", text): raise ValueError("hyphenated code") def ident(m): digits = m[0] if digits.startswith("998") or len(digits) > 12: return digits groups, rest = [digits[:3]], digits[3:] while rest: take = 3 if len(rest) == 3 else 2 groups.append(rest[:take]) rest = rest[take:] return ", ".join(say_number(int(g)) if not g.startswith("0") else " ".join(say_number(int(c)) for c in g) for g in groups) text = re.sub(r"(? str: text = re.sub(r"\bto'qqiz to'qqiz sakkiz\b", "to'qqiz yuz to'qson sakkiz", text) text = re.sub(r"\b(" + "|".join(map(re.escape, NUMBER_WORDS)) + r") (dagi|dan|da|gacha|ga|ni|ning|larda|lardan)\b", lambda m: attach(m[1], m[2]), text, flags=re.IGNORECASE) text = re.sub(r"\b([Bb])ir yuz\b", lambda m: "Yuz" if m[1] == "B" else "yuz", text) return text FOREIGN = re.compile(r"[^A-Za-z0-9\s.,!?;:'\"()%+$№/«»“”‘’ʻʼ`\-–—]") ABBREV = re.compile(r"(? str: return " ".join(text_normalization_for_tts(span).split()) def verbalize(raw: str, terms: str = "spoken") -> str: from terms import swap_in, swap_out raw = normalize_uzbek_apostrophes(" ".join(raw.split())) if FOREIGN.search(raw): raise ValueError(f"characters outside Uzbek Latin: {FOREIGN.search(raw)[0]!r}") if ABBREV.search(raw): raise ValueError(f"abbreviation: {ABBREV.search(raw)[0]!r}") if re.search(r"[A-Za-z]\d|\d[xX×]\d", raw): raise ValueError("letters glued to digits (M500, 3x4)") held, found = swap_out(raw) held = re.sub(r"(\d)\s?%", r"\1 foiz", held) held = UNIT_RE.sub(lambda m: f"{m[1]} {UNITS[m[2].lower()]}", held) text = prenorm(held) text = SPAN.sub(lambda m: normalize_span(m[0]), text) out = spoken_conventions(" ".join(text.split())) out = re.sub(r"\s+([,.!?;:])", r"\1", out) out = re.sub(r"\b(" + "|".join(map(re.escape, NUMBER_WORDS)) + r") ta(?![\w'])", r"\1ta", out) out = re.sub(r"\b([Bb])irta(?![\w'])", r"\1itta", out) out = re.sub(r"\bfoiz (ni|ning|ga|dan|da|gacha|lik|lar)(?![\w'])", r"foiz\1", out) out = out[:1].upper() + out[1:] bad = BAD_AFTER.search(out) or STRAY_U.search(out) if bad: raise ValueError(f"normalizer output still unsafe: {bad[0]!r} in {out!r}") out = swap_in(out, found, terms) if re.search(r"zqterm", out, re.IGNORECASE): raise ValueError(f"term placeholder lost in normalization: {out!r}") return out[:1].upper() + out[1:] if out[:1].islower() and not out.startswith("iPhone") else out