File size: 2,503 Bytes
3483aba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Dizione: English text to phonemes.

A dictionary built from CMUdict, a small model for words the dictionary
lacks, homograph rules, and text rules for numbers, dates, money, units and
chat forms. See ``docs/design.md``.

    import dizione
    print(dizione.phonemize("Hello, it costs $3.50."))
"""

import hashlib
import json
from pathlib import Path

from .symbols import Phonemized, tokenize

__version__ = "0.1.0"
VERSION = 4          # token-convention version recorded by models trained on the output
_BUILT = ("lexicon.tsv", "align.json", "g2p.json", "g2p.npz", "homographs.json")
_default = None


def get():
    """The phonemizer over the default data directory, loaded once."""
    global _default
    if _default is None:
        from .engine import Phonemizer
        _default = Phonemizer()
    return _default


def reset():
    """Forget the loaded phonemizer (after the data directory or the tables changed)."""
    global _default
    _default = None


def pieces(text):
    """``(True, mark)`` for punctuation and ``(False, words)`` for text, in order."""
    return get().pieces(text)


def phonemize(text):
    """Text -> ``Phonemized`` (symbols, ids, stress)."""
    if not isinstance(text, str) or not text.strip():
        raise ValueError("text must not be empty")
    return tokenize(pieces(text))


def identity(directory=None):
    """Exactly which build produces the tokens: version, built files, tables
    and code. Record the fingerprint next to anything derived from the output,
    so tokens from two builds are never mixed."""
    from .engine import default_directory
    from .tables import DATA
    directory = Path(directory) if directory else default_directory()
    built = {name: hashlib.sha256((directory / name).read_bytes()).hexdigest() for name in _BUILT if (directory / name).exists()}
    tables = hashlib.sha256()
    for path in sorted(DATA.glob("*.txt")):
        tables.update(path.name.encode())
        tables.update(path.read_bytes())
    code = hashlib.sha256()
    for path in sorted(Path(__file__).parent.glob("*.py")):
        code.update(path.name.encode())
        code.update(path.read_bytes())
    parts = {"version": VERSION, "built": built, "tables": tables.hexdigest(), "code": code.hexdigest()}
    fingerprint = hashlib.sha256(json.dumps(parts, sort_keys=True).encode()).hexdigest()
    return {**parts, "fingerprint": fingerprint}


__all__ = ["Phonemized", "phonemize", "pieces", "identity", "get", "reset", "tokenize", "VERSION"]