Download code/dizione/__init__.py from oddadmix/tiny-voca-english: direct link, hf CLI and curl.
- Browser
- Download file 2.5 kB
-
https://huggingface.co/oddadmix/tiny-voca-english/resolve/main/code/dizione/__init__.py
- Command line
-
hf download hf://oddadmix/tiny-voca-english/code/dizione/__init__.py
-
curl -L -o __init__.py https://huggingface.co/oddadmix/tiny-voca-english/resolve/main/code/dizione/__init__.py
2.5 kB
| """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"] | |