Download lingologos_thinkingpython/lexicon.py from tailot/lingologos-thinkingpython: direct link, hf CLI and curl.
- Browser
- Download file 2.81 kB
-
https://huggingface.co/datasets/tailot/lingologos-thinkingpython/resolve/main/lingologos_thinkingpython/lexicon.py
- Command line
-
hf download hf://datasets/tailot/lingologos-thinkingpython/lingologos_thinkingpython/lexicon.py
-
curl -L -o lexicon.py https://huggingface.co/datasets/tailot/lingologos-thinkingpython/resolve/main/lingologos_thinkingpython/lexicon.py
2.81 kB
| """The words of the bundle: one file per variety, validated before use. | |
| Section 6.5 of SPECIFICATION.md. The six varieties are the ones the genesis gateway | |
| carries. A variety is declared, never inferred, and a declared variety without a file | |
| answers instead of crashing. | |
| """ | |
| import json | |
| import os | |
| import re | |
| from typing import Any, Dict, Optional | |
| ROOT = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) | |
| LEXICON_DIR = os.path.join(ROOT, "lexicon") | |
| #: The six varieties this bundle carries (SPECIFICATION.md 6.5). | |
| LANGS = ("ar", "en", "es", "hi", "it", "zh") | |
| REQUIRED_KEYS = ("lang", "name", "noise", "units", "frames", "labels", "examples") | |
| LABEL_KEYS = ("state", "level", "domain", "not_understood", "out_of_domain", "refused", | |
| "clarify", "verified", "unknown", "single_prover", "progress") | |
| _CACHE: Dict[str, Dict[str, Any]] = {} | |
| class LexiconError(Exception): | |
| """Raised when a variety file is missing, incomplete or inconsistent.""" | |
| def normalize(text: str) -> str: | |
| raw = str(text or "").strip().replace("_", "-") | |
| if not raw: | |
| return "en" | |
| return raw.split("-")[0].lower() | |
| def has(code: str) -> bool: | |
| return normalize(code) in LANGS and os.path.isfile(os.path.join(LEXICON_DIR, normalize(code) + ".json")) | |
| def load(code: str) -> Dict[str, Any]: | |
| code = normalize(code) | |
| if code in _CACHE: | |
| return _CACHE[code] | |
| path = os.path.join(LEXICON_DIR, code + ".json") | |
| if not os.path.isfile(path): | |
| raise LexiconError("variety %r has no lexicon file in this bundle" % code) | |
| with open(path, "r", encoding="utf-8") as handle: | |
| raw = json.load(handle) | |
| missing = [key for key in REQUIRED_KEYS if key not in raw] | |
| if missing: | |
| raise LexiconError("lexicon %s misses keys: %s" % (code, ", ".join(missing))) | |
| if raw["lang"] != code: | |
| raise LexiconError("lexicon %s declares lang %r" % (code, raw["lang"])) | |
| labels = raw["labels"] or {} | |
| missing_labels = [key for key in LABEL_KEYS if key not in labels] | |
| if missing_labels: | |
| raise LexiconError("lexicon %s misses labels: %s" % (code, ", ".join(missing_labels))) | |
| if not raw["frames"]: | |
| raise LexiconError("lexicon %s carries no frames" % code) | |
| _CACHE[code] = raw | |
| return raw | |
| def label(code: str, key: str, fallback: Optional[str] = None) -> str: | |
| try: | |
| return str(load(code)["labels"].get(key, fallback or key)) | |
| except LexiconError: | |
| return fallback or key | |
| _FOLD = re.compile(r"\s+") | |
| def fold(text: str, code: str) -> str: | |
| """Lowercase, drop the punctuation native to the variety, squeeze whitespace.""" | |
| data = load(code) | |
| out = str(text or "").lower().replace("-", " ") | |
| for ch in data.get("noise", []): | |
| out = out.replace(ch, " ") | |
| return _FOLD.sub(" ", out).strip() | |