@hakkacl/hakka 0.0.0-stage → 0.1.1
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.
- package/LICENSE +29 -0
- package/README.md +205 -2
- package/data/dialects.json +1 -0
- package/package.json +48 -4
- package/src/dialects.js +207 -0
- package/src/index.js +12 -0
- package/src/phon/converter.js +365 -0
- package/src/phon/parser.js +991 -0
- package/src/phon/schemes.js +356 -0
- package/src/phon/syllable.js +258 -0
- package/src/phon/validate.js +118 -0
- package/src/sandhi.js +398 -0
- package/types/index.d.ts +370 -0
|
@@ -0,0 +1,991 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parsers converting Hakka transcriptions in multiple schemes into the
|
|
3
|
+
* canonical syllable IR.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { DEFAULT_DIALECT, getDialect } from "../dialects.js";
|
|
7
|
+
import {
|
|
8
|
+
CHAO_TO_DIGITS,
|
|
9
|
+
DEFAULT_HPP_DIACRITICS,
|
|
10
|
+
FROM_SUPERSCRIPT_DIGITS,
|
|
11
|
+
TONE_6_TO_8,
|
|
12
|
+
getPhonProfile,
|
|
13
|
+
normalizeScheme,
|
|
14
|
+
} from "./schemes.js";
|
|
15
|
+
import {
|
|
16
|
+
CHECKED_CODAS,
|
|
17
|
+
HPP_ONSETS,
|
|
18
|
+
SYLLABIC_CONSONANTS,
|
|
19
|
+
createSyllable,
|
|
20
|
+
} from "./syllable.js";
|
|
21
|
+
|
|
22
|
+
export const READING_MARKS = new Set(["-", "=", "+", "*", "@", "?"]);
|
|
23
|
+
|
|
24
|
+
const TRAILING_TONE_RE = /([0-9]+|[˩˨˧˦˥]+)$/u;
|
|
25
|
+
const HPP_DIACRITIC_RE = /([ˊˇˋ+^])$/u;
|
|
26
|
+
|
|
27
|
+
/** Ordered IPA onset table: [ipa_prefix, hpp_onset]. */
|
|
28
|
+
const IPA_ONSETS = Object.freeze([
|
|
29
|
+
["tʃʰ", "ch"],
|
|
30
|
+
["tʂʰ", "ch"],
|
|
31
|
+
["ʧʰ", "ch"],
|
|
32
|
+
["tɕʰ", "q"],
|
|
33
|
+
["tsʰ", "c"],
|
|
34
|
+
["dʑ", "jj"],
|
|
35
|
+
["dz", "zz"],
|
|
36
|
+
["tʃ", "zh"],
|
|
37
|
+
["tʂ", "zh"],
|
|
38
|
+
["ʧ", "zh"],
|
|
39
|
+
["tɕ", "j"],
|
|
40
|
+
["ts", "z"],
|
|
41
|
+
["pʰ", "p"],
|
|
42
|
+
["tʰ", "t"],
|
|
43
|
+
["kʰ", "k"],
|
|
44
|
+
["ȶʰ", "q"],
|
|
45
|
+
["cʰ", "q"],
|
|
46
|
+
["p'", "p"],
|
|
47
|
+
["t'", "t"],
|
|
48
|
+
["k'", "k"],
|
|
49
|
+
["ts'", "c"],
|
|
50
|
+
["tʃ'", "ch"],
|
|
51
|
+
["tʂ'", "ch"],
|
|
52
|
+
["tɕ'", "q"],
|
|
53
|
+
["b", "bb"],
|
|
54
|
+
["p", "b"],
|
|
55
|
+
["d", "dd"],
|
|
56
|
+
["t", "d"],
|
|
57
|
+
["ɡ", "gg"],
|
|
58
|
+
["g", "gg"],
|
|
59
|
+
["k", "g"],
|
|
60
|
+
["m", "m"],
|
|
61
|
+
["n", "n"],
|
|
62
|
+
["ŋ", "ng"],
|
|
63
|
+
["ȵ", "nj"],
|
|
64
|
+
["ɲ", "nj"],
|
|
65
|
+
["ɳ", "nj"],
|
|
66
|
+
["f", "f"],
|
|
67
|
+
["ɸ", "f"],
|
|
68
|
+
["v", "v"],
|
|
69
|
+
["ʋ", "v"],
|
|
70
|
+
["w", "v"],
|
|
71
|
+
["l", "l"],
|
|
72
|
+
["ɬ", "sl"],
|
|
73
|
+
["θ", "th"],
|
|
74
|
+
["s", "s"],
|
|
75
|
+
["z", "zz"],
|
|
76
|
+
["ʃ", "sh"],
|
|
77
|
+
["ʂ", "sh"],
|
|
78
|
+
["ʒ", "rh"],
|
|
79
|
+
["ʐ", "rh"],
|
|
80
|
+
["ɕ", "x"],
|
|
81
|
+
["ç", "x"],
|
|
82
|
+
["ʑ", "jj"],
|
|
83
|
+
["ȶ", "j"],
|
|
84
|
+
["c", "j"],
|
|
85
|
+
["ɟ", "jj"],
|
|
86
|
+
["ʝ", "r"],
|
|
87
|
+
["ɣ", "gh"],
|
|
88
|
+
["h", "h"],
|
|
89
|
+
["x", "h"],
|
|
90
|
+
["ɦ", "hh"],
|
|
91
|
+
["r", "r"],
|
|
92
|
+
["ɹ", "r"],
|
|
93
|
+
]);
|
|
94
|
+
|
|
95
|
+
const IPA_CODA_MAP = Object.freeze([
|
|
96
|
+
["ŋ", "ng"],
|
|
97
|
+
["m", "m"],
|
|
98
|
+
["n", "n"],
|
|
99
|
+
["p", "b"],
|
|
100
|
+
["t", "d"],
|
|
101
|
+
["k", "g"],
|
|
102
|
+
["ʔ", "h"],
|
|
103
|
+
["ˀ", "h"],
|
|
104
|
+
]);
|
|
105
|
+
|
|
106
|
+
const IPA_VOWEL_ATOMS = new Set([
|
|
107
|
+
..."aeioɔɛæəɤʌɜɘɚɑɐᴀᴇɪɒʊɯuyʉøœɵɿʅɨʮʯ",
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
const VALID_NUCLEI = new Set([
|
|
111
|
+
"a",
|
|
112
|
+
"e",
|
|
113
|
+
"ee",
|
|
114
|
+
"ae",
|
|
115
|
+
"i",
|
|
116
|
+
"ii",
|
|
117
|
+
"o",
|
|
118
|
+
"oo",
|
|
119
|
+
"u",
|
|
120
|
+
"y",
|
|
121
|
+
"oe",
|
|
122
|
+
"er",
|
|
123
|
+
]);
|
|
124
|
+
|
|
125
|
+
const VALID_MEDIALS = Object.freeze(["i", "u", "y"]);
|
|
126
|
+
|
|
127
|
+
const IPA_SYLLABIC_CONSONANTS = Object.freeze({
|
|
128
|
+
m: "m",
|
|
129
|
+
n: "n",
|
|
130
|
+
ŋ: "ng",
|
|
131
|
+
ȵ: "nj",
|
|
132
|
+
ɲ: "nj",
|
|
133
|
+
v: "v",
|
|
134
|
+
ʋ: "v",
|
|
135
|
+
l: "l",
|
|
136
|
+
});
|
|
137
|
+
|
|
138
|
+
const IPA_SYLLABIC_CODA_CHARS = new Set(["m", "n", "ŋ", "v", "ʋ"]);
|
|
139
|
+
|
|
140
|
+
const NJ_FOLDING_DIALECTS = new Set([
|
|
141
|
+
"meixian",
|
|
142
|
+
"changting",
|
|
143
|
+
"xingning",
|
|
144
|
+
"wuhua",
|
|
145
|
+
"dabu-baihou",
|
|
146
|
+
"huiyang-danshui",
|
|
147
|
+
"huiyang-1987",
|
|
148
|
+
"huidong-daling",
|
|
149
|
+
"shenzhen-baoan",
|
|
150
|
+
"hongkong-newterritories",
|
|
151
|
+
]);
|
|
152
|
+
|
|
153
|
+
function hasOwn(object, key) {
|
|
154
|
+
return Object.hasOwn(object, key);
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function translateCharacters(text, table) {
|
|
158
|
+
let output = "";
|
|
159
|
+
for (const char of text) {
|
|
160
|
+
output += hasOwn(table, char) ? table[char] : char;
|
|
161
|
+
}
|
|
162
|
+
return output;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
function containsChaoLetter(text) {
|
|
166
|
+
return /[˩˨˧˦˥]/u.test(text);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function containsSuperscriptDigit(text) {
|
|
170
|
+
return /[⁰¹²³⁴⁵⁶⁷⁸⁹]/u.test(text);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
function stripCharacters(text, characters) {
|
|
174
|
+
let start = 0;
|
|
175
|
+
let end = text.length;
|
|
176
|
+
while (start < end && characters.includes(text[start])) start += 1;
|
|
177
|
+
while (end > start && characters.includes(text[end - 1])) end -= 1;
|
|
178
|
+
return text.slice(start, end);
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
function stripTrailingReadingMarks(text) {
|
|
182
|
+
let work = text;
|
|
183
|
+
let mark = "";
|
|
184
|
+
while (work && READING_MARKS.has(work.at(-1))) {
|
|
185
|
+
mark = work.at(-1) + mark;
|
|
186
|
+
work = work.slice(0, -1);
|
|
187
|
+
}
|
|
188
|
+
return { work, mark };
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* Split an H++ vowel cluster into `[medial, nucleus, vowelCoda]`.
|
|
193
|
+
* @param {string} vpart
|
|
194
|
+
* @param {boolean} hasConsonantCoda
|
|
195
|
+
* @returns {[string, string, string]}
|
|
196
|
+
*/
|
|
197
|
+
export function splitVowelCluster(vpart, hasConsonantCoda) {
|
|
198
|
+
if (!vpart) {
|
|
199
|
+
throw new Error("Empty vowel cluster in syllable");
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (VALID_NUCLEI.has(vpart)) {
|
|
203
|
+
return ["", vpart, ""];
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// 1. No consonant coda: check pure diphthong without a medial.
|
|
207
|
+
if (!hasConsonantCoda) {
|
|
208
|
+
for (const nucleus of ["ee", "ae", "oo", "ii", "er", "oe", "a", "e", "o"]) {
|
|
209
|
+
if (vpart.startsWith(nucleus)) {
|
|
210
|
+
const remainder = vpart.slice(nucleus.length);
|
|
211
|
+
if (remainder === "i" || remainder === "u") {
|
|
212
|
+
return ["", nucleus, remainder];
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
// 2. Medial + (nucleus + optional vowel coda).
|
|
219
|
+
for (const medial of VALID_MEDIALS) {
|
|
220
|
+
if (vpart.startsWith(medial) && vpart.length > medial.length) {
|
|
221
|
+
const rest = vpart.slice(medial.length);
|
|
222
|
+
if (VALID_NUCLEI.has(rest)) {
|
|
223
|
+
return [medial, rest, ""];
|
|
224
|
+
}
|
|
225
|
+
if (!hasConsonantCoda) {
|
|
226
|
+
for (const nucleus of ["ee", "ae", "oo", "ii", "er", "oe", "a", "e", "o", "i", "u", "y"]) {
|
|
227
|
+
if (rest.startsWith(nucleus)) {
|
|
228
|
+
const remainder = rest.slice(nucleus.length);
|
|
229
|
+
if (remainder === "i" || remainder === "u") {
|
|
230
|
+
return [medial, nucleus, remainder];
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// 3. Fallback for rare triphthongs / vowel glides (iui, uee, uoe, uoo, ...).
|
|
239
|
+
if (!hasConsonantCoda && vpart.length >= 2 && (vpart.at(-1) === "i" || vpart.at(-1) === "u")) {
|
|
240
|
+
return ["", vpart.slice(0, -1), vpart.at(-1)];
|
|
241
|
+
}
|
|
242
|
+
return ["", vpart, ""];
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
/**
|
|
246
|
+
* Parse a tone-stripped H++ syllable.
|
|
247
|
+
* @param {string} base
|
|
248
|
+
* @returns {{onset: string, medial: string, nucleus: string, coda: string, nasalized: boolean}}
|
|
249
|
+
*/
|
|
250
|
+
export function parseHpsExtBase(base) {
|
|
251
|
+
if (!base) {
|
|
252
|
+
throw new Error("Empty syllable base");
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
// Standalone syllabic consonants (m, n, ng, v) with optional glottal coda.
|
|
256
|
+
if (SYLLABIC_CONSONANTS.has(base)) {
|
|
257
|
+
return { onset: "", medial: "", nucleus: base, coda: "", nasalized: false };
|
|
258
|
+
}
|
|
259
|
+
if (base.endsWith("h") && SYLLABIC_CONSONANTS.has(base.slice(0, -1))) {
|
|
260
|
+
return { onset: "", medial: "", nucleus: base.slice(0, -1), coda: "h", nasalized: false };
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
let work = base;
|
|
264
|
+
let onset = "";
|
|
265
|
+
|
|
266
|
+
// Strip onset greedily while leaving a non-empty rime.
|
|
267
|
+
for (const candidate of HPP_ONSETS) {
|
|
268
|
+
if (work.startsWith(candidate) && work.length > candidate.length) {
|
|
269
|
+
const remainder = work.slice(candidate.length);
|
|
270
|
+
if (remainder === "h") continue;
|
|
271
|
+
onset = candidate;
|
|
272
|
+
work = remainder;
|
|
273
|
+
break;
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// Strip glottal stop coda before -nn, so -nnh parses cleanly.
|
|
278
|
+
let coda = "";
|
|
279
|
+
if (work.length > 1 && work.endsWith("h")) {
|
|
280
|
+
coda = "h";
|
|
281
|
+
work = work.slice(0, -1);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
// Nasalization suffix -nn.
|
|
285
|
+
let nasalized = false;
|
|
286
|
+
if (work.length > 2 && work.endsWith("nn")) {
|
|
287
|
+
nasalized = true;
|
|
288
|
+
work = work.slice(0, -2);
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
// Rime is a syllabic consonant after an onset (hng, hvh, hmh, ...).
|
|
292
|
+
if (SYLLABIC_CONSONANTS.has(work)) {
|
|
293
|
+
return { onset, medial: "", nucleus: work, coda, nasalized };
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
// Strip remaining consonant codas when vowels remain before them.
|
|
297
|
+
if (!coda) {
|
|
298
|
+
for (const candidateCoda of ["ng", "m", "n", "b", "d", "g"]) {
|
|
299
|
+
if (work.endsWith(candidateCoda) && work.length > candidateCoda.length) {
|
|
300
|
+
coda = candidateCoda;
|
|
301
|
+
work = work.slice(0, -candidateCoda.length);
|
|
302
|
+
break;
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const [medial, nucleus, vowelCoda] = splitVowelCluster(work, Boolean(coda));
|
|
308
|
+
const finalCoda = coda || vowelCoda;
|
|
309
|
+
return { onset, medial, nucleus, coda: finalCoda, nasalized };
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
/**
|
|
313
|
+
* Resolve `[toneClass, toneValue]` from a numeric tone string.
|
|
314
|
+
* @param {string} rawTone
|
|
315
|
+
* @param {boolean} isChecked
|
|
316
|
+
* @param {object | null} dialectInfo
|
|
317
|
+
* @param {boolean} [forceToneValue]
|
|
318
|
+
* @param {boolean} [allowDialectToneValue]
|
|
319
|
+
* @returns {[string, string]}
|
|
320
|
+
*/
|
|
321
|
+
export function resolveTonePair(
|
|
322
|
+
rawTone,
|
|
323
|
+
isChecked,
|
|
324
|
+
dialectInfo,
|
|
325
|
+
forceToneValue = false,
|
|
326
|
+
allowDialectToneValue = false,
|
|
327
|
+
) {
|
|
328
|
+
if (!rawTone) {
|
|
329
|
+
return ["", ""];
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
let isValue = forceToneValue || rawTone.length >= 2;
|
|
333
|
+
if (
|
|
334
|
+
!isValue &&
|
|
335
|
+
allowDialectToneValue &&
|
|
336
|
+
dialectInfo !== null &&
|
|
337
|
+
dialectInfo !== undefined &&
|
|
338
|
+
dialectInfo.use_tone_value
|
|
339
|
+
) {
|
|
340
|
+
isValue = true;
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
if (isValue) {
|
|
344
|
+
const toneValue = rawTone;
|
|
345
|
+
let toneClass = "";
|
|
346
|
+
if (dialectInfo) {
|
|
347
|
+
const matches = Object.keys(dialectInfo.tones).filter(
|
|
348
|
+
(candidate) => dialectInfo.tones[candidate].value === toneValue,
|
|
349
|
+
);
|
|
350
|
+
if (matches.length === 1) {
|
|
351
|
+
toneClass = matches[0];
|
|
352
|
+
} else if (matches.length > 1) {
|
|
353
|
+
const checkedMatches = matches.filter((candidate) => candidate === "7" || candidate === "8");
|
|
354
|
+
const openMatches = matches.filter((candidate) => candidate !== "7" && candidate !== "8");
|
|
355
|
+
if (isChecked && checkedMatches.length > 0) {
|
|
356
|
+
toneClass = checkedMatches[0];
|
|
357
|
+
} else if (!isChecked && openMatches.length > 0) {
|
|
358
|
+
toneClass = openMatches[0];
|
|
359
|
+
} else {
|
|
360
|
+
toneClass = matches[0];
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
return [toneClass, toneValue];
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
const toneClass = rawTone;
|
|
368
|
+
const toneValue = dialectInfo ? dialectInfo.toneValueFor(toneClass) : "";
|
|
369
|
+
return [toneClass, toneValue];
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
function parseHpsExt(token, dialectInfo) {
|
|
373
|
+
const raw = String(token).trim();
|
|
374
|
+
if (!raw) {
|
|
375
|
+
throw new Error("Empty syllable token");
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
let { work, mark } = stripTrailingReadingMarks(raw);
|
|
379
|
+
work = translateCharacters(work, FROM_SUPERSCRIPT_DIGITS);
|
|
380
|
+
|
|
381
|
+
let toneString = "";
|
|
382
|
+
let forceValue = false;
|
|
383
|
+
const toneMatch = TRAILING_TONE_RE.exec(work);
|
|
384
|
+
if (toneMatch) {
|
|
385
|
+
toneString = toneMatch[1];
|
|
386
|
+
if (containsChaoLetter(toneString)) {
|
|
387
|
+
toneString = translateCharacters(toneString, CHAO_TO_DIGITS);
|
|
388
|
+
forceValue = true;
|
|
389
|
+
}
|
|
390
|
+
work = work.slice(0, toneMatch.index);
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
const base = work.toLowerCase();
|
|
394
|
+
const parsed = parseHpsExtBase(base);
|
|
395
|
+
const [tone, toneValue] = resolveTonePair(
|
|
396
|
+
toneString,
|
|
397
|
+
CHECKED_CODAS.has(parsed.coda),
|
|
398
|
+
dialectInfo,
|
|
399
|
+
forceValue,
|
|
400
|
+
false,
|
|
401
|
+
);
|
|
402
|
+
|
|
403
|
+
return createSyllable({
|
|
404
|
+
...parsed,
|
|
405
|
+
tone,
|
|
406
|
+
tone_value: toneValue,
|
|
407
|
+
reading_mark: mark,
|
|
408
|
+
raw,
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
/**
|
|
413
|
+
* Return tone-class keys in canonical numeric order, with 0 last.
|
|
414
|
+
* JavaScript reorders integer-like object keys numerically; this restores the
|
|
415
|
+
* source registry's intended order.
|
|
416
|
+
* @param {object} tones
|
|
417
|
+
* @returns {string[]}
|
|
418
|
+
*/
|
|
419
|
+
function orderedToneClasses(tones) {
|
|
420
|
+
return Object.keys(tones).sort((a, b) => {
|
|
421
|
+
if (a === "0") return 1;
|
|
422
|
+
if (b === "0") return -1;
|
|
423
|
+
return Number(a) - Number(b);
|
|
424
|
+
});
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
function parseHpsDiacritic(token, dialectInfo) {
|
|
428
|
+
const raw = String(token).trim();
|
|
429
|
+
if (!raw) {
|
|
430
|
+
throw new Error("Empty syllable token");
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
let work = raw;
|
|
434
|
+
let diacritic = "";
|
|
435
|
+
const diacriticMatch = HPP_DIACRITIC_RE.exec(work);
|
|
436
|
+
if (diacriticMatch) {
|
|
437
|
+
diacritic = diacriticMatch[1];
|
|
438
|
+
work = work.slice(0, diacriticMatch.index);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
const base = work.toLowerCase();
|
|
442
|
+
const parsed = parseHpsExtBase(base);
|
|
443
|
+
const checked = CHECKED_CODAS.has(parsed.coda);
|
|
444
|
+
|
|
445
|
+
const slug = dialectInfo ? dialectInfo.slug : "meixian";
|
|
446
|
+
const phonProfile = getPhonProfile(slug);
|
|
447
|
+
const diacriticMap = phonProfile.hpp_diacritics ?? DEFAULT_HPP_DIACRITICS;
|
|
448
|
+
|
|
449
|
+
const candidates = orderedToneClasses(diacriticMap).filter(
|
|
450
|
+
(toneClass) => diacriticMap[toneClass] === diacritic,
|
|
451
|
+
);
|
|
452
|
+
|
|
453
|
+
let tone = "";
|
|
454
|
+
if (candidates.length > 0) {
|
|
455
|
+
const checkedCandidates = candidates.filter(
|
|
456
|
+
(candidate) => candidate === "7" || candidate === "8",
|
|
457
|
+
);
|
|
458
|
+
const openCandidates = candidates.filter(
|
|
459
|
+
(candidate) => candidate !== "7" && candidate !== "8",
|
|
460
|
+
);
|
|
461
|
+
if (checked && checkedCandidates.length > 0) {
|
|
462
|
+
tone = checkedCandidates[0];
|
|
463
|
+
} else if (!checked && openCandidates.length > 0) {
|
|
464
|
+
tone = openCandidates[0];
|
|
465
|
+
} else {
|
|
466
|
+
tone = candidates[0];
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
const toneValue = dialectInfo && tone ? dialectInfo.toneValueFor(tone) : "";
|
|
471
|
+
|
|
472
|
+
return createSyllable({
|
|
473
|
+
...parsed,
|
|
474
|
+
tone,
|
|
475
|
+
tone_value: toneValue,
|
|
476
|
+
reading_mark: "",
|
|
477
|
+
raw,
|
|
478
|
+
});
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function parseGd(token, dialectInfo) {
|
|
482
|
+
const raw = String(token).trim();
|
|
483
|
+
if (!raw) {
|
|
484
|
+
throw new Error("Empty syllable token");
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
let work = translateCharacters(raw, FROM_SUPERSCRIPT_DIGITS);
|
|
488
|
+
let tone6 = "";
|
|
489
|
+
const toneMatch = TRAILING_TONE_RE.exec(work);
|
|
490
|
+
if (toneMatch) {
|
|
491
|
+
tone6 = toneMatch[1];
|
|
492
|
+
work = work.slice(0, toneMatch.index);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
const base = work.toLowerCase();
|
|
496
|
+
if (base === "m" || base === "n" || base === "ng") {
|
|
497
|
+
const tone = hasOwn(TONE_6_TO_8, tone6) ? TONE_6_TO_8[tone6] : tone6;
|
|
498
|
+
const toneValue = dialectInfo ? dialectInfo.toneValueFor(tone) : "";
|
|
499
|
+
return createSyllable({
|
|
500
|
+
onset: "",
|
|
501
|
+
medial: "",
|
|
502
|
+
nucleus: base,
|
|
503
|
+
coda: "",
|
|
504
|
+
nasalized: false,
|
|
505
|
+
tone,
|
|
506
|
+
tone_value: toneValue,
|
|
507
|
+
reading_mark: "",
|
|
508
|
+
raw,
|
|
509
|
+
});
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
// Guangdong `ê` -> `e`, while preserving `e` after z/c/s before m/n/b/d.
|
|
513
|
+
const hasCircumflex = base.includes("ê");
|
|
514
|
+
const clean = base.split("ê").join("e");
|
|
515
|
+
|
|
516
|
+
const gdOnsets = [
|
|
517
|
+
"ng",
|
|
518
|
+
"b",
|
|
519
|
+
"p",
|
|
520
|
+
"m",
|
|
521
|
+
"f",
|
|
522
|
+
"v",
|
|
523
|
+
"d",
|
|
524
|
+
"t",
|
|
525
|
+
"n",
|
|
526
|
+
"l",
|
|
527
|
+
"g",
|
|
528
|
+
"k",
|
|
529
|
+
"h",
|
|
530
|
+
"j",
|
|
531
|
+
"q",
|
|
532
|
+
"x",
|
|
533
|
+
"z",
|
|
534
|
+
"c",
|
|
535
|
+
"s",
|
|
536
|
+
"y",
|
|
537
|
+
];
|
|
538
|
+
let onset = "";
|
|
539
|
+
let rest = clean;
|
|
540
|
+
for (const candidate of gdOnsets) {
|
|
541
|
+
if (clean.startsWith(candidate) && clean.length > candidate.length) {
|
|
542
|
+
onset = candidate;
|
|
543
|
+
rest = clean.slice(candidate.length);
|
|
544
|
+
break;
|
|
545
|
+
}
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
if (onset === "y") {
|
|
549
|
+
// y- -> zero onset + i- (yi -> i, yiu -> iu, yan -> ian, yung -> iung).
|
|
550
|
+
onset = "";
|
|
551
|
+
if (!rest.startsWith("i")) {
|
|
552
|
+
rest = `i${rest}`;
|
|
553
|
+
}
|
|
554
|
+
} else if (onset === "j" || onset === "q" || onset === "x") {
|
|
555
|
+
// j/q/x (palatalized before i) -> z/c/s.
|
|
556
|
+
onset = { j: "z", q: "c", x: "s" }[onset];
|
|
557
|
+
} else if (onset === "z" || onset === "c" || onset === "s") {
|
|
558
|
+
// z/c/s + i (apical vowel [ɿ]) -> ii.
|
|
559
|
+
if (rest === "i") {
|
|
560
|
+
rest = "ii";
|
|
561
|
+
} else if (!hasCircumflex && (rest === "em" || rest === "en" || rest === "eb" || rest === "ed")) {
|
|
562
|
+
rest = `ii${rest.slice(1)}`;
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
// Accept -p/-t/-k alongside the standard -b/-d/-g spellings.
|
|
567
|
+
if (rest.length > 1 && (rest.endsWith("p") || rest.endsWith("t") || rest.endsWith("k"))) {
|
|
568
|
+
rest = rest.slice(0, -1) + { p: "b", t: "d", k: "g" }[rest.at(-1)];
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
const parsed = parseHpsExtBase(onset + rest);
|
|
572
|
+
const tone = hasOwn(TONE_6_TO_8, tone6) ? TONE_6_TO_8[tone6] : tone6;
|
|
573
|
+
const toneValue = dialectInfo ? dialectInfo.toneValueFor(tone) : "";
|
|
574
|
+
|
|
575
|
+
return createSyllable({
|
|
576
|
+
...parsed,
|
|
577
|
+
tone,
|
|
578
|
+
tone_value: toneValue,
|
|
579
|
+
reading_mark: "",
|
|
580
|
+
raw,
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function parseHkilang(token, dialectInfo) {
|
|
585
|
+
const raw = String(token).trim().replace(/\.+$/u, "");
|
|
586
|
+
if (!raw) {
|
|
587
|
+
throw new Error("Empty syllable token");
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
let work = translateCharacters(raw, FROM_SUPERSCRIPT_DIGITS);
|
|
591
|
+
let tone6 = "";
|
|
592
|
+
const toneMatch = TRAILING_TONE_RE.exec(work);
|
|
593
|
+
if (toneMatch) {
|
|
594
|
+
tone6 = toneMatch[1];
|
|
595
|
+
work = work.slice(0, toneMatch.index);
|
|
596
|
+
}
|
|
597
|
+
|
|
598
|
+
let base = work.toLowerCase();
|
|
599
|
+
if (base.length > 1 && (base.endsWith("p") || base.endsWith("t") || base.endsWith("k"))) {
|
|
600
|
+
base = base.slice(0, -1) + { p: "b", t: "d", k: "g" }[base.at(-1)];
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
// HKILANG marks palatal zero-onsets with `y-` and uses bare `iu` for 歐.
|
|
604
|
+
if (base === "iu") {
|
|
605
|
+
base = "'iu";
|
|
606
|
+
} else if (base.startsWith("y") && base.length > 1) {
|
|
607
|
+
const remainder = base.slice(1);
|
|
608
|
+
base = remainder.startsWith("i") ? remainder : `i${remainder}`;
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
const parsed = parseHpsExtBase(base);
|
|
612
|
+
const tone = hasOwn(TONE_6_TO_8, tone6) ? TONE_6_TO_8[tone6] : tone6;
|
|
613
|
+
const toneValue = dialectInfo ? dialectInfo.toneValueFor(tone) : "";
|
|
614
|
+
|
|
615
|
+
return createSyllable({
|
|
616
|
+
...parsed,
|
|
617
|
+
tone,
|
|
618
|
+
tone_value: toneValue,
|
|
619
|
+
reading_mark: "",
|
|
620
|
+
raw,
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
|
|
624
|
+
/**
|
|
625
|
+
* Convert a cleaned IPA vowel sequence into an H++ vowel cluster.
|
|
626
|
+
* @param {string} vseq
|
|
627
|
+
* @param {string} onset
|
|
628
|
+
* @param {string} coda
|
|
629
|
+
* @param {string} dialectSlug
|
|
630
|
+
* @returns {string}
|
|
631
|
+
*/
|
|
632
|
+
export function mapIpaVowels(vseq, onset, coda, dialectSlug) {
|
|
633
|
+
const phonProfile = getPhonProfile(dialectSlug);
|
|
634
|
+
let sequence = vseq;
|
|
635
|
+
|
|
636
|
+
// Changting diphthong `əɯ` -> `eu` (`iəɯ` -> `iu`).
|
|
637
|
+
if (sequence.includes("əɯ")) {
|
|
638
|
+
sequence = sequence.split("iəɯ").join("iu").split("əɯ").join("eu");
|
|
639
|
+
}
|
|
640
|
+
if (sequence.includes("ɐɯ")) {
|
|
641
|
+
sequence = sequence.split("ɐɯ").join("eu");
|
|
642
|
+
}
|
|
643
|
+
if (sequence.includes("ɑɔ")) {
|
|
644
|
+
sequence = sequence.split("ɑɔ").join("au");
|
|
645
|
+
}
|
|
646
|
+
|
|
647
|
+
// Dabu Baihou `oŋ/ok` -> `ung/ug` vs `ɔŋ/ɔk` -> `ong/og`.
|
|
648
|
+
if (phonProfile.ung_as_ong && (coda === "ng" || coda === "g")) {
|
|
649
|
+
if (sequence === "o") return "u";
|
|
650
|
+
if (sequence === "io") return "iu";
|
|
651
|
+
if (sequence === "ɔ") return "o";
|
|
652
|
+
if (sequence === "iɔ") return "io";
|
|
653
|
+
if (sequence === "uɔ") return "uo";
|
|
654
|
+
}
|
|
655
|
+
|
|
656
|
+
// Meixian / Sixian `[ə]` after z/c/s before m/n/b/d -> `ii`.
|
|
657
|
+
if (
|
|
658
|
+
phonProfile.ii_closed_as_schwa &&
|
|
659
|
+
sequence === "ə" &&
|
|
660
|
+
(onset === "z" || onset === "c" || onset === "s") &&
|
|
661
|
+
(coda === "m" || coda === "n" || coda === "b" || coda === "d")
|
|
662
|
+
) {
|
|
663
|
+
return "ii";
|
|
664
|
+
}
|
|
665
|
+
|
|
666
|
+
let output = "";
|
|
667
|
+
for (const char of sequence) {
|
|
668
|
+
if (char === "ɿ" || char === "ʅ" || char === "ɨ" || char === "ʮ" || char === "ʯ") {
|
|
669
|
+
output += "ii";
|
|
670
|
+
} else if (char === "æ") {
|
|
671
|
+
output += phonProfile.ee_ipa === "æ" ? "ee" : "ae";
|
|
672
|
+
} else if (char === "ɛ") {
|
|
673
|
+
output += phonProfile.ee_ipa === "ɛ" ? "ee" : "e";
|
|
674
|
+
} else if (char === "ɔ") {
|
|
675
|
+
output += phonProfile.o_oo_contrast ? "oo" : "o";
|
|
676
|
+
} else if (
|
|
677
|
+
char === "ə" ||
|
|
678
|
+
char === "ɤ" ||
|
|
679
|
+
char === "ʌ" ||
|
|
680
|
+
char === "ɜ" ||
|
|
681
|
+
char === "ɘ" ||
|
|
682
|
+
char === "ɚ"
|
|
683
|
+
) {
|
|
684
|
+
output += "er";
|
|
685
|
+
} else if (char === "ɑ" || char === "ɐ" || char === "ᴀ" || char === "a") {
|
|
686
|
+
output += "a";
|
|
687
|
+
} else if (char === "ɪ" || char === "i") {
|
|
688
|
+
output += "i";
|
|
689
|
+
} else if (char === "ᴇ" || char === "e") {
|
|
690
|
+
output += "e";
|
|
691
|
+
} else if (char === "ɒ" || char === "o") {
|
|
692
|
+
output += "o";
|
|
693
|
+
} else if (char === "ʊ" || char === "ɯ" || char === "u") {
|
|
694
|
+
output += "u";
|
|
695
|
+
} else if (char === "y" || char === "ʉ") {
|
|
696
|
+
output += "y";
|
|
697
|
+
} else if (char === "ø" || char === "œ" || char === "ɵ") {
|
|
698
|
+
output += "oe";
|
|
699
|
+
}
|
|
700
|
+
}
|
|
701
|
+
return output;
|
|
702
|
+
}
|
|
703
|
+
|
|
704
|
+
function parseIpa(token, dialectInfo, forceToneValue = false) {
|
|
705
|
+
const raw = String(token).trim();
|
|
706
|
+
if (!raw) {
|
|
707
|
+
throw new Error("Empty IPA token");
|
|
708
|
+
}
|
|
709
|
+
|
|
710
|
+
let work = stripCharacters(raw, "[]/ ");
|
|
711
|
+
work = work.split("`").join("");
|
|
712
|
+
|
|
713
|
+
let mark = "";
|
|
714
|
+
({ work, mark } = stripTrailingReadingMarks(work));
|
|
715
|
+
|
|
716
|
+
const hadSuperscript = containsSuperscriptDigit(work);
|
|
717
|
+
work = translateCharacters(work, FROM_SUPERSCRIPT_DIGITS);
|
|
718
|
+
|
|
719
|
+
let toneString = "";
|
|
720
|
+
let forceValue = forceToneValue || hadSuperscript;
|
|
721
|
+
const toneMatch = TRAILING_TONE_RE.exec(work);
|
|
722
|
+
if (toneMatch) {
|
|
723
|
+
toneString = toneMatch[1];
|
|
724
|
+
if (containsChaoLetter(toneString)) {
|
|
725
|
+
toneString = translateCharacters(toneString, CHAO_TO_DIGITS);
|
|
726
|
+
forceValue = true;
|
|
727
|
+
}
|
|
728
|
+
work = work.slice(0, toneMatch.index);
|
|
729
|
+
}
|
|
730
|
+
|
|
731
|
+
// Decompose precomposed nasalized vowels (ã -> a + combining tilde),
|
|
732
|
+
// restoring `ç` because NFD would otherwise split it.
|
|
733
|
+
work = work.normalize("NFD").split("c\u0327").join("ç");
|
|
734
|
+
|
|
735
|
+
// Vowel nasalization: combining tilde or superscript n.
|
|
736
|
+
const nasalized = work.includes("\u0303") || work.includes("ⁿ");
|
|
737
|
+
work = work.split("\u0303").join("").split("ⁿ").join("");
|
|
738
|
+
|
|
739
|
+
// Normalize aspiration apostrophes and remove non-phonemic modifiers.
|
|
740
|
+
work = work
|
|
741
|
+
.split("‘")
|
|
742
|
+
.join("'")
|
|
743
|
+
.split("ʻ")
|
|
744
|
+
.join("'")
|
|
745
|
+
.split("’")
|
|
746
|
+
.join("'");
|
|
747
|
+
work = work.split("\u0361").join(""); // tie bar
|
|
748
|
+
work = work.split("\u032F").join(""); // non-syllabic arch
|
|
749
|
+
work = work.split("\u031C").join(""); // less rounded
|
|
750
|
+
work = work.split("\u030A").join(""); // voiceless ring
|
|
751
|
+
work = work.split("\u0307").join(""); // overdot
|
|
752
|
+
work = work.split("\u0304").join(""); // macron
|
|
753
|
+
work = work.split("ː").join("").split("ˑ").join("").split("̚").join("");
|
|
754
|
+
// Superscript micro-glides.
|
|
755
|
+
work = work.split("ᵘ").join("u").split("ᶠ").join("").split("ᵗ").join("");
|
|
756
|
+
|
|
757
|
+
const hasSyllabicMark = work.includes("\u0329") || work.includes("\u030D");
|
|
758
|
+
work = work.split("\u0329").join("").split("\u030D").join("");
|
|
759
|
+
|
|
760
|
+
if (!work) {
|
|
761
|
+
throw new Error(`IPA token '${raw}' has no segmental content`);
|
|
762
|
+
}
|
|
763
|
+
|
|
764
|
+
// Initial glottal stop: non-phonemic before syllabic nasals, but an
|
|
765
|
+
// explicit H++ glottal onset before vowels.
|
|
766
|
+
let explicitGlottal = false;
|
|
767
|
+
if (work.startsWith("ʔ") && work.length > 1) {
|
|
768
|
+
const afterGlottal = work.slice(1);
|
|
769
|
+
if (["m", "n", "ŋ", "ȵ", "ɲ", "v", "ʋ"].includes(afterGlottal)) {
|
|
770
|
+
work = afterGlottal;
|
|
771
|
+
} else {
|
|
772
|
+
explicitGlottal = true;
|
|
773
|
+
work = afterGlottal;
|
|
774
|
+
}
|
|
775
|
+
}
|
|
776
|
+
|
|
777
|
+
// Standalone syllabic consonant, optionally with a glottal/stop coda.
|
|
778
|
+
if (hasOwn(IPA_SYLLABIC_CONSONANTS, work) && (work !== "l" || hasSyllabicMark)) {
|
|
779
|
+
const nucleus = IPA_SYLLABIC_CONSONANTS[work];
|
|
780
|
+
const [tone, toneValue] = resolveTonePair(toneString, false, dialectInfo, forceValue, true);
|
|
781
|
+
return createSyllable({
|
|
782
|
+
onset: "",
|
|
783
|
+
medial: "",
|
|
784
|
+
nucleus,
|
|
785
|
+
coda: "",
|
|
786
|
+
nasalized,
|
|
787
|
+
tone,
|
|
788
|
+
tone_value: toneValue,
|
|
789
|
+
reading_mark: mark,
|
|
790
|
+
raw,
|
|
791
|
+
});
|
|
792
|
+
}
|
|
793
|
+
|
|
794
|
+
const slug = dialectInfo ? dialectInfo.slug : "meixian";
|
|
795
|
+
|
|
796
|
+
// IPA palatal glide `j` before vowels / syllabic nasals -> zero-onset i-.
|
|
797
|
+
let core = work;
|
|
798
|
+
if (!explicitGlottal && core.startsWith("j") && core.length > 1) {
|
|
799
|
+
const afterJ = core.slice(1);
|
|
800
|
+
const hasVowel = [...afterJ].some((char) => IPA_VOWEL_ATOMS.has(char));
|
|
801
|
+
if (hasVowel || afterJ === "m" || afterJ === "n" || afterJ === "ŋ") {
|
|
802
|
+
core = afterJ.startsWith("i") ? afterJ : `i${afterJ}`;
|
|
803
|
+
}
|
|
804
|
+
}
|
|
805
|
+
|
|
806
|
+
// Strip IPA consonant coda before onset check.
|
|
807
|
+
let coda = "";
|
|
808
|
+
if (core.endsWith("ɡʔ") && core.length > 2) {
|
|
809
|
+
coda = "h";
|
|
810
|
+
core = core.slice(0, -2);
|
|
811
|
+
} else {
|
|
812
|
+
for (const [ipaCoda, hppCoda] of IPA_CODA_MAP) {
|
|
813
|
+
if (core.endsWith(ipaCoda) && core.length > ipaCoda.length) {
|
|
814
|
+
const remainder = core.slice(0, -ipaCoda.length);
|
|
815
|
+
const remainderHasVowel = [...remainder].some((char) => IPA_VOWEL_ATOMS.has(char));
|
|
816
|
+
const remainderIsSyllabic = hasOwn(IPA_SYLLABIC_CONSONANTS, remainder);
|
|
817
|
+
const remainderEndsNasal =
|
|
818
|
+
remainder.length >= 2 && IPA_SYLLABIC_CODA_CHARS.has(remainder.at(-1));
|
|
819
|
+
if (remainderHasVowel || remainderIsSyllabic || remainderEndsNasal) {
|
|
820
|
+
coda = hppCoda;
|
|
821
|
+
core = remainder;
|
|
822
|
+
break;
|
|
823
|
+
}
|
|
824
|
+
}
|
|
825
|
+
}
|
|
826
|
+
}
|
|
827
|
+
|
|
828
|
+
// Standalone syllabic consonant with coda (ŋʔ7, nʔ8, ʋʔ8, ...).
|
|
829
|
+
if (hasOwn(IPA_SYLLABIC_CONSONANTS, core) && (core !== "l" || hasSyllabicMark)) {
|
|
830
|
+
const nucleus = IPA_SYLLABIC_CONSONANTS[core];
|
|
831
|
+
const [tone, toneValue] = resolveTonePair(
|
|
832
|
+
toneString,
|
|
833
|
+
CHECKED_CODAS.has(coda),
|
|
834
|
+
dialectInfo,
|
|
835
|
+
forceValue,
|
|
836
|
+
true,
|
|
837
|
+
);
|
|
838
|
+
return createSyllable({
|
|
839
|
+
onset: "",
|
|
840
|
+
medial: "",
|
|
841
|
+
nucleus,
|
|
842
|
+
coda,
|
|
843
|
+
nasalized,
|
|
844
|
+
tone,
|
|
845
|
+
tone_value: toneValue,
|
|
846
|
+
reading_mark: mark,
|
|
847
|
+
raw,
|
|
848
|
+
});
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
// Parse IPA onset only when the remainder contains a vowel or syllabic C.
|
|
852
|
+
let onset = explicitGlottal ? "'" : "";
|
|
853
|
+
let rest = core;
|
|
854
|
+
if (!explicitGlottal) {
|
|
855
|
+
for (const [ipaPrefix, hppOnset] of IPA_ONSETS) {
|
|
856
|
+
if (core.startsWith(ipaPrefix) && core.length > ipaPrefix.length) {
|
|
857
|
+
const remainder = core.slice(ipaPrefix.length);
|
|
858
|
+
const remainderHasVowel = [...remainder].some((char) => IPA_VOWEL_ATOMS.has(char));
|
|
859
|
+
if (
|
|
860
|
+
remainderHasVowel ||
|
|
861
|
+
remainder === "m" ||
|
|
862
|
+
remainder === "n" ||
|
|
863
|
+
remainder === "ŋ" ||
|
|
864
|
+
remainder === "v" ||
|
|
865
|
+
remainder === "ʋ"
|
|
866
|
+
) {
|
|
867
|
+
onset = hppOnset;
|
|
868
|
+
rest = remainder;
|
|
869
|
+
break;
|
|
870
|
+
}
|
|
871
|
+
}
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
|
|
875
|
+
// All ten curated core dialects fold palatal nasal ȵ/ɲ into ng.
|
|
876
|
+
if (onset === "nj" && NJ_FOLDING_DIALECTS.has(slug)) {
|
|
877
|
+
onset = "ng";
|
|
878
|
+
}
|
|
879
|
+
|
|
880
|
+
// Syllabic consonant after an onset (lʋ̩2, hʋʔ8, hmʔ7, ...).
|
|
881
|
+
if (rest === "m" || rest === "n" || rest === "ŋ" || rest === "v" || rest === "ʋ") {
|
|
882
|
+
const nucleus = IPA_SYLLABIC_CONSONANTS[rest];
|
|
883
|
+
const [tone, toneValue] = resolveTonePair(
|
|
884
|
+
toneString,
|
|
885
|
+
CHECKED_CODAS.has(coda),
|
|
886
|
+
dialectInfo,
|
|
887
|
+
forceValue,
|
|
888
|
+
true,
|
|
889
|
+
);
|
|
890
|
+
return createSyllable({
|
|
891
|
+
onset,
|
|
892
|
+
medial: "",
|
|
893
|
+
nucleus,
|
|
894
|
+
coda,
|
|
895
|
+
nasalized,
|
|
896
|
+
tone,
|
|
897
|
+
tone_value: toneValue,
|
|
898
|
+
reading_mark: mark,
|
|
899
|
+
raw,
|
|
900
|
+
});
|
|
901
|
+
}
|
|
902
|
+
|
|
903
|
+
const vowelCluster = mapIpaVowels(rest, onset, coda, slug);
|
|
904
|
+
if (!vowelCluster) {
|
|
905
|
+
throw new Error(`Could not parse vowel nucleus from IPA '${raw}'`);
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
const [medial, nucleus, vowelCoda] = splitVowelCluster(vowelCluster, Boolean(coda));
|
|
909
|
+
const finalCoda = coda || vowelCoda;
|
|
910
|
+
const [tone, toneValue] = resolveTonePair(
|
|
911
|
+
toneString,
|
|
912
|
+
CHECKED_CODAS.has(finalCoda),
|
|
913
|
+
dialectInfo,
|
|
914
|
+
forceValue,
|
|
915
|
+
true,
|
|
916
|
+
);
|
|
917
|
+
|
|
918
|
+
return createSyllable({
|
|
919
|
+
onset,
|
|
920
|
+
medial,
|
|
921
|
+
nucleus,
|
|
922
|
+
coda: finalCoda,
|
|
923
|
+
nasalized,
|
|
924
|
+
tone,
|
|
925
|
+
tone_value: toneValue,
|
|
926
|
+
reading_mark: mark,
|
|
927
|
+
raw,
|
|
928
|
+
});
|
|
929
|
+
}
|
|
930
|
+
|
|
931
|
+
function resolveParseArguments(schemeOrOptions, dialect) {
|
|
932
|
+
if (schemeOrOptions !== null && typeof schemeOrOptions === "object") {
|
|
933
|
+
const options = schemeOrOptions;
|
|
934
|
+
return {
|
|
935
|
+
scheme: options.scheme ?? "hpp",
|
|
936
|
+
dialect: Object.hasOwn(options, "dialect") ? options.dialect : DEFAULT_DIALECT,
|
|
937
|
+
};
|
|
938
|
+
}
|
|
939
|
+
return {
|
|
940
|
+
scheme: schemeOrOptions ?? "hpp",
|
|
941
|
+
dialect: dialect === undefined ? DEFAULT_DIALECT : dialect,
|
|
942
|
+
};
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
/**
|
|
946
|
+
* Parse a single Hakka syllable from `scheme` into canonical IR.
|
|
947
|
+
*
|
|
948
|
+
* @param {string} text
|
|
949
|
+
* @param {string} [scheme="hpp"]
|
|
950
|
+
* @param {string | null} [dialect="huiyang"]
|
|
951
|
+
* @returns {object}
|
|
952
|
+
*/
|
|
953
|
+
export function parseSyllable(text, scheme = "hpp", dialect = DEFAULT_DIALECT) {
|
|
954
|
+
const resolved = resolveParseArguments(scheme, dialect);
|
|
955
|
+
const canonicalScheme = normalizeScheme(resolved.scheme);
|
|
956
|
+
const dialectInfo = resolved.dialect ? getDialect(resolved.dialect) : null;
|
|
957
|
+
|
|
958
|
+
if (canonicalScheme === "hpp") {
|
|
959
|
+
return parseHpsExt(text, dialectInfo);
|
|
960
|
+
}
|
|
961
|
+
if (canonicalScheme === "hpp_diacritic") {
|
|
962
|
+
return parseHpsDiacritic(text, dialectInfo);
|
|
963
|
+
}
|
|
964
|
+
if (canonicalScheme === "ipa" || canonicalScheme === "ipa_value" || canonicalScheme === "ipa_chao") {
|
|
965
|
+
return parseIpa(text, dialectInfo, canonicalScheme !== "ipa");
|
|
966
|
+
}
|
|
967
|
+
if (canonicalScheme === "gd") {
|
|
968
|
+
return parseGd(text, dialectInfo);
|
|
969
|
+
}
|
|
970
|
+
if (canonicalScheme === "hkilang") {
|
|
971
|
+
return parseHkilang(text, dialectInfo);
|
|
972
|
+
}
|
|
973
|
+
throw new Error(`Unsupported scheme for parsing: '${resolved.scheme}'`);
|
|
974
|
+
}
|
|
975
|
+
|
|
976
|
+
/**
|
|
977
|
+
* Parse a whitespace- or hyphen-separated sequence of syllables.
|
|
978
|
+
*
|
|
979
|
+
* @param {string} text
|
|
980
|
+
* @param {string} [scheme="hpp"]
|
|
981
|
+
* @param {string | null} [dialect="huiyang"]
|
|
982
|
+
* @returns {object[]}
|
|
983
|
+
*/
|
|
984
|
+
export function parseSyllables(text, scheme = "hpp", dialect = DEFAULT_DIALECT) {
|
|
985
|
+
const resolved = resolveParseArguments(scheme, dialect);
|
|
986
|
+
const tokens = String(text)
|
|
987
|
+
.trim()
|
|
988
|
+
.split(/[\s\-]+/u)
|
|
989
|
+
.filter(Boolean);
|
|
990
|
+
return tokens.map((token) => parseSyllable(token, resolved.scheme, resolved.dialect));
|
|
991
|
+
}
|