PyHakka 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,319 @@
1
+ """Supported Hakka romanization / transcription schemes and dialect phonetic profiles."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+ CANONICAL_SCHEMES: tuple[str, ...] = (
8
+ "hps_ext",
9
+ "hps_diacritic",
10
+ "ipa",
11
+ "ipa_value",
12
+ "ipa_chao",
13
+ "gd",
14
+ "hkilang",
15
+ )
16
+
17
+ SCHEME_ALIASES: dict[str, str] = {
18
+ "hps_ext": "hps_ext",
19
+ "hps-ext": "hps_ext",
20
+ "hps": "hps_ext",
21
+ "hakka_pinyin": "hps_ext",
22
+ "pinyin": "hps_ext",
23
+ "hps_diacritic": "hps_diacritic",
24
+ "hps-diacritic": "hps_diacritic",
25
+ "hps_mark": "hps_diacritic",
26
+ "diacritic": "hps_diacritic",
27
+ "ipa": "ipa",
28
+ "ipa_value": "ipa_value",
29
+ "ipa-value": "ipa_value",
30
+ "ipa_numeric": "ipa_value",
31
+ "ipa_chao": "ipa_chao",
32
+ "ipa-chao": "ipa_chao",
33
+ "chao": "ipa_chao",
34
+ "gd": "gd",
35
+ "guangdong": "gd",
36
+ "gd_pinyin": "gd",
37
+ "gd-pinyin": "gd",
38
+ "meixian_pinyin": "gd",
39
+ "hkilang": "hkilang",
40
+ "hk": "hkilang",
41
+ "hk_assoc": "hkilang",
42
+ }
43
+
44
+ # Tone 1-6 (Guangdong / HKILANG) <-> canonical 1-8 tone classes
45
+ TONE_6_TO_8: dict[str, str] = {
46
+ "1": "1",
47
+ "2": "2",
48
+ "3": "3",
49
+ "4": "5",
50
+ "5": "7",
51
+ "6": "8",
52
+ "0": "0",
53
+ "": "",
54
+ }
55
+
56
+ TONE_8_TO_6: dict[str, str] = {
57
+ "1": "1",
58
+ "2": "2",
59
+ "3": "3",
60
+ "4": "3",
61
+ "5": "4",
62
+ "6": "4",
63
+ "7": "5",
64
+ "8": "6",
65
+ "0": "0",
66
+ "": "",
67
+ }
68
+
69
+ SUPERSCRIPT_DIGITS = str.maketrans("0123456789", "⁰¹²³⁴⁵⁶⁷⁸⁹")
70
+ FROM_SUPERSCRIPT_DIGITS = str.maketrans("⁰¹²³⁴⁵⁶⁷⁸⁹", "0123456789")
71
+ CHAO_TO_DIGITS = str.maketrans("˩˨˧˦˥", "12345")
72
+ DIGITS_TO_CHAO = str.maketrans("12345", "˩˨˧˦˥")
73
+
74
+
75
+ @dataclass(frozen=True)
76
+ class DialectPhonProfile:
77
+ """Dialect-specific IPA realization details for lossless HPS-Ext <-> IPA conversion."""
78
+
79
+ slug: str
80
+ # Realization of zh/ch/sh/rh: "postalveolar" (tʃ/tʃʰ/ʃ/ʒ) or "retroflex" (tʂ/tʂʰ/ʂ/ʐ)
81
+ sibilant_series: str = "postalveolar"
82
+ # IPA for HPS-Ext `o` when not contrasting with `oo`
83
+ o_ipa: str = "ɔ"
84
+ # IPA for HPS-Ext `e`
85
+ e_ipa: str = "e"
86
+ # IPA for HPS-Ext `ee`
87
+ ee_ipa: str = "ɛ"
88
+ # Whether `o` and `oo` contrast phonemically (e.g. Changting, Zhao'an)
89
+ o_oo_contrast: bool = False
90
+ # Whether `ii` before m/n/b/d represents [ə] (Meixian / Sixian)
91
+ ii_closed_as_schwa: bool = False
92
+ # IPA for `ii` after zh/ch/sh/rh
93
+ ii_post_sibilant: str = "ʅ"
94
+ # IPA for `x` onset (ɕ vs ç)
95
+ x_ipa: str = "ɕ"
96
+ # Whether `ung/ug` is realized as `oŋ/ok` (Dabu Baihou)
97
+ ung_as_ong: bool = False
98
+ # Whether `a` is written as `ɑ` in IPA (Changting)
99
+ a_ipa: str = "a"
100
+ # Whether `eu` is realized as `əɯ` (Changting)
101
+ eu_ipa: str = "eu"
102
+ # HPS tone diacritic map: tone_class -> diacritic suffix
103
+ hps_diacritics: dict[str, str] | None = None
104
+
105
+
106
+ DEFAULT_HPS_DIACRITICS: dict[str, str] = {
107
+ "1": "",
108
+ "2": "ˇ",
109
+ "3": "ˋ",
110
+ "4": "ˋ",
111
+ "5": "^",
112
+ "6": "+",
113
+ "7": "ˋ",
114
+ "8": "",
115
+ "0": "",
116
+ }
117
+
118
+ SIXIAN_DIACRITICS: dict[str, str] = {
119
+ "1": "ˊ",
120
+ "2": "ˇ",
121
+ "3": "ˋ",
122
+ "5": "",
123
+ "6": "",
124
+ "7": "ˋ",
125
+ "8": "",
126
+ }
127
+
128
+ HAILU_DIACRITICS: dict[str, str] = {
129
+ "1": "ˋ",
130
+ "2": "",
131
+ "3": "ˊ",
132
+ "5": "ˇ",
133
+ "6": "+",
134
+ "7": "",
135
+ "8": "ˋ",
136
+ }
137
+
138
+ DABU_TW_DIACRITICS: dict[str, str] = {
139
+ "1": "+",
140
+ "2": "ˇ",
141
+ "3": "^",
142
+ "5": "ˋ",
143
+ "7": "^",
144
+ "8": "ˋ",
145
+ }
146
+
147
+ RAOPING_DIACRITICS: dict[str, str] = {
148
+ "1": "ˇ",
149
+ "2": "",
150
+ "3": "ˋ",
151
+ "5": "ˊ",
152
+ "7": "ˋ",
153
+ "8": "",
154
+ }
155
+
156
+ ZHAOAN_DIACRITICS: dict[str, str] = {
157
+ "1": "ˇ",
158
+ "2": "ˋ",
159
+ "3": "^",
160
+ "5": "",
161
+ "7": "ˊ",
162
+ "8": "ˋ",
163
+ }
164
+
165
+ CHANGTING_DIACRITICS: dict[str, str] = {
166
+ "1": "+",
167
+ "2": "ˊ",
168
+ "3": "ˋ",
169
+ "5": "^",
170
+ "6": "ˇ",
171
+ }
172
+
173
+ DIALECT_PHON_PROFILES: dict[str, DialectPhonProfile] = {
174
+ "huiyang": DialectPhonProfile(
175
+ slug="huiyang",
176
+ o_ipa="o",
177
+ e_ipa="e",
178
+ hps_diacritics=DEFAULT_HPS_DIACRITICS,
179
+ ),
180
+ "meixian": DialectPhonProfile(
181
+ slug="meixian",
182
+ o_ipa="ɔ",
183
+ e_ipa="e",
184
+ ii_closed_as_schwa=True,
185
+ hps_diacritics=DEFAULT_HPS_DIACRITICS,
186
+ ),
187
+ "changting": DialectPhonProfile(
188
+ slug="changting",
189
+ sibilant_series="postalveolar",
190
+ o_ipa="o",
191
+ e_ipa="e",
192
+ o_oo_contrast=True,
193
+ ii_post_sibilant="ɨ",
194
+ a_ipa="ɑ",
195
+ eu_ipa="əɯ",
196
+ hps_diacritics=CHANGTING_DIACRITICS,
197
+ ),
198
+ "xingning": DialectPhonProfile(
199
+ slug="xingning",
200
+ sibilant_series="retroflex",
201
+ o_ipa="ɔ",
202
+ e_ipa="e",
203
+ ii_post_sibilant="ʅ",
204
+ hps_diacritics=DEFAULT_HPS_DIACRITICS,
205
+ ),
206
+ "wuhua": DialectPhonProfile(
207
+ slug="wuhua",
208
+ sibilant_series="postalveolar",
209
+ o_ipa="o",
210
+ e_ipa="e",
211
+ ii_post_sibilant="ʅ",
212
+ hps_diacritics={
213
+ "1": "",
214
+ "2": "ˊ",
215
+ "3": "ˋ",
216
+ "5": "^",
217
+ "7": "ˋ",
218
+ "8": "",
219
+ },
220
+ ),
221
+ "dabu-baihou": DialectPhonProfile(
222
+ slug="dabu-baihou",
223
+ sibilant_series="postalveolar",
224
+ o_ipa="ɔ",
225
+ e_ipa="e",
226
+ ee_ipa="æ",
227
+ x_ipa="ç",
228
+ ung_as_ong=True,
229
+ ii_post_sibilant="ʅ",
230
+ hps_diacritics={
231
+ "1": "ˊ",
232
+ "2": "ˇ",
233
+ "3": "ˋ",
234
+ "5": "^",
235
+ "7": "ˋ",
236
+ "8": "",
237
+ },
238
+ ),
239
+ "huiyang-danshui": DialectPhonProfile(
240
+ slug="huiyang-danshui",
241
+ o_ipa="o",
242
+ e_ipa="e",
243
+ hps_diacritics=DEFAULT_HPS_DIACRITICS,
244
+ ),
245
+ "huiyang-1987": DialectPhonProfile(
246
+ slug="huiyang-1987",
247
+ o_ipa="o",
248
+ e_ipa="e",
249
+ hps_diacritics=DEFAULT_HPS_DIACRITICS,
250
+ ),
251
+ "huidong-daling": DialectPhonProfile(
252
+ slug="huidong-daling",
253
+ o_ipa="o",
254
+ e_ipa="e",
255
+ hps_diacritics=DEFAULT_HPS_DIACRITICS,
256
+ ),
257
+ "shenzhen-baoan": DialectPhonProfile(
258
+ slug="shenzhen-baoan",
259
+ o_ipa="o",
260
+ e_ipa="e",
261
+ hps_diacritics=DEFAULT_HPS_DIACRITICS,
262
+ ),
263
+ "hongkong-newterritories": DialectPhonProfile(
264
+ slug="hongkong-newterritories",
265
+ o_ipa="o",
266
+ e_ipa="e",
267
+ hps_diacritics=DEFAULT_HPS_DIACRITICS,
268
+ ),
269
+ "mcp:臺灣四縣腔": DialectPhonProfile(
270
+ slug="mcp:臺灣四縣腔",
271
+ o_ipa="o",
272
+ e_ipa="e",
273
+ ii_closed_as_schwa=True,
274
+ hps_diacritics=SIXIAN_DIACRITICS,
275
+ ),
276
+ "mcp:臺灣南四縣腔": DialectPhonProfile(
277
+ slug="mcp:臺灣南四縣腔",
278
+ o_ipa="o",
279
+ e_ipa="e",
280
+ ii_closed_as_schwa=True,
281
+ hps_diacritics=SIXIAN_DIACRITICS,
282
+ ),
283
+ "mcp:臺灣海陸腔": DialectPhonProfile(
284
+ slug="mcp:臺灣海陸腔",
285
+ sibilant_series="postalveolar",
286
+ o_ipa="o",
287
+ e_ipa="e",
288
+ hps_diacritics=HAILU_DIACRITICS,
289
+ ),
290
+ "mcp:臺灣饒平腔": DialectPhonProfile(
291
+ slug="mcp:臺灣饒平腔",
292
+ o_ipa="o",
293
+ e_ipa="e",
294
+ hps_diacritics=RAOPING_DIACRITICS,
295
+ ),
296
+ "mcp:臺灣詔安腔": DialectPhonProfile(
297
+ slug="mcp:臺灣詔安腔",
298
+ o_ipa="o",
299
+ e_ipa="e",
300
+ ee_ipa="ɛ",
301
+ o_oo_contrast=True,
302
+ hps_diacritics=ZHAOAN_DIACRITICS,
303
+ ),
304
+ }
305
+
306
+ DEFAULT_PHON_PROFILE = DialectPhonProfile(slug="default")
307
+
308
+
309
+ def normalize_scheme(scheme: str) -> str:
310
+ """Normalize a scheme alias to one of `CANONICAL_SCHEMES`."""
311
+ key = scheme.strip().lower()
312
+ if key not in SCHEME_ALIASES:
313
+ valid = ", ".join(CANONICAL_SCHEMES)
314
+ raise ValueError(f"Unknown phonetic scheme {scheme!r}. Supported: {valid}")
315
+ return SCHEME_ALIASES[key]
316
+
317
+
318
+ def get_phon_profile(dialect_slug: str) -> DialectPhonProfile:
319
+ return DIALECT_PHON_PROFILES.get(dialect_slug, DEFAULT_PHON_PROFILE)
@@ -0,0 +1,164 @@
1
+ """Canonical intermediate representation (IR) for a Hakka syllable."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass, replace
6
+
7
+ CHECKED_CODAS = frozenset({"b", "d", "g", "h"})
8
+ NASAL_CODAS = frozenset({"m", "n", "ng"})
9
+ SYLLABIC_CONSONANTS = frozenset({"m", "n", "ng", "v"})
10
+
11
+ # Canonical ordered onsets (longest first for greedy matching)
12
+ HPS_EXT_ONSETS: tuple[str, ...] = (
13
+ "zh",
14
+ "ch",
15
+ "sh",
16
+ "rh",
17
+ "ng",
18
+ "nj",
19
+ "bb",
20
+ "dd",
21
+ "gg",
22
+ "gh",
23
+ "zz",
24
+ "jj",
25
+ "hh",
26
+ "sl",
27
+ "th",
28
+ "b",
29
+ "p",
30
+ "m",
31
+ "f",
32
+ "v",
33
+ "d",
34
+ "t",
35
+ "n",
36
+ "l",
37
+ "r",
38
+ "g",
39
+ "k",
40
+ "h",
41
+ "z",
42
+ "c",
43
+ "s",
44
+ "j",
45
+ "q",
46
+ "x",
47
+ "'",
48
+ )
49
+
50
+ HPS_EXT_ONSET_SET = frozenset(HPS_EXT_ONSETS)
51
+
52
+ # Canonical ordered nuclei (longest first)
53
+ HPS_EXT_NUCLEI: tuple[str, ...] = (
54
+ "ii",
55
+ "ee",
56
+ "ae",
57
+ "oo",
58
+ "oe",
59
+ "er",
60
+ "a",
61
+ "e",
62
+ "i",
63
+ "o",
64
+ "u",
65
+ "y",
66
+ )
67
+
68
+ HPS_EXT_CODAS: tuple[str, ...] = (
69
+ "ng",
70
+ "m",
71
+ "n",
72
+ "b",
73
+ "d",
74
+ "g",
75
+ "h",
76
+ "i",
77
+ "u",
78
+ )
79
+
80
+
81
+ @dataclass(frozen=True)
82
+ class Syllable:
83
+ """Structured phonological representation of a Hakka syllable.
84
+
85
+ Fields use canonical HPS-Ext ASCII segments:
86
+ - `onset`: initial consonant (`""` for zero onset, `"'"` for explicit glottal onset `[ʔ-]`)
87
+ - `medial`: glide (`""`, `"i"`, `"u"`, `"y"`)
88
+ - `nucleus`: main vowel (`a/e/ee/ae/i/ii/o/oo/u/y/oe/er`) or syllabic consonant (`m/n/ng/v`)
89
+ - `coda`: ending (`""`, `"i"`, `"u"`, `"m"`, `"n"`, `"ng"`, `"b"`, `"d"`, `"g"`, `"h"`)
90
+ - `nasalized`: True if the vowel is nasalized (`-nn` in HPS-Ext, `̃` in IPA)
91
+ - `tone`: canonical 4-tone × yin/yang class (`"1"`..`"8"`, `"0"` neutral, or `""`)
92
+ - `tone_value`: optional 5-level Chao tone contour (e.g. `"44"`, `"45"`, `"31"`, `"1"`)
93
+ - `reading_mark`: optional literary/colloquial mark (`-`, `=`, `+`, `*`, `@`, `?`)
94
+ - `raw`: original input token before parsing
95
+ """
96
+
97
+ onset: str
98
+ medial: str
99
+ nucleus: str
100
+ coda: str = ""
101
+ nasalized: bool = False
102
+ tone: str = ""
103
+ tone_value: str = ""
104
+ reading_mark: str = ""
105
+ raw: str = ""
106
+
107
+ @property
108
+ def final(self) -> str:
109
+ """Full rime (medial + nucleus + coda + optional `-nn`, with `-nnh` order for glottal coda)."""
110
+ if self.nasalized:
111
+ if self.coda == "h":
112
+ return f"{self.medial}{self.nucleus}nnh"
113
+ return f"{self.medial}{self.nucleus}{self.coda}nn"
114
+ return f"{self.medial}{self.nucleus}{self.coda}"
115
+
116
+ @property
117
+ def base(self) -> str:
118
+ """Segmental syllable without tone (`onset + final`)."""
119
+ return f"{self.onset}{self.final}"
120
+
121
+ @property
122
+ def hps_ext(self) -> str:
123
+ """Canonical HPS-Ext spelling with tone class number."""
124
+ return f"{self.base}{self.tone}"
125
+
126
+ @property
127
+ def is_checked(self) -> bool:
128
+ """True if this syllable ends in a stop coda (`-b/-d/-g/-h`, 入聲韻)."""
129
+ return self.coda in CHECKED_CODAS
130
+
131
+ @property
132
+ def is_nasal_coda(self) -> bool:
133
+ """True if this syllable ends in a nasal coda (`-m/-n/-ng`, 陽聲韻)."""
134
+ return self.coda in NASAL_CODAS
135
+
136
+ @property
137
+ def is_syllabic_consonant(self) -> bool:
138
+ """True if the rime is a standalone syllabic consonant (`m`, `n`, `ng`, `v`)."""
139
+ return (
140
+ self.onset == ""
141
+ and self.medial == ""
142
+ and self.coda == ""
143
+ and self.nucleus in SYLLABIC_CONSONANTS
144
+ )
145
+
146
+ def with_tone(self, tone: str, tone_value: str | None = None) -> Syllable:
147
+ return replace(
148
+ self,
149
+ tone=tone,
150
+ tone_value=self.tone_value if tone_value is None else tone_value,
151
+ )
152
+
153
+ def with_tone_value(self, tone_value: str, tone: str | None = None) -> Syllable:
154
+ return replace(
155
+ self,
156
+ tone=self.tone if tone is None else tone,
157
+ tone_value=tone_value,
158
+ )
159
+
160
+ def with_onset(self, onset: str) -> Syllable:
161
+ return replace(self, onset=onset)
162
+
163
+ def __str__(self) -> str:
164
+ return self.hps_ext
@@ -0,0 +1,79 @@
1
+ """Syllable validation and phonological constraint checking."""
2
+
3
+ from __future__ import annotations
4
+
5
+ from dataclasses import dataclass
6
+
7
+ from pyhakka.dialects import DEFAULT_DIALECT, get_dialect
8
+ from pyhakka.phon.parser import parse_syllable
9
+ from pyhakka.phon.syllable import HPS_EXT_ONSET_SET, SYLLABIC_CONSONANTS, Syllable
10
+
11
+
12
+ @dataclass(frozen=True)
13
+ class ValidationResult:
14
+ valid: bool
15
+ syllable: Syllable | None
16
+ errors: tuple[str, ...] = ()
17
+ warnings: tuple[str, ...] = ()
18
+
19
+
20
+ def validate_syllable(
21
+ text: str,
22
+ scheme: str = "hps_ext",
23
+ dialect: str | None = DEFAULT_DIALECT,
24
+ ) -> ValidationResult:
25
+ """Validate whether `text` is a well-formed Hakka syllable in `scheme` / `dialect`."""
26
+ errors: list[str] = []
27
+ warnings: list[str] = []
28
+ try:
29
+ syl = parse_syllable(text, scheme=scheme, dialect=dialect or DEFAULT_DIALECT)
30
+ except ValueError as exc:
31
+ return ValidationResult(valid=False, syllable=None, errors=(str(exc),))
32
+
33
+ if syl.onset and syl.onset not in HPS_EXT_ONSET_SET:
34
+ errors.append(f"Unknown onset {syl.onset!r}")
35
+
36
+ if not syl.nucleus:
37
+ errors.append("Missing vowel nucleus")
38
+ elif syl.nucleus not in SYLLABIC_CONSONANTS and syl.nucleus not in {
39
+ "a",
40
+ "e",
41
+ "ee",
42
+ "ae",
43
+ "i",
44
+ "ii",
45
+ "o",
46
+ "oo",
47
+ "u",
48
+ "y",
49
+ "oe",
50
+ "er",
51
+ }:
52
+ errors.append(f"Non-canonical nucleus {syl.nucleus!r}")
53
+
54
+ # Checked tone / coda consistency check (入聲韻尾與入聲調一致性)
55
+ if syl.tone:
56
+ if syl.is_checked and syl.tone not in ("7", "8", "0"):
57
+ warnings.append(
58
+ f"Checked coda -{syl.coda} paired with non-checked tone class {syl.tone}"
59
+ )
60
+ elif not syl.is_checked and syl.tone in ("7", "8"):
61
+ # Note: Changting/some varieties have lost stop codas, so warn rather than error
62
+ warnings.append(
63
+ f"Open/nasal rime {syl.final!r} paired with checked tone class {syl.tone}"
64
+ )
65
+
66
+ if dialect:
67
+ dinfo = get_dialect(dialect)
68
+ if syl.tone and syl.tone != "0" and dinfo.tones and syl.tone not in dinfo.tones:
69
+ warnings.append(
70
+ f"Tone class {syl.tone!r} is not in {dinfo.slug}'s tone inventory "
71
+ f"({sorted(dinfo.tones.keys())})"
72
+ )
73
+
74
+ return ValidationResult(
75
+ valid=len(errors) == 0,
76
+ syllable=syl,
77
+ errors=tuple(errors),
78
+ warnings=tuple(warnings),
79
+ )
pyhakka/py.typed ADDED
File without changes
@@ -0,0 +1,17 @@
1
+ """Tone sandhi and progressive assimilation module."""
2
+
3
+ from pyhakka.sandhi.rules import (
4
+ SUPPORTED_SANDHI,
5
+ SandhiChange,
6
+ SandhiResult,
7
+ apply_sandhi,
8
+ list_sandhi_dialects,
9
+ )
10
+
11
+ __all__ = [
12
+ "SUPPORTED_SANDHI",
13
+ "SandhiChange",
14
+ "SandhiResult",
15
+ "apply_sandhi",
16
+ "list_sandhi_dialects",
17
+ ]