File size: 3,860 Bytes
3ef3048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259417d
 
3ef3048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
259417d
 
 
 
 
 
 
3ef3048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""Text front for Uzbek TTS, the chain the text goes through before the voice:

  1. dictionary (lexicon.py, optional): English words, brands and acronyms get their Uzbek readings, held as
     stand-ins so that the normaliser below cannot rewrite them;
  2. respellings of a few names as they sound (Alo -> Alyo, Excel -> Eksel, Google -> Gugl);
  3. normaliser (frontend/tts_frontend.py): Unicode and Uzbek apostrophes, markdown, emoji and quotes cleaned; sound
     tags kept ([laugh], [pause], [breath]); Cyrillic to Uzbek Latin; known terms read out (QR -> kyu-ar); numbers,
     dates, times, money, percentages, ranges, units and phone numbers to words; register "conversational" adds the
     Tashkent spoken forms, "literary" keeps the written ones; spacing, capitals and a closing mark;
  4. the readings of step 1 back in place.

    prepare("Narxi 1 250 000 so'm, Payme orqali to'lang", lexicon=Lexicon.load("lexicon/en_uz.tsv"))
    -> "Narxi bir million ikki yuz ellik ming so'm, Peymi orqali to'lang."
"""

from __future__ import annotations

import re
import sys
from pathlib import Path

sys.path.insert(0, str(Path(__file__).resolve().parent / "frontend"))
import tts_frontend  # noqa: E402

REGISTERS = ("literary", "conversational")
_BREATHS = re.compile(r"\[(sigh|breath|inhale|exhales)\]")   # what the normaliser turns into [breath], in order
_BREATH = re.compile(r"\[breath\]")
PRONOUNCE = [
    (re.compile(r"\b(a)l+o\b", re.I), lambda m: m.group(1) + "lyo"),
    (re.compile(r"\bLux\b"), lambda m: "Lyuks"),
    (re.compile(r"\bTechno", re.I), lambda m: "Texno"),
    (re.compile(r"\bamo\s*CRM\b", re.I), lambda m: "amo CRM"),
    (re.compile(r"\bExcel", re.I), lambda m: "Eksel"),
    (re.compile(r"\bGoogle\s*Sheets?", re.I), lambda m: "Gugl Shits"),
    (re.compile(r"\bGoogle\s*Meet", re.I), lambda m: "Gugl Mit"),
    (re.compile(r"\bGoogle", re.I), lambda m: "Gugl"),
    (re.compile(r"\bBitrix(?!\s*\d)", re.I), lambda m: "Bitriks"),
]


def said(text: str, register: str = "literary") -> str:
    """Steps 2 and 3: the respellings, then the normaliser in the given register."""
    if register not in REGISTERS:
        raise ValueError(f"register {register!r}: choose from {REGISTERS}")
    for pattern, spoken in PRONOUNCE:
        text = pattern.sub(spoken, text)
    spoken = tts_frontend.normalize(text, register).text
    # the normaliser knows three sounds and writes a sigh as a breath; this voice has both, so the sighs go back in
    kinds = _BREATHS.findall(text)
    if "sigh" in kinds and len(_BREATH.findall(spoken)) == len(kinds):
        left = iter(kinds)
        spoken = _BREATH.sub(lambda m: "[sigh]" if next(left) == "sigh" else m.group(0), spoken)
    return spoken


def prepare(text: str, lexicon=None, register: str = "literary") -> str:
    """The whole chain: the text the voice is given."""
    if lexicon is None:
        return said(text, register)
    held = lexicon.hold(text)
    if not held.readings:
        return said(text, register)
    spoken = held.restore(said(held.text, register))
    if spoken is not None:
        return spoken
    plain = held.text   # a stand-in did not survive: normalise the text with the readings in it
    for key, reading in held.readings:
        plain = plain.replace(key, reading)
    return said(plain, register)


if __name__ == "__main__":
    import argparse

    ap = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter)
    ap.add_argument("text", nargs="*")
    ap.add_argument("--lexicon")
    ap.add_argument("--register", default="literary", choices=REGISTERS)
    a = ap.parse_args()
    lex = None
    if a.lexicon:
        from lexicon import Lexicon

        lex = Lexicon.load(a.lexicon)
    for line in (a.text or sys.stdin.read().splitlines()):
        print(prepare(line, lex, a.register))