-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlang.py
More file actions
91 lines (63 loc) · 2.58 KB
/
Copy pathlang.py
File metadata and controls
91 lines (63 loc) · 2.58 KB
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
88
89
90
91
"""Language configuration and path helpers for multi-language support."""
import json
from pathlib import Path
_ROOT = Path(__file__).parent
_current_lang: str | None = None
_config_cache: dict[str, dict] = {}
def available_languages() -> list[str]:
"""Return language codes with a config.json, ordered by the config's
"sort" rank — web-content share per
https://en.wikipedia.org/wiki/Languages_used_on_the_Internet — then code.
"""
langs_dir = _ROOT / "languages"
ranked = []
for d in langs_dir.iterdir():
cfg_path = d / "config.json"
if not (d.is_dir() and cfg_path.exists()):
continue
with open(cfg_path, encoding="utf-8") as f:
rank = json.load(f).get("sort", 999)
ranked.append((rank, d.name))
return [code for _, code in sorted(ranked)]
def set_lang(lang: str) -> None:
"""Set the active language. Must be called before loading language data."""
global _current_lang
ld = _ROOT / "languages" / lang
if not ld.exists():
raise ValueError(f"Unknown language: {lang!r} (no directory {ld})")
_current_lang = lang
def get_lang() -> str:
"""Return the active language code. Raises if set_lang() was not called."""
if _current_lang is None:
raise RuntimeError("Language not set. Call lang.set_lang() first.")
return _current_lang
def config() -> dict:
"""Load and cache the config.json for the active language."""
lang = get_lang()
if lang not in _config_cache:
path = _ROOT / "languages" / lang / "config.json"
with open(path, encoding="utf-8") as f:
_config_cache[lang] = json.load(f)
return _config_cache[lang]
def lang_dir() -> Path:
"""Path to languages/<lang>/ (features, corpus config)."""
return _ROOT / "languages" / get_lang()
def data_dir() -> Path:
"""Path to data/<lang>/ (generated wordlists and transition tables)."""
return _ROOT / "data" / get_lang()
def features_path() -> Path:
return lang_dir() / "letter_features.json"
def corpus_dir() -> Path:
return lang_dir() / "corpus"
def wordlist_path() -> Path:
return data_dir() / "wordlist.json"
def bigram_transitions_path() -> Path:
return data_dir() / "bigram_transitions.json"
def trigram_transitions_path() -> Path:
return data_dir() / "trigram_transitions.json"
def alphabet() -> str:
"""Return the ordered alphabet string for the current language."""
return config()["alphabet"]
def alphabet_regex() -> str:
"""Return a regex character class body for the current language."""
return config()["alphabet_regex"]