@hakkacl/hakka 0.0.0-stage → 0.1.0

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,360 @@
1
+ /**
2
+ * Supported Hakka romanization / transcription schemes and dialect phonetic
3
+ * profiles.
4
+ */
5
+
6
+ /** Canonical scheme names understood by all conversion APIs. */
7
+ export const CANONICAL_SCHEMES = Object.freeze([
8
+ "hps_ext",
9
+ "hps_diacritic",
10
+ "ipa",
11
+ "ipa_value",
12
+ "ipa_chao",
13
+ "gd",
14
+ "hkilang",
15
+ ]);
16
+
17
+ /** Accepted aliases -> canonical scheme name. */
18
+ export const SCHEME_ALIASES = Object.freeze({
19
+ hps_ext: "hps_ext",
20
+ "hps-ext": "hps_ext",
21
+ hps: "hps_ext",
22
+ hakka_pinyin: "hps_ext",
23
+ pinyin: "hps_ext",
24
+ hps_diacritic: "hps_diacritic",
25
+ "hps-diacritic": "hps_diacritic",
26
+ hps_mark: "hps_diacritic",
27
+ diacritic: "hps_diacritic",
28
+ ipa: "ipa",
29
+ ipa_value: "ipa_value",
30
+ "ipa-value": "ipa_value",
31
+ ipa_numeric: "ipa_value",
32
+ ipa_chao: "ipa_chao",
33
+ "ipa-chao": "ipa_chao",
34
+ chao: "ipa_chao",
35
+ gd: "gd",
36
+ guangdong: "gd",
37
+ gd_pinyin: "gd",
38
+ "gd-pinyin": "gd",
39
+ meixian_pinyin: "gd",
40
+ hkilang: "hkilang",
41
+ hk: "hkilang",
42
+ hk_assoc: "hkilang",
43
+ });
44
+
45
+ /** Tone 1-6 (Guangdong / HKILANG) <-> canonical 1-8 tone classes. */
46
+ export const TONE_6_TO_8 = Object.freeze({
47
+ 1: "1",
48
+ 2: "2",
49
+ 3: "3",
50
+ 4: "5",
51
+ 5: "7",
52
+ 6: "8",
53
+ 0: "0",
54
+ "": "",
55
+ });
56
+
57
+ /** Canonical 1-8 tone classes <-> tone 1-6 (Guangdong / HKILANG). */
58
+ export const TONE_8_TO_6 = Object.freeze({
59
+ 1: "1",
60
+ 2: "2",
61
+ 3: "3",
62
+ 4: "3",
63
+ 5: "4",
64
+ 6: "4",
65
+ 7: "5",
66
+ 8: "6",
67
+ 0: "0",
68
+ "": "",
69
+ });
70
+
71
+ export const SUPERSCRIPT_DIGITS = Object.freeze({
72
+ 0: "⁰",
73
+ 1: "¹",
74
+ 2: "²",
75
+ 3: "³",
76
+ 4: "⁴",
77
+ 5: "⁵",
78
+ 6: "⁶",
79
+ 7: "⁷",
80
+ 8: "⁸",
81
+ 9: "⁹",
82
+ });
83
+
84
+ export const FROM_SUPERSCRIPT_DIGITS = Object.freeze({
85
+ "⁰": "0",
86
+ "¹": "1",
87
+ "²": "2",
88
+ "³": "3",
89
+ "⁴": "4",
90
+ "⁵": "5",
91
+ "⁶": "6",
92
+ "⁷": "7",
93
+ "⁸": "8",
94
+ "⁹": "9",
95
+ });
96
+
97
+ export const CHAO_TO_DIGITS = Object.freeze({
98
+ "˩": "1",
99
+ "˨": "2",
100
+ "˧": "3",
101
+ "˦": "4",
102
+ "˥": "5",
103
+ });
104
+
105
+ export const DIGITS_TO_CHAO = Object.freeze({
106
+ 1: "˩",
107
+ 2: "˨",
108
+ 3: "˧",
109
+ 4: "˦",
110
+ 5: "˥",
111
+ });
112
+
113
+ export const DEFAULT_HPS_DIACRITICS = Object.freeze({
114
+ 1: "",
115
+ 2: "ˇ",
116
+ 3: "ˋ",
117
+ 4: "ˋ",
118
+ 5: "^",
119
+ 6: "+",
120
+ 7: "ˋ",
121
+ 8: "",
122
+ 0: "",
123
+ });
124
+
125
+ export const SIXIAN_DIACRITICS = Object.freeze({
126
+ 1: "ˊ",
127
+ 2: "ˇ",
128
+ 3: "ˋ",
129
+ 5: "",
130
+ 6: "",
131
+ 7: "ˋ",
132
+ 8: "",
133
+ });
134
+
135
+ export const HAILU_DIACRITICS = Object.freeze({
136
+ 1: "ˋ",
137
+ 2: "",
138
+ 3: "ˊ",
139
+ 5: "ˇ",
140
+ 6: "+",
141
+ 7: "",
142
+ 8: "ˋ",
143
+ });
144
+
145
+ export const DABU_TW_DIACRITICS = Object.freeze({
146
+ 1: "+",
147
+ 2: "ˇ",
148
+ 3: "^",
149
+ 5: "ˋ",
150
+ 7: "^",
151
+ 8: "ˋ",
152
+ });
153
+
154
+ export const RAOPING_DIACRITICS = Object.freeze({
155
+ 1: "ˇ",
156
+ 2: "",
157
+ 3: "ˋ",
158
+ 5: "ˊ",
159
+ 7: "ˋ",
160
+ 8: "",
161
+ });
162
+
163
+ export const ZHAOAN_DIACRITICS = Object.freeze({
164
+ 1: "ˇ",
165
+ 2: "ˋ",
166
+ 3: "^",
167
+ 5: "",
168
+ 7: "ˊ",
169
+ 8: "ˋ",
170
+ });
171
+
172
+ export const CHANGTING_DIACRITICS = Object.freeze({
173
+ 1: "+",
174
+ 2: "ˊ",
175
+ 3: "ˋ",
176
+ 5: "^",
177
+ 6: "ˇ",
178
+ });
179
+
180
+ /**
181
+ * @typedef {object} DialectPhonProfile
182
+ * @property {string} slug
183
+ * @property {string} sibilant_series
184
+ * @property {string} o_ipa
185
+ * @property {string} e_ipa
186
+ * @property {string} ee_ipa
187
+ * @property {boolean} o_oo_contrast
188
+ * @property {boolean} ii_closed_as_schwa
189
+ * @property {string} ii_post_sibilant
190
+ * @property {string} x_ipa
191
+ * @property {boolean} ung_as_ong
192
+ * @property {string} a_ipa
193
+ * @property {string} eu_ipa
194
+ * @property {Readonly<Record<string, string>> | null} hps_diacritics
195
+ */
196
+
197
+ const PROFILE_DEFAULTS = Object.freeze({
198
+ sibilant_series: "postalveolar",
199
+ o_ipa: "ɔ",
200
+ e_ipa: "e",
201
+ ee_ipa: "ɛ",
202
+ o_oo_contrast: false,
203
+ ii_closed_as_schwa: false,
204
+ ii_post_sibilant: "ʅ",
205
+ x_ipa: "ɕ",
206
+ ung_as_ong: false,
207
+ a_ipa: "a",
208
+ eu_ipa: "eu",
209
+ hps_diacritics: null,
210
+ });
211
+
212
+ function profile(slug, overrides = {}) {
213
+ return Object.freeze({ slug, ...PROFILE_DEFAULTS, ...overrides });
214
+ }
215
+
216
+ /** Dialect-specific IPA realization details. */
217
+ export const DIALECT_PHON_PROFILES = Object.freeze({
218
+ huiyang: profile("huiyang", {
219
+ o_ipa: "o",
220
+ e_ipa: "e",
221
+ hps_diacritics: DEFAULT_HPS_DIACRITICS,
222
+ }),
223
+ meixian: profile("meixian", {
224
+ o_ipa: "ɔ",
225
+ e_ipa: "e",
226
+ ii_closed_as_schwa: true,
227
+ hps_diacritics: DEFAULT_HPS_DIACRITICS,
228
+ }),
229
+ changting: profile("changting", {
230
+ sibilant_series: "postalveolar",
231
+ o_ipa: "o",
232
+ e_ipa: "e",
233
+ o_oo_contrast: true,
234
+ ii_post_sibilant: "ɨ",
235
+ a_ipa: "ɑ",
236
+ eu_ipa: "əɯ",
237
+ hps_diacritics: CHANGTING_DIACRITICS,
238
+ }),
239
+ xingning: profile("xingning", {
240
+ sibilant_series: "retroflex",
241
+ o_ipa: "ɔ",
242
+ e_ipa: "e",
243
+ ii_post_sibilant: "ʅ",
244
+ hps_diacritics: DEFAULT_HPS_DIACRITICS,
245
+ }),
246
+ wuhua: profile("wuhua", {
247
+ sibilant_series: "postalveolar",
248
+ o_ipa: "o",
249
+ e_ipa: "e",
250
+ ii_post_sibilant: "ʅ",
251
+ hps_diacritics: {
252
+ 1: "",
253
+ 2: "ˊ",
254
+ 3: "ˋ",
255
+ 5: "^",
256
+ 7: "ˋ",
257
+ 8: "",
258
+ },
259
+ }),
260
+ "dabu-baihou": profile("dabu-baihou", {
261
+ sibilant_series: "postalveolar",
262
+ o_ipa: "ɔ",
263
+ e_ipa: "e",
264
+ ee_ipa: "æ",
265
+ x_ipa: "ç",
266
+ ung_as_ong: true,
267
+ ii_post_sibilant: "ʅ",
268
+ hps_diacritics: {
269
+ 1: "ˊ",
270
+ 2: "ˇ",
271
+ 3: "ˋ",
272
+ 5: "^",
273
+ 7: "ˋ",
274
+ 8: "",
275
+ },
276
+ }),
277
+ "huiyang-danshui": profile("huiyang-danshui", {
278
+ o_ipa: "o",
279
+ e_ipa: "e",
280
+ hps_diacritics: DEFAULT_HPS_DIACRITICS,
281
+ }),
282
+ "huiyang-1987": profile("huiyang-1987", {
283
+ o_ipa: "o",
284
+ e_ipa: "e",
285
+ hps_diacritics: DEFAULT_HPS_DIACRITICS,
286
+ }),
287
+ "huidong-daling": profile("huidong-daling", {
288
+ o_ipa: "o",
289
+ e_ipa: "e",
290
+ hps_diacritics: DEFAULT_HPS_DIACRITICS,
291
+ }),
292
+ "shenzhen-baoan": profile("shenzhen-baoan", {
293
+ o_ipa: "o",
294
+ e_ipa: "e",
295
+ hps_diacritics: DEFAULT_HPS_DIACRITICS,
296
+ }),
297
+ "hongkong-newterritories": profile("hongkong-newterritories", {
298
+ o_ipa: "o",
299
+ e_ipa: "e",
300
+ hps_diacritics: DEFAULT_HPS_DIACRITICS,
301
+ }),
302
+ "mcp:臺灣四縣腔": profile("mcp:臺灣四縣腔", {
303
+ o_ipa: "o",
304
+ e_ipa: "e",
305
+ ii_closed_as_schwa: true,
306
+ hps_diacritics: SIXIAN_DIACRITICS,
307
+ }),
308
+ "mcp:臺灣南四縣腔": profile("mcp:臺灣南四縣腔", {
309
+ o_ipa: "o",
310
+ e_ipa: "e",
311
+ ii_closed_as_schwa: true,
312
+ hps_diacritics: SIXIAN_DIACRITICS,
313
+ }),
314
+ "mcp:臺灣海陸腔": profile("mcp:臺灣海陸腔", {
315
+ sibilant_series: "postalveolar",
316
+ o_ipa: "o",
317
+ e_ipa: "e",
318
+ hps_diacritics: HAILU_DIACRITICS,
319
+ }),
320
+ "mcp:臺灣饒平腔": profile("mcp:臺灣饒平腔", {
321
+ o_ipa: "o",
322
+ e_ipa: "e",
323
+ hps_diacritics: RAOPING_DIACRITICS,
324
+ }),
325
+ "mcp:臺灣詔安腔": profile("mcp:臺灣詔安腔", {
326
+ o_ipa: "o",
327
+ e_ipa: "e",
328
+ ee_ipa: "ɛ",
329
+ o_oo_contrast: true,
330
+ hps_diacritics: ZHAOAN_DIACRITICS,
331
+ }),
332
+ });
333
+
334
+ export const DEFAULT_PHON_PROFILE = profile("default");
335
+
336
+ /**
337
+ * Normalize a scheme alias to one of {@link CANONICAL_SCHEMES}.
338
+ * @param {string} scheme
339
+ * @returns {string}
340
+ */
341
+ export function normalizeScheme(scheme) {
342
+ const key = String(scheme ?? "").trim().toLowerCase();
343
+ if (!Object.hasOwn(SCHEME_ALIASES, key)) {
344
+ throw new Error(
345
+ `Unknown phonetic scheme '${scheme}'. Supported: ${CANONICAL_SCHEMES.join(", ")}`,
346
+ );
347
+ }
348
+ return SCHEME_ALIASES[key];
349
+ }
350
+
351
+ /**
352
+ * Look up dialect-specific IPA details.
353
+ * @param {string} dialectSlug
354
+ * @returns {DialectPhonProfile}
355
+ */
356
+ export function getPhonProfile(dialectSlug) {
357
+ return Object.hasOwn(DIALECT_PHON_PROFILES, dialectSlug)
358
+ ? DIALECT_PHON_PROFILES[dialectSlug]
359
+ : DEFAULT_PHON_PROFILE;
360
+ }
@@ -0,0 +1,258 @@
1
+ /**
2
+ * Canonical intermediate representation (IR) for a Hakka syllable.
3
+ *
4
+ * A syllable is represented as a frozen plain object so that it is directly
5
+ * JSON-serializable and comparable with ordinary object literals. Helper
6
+ * accessors and immutable update methods are installed as non-enumerable
7
+ * properties.
8
+ */
9
+
10
+ /** Codas that make a syllable "checked" (入聲). */
11
+ export const CHECKED_CODAS = new Set(["b", "d", "g", "h"]);
12
+
13
+ /** Nasal codas (陽聲韻). */
14
+ export const NASAL_CODAS = new Set(["m", "n", "ng"]);
15
+
16
+ /** Syllabic consonants that may form a rime on their own. */
17
+ export const SYLLABIC_CONSONANTS = new Set(["m", "n", "ng", "v"]);
18
+
19
+ /** Canonical ordered onsets (longest first for greedy matching). */
20
+ export const HPS_EXT_ONSETS = Object.freeze([
21
+ "zh",
22
+ "ch",
23
+ "sh",
24
+ "rh",
25
+ "ng",
26
+ "nj",
27
+ "bb",
28
+ "dd",
29
+ "gg",
30
+ "gh",
31
+ "zz",
32
+ "jj",
33
+ "hh",
34
+ "sl",
35
+ "th",
36
+ "b",
37
+ "p",
38
+ "m",
39
+ "f",
40
+ "v",
41
+ "d",
42
+ "t",
43
+ "n",
44
+ "l",
45
+ "r",
46
+ "g",
47
+ "k",
48
+ "h",
49
+ "z",
50
+ "c",
51
+ "s",
52
+ "j",
53
+ "q",
54
+ "x",
55
+ "'",
56
+ ]);
57
+
58
+ /** Set form of {@link HPS_EXT_ONSETS}. */
59
+ export const HPS_EXT_ONSET_SET = new Set(HPS_EXT_ONSETS);
60
+
61
+ /** Canonical ordered nuclei (longest first). */
62
+ export const HPS_EXT_NUCLEI = Object.freeze([
63
+ "ii",
64
+ "ee",
65
+ "ae",
66
+ "oo",
67
+ "oe",
68
+ "er",
69
+ "a",
70
+ "e",
71
+ "i",
72
+ "o",
73
+ "u",
74
+ "y",
75
+ ]);
76
+
77
+ /** Canonical codas, ordered longest first. */
78
+ export const HPS_EXT_CODAS = Object.freeze([
79
+ "ng",
80
+ "m",
81
+ "n",
82
+ "b",
83
+ "d",
84
+ "g",
85
+ "h",
86
+ "i",
87
+ "u",
88
+ ]);
89
+
90
+ const SYLLABLE_FIELDS = Object.freeze([
91
+ "onset",
92
+ "medial",
93
+ "nucleus",
94
+ "coda",
95
+ "nasalized",
96
+ "tone",
97
+ "tone_value",
98
+ "reading_mark",
99
+ "raw",
100
+ ]);
101
+
102
+ const SYLLABLE_DEFAULTS = Object.freeze({
103
+ onset: "",
104
+ medial: "",
105
+ nucleus: "",
106
+ coda: "",
107
+ nasalized: false,
108
+ tone: "",
109
+ tone_value: "",
110
+ reading_mark: "",
111
+ raw: "",
112
+ });
113
+
114
+ /** Return the full rime (medial + nucleus + coda + optional nasalization). */
115
+ export function finalOf(syllable) {
116
+ const medial = syllable.medial ?? "";
117
+ const nucleus = syllable.nucleus ?? "";
118
+ const coda = syllable.coda ?? "";
119
+ if (syllable.nasalized) {
120
+ if (coda === "h") {
121
+ return `${medial}${nucleus}nnh`;
122
+ }
123
+ return `${medial}${nucleus}${coda}nn`;
124
+ }
125
+ return `${medial}${nucleus}${coda}`;
126
+ }
127
+
128
+ /** Return the segmental spelling without tone. */
129
+ export function baseOf(syllable) {
130
+ return `${syllable.onset ?? ""}${finalOf(syllable)}`;
131
+ }
132
+
133
+ /** Return the canonical HPS-Ext spelling (base + tone class). */
134
+ export function hpsExtOf(syllable) {
135
+ return `${baseOf(syllable)}${syllable.tone ?? ""}`;
136
+ }
137
+
138
+ /** True if the syllable ends in a stop coda (-b/-d/-g/-h). */
139
+ export function isChecked(syllable) {
140
+ return CHECKED_CODAS.has(syllable.coda ?? "");
141
+ }
142
+
143
+ /** True if the syllable ends in a nasal coda (-m/-n/-ng). */
144
+ export function isNasalCoda(syllable) {
145
+ return NASAL_CODAS.has(syllable.coda ?? "");
146
+ }
147
+
148
+ /** True if the rime is a standalone syllabic consonant. */
149
+ export function isSyllabicConsonant(syllable) {
150
+ return (
151
+ (syllable.onset ?? "") === "" &&
152
+ (syllable.medial ?? "") === "" &&
153
+ (syllable.coda ?? "") === "" &&
154
+ SYLLABIC_CONSONANTS.has(syllable.nucleus ?? "")
155
+ );
156
+ }
157
+
158
+ /**
159
+ * Copy the nine canonical fields from any syllable-like object.
160
+ * @param {object} syllable
161
+ * @returns {object}
162
+ */
163
+ export function syllableFields(syllable) {
164
+ const fields = {};
165
+ for (const key of SYLLABLE_FIELDS) {
166
+ let value = syllable[key];
167
+ if (value === undefined || value === null) {
168
+ if (key === "tone_value") value = syllable.toneValue;
169
+ else if (key === "reading_mark") value = syllable.readingMark;
170
+ }
171
+ fields[key] = value === undefined || value === null ? SYLLABLE_DEFAULTS[key] : value;
172
+ }
173
+ fields.nasalized = Boolean(fields.nasalized);
174
+ return fields;
175
+ }
176
+
177
+ /**
178
+ * Create a frozen canonical syllable.
179
+ *
180
+ * @param {object} [fields]
181
+ * @param {string} [fields.onset]
182
+ * @param {string} [fields.medial]
183
+ * @param {string} [fields.nucleus]
184
+ * @param {string} [fields.coda]
185
+ * @param {boolean} [fields.nasalized]
186
+ * @param {string} [fields.tone]
187
+ * @param {string} [fields.tone_value]
188
+ * @param {string} [fields.reading_mark]
189
+ * @param {string} [fields.raw]
190
+ * @returns {object}
191
+ */
192
+ export function createSyllable(fields = {}) {
193
+ const syllable = syllableFields(fields);
194
+
195
+ const define = (name, descriptor) => {
196
+ Object.defineProperty(syllable, name, {
197
+ enumerable: false,
198
+ configurable: false,
199
+ ...descriptor,
200
+ });
201
+ };
202
+
203
+ for (const [alias, field] of [
204
+ ["toneValue", "tone_value"],
205
+ ["readingMark", "reading_mark"],
206
+ ]) {
207
+ define(alias, { get: () => syllable[field] });
208
+ }
209
+
210
+ define("final", { get: () => finalOf(syllable) });
211
+ define("base", { get: () => baseOf(syllable) });
212
+ define("hps_ext", { get: () => hpsExtOf(syllable) });
213
+ define("hpsExt", { get: () => hpsExtOf(syllable) });
214
+ define("is_checked", { get: () => isChecked(syllable) });
215
+ define("isChecked", { get: () => isChecked(syllable) });
216
+ define("is_nasal_coda", { get: () => isNasalCoda(syllable) });
217
+ define("isNasalCoda", { get: () => isNasalCoda(syllable) });
218
+ define("is_syllabic_consonant", { get: () => isSyllabicConsonant(syllable) });
219
+ define("isSyllabicConsonant", { get: () => isSyllabicConsonant(syllable) });
220
+
221
+ define("withTone", {
222
+ value(tone, toneValue) {
223
+ return createSyllable({
224
+ ...syllableFields(syllable),
225
+ tone,
226
+ tone_value: toneValue === undefined || toneValue === null ? syllable.tone_value : toneValue,
227
+ });
228
+ },
229
+ });
230
+ define("with_tone", { value: (tone, toneValue) => syllable.withTone(tone, toneValue) });
231
+
232
+ define("withToneValue", {
233
+ value(toneValue, tone) {
234
+ return createSyllable({
235
+ ...syllableFields(syllable),
236
+ tone: tone === undefined || tone === null ? syllable.tone : tone,
237
+ tone_value: toneValue,
238
+ });
239
+ },
240
+ });
241
+ define("with_tone_value", {
242
+ value: (toneValue, tone) => syllable.withToneValue(toneValue, tone),
243
+ });
244
+
245
+ define("withOnset", {
246
+ value(onset) {
247
+ return createSyllable({ ...syllableFields(syllable), onset });
248
+ },
249
+ });
250
+ define("with_onset", { value: (onset) => syllable.withOnset(onset) });
251
+
252
+ define("toString", { value: () => hpsExtOf(syllable) });
253
+ define("toJSON", {
254
+ value: () => ({ ...syllableFields(syllable), hps_ext: hpsExtOf(syllable) }),
255
+ });
256
+
257
+ return Object.freeze(syllable);
258
+ }