File size: 3,910 Bytes
927ea15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Greek frontend configuration. Install before importing upstream model modules."""
import hashlib
import json
import re
import sys
import unicodedata
from dataclasses import dataclass
from functools import lru_cache

CONSONANTS = tuple("p b t d k ɡ f v θ ð s z m n ŋ l r ɾ j x ɣ ç ʝ ɲ ʎ c ɟ".split())
VOWELS = ("a", "e", "i", "o", "u")
SPECIALS = ("<pad>", "<bos>", "<eos>", "<sp>", "<unk>")
PUNCTUATION = (",", ".", "?", "!", ";", ":", '"', "'", "(", ")", "—", "…")
SYMBOLS = SPECIALS + PUNCTUATION + CONSONANTS + VOWELS

def normalize_text(text):
    # Preserve tonos and dialytika. Greek semicolon/question mark -> question.
    text = unicodedata.normalize("NFC", text)
    text = text.translate(str.maketrans({";": "?", ";": "?", "·": ":", "·": ":",
        "«": '"', "»": '"', "“": '"', "”": '"', "’": "'", "–": "—"}))
    text = re.sub(r"\.{3,}", "…", text)
    return " ".join(text.split())

def split_key(text):
    return hashlib.sha256(normalize_text(text).casefold().encode()).hexdigest()

def install():
    """Configure upstream's tested IPA parser without its English phone fallbacks.

    This is a process-local adapter. Always launch via tiny_voca_gr.run.
    Inventory changes deliberately invalidate English checkpoints.
    """
    existing = sys.modules.get("tiny_voca.text")
    if existing is not None and getattr(existing, "_tiny_voca_gr_installed", False):
        return existing
    forbidden = {"tiny_voca.config", "tiny_voca.model", "tiny_voca.align", "tiny_voca.annotate",
                 "tiny_voca.data", "tiny_voca.train", "tiny_voca.infer"}
    if forbidden.intersection(sys.modules):
        raise RuntimeError("Install Greek frontend before importing tiny-voca model/config/alignment")
    from tiny_voca import text
    text.set_frontend("espeak")
    text.FRONTENDS = ("espeak",)
    text.ESPEAK_VOICE = "el"
    text.FRONTEND_VERSION = 1001
    text.CONSONANTS, text.VOWELS = CONSONANTS, VOWELS
    text.PHONES = CONSONANTS + VOWELS
    text.SYMBOLS = SYMBOLS
    text.SYMBOL_TO_ID = {s: i for i, s in enumerate(SYMBOLS)}
    text.PHONE_SET = frozenset(text.PHONES)
    text.PHONE_IDS = frozenset(text.SYMBOL_TO_ID[p] for p in text.PHONES) | {text.SYMBOL_TO_ID[text.UNK]}
    text.VOWEL_SET = frozenset(VOWELS)
    text.VOICELESS = frozenset("p t k f θ s x ç c".split())
    text.FALLBACK = {"g": ("ɡ",), "ts": ("t", "s"), "dz": ("d", "z")}
    text.ABBREVIATIONS = set()
    text.normalize_text = normalize_text
    text.espeak_version = lru_cache(maxsize=1)(text.espeak_version)
    # Include the actual G2P version so training and inference cannot silently drift.
    base_identity = text.frontend_identity
    def identity():
        return {**base_identity(), "normalization": "greek-nfc-v1", **text.frontend_runtime()}
    text.frontend_identity = identity
    # Upstream evaluates Config() in TinyVoca.__init__'s default argument at
    # import time. Passing --config alone cannot fix the English vocab default.
    from tiny_voca import config as upstream_config
    @dataclass(frozen=True)
    class GreekConfig(upstream_config.Config):
        speakers: tuple[str, ...] = ("M", "F")
        vocab_size: int = len(SYMBOLS)
    upstream_config.Config = GreekConfig
    # Current upstream imports model from its package initializer before this
    # adapter can configure text. Refresh the names model captured at import.
    from tiny_voca import model as upstream_model
    upstream_model.PHONE_IDS = text.PHONE_IDS
    upstream_model.SYMBOL_TO_ID = text.SYMBOL_TO_ID
    upstream_model.VOWELS = text.VOWELS
    upstream_model.Config = GreekConfig
    upstream_model.TinyVoca.__init__.__defaults__ = (GreekConfig(),)
    text._tiny_voca_gr_installed = True
    return text

def config():
    return {"speakers": ["M", "F"], "vocab_size": len(SYMBOLS)}

if __name__ == "__main__":
    print(json.dumps(config(), indent=2))