@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.
- 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 +360 -0
- package/src/phon/syllable.js +258 -0
- package/src/phon/validate.js +118 -0
- package/src/sandhi.js +402 -0
- package/types/index.d.ts +378 -0
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Syllable validation and phonological constraint checking.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { DEFAULT_DIALECT, UnknownDialectError, getDialect } from "../dialects.js";
|
|
6
|
+
import { parseSyllable } from "./parser.js";
|
|
7
|
+
import { HPS_EXT_ONSET_SET, SYLLABIC_CONSONANTS } from "./syllable.js";
|
|
8
|
+
|
|
9
|
+
const CANONICAL_NUCLEI = new Set([
|
|
10
|
+
"a",
|
|
11
|
+
"e",
|
|
12
|
+
"ee",
|
|
13
|
+
"ae",
|
|
14
|
+
"i",
|
|
15
|
+
"ii",
|
|
16
|
+
"o",
|
|
17
|
+
"oo",
|
|
18
|
+
"u",
|
|
19
|
+
"y",
|
|
20
|
+
"oe",
|
|
21
|
+
"er",
|
|
22
|
+
]);
|
|
23
|
+
|
|
24
|
+
function resolveArguments(schemeOrOptions, dialect) {
|
|
25
|
+
if (schemeOrOptions !== null && typeof schemeOrOptions === "object") {
|
|
26
|
+
const options = schemeOrOptions;
|
|
27
|
+
return {
|
|
28
|
+
scheme: options.scheme ?? "hps_ext",
|
|
29
|
+
dialect: Object.hasOwn(options, "dialect") ? options.dialect : DEFAULT_DIALECT,
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
return {
|
|
33
|
+
scheme: schemeOrOptions ?? "hps_ext",
|
|
34
|
+
dialect: dialect === undefined ? DEFAULT_DIALECT : dialect,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Validate whether `text` is a well-formed Hakka syllable.
|
|
40
|
+
*
|
|
41
|
+
* @param {string} text
|
|
42
|
+
* @param {string} [scheme="hps_ext"]
|
|
43
|
+
* @param {string | null} [dialect="huiyang"]
|
|
44
|
+
* @returns {{valid: boolean, syllable: object | null, errors: string[], warnings: string[]}}
|
|
45
|
+
*/
|
|
46
|
+
export function validateSyllable(text, scheme = "hps_ext", dialect = DEFAULT_DIALECT) {
|
|
47
|
+
const resolved = resolveArguments(scheme, dialect);
|
|
48
|
+
const effectiveDialect = resolved.dialect || DEFAULT_DIALECT;
|
|
49
|
+
|
|
50
|
+
let syllable;
|
|
51
|
+
try {
|
|
52
|
+
syllable = parseSyllable(text, resolved.scheme, effectiveDialect);
|
|
53
|
+
} catch (error) {
|
|
54
|
+
// Python parity: dialect resolution raises, while scheme/token parse
|
|
55
|
+
// errors are reported as a validation failure.
|
|
56
|
+
if (error instanceof UnknownDialectError) throw error;
|
|
57
|
+
return Object.freeze({
|
|
58
|
+
valid: false,
|
|
59
|
+
syllable: null,
|
|
60
|
+
errors: [error instanceof Error ? error.message : String(error)],
|
|
61
|
+
warnings: [],
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
const errors = [];
|
|
66
|
+
const warnings = [];
|
|
67
|
+
|
|
68
|
+
if (syllable.onset && !HPS_EXT_ONSET_SET.has(syllable.onset)) {
|
|
69
|
+
errors.push(`Unknown onset '${syllable.onset}'`);
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
if (!syllable.nucleus) {
|
|
73
|
+
errors.push("Missing vowel nucleus");
|
|
74
|
+
} else if (
|
|
75
|
+
!SYLLABIC_CONSONANTS.has(syllable.nucleus) &&
|
|
76
|
+
!CANONICAL_NUCLEI.has(syllable.nucleus)
|
|
77
|
+
) {
|
|
78
|
+
errors.push(`Non-canonical nucleus '${syllable.nucleus}'`);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Checked tone / coda consistency (入聲韻尾與入聲調一致性).
|
|
82
|
+
if (syllable.tone) {
|
|
83
|
+
if (syllable.is_checked && !["7", "8", "0"].includes(syllable.tone)) {
|
|
84
|
+
warnings.push(
|
|
85
|
+
`Checked coda -${syllable.coda} paired with non-checked tone class ${syllable.tone}`,
|
|
86
|
+
);
|
|
87
|
+
} else if (!syllable.is_checked && (syllable.tone === "7" || syllable.tone === "8")) {
|
|
88
|
+
warnings.push(
|
|
89
|
+
`Open/nasal rime '${syllable.final}' paired with checked tone class ${syllable.tone}`,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
if (effectiveDialect) {
|
|
95
|
+
const dialectInfo = getDialect(effectiveDialect);
|
|
96
|
+
if (
|
|
97
|
+
syllable.tone &&
|
|
98
|
+
syllable.tone !== "0" &&
|
|
99
|
+
Object.keys(dialectInfo.tones).length > 0 &&
|
|
100
|
+
!Object.hasOwn(dialectInfo.tones, syllable.tone)
|
|
101
|
+
) {
|
|
102
|
+
const inventory = Object.keys(dialectInfo.tones)
|
|
103
|
+
.sort()
|
|
104
|
+
.map((toneClass) => `'${toneClass}'`)
|
|
105
|
+
.join(", ");
|
|
106
|
+
warnings.push(
|
|
107
|
+
`Tone class '${syllable.tone}' is not in ${dialectInfo.slug}'s tone inventory ([${inventory}])`,
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return Object.freeze({
|
|
113
|
+
valid: errors.length === 0,
|
|
114
|
+
syllable,
|
|
115
|
+
errors,
|
|
116
|
+
warnings,
|
|
117
|
+
});
|
|
118
|
+
}
|
package/src/sandhi.js
ADDED
|
@@ -0,0 +1,402 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Tone sandhi and progressive segmental assimilation rules for Hakka varieties.
|
|
3
|
+
*
|
|
4
|
+
* Rule sets follow published dialectological descriptions:
|
|
5
|
+
* - Meixian: 張維耿 (2020) and 黃雪貞 (1995), standard and traditional profiles.
|
|
6
|
+
* - Wuhua: 朱炳玉 (2010), suffix 哩 progressive assimilation and tone sandhi.
|
|
7
|
+
* - Changting: 上聲連讀 and the 饒長溶 two-syllable system.
|
|
8
|
+
* - Taiwan Sixian / South Sixian: 陰平 sandhi.
|
|
9
|
+
* - Taiwan Hailu: 上聲 and 陽入 non-final sandhi.
|
|
10
|
+
* - Dabu Baihou: explicitly no sandhi.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import { DEFAULT_DIALECT, getDialect } from "./dialects.js";
|
|
14
|
+
import { parseSyllables } from "./phon/parser.js";
|
|
15
|
+
import {
|
|
16
|
+
baseOf,
|
|
17
|
+
createSyllable,
|
|
18
|
+
finalOf,
|
|
19
|
+
hpsExtOf,
|
|
20
|
+
isSyllabicConsonant,
|
|
21
|
+
syllableFields,
|
|
22
|
+
} from "./phon/syllable.js";
|
|
23
|
+
|
|
24
|
+
/** Supported sandhi profiles by canonical dialect slug. */
|
|
25
|
+
export const SUPPORTED_SANDHI = Object.freeze({
|
|
26
|
+
meixian: Object.freeze(["standard", "traditional"]),
|
|
27
|
+
wuhua: Object.freeze(["standard"]),
|
|
28
|
+
changting: Object.freeze(["standard", "rao"]),
|
|
29
|
+
"mcp:臺灣四縣腔": Object.freeze(["standard"]),
|
|
30
|
+
"mcp:臺灣南四縣腔": Object.freeze(["standard"]),
|
|
31
|
+
"mcp:臺灣海陸腔": Object.freeze(["standard"]),
|
|
32
|
+
"dabu-baihou": Object.freeze(["standard"]),
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
/** Progressive coda-to-onset assimilation map for Wuhua suffix 哩. */
|
|
36
|
+
const WUHUA_LI_ASSIM = Object.freeze({
|
|
37
|
+
m: "m",
|
|
38
|
+
n: "n",
|
|
39
|
+
ng: "ng",
|
|
40
|
+
b: "b",
|
|
41
|
+
d: "d",
|
|
42
|
+
g: "g",
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
const WUHUA_PRONOUN_PREFIXES = new Set(["樣", "样", "咹", "咁", "般"]);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Return the mapping of dialect slug -> supported sandhi profile names.
|
|
49
|
+
* @returns {Record<string, string[]>}
|
|
50
|
+
*/
|
|
51
|
+
export function listSandhiDialects() {
|
|
52
|
+
const result = {};
|
|
53
|
+
for (const [slug, profiles] of Object.entries(SUPPORTED_SANDHI)) {
|
|
54
|
+
result[slug] = [...profiles];
|
|
55
|
+
}
|
|
56
|
+
return result;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function cloneWith(syllable, fields) {
|
|
60
|
+
return createSyllable({ ...syllableFields(syllable), ...fields });
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function withTone(syllable, tone, toneValue) {
|
|
64
|
+
return cloneWith(syllable, { tone, tone_value: toneValue });
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function withToneValue(syllable, toneValue, tone) {
|
|
68
|
+
return cloneWith(syllable, { tone, tone_value: toneValue });
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function withOnset(syllable, onset) {
|
|
72
|
+
return cloneWith(syllable, { onset });
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
function createChange(index, char, before, after, rule) {
|
|
76
|
+
return Object.freeze({ index, char, before, after, rule });
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function createResult(syllables, changes, dialect, profileName) {
|
|
80
|
+
const result = {
|
|
81
|
+
syllables,
|
|
82
|
+
changes,
|
|
83
|
+
dialect,
|
|
84
|
+
profile: profileName,
|
|
85
|
+
};
|
|
86
|
+
Object.defineProperties(result, {
|
|
87
|
+
hps_ext: {
|
|
88
|
+
enumerable: false,
|
|
89
|
+
get: () => result.syllables.map((syllable) => hpsExtOf(syllable)).join(" "),
|
|
90
|
+
},
|
|
91
|
+
hpsExt: {
|
|
92
|
+
enumerable: false,
|
|
93
|
+
get: () => result.syllables.map((syllable) => hpsExtOf(syllable)).join(" "),
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
return Object.freeze(result);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function applyMeixian(syllables, chars, profileName) {
|
|
100
|
+
const out = [...syllables];
|
|
101
|
+
const changes = [];
|
|
102
|
+
const n = out.length;
|
|
103
|
+
|
|
104
|
+
for (let i = 0; i < n - 1; i += 1) {
|
|
105
|
+
const cur = out[i];
|
|
106
|
+
const next = out[i + 1];
|
|
107
|
+
|
|
108
|
+
// 張維耿 (2020): 陰平(44) + 陽平(11)/上聲(31)/陰入(1) -> 陰平尾音微升 45.
|
|
109
|
+
if (cur.tone === "1" && ["2", "3", "7"].includes(next.tone)) {
|
|
110
|
+
const updated = withTone(cur, "1", "45");
|
|
111
|
+
out[i] = updated;
|
|
112
|
+
changes.push(
|
|
113
|
+
createChange(
|
|
114
|
+
i,
|
|
115
|
+
i < chars.length ? chars[i] : "",
|
|
116
|
+
`${hpsExtOf(cur)}[${cur.tone_value || "44"}]`,
|
|
117
|
+
`${hpsExtOf(updated)}[45]`,
|
|
118
|
+
"meixian:yinping_44_to_45_before_low_onset",
|
|
119
|
+
),
|
|
120
|
+
);
|
|
121
|
+
} else if (
|
|
122
|
+
profileName === "traditional" &&
|
|
123
|
+
cur.tone === "5" &&
|
|
124
|
+
["1", "5", "8"].includes(next.tone)
|
|
125
|
+
) {
|
|
126
|
+
// 傳統梅縣變調 (黃雪貞 1995): 去聲(52) + 陰平/去聲/陽入 -> 55.
|
|
127
|
+
const updated = withTone(cur, "5", "55");
|
|
128
|
+
out[i] = updated;
|
|
129
|
+
changes.push(
|
|
130
|
+
createChange(
|
|
131
|
+
i,
|
|
132
|
+
i < chars.length ? chars[i] : "",
|
|
133
|
+
`${hpsExtOf(cur)}[${cur.tone_value || "52"}]`,
|
|
134
|
+
`${hpsExtOf(updated)}[55]`,
|
|
135
|
+
"meixian:qusheng_52_to_55_before_high_onset",
|
|
136
|
+
),
|
|
137
|
+
);
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return [out, changes];
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function applyWuhua(syllables, chars, _profileName) {
|
|
144
|
+
const out = [...syllables];
|
|
145
|
+
const changes = [];
|
|
146
|
+
const n = out.length;
|
|
147
|
+
const hasChars = chars.length > 0;
|
|
148
|
+
|
|
149
|
+
for (let i = 1; i < n; i += 1) {
|
|
150
|
+
const cur = out[i];
|
|
151
|
+
const char = i < chars.length ? chars[i] : "";
|
|
152
|
+
const prev = out[i - 1];
|
|
153
|
+
const prevChar = i - 1 < chars.length ? chars[i - 1] : "";
|
|
154
|
+
|
|
155
|
+
// Match suffix 哩 by character, or by li3 phonology when chars are omitted.
|
|
156
|
+
const isLiSuffix =
|
|
157
|
+
char === "哩" || (!hasChars && cur.onset === "l" && finalOf(cur) === "i" && cur.tone === "3");
|
|
158
|
+
if (!isLiSuffix) continue;
|
|
159
|
+
|
|
160
|
+
let updated = cur;
|
|
161
|
+
const applied = [];
|
|
162
|
+
|
|
163
|
+
// 1. Progressive coda-to-onset assimilation: -m/-n/-ng/-b/-d/-g.
|
|
164
|
+
const newOnset = WUHUA_LI_ASSIM[prev.coda];
|
|
165
|
+
if (newOnset && updated.onset === "l") {
|
|
166
|
+
updated = withOnset(updated, newOnset);
|
|
167
|
+
applied.push(`wuhua:li_assim_${prev.coda}_to_${newOnset}`);
|
|
168
|
+
} else if (isSyllabicConsonant(prev) && WUHUA_LI_ASSIM[prev.nucleus] && updated.onset === "l") {
|
|
169
|
+
// e.g. 魚哩 [ŋ³⁵ ŋi⁵⁵].
|
|
170
|
+
const assimilated = WUHUA_LI_ASSIM[prev.nucleus];
|
|
171
|
+
updated = withOnset(updated, assimilated);
|
|
172
|
+
applied.push(`wuhua:li_assim_syllabic_${prev.nucleus}_to_${assimilated}`);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
// 2. Tone sandhi 3 -> 1 (55):
|
|
176
|
+
// (a) after Yangping (tone 2);
|
|
177
|
+
// (b) after reduplication XX哩;
|
|
178
|
+
// (c) after V啊V哩;
|
|
179
|
+
// (d) after a demonstrative/interrogative pronoun prefix.
|
|
180
|
+
const isRedup =
|
|
181
|
+
i >= 2 &&
|
|
182
|
+
((hasChars && prevChar && prevChar === chars[i - 2]) ||
|
|
183
|
+
(!hasChars &&
|
|
184
|
+
baseOf(prev) === baseOf(out[i - 2]) &&
|
|
185
|
+
prev.tone === out[i - 2].tone));
|
|
186
|
+
const isVaav =
|
|
187
|
+
i >= 3 &&
|
|
188
|
+
hasChars &&
|
|
189
|
+
(chars[i - 2] === "啊" || chars[i - 2] === "阿") &&
|
|
190
|
+
prevChar === chars[i - 3];
|
|
191
|
+
const isPronoun = Boolean(prevChar) && WUHUA_PRONOUN_PREFIXES.has(prevChar);
|
|
192
|
+
|
|
193
|
+
if (
|
|
194
|
+
updated.tone === "3" &&
|
|
195
|
+
(prev.tone === "2" || isRedup || isVaav || isPronoun)
|
|
196
|
+
) {
|
|
197
|
+
updated = withTone(updated, "1", "55");
|
|
198
|
+
const reason = isRedup || isVaav ? "redup" : isPronoun ? "pronoun" : "after_yangping";
|
|
199
|
+
applied.push(`wuhua:li_tone_3_to_1_${reason}`);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
if (applied.length > 0) {
|
|
203
|
+
out[i] = updated;
|
|
204
|
+
changes.push(createChange(i, char, hpsExtOf(cur), hpsExtOf(updated), applied.join("+")));
|
|
205
|
+
}
|
|
206
|
+
}
|
|
207
|
+
return [out, changes];
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
function applyChangting(syllables, chars, profileName) {
|
|
211
|
+
const out = [...syllables];
|
|
212
|
+
const changes = [];
|
|
213
|
+
const n = out.length;
|
|
214
|
+
|
|
215
|
+
for (let i = 0; i < n - 1; i += 1) {
|
|
216
|
+
const cur = out[i];
|
|
217
|
+
const next = out[i + 1];
|
|
218
|
+
|
|
219
|
+
if (cur.tone === "3" && next.tone === "3") {
|
|
220
|
+
const updated = withTone(cur, "2", "24");
|
|
221
|
+
out[i] = updated;
|
|
222
|
+
changes.push(
|
|
223
|
+
createChange(
|
|
224
|
+
i,
|
|
225
|
+
i < chars.length ? chars[i] : "",
|
|
226
|
+
hpsExtOf(cur),
|
|
227
|
+
hpsExtOf(updated),
|
|
228
|
+
"changting:shang_3_to_2_before_shang",
|
|
229
|
+
),
|
|
230
|
+
);
|
|
231
|
+
} else if (profileName === "rao") {
|
|
232
|
+
if ((cur.tone === "1" || cur.tone === "2") && next.tone === "3") {
|
|
233
|
+
const updated = withToneValue(cur, "11", cur.tone);
|
|
234
|
+
out[i] = updated;
|
|
235
|
+
changes.push(
|
|
236
|
+
createChange(
|
|
237
|
+
i,
|
|
238
|
+
i < chars.length ? chars[i] : "",
|
|
239
|
+
`${hpsExtOf(cur)}[${cur.tone_value}]`,
|
|
240
|
+
`${hpsExtOf(updated)}[11]`,
|
|
241
|
+
"changting:ping_to_11_before_shang",
|
|
242
|
+
),
|
|
243
|
+
);
|
|
244
|
+
} else if (cur.tone === "6" && next.tone === "5") {
|
|
245
|
+
const updated = withTone(cur, "2", "24");
|
|
246
|
+
out[i] = updated;
|
|
247
|
+
changes.push(
|
|
248
|
+
createChange(
|
|
249
|
+
i,
|
|
250
|
+
i < chars.length ? chars[i] : "",
|
|
251
|
+
hpsExtOf(cur),
|
|
252
|
+
hpsExtOf(updated),
|
|
253
|
+
"changting:yangqu_6_to_2_before_yinqu",
|
|
254
|
+
),
|
|
255
|
+
);
|
|
256
|
+
} else if (cur.tone === "5" && ["1", "2", "5"].includes(next.tone)) {
|
|
257
|
+
const updated = withToneValue(cur, "55", "5");
|
|
258
|
+
out[i] = updated;
|
|
259
|
+
changes.push(
|
|
260
|
+
createChange(
|
|
261
|
+
i,
|
|
262
|
+
i < chars.length ? chars[i] : "",
|
|
263
|
+
`${hpsExtOf(cur)}[${cur.tone_value || "54"}]`,
|
|
264
|
+
`${hpsExtOf(updated)}[55]`,
|
|
265
|
+
"changting:yinqu_54_to_55",
|
|
266
|
+
),
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
return [out, changes];
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
function applySixian(syllables, chars, _profileName) {
|
|
275
|
+
const out = [...syllables];
|
|
276
|
+
const changes = [];
|
|
277
|
+
|
|
278
|
+
for (let i = 0; i < out.length - 1; i += 1) {
|
|
279
|
+
const cur = out[i];
|
|
280
|
+
const next = out[i + 1];
|
|
281
|
+
if (cur.tone === "1" && ["1", "2", "7"].includes(next.tone)) {
|
|
282
|
+
const updated = withTone(cur, "2", "11");
|
|
283
|
+
out[i] = updated;
|
|
284
|
+
changes.push(
|
|
285
|
+
createChange(
|
|
286
|
+
i,
|
|
287
|
+
i < chars.length ? chars[i] : "",
|
|
288
|
+
hpsExtOf(cur),
|
|
289
|
+
hpsExtOf(updated),
|
|
290
|
+
"sixian:yinping_1_to_2_before_1_2_7",
|
|
291
|
+
),
|
|
292
|
+
);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return [out, changes];
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
function applyHailu(syllables, chars, _profileName) {
|
|
299
|
+
const out = [...syllables];
|
|
300
|
+
const changes = [];
|
|
301
|
+
|
|
302
|
+
for (let i = 0; i < out.length - 1; i += 1) {
|
|
303
|
+
const cur = out[i];
|
|
304
|
+
if (cur.tone === "3") {
|
|
305
|
+
const updated = withTone(cur, "6", "33");
|
|
306
|
+
out[i] = updated;
|
|
307
|
+
changes.push(
|
|
308
|
+
createChange(
|
|
309
|
+
i,
|
|
310
|
+
i < chars.length ? chars[i] : "",
|
|
311
|
+
hpsExtOf(cur),
|
|
312
|
+
hpsExtOf(updated),
|
|
313
|
+
"hailu:shang_3_to_6_nonfinal",
|
|
314
|
+
),
|
|
315
|
+
);
|
|
316
|
+
} else if (cur.tone === "8") {
|
|
317
|
+
const updated = withTone(cur, "7", "5");
|
|
318
|
+
out[i] = updated;
|
|
319
|
+
changes.push(
|
|
320
|
+
createChange(
|
|
321
|
+
i,
|
|
322
|
+
i < chars.length ? chars[i] : "",
|
|
323
|
+
hpsExtOf(cur),
|
|
324
|
+
hpsExtOf(updated),
|
|
325
|
+
"hailu:yangru_8_to_7_nonfinal",
|
|
326
|
+
),
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
}
|
|
330
|
+
return [out, changes];
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
function resolveSandhiArguments(dialectOrOptions, extraOptions) {
|
|
334
|
+
if (dialectOrOptions !== null && typeof dialectOrOptions === "object") {
|
|
335
|
+
return { ...dialectOrOptions };
|
|
336
|
+
}
|
|
337
|
+
const options = extraOptions !== null && typeof extraOptions === "object" ? extraOptions : {};
|
|
338
|
+
return {
|
|
339
|
+
...options,
|
|
340
|
+
dialect:
|
|
341
|
+
options.dialect ?? (dialectOrOptions === undefined ? DEFAULT_DIALECT : dialectOrOptions),
|
|
342
|
+
};
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
/**
|
|
346
|
+
* Apply tone sandhi and segmental assimilation rules.
|
|
347
|
+
*
|
|
348
|
+
* @param {string | object[]} syllables Space-separated transcription or IR array.
|
|
349
|
+
* @param {string | object} [dialectOrOptions="huiyang"] Dialect name or options.
|
|
350
|
+
* @param {object} [options]
|
|
351
|
+
* @param {string | string[]} [options.chars] Aligned Chinese characters.
|
|
352
|
+
* @param {string} [options.profile="standard"] Rule profile.
|
|
353
|
+
* @param {string} [options.scheme="hps_ext"] Input scheme for a string.
|
|
354
|
+
* @returns {object}
|
|
355
|
+
*/
|
|
356
|
+
export function applySandhi(syllables, dialectOrOptions = DEFAULT_DIALECT, options = {}) {
|
|
357
|
+
const resolved = resolveSandhiArguments(dialectOrOptions, options);
|
|
358
|
+
const dialectName = resolved.dialect ?? DEFAULT_DIALECT;
|
|
359
|
+
const profileName = resolved.profile ?? "standard";
|
|
360
|
+
const scheme = resolved.scheme ?? "hps_ext";
|
|
361
|
+
|
|
362
|
+
const dialectInfo = getDialect(dialectName);
|
|
363
|
+
const slug = dialectInfo.slug;
|
|
364
|
+
|
|
365
|
+
const syllableList =
|
|
366
|
+
typeof syllables === "string"
|
|
367
|
+
? parseSyllables(syllables, scheme, slug)
|
|
368
|
+
: Array.from(syllables ?? []);
|
|
369
|
+
|
|
370
|
+
let chars = [];
|
|
371
|
+
const rawChars = resolved.chars;
|
|
372
|
+
if (typeof rawChars === "string") {
|
|
373
|
+
chars = [...rawChars];
|
|
374
|
+
} else if (rawChars !== null && rawChars !== undefined) {
|
|
375
|
+
chars = Array.from(rawChars);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
if (Object.hasOwn(SUPPORTED_SANDHI, slug) && !SUPPORTED_SANDHI[slug].includes(profileName)) {
|
|
379
|
+
const valid = SUPPORTED_SANDHI[slug].join(", ");
|
|
380
|
+
throw new Error(`Unknown sandhi profile '${profileName}' for '${slug}'. Supported: ${valid}`);
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
let out;
|
|
384
|
+
let changes;
|
|
385
|
+
if (slug === "meixian") {
|
|
386
|
+
[out, changes] = applyMeixian(syllableList, chars, profileName);
|
|
387
|
+
} else if (slug === "wuhua") {
|
|
388
|
+
[out, changes] = applyWuhua(syllableList, chars, profileName);
|
|
389
|
+
} else if (slug === "changting") {
|
|
390
|
+
[out, changes] = applyChangting(syllableList, chars, profileName);
|
|
391
|
+
} else if (slug === "mcp:臺灣四縣腔" || slug === "mcp:臺灣南四縣腔") {
|
|
392
|
+
[out, changes] = applySixian(syllableList, chars, profileName);
|
|
393
|
+
} else if (slug === "mcp:臺灣海陸腔") {
|
|
394
|
+
[out, changes] = applyHailu(syllableList, chars, profileName);
|
|
395
|
+
} else {
|
|
396
|
+
// Dabu Baihou and dialects without encoded rules pass through unchanged.
|
|
397
|
+
out = syllableList;
|
|
398
|
+
changes = [];
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
return createResult(out, changes, slug, profileName);
|
|
402
|
+
}
|