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.
- pyhakka/__init__.py +72 -0
- pyhakka/_db.py +80 -0
- pyhakka/cli.py +293 -0
- pyhakka/data/hakka.sqlite3 +4 -0
- pyhakka/data/manifest.json +106 -0
- pyhakka/dialects.py +163 -0
- pyhakka/lexicon/__init__.py +22 -0
- pyhakka/lexicon/db.py +25 -0
- pyhakka/lexicon/query.py +385 -0
- pyhakka/phon/__init__.py +26 -0
- pyhakka/phon/converter.py +256 -0
- pyhakka/phon/parser.py +748 -0
- pyhakka/phon/schemes.py +319 -0
- pyhakka/phon/syllable.py +164 -0
- pyhakka/phon/validate.py +79 -0
- pyhakka/py.typed +0 -0
- pyhakka/sandhi/__init__.py +17 -0
- pyhakka/sandhi/rules.py +369 -0
- pyhakka-0.1.0.dist-info/METADATA +40 -0
- pyhakka-0.1.0.dist-info/RECORD +24 -0
- pyhakka-0.1.0.dist-info/WHEEL +5 -0
- pyhakka-0.1.0.dist-info/entry_points.txt +2 -0
- pyhakka-0.1.0.dist-info/licenses/LICENSE +29 -0
- pyhakka-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,256 @@
|
|
|
1
|
+
"""Formatters and bidirectional converters between Hakka phonetic schemes."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from pyhakka.dialects import DEFAULT_DIALECT, DialectInfo, get_dialect
|
|
6
|
+
from pyhakka.phon.parser import parse_syllables
|
|
7
|
+
from pyhakka.phon.schemes import (
|
|
8
|
+
DEFAULT_HPS_DIACRITICS,
|
|
9
|
+
DIGITS_TO_CHAO,
|
|
10
|
+
SUPERSCRIPT_DIGITS,
|
|
11
|
+
TONE_8_TO_6,
|
|
12
|
+
get_phon_profile,
|
|
13
|
+
normalize_scheme,
|
|
14
|
+
)
|
|
15
|
+
from pyhakka.phon.syllable import Syllable
|
|
16
|
+
|
|
17
|
+
_ONSET_TO_IPA_BASE: dict[str, str] = {
|
|
18
|
+
"": "",
|
|
19
|
+
"b": "p",
|
|
20
|
+
"p": "pʰ",
|
|
21
|
+
"bb": "b",
|
|
22
|
+
"m": "m",
|
|
23
|
+
"f": "f",
|
|
24
|
+
"v": "v",
|
|
25
|
+
"d": "t",
|
|
26
|
+
"t": "tʰ",
|
|
27
|
+
"dd": "d",
|
|
28
|
+
"n": "n",
|
|
29
|
+
"l": "l",
|
|
30
|
+
"r": "ʝ",
|
|
31
|
+
"g": "k",
|
|
32
|
+
"k": "kʰ",
|
|
33
|
+
"gg": "ɡ",
|
|
34
|
+
"gh": "ɣ",
|
|
35
|
+
"ng": "ŋ",
|
|
36
|
+
"nj": "ȵ",
|
|
37
|
+
"h": "h",
|
|
38
|
+
"hh": "ɦ",
|
|
39
|
+
"z": "ts",
|
|
40
|
+
"c": "tsʰ",
|
|
41
|
+
"s": "s",
|
|
42
|
+
"zz": "z",
|
|
43
|
+
"j": "tɕ",
|
|
44
|
+
"q": "tɕʰ",
|
|
45
|
+
"jj": "ʑ",
|
|
46
|
+
"sl": "ɬ",
|
|
47
|
+
"th": "θ",
|
|
48
|
+
"'": "ʔ",
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
_CODA_TO_IPA: dict[str, str] = {
|
|
52
|
+
"": "",
|
|
53
|
+
"i": "i",
|
|
54
|
+
"u": "u",
|
|
55
|
+
"m": "m",
|
|
56
|
+
"n": "n",
|
|
57
|
+
"ng": "ŋ",
|
|
58
|
+
"b": "p",
|
|
59
|
+
"d": "t",
|
|
60
|
+
"g": "k",
|
|
61
|
+
"h": "ʔ",
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _onset_to_ipa(onset: str, dialect_slug: str) -> str:
|
|
66
|
+
profile = get_phon_profile(dialect_slug)
|
|
67
|
+
if onset in ("zh", "ch", "sh", "rh"):
|
|
68
|
+
if profile.sibilant_series == "retroflex":
|
|
69
|
+
return {"zh": "tʂ", "ch": "tʂʰ", "sh": "ʂ", "rh": "ʐ"}[onset]
|
|
70
|
+
return {"zh": "tʃ", "ch": "tʃʰ", "sh": "ʃ", "rh": "ʒ"}[onset]
|
|
71
|
+
if onset == "x":
|
|
72
|
+
return profile.x_ipa
|
|
73
|
+
return _ONSET_TO_IPA_BASE.get(onset, onset)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _format_ipa_segments(syl: Syllable, dialect_slug: str) -> str:
|
|
77
|
+
"""Format segmental IPA (without tone) for `syl` using the dialect's profile."""
|
|
78
|
+
profile = get_phon_profile(dialect_slug)
|
|
79
|
+
|
|
80
|
+
if syl.is_syllabic_consonant:
|
|
81
|
+
base_sc = {"m": "m̩", "n": "n̩", "ng": "ŋ̍", "v": "v̩"}[syl.nucleus]
|
|
82
|
+
return base_sc
|
|
83
|
+
|
|
84
|
+
onset_ipa = _onset_to_ipa(syl.onset, dialect_slug)
|
|
85
|
+
|
|
86
|
+
# Special syllabic consonant with onset (e.g. l + v)
|
|
87
|
+
if syl.nucleus in ("m", "n", "ng", "v") and not syl.medial and not syl.coda:
|
|
88
|
+
nuc_ipa = {"m": "m̩", "n": "n̩", "ng": "ŋ̍", "v": "v̩"}[syl.nucleus]
|
|
89
|
+
return onset_ipa + nuc_ipa
|
|
90
|
+
|
|
91
|
+
# Medial
|
|
92
|
+
med_ipa = {"": "", "i": "i", "u": "u", "y": "y"}.get(syl.medial, syl.medial)
|
|
93
|
+
|
|
94
|
+
# Changting `eu` -> `əɯ` and `iu` -> `iəɯ`
|
|
95
|
+
if profile.eu_ipa == "əɯ":
|
|
96
|
+
if syl.medial == "" and syl.nucleus == "e" and syl.coda == "u":
|
|
97
|
+
return onset_ipa + "əɯ"
|
|
98
|
+
if syl.medial == "i" and syl.nucleus == "u" and syl.coda == "":
|
|
99
|
+
return onset_ipa + "iəɯ"
|
|
100
|
+
|
|
101
|
+
# Dabu Baihou `ung/ug` -> `oŋ/ok`
|
|
102
|
+
if profile.ung_as_ong and syl.nucleus == "u" and syl.coda in ("ng", "g"):
|
|
103
|
+
nuc_ipa = "o"
|
|
104
|
+
elif syl.nucleus == "ii":
|
|
105
|
+
if profile.ii_closed_as_schwa and syl.coda in ("m", "n", "b", "d"):
|
|
106
|
+
nuc_ipa = "ə"
|
|
107
|
+
elif syl.onset in ("zh", "ch", "sh", "rh"):
|
|
108
|
+
nuc_ipa = profile.ii_post_sibilant
|
|
109
|
+
else:
|
|
110
|
+
nuc_ipa = "ɿ"
|
|
111
|
+
elif syl.nucleus == "a":
|
|
112
|
+
nuc_ipa = profile.a_ipa
|
|
113
|
+
elif syl.nucleus == "e":
|
|
114
|
+
nuc_ipa = profile.e_ipa
|
|
115
|
+
elif syl.nucleus == "ee":
|
|
116
|
+
nuc_ipa = profile.ee_ipa
|
|
117
|
+
elif syl.nucleus == "ae":
|
|
118
|
+
nuc_ipa = "æ"
|
|
119
|
+
elif syl.nucleus == "o":
|
|
120
|
+
nuc_ipa = profile.o_ipa
|
|
121
|
+
elif syl.nucleus == "oo":
|
|
122
|
+
nuc_ipa = "ɔ"
|
|
123
|
+
elif syl.nucleus == "er":
|
|
124
|
+
nuc_ipa = "ə"
|
|
125
|
+
elif syl.nucleus == "y":
|
|
126
|
+
nuc_ipa = "y"
|
|
127
|
+
elif syl.nucleus == "oe":
|
|
128
|
+
nuc_ipa = "œ"
|
|
129
|
+
else:
|
|
130
|
+
nuc_ipa = syl.nucleus
|
|
131
|
+
|
|
132
|
+
if syl.nasalized:
|
|
133
|
+
nuc_ipa = nuc_ipa + "\u0303"
|
|
134
|
+
|
|
135
|
+
coda_ipa = _CODA_TO_IPA.get(syl.coda, syl.coda)
|
|
136
|
+
return f"{onset_ipa}{med_ipa}{nuc_ipa}{coda_ipa}"
|
|
137
|
+
|
|
138
|
+
|
|
139
|
+
def _format_gd(syl: Syllable) -> str:
|
|
140
|
+
"""Format canonical `Syllable` to Guangdong Hakka Pinyin (廣東客拼, 1-6 tones)."""
|
|
141
|
+
tone6 = TONE_8_TO_6.get(syl.tone, syl.tone)
|
|
142
|
+
if syl.is_syllabic_consonant:
|
|
143
|
+
return f"{syl.nucleus}{tone6}"
|
|
144
|
+
|
|
145
|
+
onset = syl.onset
|
|
146
|
+
medial = syl.medial
|
|
147
|
+
nucleus = syl.nucleus
|
|
148
|
+
coda = syl.coda
|
|
149
|
+
|
|
150
|
+
# 1. Zero onset with `i` medial/nucleus -> `y` onset
|
|
151
|
+
if onset == "" and (medial == "i" or nucleus == "i"):
|
|
152
|
+
onset = "y"
|
|
153
|
+
if medial == "i":
|
|
154
|
+
medial = ""
|
|
155
|
+
# 2. `z/c/s` + `i` (palatalized) -> `j/q/x`
|
|
156
|
+
elif onset in ("z", "c", "s") and (medial == "i" or nucleus == "i"):
|
|
157
|
+
onset = {"z": "j", "c": "q", "s": "x"}[onset]
|
|
158
|
+
# 3. `z/c/s` + `ii`: open `ii` -> `i`, closed `iim/iin/iib/iid` -> `em/en/eb/ed`
|
|
159
|
+
elif onset in ("z", "c", "s") and nucleus == "ii":
|
|
160
|
+
if coda in ("m", "n", "b", "d"):
|
|
161
|
+
nucleus = "e"
|
|
162
|
+
else:
|
|
163
|
+
nucleus = "i"
|
|
164
|
+
# 4. `[ɛ]` (`e` in Meixian) -> `ê`
|
|
165
|
+
elif nucleus == "e":
|
|
166
|
+
nucleus = "ê"
|
|
167
|
+
|
|
168
|
+
# Normalize HPS-Ext vowels outside GD core gracefully
|
|
169
|
+
if nucleus == "ee":
|
|
170
|
+
nucleus = "ê"
|
|
171
|
+
elif nucleus == "oo":
|
|
172
|
+
nucleus = "o"
|
|
173
|
+
elif nucleus == "er":
|
|
174
|
+
nucleus = "e"
|
|
175
|
+
|
|
176
|
+
nasal = "nn" if syl.nasalized else ""
|
|
177
|
+
return f"{onset}{medial}{nucleus}{coda}{nasal}{tone6}"
|
|
178
|
+
|
|
179
|
+
|
|
180
|
+
def _format_hkilang(syl: Syllable) -> str:
|
|
181
|
+
"""Format canonical `Syllable` to HKILANG romanization (1-6 tones, -p/-t/-k, `y-` for i-)."""
|
|
182
|
+
tone6 = TONE_8_TO_6.get(syl.tone, syl.tone)
|
|
183
|
+
if syl.is_syllabic_consonant:
|
|
184
|
+
return f"{syl.nucleus}{tone6}"
|
|
185
|
+
coda = {"b": "p", "d": "t", "g": "k"}.get(syl.coda, syl.coda)
|
|
186
|
+
nasal = "nn" if syl.nasalized else ""
|
|
187
|
+
|
|
188
|
+
onset = syl.onset
|
|
189
|
+
medial = syl.medial
|
|
190
|
+
nucleus = syl.nucleus
|
|
191
|
+
|
|
192
|
+
if onset == "'":
|
|
193
|
+
# Glottal zero-onset (`'iu1` 歐) -> bare `iu1` in HKILANG
|
|
194
|
+
onset = ""
|
|
195
|
+
elif onset == "" and (medial == "i" or nucleus == "i"):
|
|
196
|
+
# Palatal zero-onset (`id7 / iu1 / ia2 / iung5`) -> HKILANG `yit5 / yiu1 / ya2 / yung4`
|
|
197
|
+
onset = "y"
|
|
198
|
+
if medial == "i" and not (nucleus == "u" and coda == ""):
|
|
199
|
+
medial = ""
|
|
200
|
+
|
|
201
|
+
return f"{onset}{medial}{nucleus}{coda}{nasal}{tone6}"
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _format_hps_diacritic(syl: Syllable, dialect_slug: str) -> str:
|
|
205
|
+
profile = get_phon_profile(dialect_slug)
|
|
206
|
+
dmap = profile.hps_diacritics or DEFAULT_HPS_DIACRITICS
|
|
207
|
+
diac = dmap.get(syl.tone, "")
|
|
208
|
+
return f"{syl.base}{diac}"
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
def format_syllable(
|
|
212
|
+
syl: Syllable,
|
|
213
|
+
scheme: str = "hps_ext",
|
|
214
|
+
dialect: str = DEFAULT_DIALECT,
|
|
215
|
+
) -> str:
|
|
216
|
+
"""Format a `Syllable` into the requested transcription `scheme`."""
|
|
217
|
+
canon = normalize_scheme(scheme)
|
|
218
|
+
dinfo: DialectInfo | None = get_dialect(dialect) if dialect else None
|
|
219
|
+
slug = dinfo.slug if dinfo is not None else "meixian"
|
|
220
|
+
|
|
221
|
+
if canon == "hps_ext":
|
|
222
|
+
return syl.hps_ext
|
|
223
|
+
if canon == "hps_diacritic":
|
|
224
|
+
return _format_hps_diacritic(syl, slug)
|
|
225
|
+
if canon == "gd":
|
|
226
|
+
return _format_gd(syl)
|
|
227
|
+
if canon == "hkilang":
|
|
228
|
+
return _format_hkilang(syl)
|
|
229
|
+
|
|
230
|
+
seg_ipa = _format_ipa_segments(syl, slug)
|
|
231
|
+
if canon == "ipa":
|
|
232
|
+
tone_part = syl.tone or syl.tone_value
|
|
233
|
+
return f"{seg_ipa}{tone_part}{syl.reading_mark}"
|
|
234
|
+
|
|
235
|
+
# Tone value lookup (explicit tone_value takes precedence — e.g. after sandhi!)
|
|
236
|
+
tv = syl.tone_value or (dinfo.tone_value_for(syl.tone) if (dinfo and syl.tone) else "")
|
|
237
|
+
if canon == "ipa_value":
|
|
238
|
+
sup = tv.translate(SUPERSCRIPT_DIGITS) if tv else syl.tone.translate(SUPERSCRIPT_DIGITS)
|
|
239
|
+
return f"{seg_ipa}{sup}{syl.reading_mark}"
|
|
240
|
+
if canon == "ipa_chao":
|
|
241
|
+
chao = tv.translate(DIGITS_TO_CHAO) if tv else syl.tone
|
|
242
|
+
return f"{seg_ipa}{chao}{syl.reading_mark}"
|
|
243
|
+
|
|
244
|
+
raise ValueError(f"Unsupported output scheme: {scheme!r}")
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def convert(
|
|
248
|
+
text: str,
|
|
249
|
+
src: str = "hps_ext",
|
|
250
|
+
dst: str = "ipa",
|
|
251
|
+
dialect: str = DEFAULT_DIALECT,
|
|
252
|
+
sep: str = " ",
|
|
253
|
+
) -> str:
|
|
254
|
+
"""Convert a whitespace-separated Hakka transcription from `src` scheme to `dst` scheme."""
|
|
255
|
+
syllables = parse_syllables(text, scheme=src, dialect=dialect)
|
|
256
|
+
return sep.join(format_syllable(s, scheme=dst, dialect=dialect) for s in syllables)
|