mandarin-ipa 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 +21 -0
- package/README.md +189 -2
- package/dist/converter.d.ts +50 -0
- package/dist/converter.js +896 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.js +2 -0
- package/dist/ipaRules.d.ts +10 -0
- package/dist/ipaRules.js +412 -0
- package/dist/pinyin.d.ts +23 -0
- package/dist/pinyin.js +517 -0
- package/dist/sandhi.d.ts +19 -0
- package/dist/sandhi.js +396 -0
- package/dist/types.d.ts +125 -0
- package/dist/types.js +89 -0
- package/package.json +75 -4
package/dist/pinyin.js
ADDED
|
@@ -0,0 +1,517 @@
|
|
|
1
|
+
import { buildIpaOptionsFromConfig, countPinyinToneDiacritics, formatIpaTone, formatPinyinSyllable, parsePinyinSyllable, pinyinSyllableToIpa } from './ipaRules.js';
|
|
2
|
+
import { applyMandarinSandhi } from './sandhi.js';
|
|
3
|
+
import { createDefaultMandarinConfig, DEFAULT_MANDARIN_CONFIG, getPresetIpaGranular } from './types.js';
|
|
4
|
+
export { applyMandarinSandhi, buildIpaOptionsFromConfig, createDefaultMandarinConfig, DEFAULT_MANDARIN_CONFIG, formatIpaTone, formatPinyinSyllable, getPresetIpaGranular, parsePinyinSyllable, pinyinSyllableToIpa };
|
|
5
|
+
// ---------------------------------------------------------------------------
|
|
6
|
+
// Shared Silence & Punctuation Utilities (Zero External Dependencies)
|
|
7
|
+
// ---------------------------------------------------------------------------
|
|
8
|
+
const WHOLE_SILENCE_TAG_RE = /^(?:sil|sp|pau|nsn|unk|<sil>|<sp>|<pau>|<unk>|<nsn>|\[sil\]|\[sp\]|\[pau\]|\[sigh\]|\[breath\]|\[noise\]|\[cough\]|\[laugh\]|\[pause\]|#|_|\.{2,})$/i;
|
|
9
|
+
const INLINE_SILENCE_TAG_RE = /^(?:<(?:sil|sp|pau|unk|nsn)>|\[(?:sil|sp|pau|sigh|breath|noise|cough|laugh|pause)\])/i;
|
|
10
|
+
const PUNCT_RE = /^\p{P}$/u;
|
|
11
|
+
const PINYIN_TOKEN_CHAR_RE = /[A-Za-züÜāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜêếềńňǹḿ0-9:'\u0304\u030c˥˦˧˨˩-]/u;
|
|
12
|
+
export function isWholeSilenceTag(text) {
|
|
13
|
+
const trimmed = text.trim();
|
|
14
|
+
return trimmed.length === 0 || WHOLE_SILENCE_TAG_RE.test(trimmed);
|
|
15
|
+
}
|
|
16
|
+
export function applyPunctuationModeToUnits(units, mode) {
|
|
17
|
+
if (mode === 'keep') {
|
|
18
|
+
return units.map((u) => ({ ...u }));
|
|
19
|
+
}
|
|
20
|
+
if (mode === 'ignore') {
|
|
21
|
+
return units.filter((u) => u.kind !== 'punctuation').map((u) => ({ ...u }));
|
|
22
|
+
}
|
|
23
|
+
// 'attach-prev'
|
|
24
|
+
const out = [];
|
|
25
|
+
let pendingLeading = '';
|
|
26
|
+
for (const unit of units) {
|
|
27
|
+
if (unit.kind === 'punctuation') {
|
|
28
|
+
if (out.length > 0) {
|
|
29
|
+
const last = out[out.length - 1];
|
|
30
|
+
last.sourceText += unit.sourceText;
|
|
31
|
+
if (last.pinyin)
|
|
32
|
+
last.pinyin += unit.pinyin;
|
|
33
|
+
if (last.ipa)
|
|
34
|
+
last.ipa += unit.ipa;
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
pendingLeading += unit.sourceText;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
const cloned = { ...unit };
|
|
42
|
+
if (pendingLeading) {
|
|
43
|
+
cloned.sourceText = pendingLeading + cloned.sourceText;
|
|
44
|
+
if (cloned.pinyin)
|
|
45
|
+
cloned.pinyin = pendingLeading + cloned.pinyin;
|
|
46
|
+
if (cloned.ipa)
|
|
47
|
+
cloned.ipa = pendingLeading + cloned.ipa;
|
|
48
|
+
pendingLeading = '';
|
|
49
|
+
}
|
|
50
|
+
out.push(cloned);
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
return out;
|
|
54
|
+
}
|
|
55
|
+
export function parseExplicitChaoTone(str) {
|
|
56
|
+
switch (str) {
|
|
57
|
+
case '˥˥':
|
|
58
|
+
case '˥':
|
|
59
|
+
return { tone: 1, isHalfThird: false };
|
|
60
|
+
case '˧˥':
|
|
61
|
+
case '˨˥':
|
|
62
|
+
return { tone: 2, isHalfThird: false };
|
|
63
|
+
case '˨˩˦':
|
|
64
|
+
case '˨˩˧':
|
|
65
|
+
return { tone: 3, isHalfThird: false };
|
|
66
|
+
case '˨˩':
|
|
67
|
+
case '˩˩':
|
|
68
|
+
return { tone: 3, isHalfThird: true };
|
|
69
|
+
case '˥˩':
|
|
70
|
+
case '˥˨':
|
|
71
|
+
case '˦˩':
|
|
72
|
+
return { tone: 4, isHalfThird: false };
|
|
73
|
+
default:
|
|
74
|
+
return null;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export function parseExplicitMultiDigitChao(str) {
|
|
78
|
+
switch (str) {
|
|
79
|
+
case '55':
|
|
80
|
+
return { tone: 1, isHalfThird: false };
|
|
81
|
+
case '35':
|
|
82
|
+
return { tone: 2, isHalfThird: false };
|
|
83
|
+
case '214':
|
|
84
|
+
return { tone: 3, isHalfThird: false };
|
|
85
|
+
case '21':
|
|
86
|
+
return { tone: 3, isHalfThird: true };
|
|
87
|
+
case '51':
|
|
88
|
+
return { tone: 4, isHalfThird: false };
|
|
89
|
+
default:
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
const AOE_ZERO_FINALS = new Set([
|
|
94
|
+
'a', 'o', 'e', 'ê', 'ai', 'ei', 'ao', 'ou', 'an', 'en', 'ang', 'eng', 'er'
|
|
95
|
+
]);
|
|
96
|
+
function tryParseSinglePinyinPiece(raw) {
|
|
97
|
+
const s = raw.trim();
|
|
98
|
+
if (!s || countPinyinToneDiacritics(s) > 1)
|
|
99
|
+
return null;
|
|
100
|
+
// 1. Explicit Chao tone letters (e.g. ma˥˥, wo˨˩)
|
|
101
|
+
const chaoMatch = /^(.+?)([˥˦˧˨˩]+)$/u.exec(s);
|
|
102
|
+
if (chaoMatch) {
|
|
103
|
+
const chao = parseExplicitChaoTone(chaoMatch[2]);
|
|
104
|
+
const base = parsePinyinSyllable(chaoMatch[1]);
|
|
105
|
+
if (chao && base.isValid && !/[0-9]/.test(chaoMatch[1])) {
|
|
106
|
+
return {
|
|
107
|
+
raw: s,
|
|
108
|
+
bare: base.bare,
|
|
109
|
+
initial: base.initial,
|
|
110
|
+
final: base.final,
|
|
111
|
+
tone: chao.tone,
|
|
112
|
+
isErhua: base.isErhua,
|
|
113
|
+
lockedTone: true,
|
|
114
|
+
explicitHalfThird: chao.isHalfThird
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
return null;
|
|
118
|
+
}
|
|
119
|
+
// 2. Explicit 5-level multi-digit Chao numbers (55, 35, 214, 21, 51)
|
|
120
|
+
const multiNumMatch = /^([A-Za-züÜāáǎàōóǒòēéěèīíǐìūúǔùǖǘǚǜêếềńňǹḿ:'\u0304\u030c]+?)(214|55|35|21|51)$/u.exec(s);
|
|
121
|
+
if (multiNumMatch) {
|
|
122
|
+
const chao = parseExplicitMultiDigitChao(multiNumMatch[2]);
|
|
123
|
+
const base = parsePinyinSyllable(multiNumMatch[1]);
|
|
124
|
+
if (chao && base.isValid && !/[0-9]/.test(multiNumMatch[1])) {
|
|
125
|
+
return {
|
|
126
|
+
raw: s,
|
|
127
|
+
bare: base.bare,
|
|
128
|
+
initial: base.initial,
|
|
129
|
+
final: base.final,
|
|
130
|
+
tone: chao.tone,
|
|
131
|
+
isErhua: base.isErhua,
|
|
132
|
+
lockedTone: true,
|
|
133
|
+
explicitHalfThird: chao.isHalfThird
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
// 3. Reject digits appearing in the middle of a syllable (except trailing [0-5], [0-5]r, [0-5]'r)
|
|
138
|
+
const withoutValidTrailingDigit = s.replace(/[0-5](?:'?r)?$/i, '');
|
|
139
|
+
if (/[0-9]/.test(withoutValidTrailingDigit)) {
|
|
140
|
+
return null;
|
|
141
|
+
}
|
|
142
|
+
const parsed = parsePinyinSyllable(s);
|
|
143
|
+
if (!parsed.isValid)
|
|
144
|
+
return null;
|
|
145
|
+
return {
|
|
146
|
+
raw: s,
|
|
147
|
+
bare: parsed.bare,
|
|
148
|
+
initial: parsed.initial,
|
|
149
|
+
final: parsed.final,
|
|
150
|
+
tone: parsed.tone,
|
|
151
|
+
isErhua: parsed.isErhua,
|
|
152
|
+
lockedTone: false,
|
|
153
|
+
explicitHalfThird: false
|
|
154
|
+
};
|
|
155
|
+
}
|
|
156
|
+
function segmentPinyinPiece(piece) {
|
|
157
|
+
const single = tryParseSinglePinyinPiece(piece);
|
|
158
|
+
if (single)
|
|
159
|
+
return [single];
|
|
160
|
+
// Dynamic programming over code-unit indices to split connected multi-syllable Pinyin words
|
|
161
|
+
// such as "nǐhǎo", "ni3hao3", "lǎohǔ", "zhǎnlǎnguǎn", "zhuōzi".
|
|
162
|
+
const n = piece.length;
|
|
163
|
+
if (n < 2)
|
|
164
|
+
return null;
|
|
165
|
+
const dp = new Array(n + 1).fill(null);
|
|
166
|
+
dp[0] = { cost: 0, syllables: [] };
|
|
167
|
+
for (let p = 0; p < n; p++) {
|
|
168
|
+
const cur = dp[p];
|
|
169
|
+
if (!cur)
|
|
170
|
+
continue;
|
|
171
|
+
const maxEnd = Math.min(n, p + 10);
|
|
172
|
+
for (let q = maxEnd; q > p; q--) {
|
|
173
|
+
const slice = piece.slice(p, q);
|
|
174
|
+
if (slice.startsWith("'") || slice.endsWith("'"))
|
|
175
|
+
continue;
|
|
176
|
+
const syl = tryParseSinglePinyinPiece(slice);
|
|
177
|
+
if (!syl)
|
|
178
|
+
continue;
|
|
179
|
+
// Base cost = 10 per syllable (strongly favors minimal syllable count / maximal onset+rime),
|
|
180
|
+
// +2 if a mid-word syllable uses erhua before the end of the connected word,
|
|
181
|
+
// +3 if a non-initial syllable starts with zero-initial a/o/e without an apostrophe.
|
|
182
|
+
let stepCost = 10;
|
|
183
|
+
if (syl.isErhua && q < n)
|
|
184
|
+
stepCost += 2;
|
|
185
|
+
if (p > 0 && syl.initial === '' && AOE_ZERO_FINALS.has(syl.final))
|
|
186
|
+
stepCost += 3;
|
|
187
|
+
if (['m', 'n', 'ng', 'hm', 'hng'].includes(syl.bare))
|
|
188
|
+
stepCost += 6;
|
|
189
|
+
const nextCost = cur.cost + stepCost;
|
|
190
|
+
const existing = dp[q];
|
|
191
|
+
if (!existing || nextCost < existing.cost) {
|
|
192
|
+
dp[q] = {
|
|
193
|
+
cost: nextCost,
|
|
194
|
+
syllables: [...cur.syllables, syl]
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
return dp[n]?.syllables ?? null;
|
|
200
|
+
}
|
|
201
|
+
function parsePinyinWordToken(word) {
|
|
202
|
+
const trimmed = word.replace(/^['-]+|['-]+$/g, '');
|
|
203
|
+
if (!trimmed)
|
|
204
|
+
return null;
|
|
205
|
+
// Check if the entire word is already a single valid syllable (e.g. "é'r", "e2'r", "huār")
|
|
206
|
+
const direct = tryParseSinglePinyinPiece(trimmed);
|
|
207
|
+
if (direct)
|
|
208
|
+
return [direct];
|
|
209
|
+
// Split on syllable-separating apostrophes or hyphens while keeping erhua "'r" intact
|
|
210
|
+
const parts = trimmed.split(/(?:'(?!r[0-5]?(?:$|'))|-)+/i).filter(Boolean);
|
|
211
|
+
if (parts.length === 0)
|
|
212
|
+
return null;
|
|
213
|
+
const collected = [];
|
|
214
|
+
for (const part of parts) {
|
|
215
|
+
const segs = segmentPinyinPiece(part);
|
|
216
|
+
if (!segs)
|
|
217
|
+
return null;
|
|
218
|
+
collected.push(...segs);
|
|
219
|
+
}
|
|
220
|
+
return collected.length > 0 ? collected : null;
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Zero-dependency multi-syllable Pinyin phrase/sentence converter.
|
|
224
|
+
* Parses space-, apostrophe-, or connected multi-syllable Pinyin strings,
|
|
225
|
+
* runs prosodic tone sandhi (`applyMandarinSandhi`), and formats both Pinyin and IPA.
|
|
226
|
+
*/
|
|
227
|
+
export function convertPinyinSequence(pinyinText, overrides) {
|
|
228
|
+
const config = createDefaultMandarinConfig(overrides);
|
|
229
|
+
const trimmed = pinyinText.trim();
|
|
230
|
+
if (!trimmed) {
|
|
231
|
+
return {
|
|
232
|
+
sourceText: pinyinText,
|
|
233
|
+
pinyinText: '',
|
|
234
|
+
ipaText: '',
|
|
235
|
+
syllables: []
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
if (WHOLE_SILENCE_TAG_RE.test(trimmed)) {
|
|
239
|
+
return {
|
|
240
|
+
sourceText: pinyinText,
|
|
241
|
+
pinyinText: trimmed,
|
|
242
|
+
ipaText: trimmed,
|
|
243
|
+
syllables: [
|
|
244
|
+
{
|
|
245
|
+
kind: 'silence',
|
|
246
|
+
sourceText: trimmed,
|
|
247
|
+
pinyin: trimmed,
|
|
248
|
+
ipa: trimmed,
|
|
249
|
+
durationWeight: 1.0,
|
|
250
|
+
acousticClass: 'other'
|
|
251
|
+
}
|
|
252
|
+
]
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
const chars = Array.from(pinyinText);
|
|
256
|
+
const intermediate = [];
|
|
257
|
+
let nextWordId = 1;
|
|
258
|
+
let i = 0;
|
|
259
|
+
while (i < chars.length) {
|
|
260
|
+
const ch = chars[i];
|
|
261
|
+
// Whitespace: single space separates Pinyin words; newline/tab/multi-space emits prosodic domain break
|
|
262
|
+
if (/^\s+$/.test(ch)) {
|
|
263
|
+
const wsStart = i;
|
|
264
|
+
while (i < chars.length && /^\s+$/.test(chars[i]))
|
|
265
|
+
i++;
|
|
266
|
+
const ws = chars.slice(wsStart, i).join('');
|
|
267
|
+
if (ws.includes('\n') || ws.includes('\r') || ws.includes('\t') || ws.length >= 2) {
|
|
268
|
+
intermediate.push({
|
|
269
|
+
kind: 'break',
|
|
270
|
+
sourceText: ws,
|
|
271
|
+
sourceCharStart: wsStart,
|
|
272
|
+
sourceCharEnd: i,
|
|
273
|
+
bare: '',
|
|
274
|
+
tone: 0,
|
|
275
|
+
isErhua: false,
|
|
276
|
+
lockedTone: true,
|
|
277
|
+
explicitHalfThird: false
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
continue;
|
|
281
|
+
}
|
|
282
|
+
// Inline silence tags (<sil>, <pau>, [sigh], etc.)
|
|
283
|
+
if (ch === '<' || ch === '[') {
|
|
284
|
+
const rest = chars.slice(i).join('');
|
|
285
|
+
const tagMatch = rest.match(INLINE_SILENCE_TAG_RE);
|
|
286
|
+
if (tagMatch) {
|
|
287
|
+
const tagLen = Array.from(tagMatch[0]).length;
|
|
288
|
+
intermediate.push({
|
|
289
|
+
kind: 'silence',
|
|
290
|
+
sourceText: tagMatch[0],
|
|
291
|
+
sourceCharStart: i,
|
|
292
|
+
sourceCharEnd: i + tagLen,
|
|
293
|
+
bare: '',
|
|
294
|
+
tone: 0,
|
|
295
|
+
isErhua: false,
|
|
296
|
+
lockedTone: true,
|
|
297
|
+
explicitHalfThird: false
|
|
298
|
+
});
|
|
299
|
+
i += tagLen;
|
|
300
|
+
continue;
|
|
301
|
+
}
|
|
302
|
+
}
|
|
303
|
+
// Pure numeric token
|
|
304
|
+
if (/[0-9]/.test(ch)) {
|
|
305
|
+
const start = i;
|
|
306
|
+
let j = i;
|
|
307
|
+
while (j < chars.length &&
|
|
308
|
+
(/[0-9]/.test(chars[j]) ||
|
|
309
|
+
(chars[j] === '.' && j + 1 < chars.length && /[0-9]/.test(chars[j + 1])))) {
|
|
310
|
+
j++;
|
|
311
|
+
}
|
|
312
|
+
const numStr = chars.slice(start, j).join('');
|
|
313
|
+
intermediate.push({
|
|
314
|
+
kind: 'digit',
|
|
315
|
+
sourceText: numStr,
|
|
316
|
+
sourceCharStart: start,
|
|
317
|
+
sourceCharEnd: j,
|
|
318
|
+
bare: '',
|
|
319
|
+
tone: 0,
|
|
320
|
+
isErhua: false,
|
|
321
|
+
lockedTone: true,
|
|
322
|
+
explicitHalfThird: false
|
|
323
|
+
});
|
|
324
|
+
i = j;
|
|
325
|
+
continue;
|
|
326
|
+
}
|
|
327
|
+
// Pinyin word or Latin token
|
|
328
|
+
if (PINYIN_TOKEN_CHAR_RE.test(ch) && ch !== "'" && ch !== ':' && ch !== '-') {
|
|
329
|
+
const start = i;
|
|
330
|
+
let j = i;
|
|
331
|
+
while (j < chars.length && PINYIN_TOKEN_CHAR_RE.test(chars[j])) {
|
|
332
|
+
j++;
|
|
333
|
+
}
|
|
334
|
+
while (j > start + 1 && /[':-]/.test(chars[j - 1])) {
|
|
335
|
+
j--;
|
|
336
|
+
}
|
|
337
|
+
const rawWord = chars.slice(start, j).join('');
|
|
338
|
+
const parsedSyllables = parsePinyinWordToken(rawWord);
|
|
339
|
+
if (parsedSyllables && parsedSyllables.length > 0) {
|
|
340
|
+
const wid = nextWordId++;
|
|
341
|
+
const wLen = parsedSyllables.length;
|
|
342
|
+
for (let p = 0; p < wLen; p++) {
|
|
343
|
+
const ps = parsedSyllables[p];
|
|
344
|
+
intermediate.push({
|
|
345
|
+
kind: 'hanzi',
|
|
346
|
+
sourceText: ps.raw,
|
|
347
|
+
sourceCharStart: start,
|
|
348
|
+
sourceCharEnd: j,
|
|
349
|
+
bare: ps.bare,
|
|
350
|
+
tone: ps.tone,
|
|
351
|
+
isErhua: ps.isErhua,
|
|
352
|
+
lockedTone: ps.lockedTone,
|
|
353
|
+
explicitHalfThird: ps.explicitHalfThird,
|
|
354
|
+
wordId: wid,
|
|
355
|
+
posInWord: p,
|
|
356
|
+
wordLength: wLen
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
intermediate.push({
|
|
362
|
+
kind: 'latin',
|
|
363
|
+
sourceText: rawWord,
|
|
364
|
+
sourceCharStart: start,
|
|
365
|
+
sourceCharEnd: j,
|
|
366
|
+
bare: '',
|
|
367
|
+
tone: 0,
|
|
368
|
+
isErhua: false,
|
|
369
|
+
lockedTone: true,
|
|
370
|
+
explicitHalfThird: false
|
|
371
|
+
});
|
|
372
|
+
}
|
|
373
|
+
i = j;
|
|
374
|
+
continue;
|
|
375
|
+
}
|
|
376
|
+
// Punctuation
|
|
377
|
+
if (PUNCT_RE.test(ch)) {
|
|
378
|
+
intermediate.push({
|
|
379
|
+
kind: 'punctuation',
|
|
380
|
+
sourceText: ch,
|
|
381
|
+
sourceCharStart: i,
|
|
382
|
+
sourceCharEnd: i + 1,
|
|
383
|
+
bare: '',
|
|
384
|
+
tone: 0,
|
|
385
|
+
isErhua: false,
|
|
386
|
+
lockedTone: true,
|
|
387
|
+
explicitHalfThird: false
|
|
388
|
+
});
|
|
389
|
+
i++;
|
|
390
|
+
continue;
|
|
391
|
+
}
|
|
392
|
+
// Other character
|
|
393
|
+
intermediate.push({
|
|
394
|
+
kind: 'other',
|
|
395
|
+
sourceText: ch,
|
|
396
|
+
sourceCharStart: i,
|
|
397
|
+
sourceCharEnd: i + 1,
|
|
398
|
+
bare: '',
|
|
399
|
+
tone: 0,
|
|
400
|
+
isErhua: false,
|
|
401
|
+
lockedTone: true,
|
|
402
|
+
explicitHalfThird: false
|
|
403
|
+
});
|
|
404
|
+
i++;
|
|
405
|
+
}
|
|
406
|
+
const sandhiInputs = intermediate.map((tok) => ({
|
|
407
|
+
char: '',
|
|
408
|
+
bare: tok.bare,
|
|
409
|
+
tone: tok.tone,
|
|
410
|
+
isHanzi: false,
|
|
411
|
+
isDomainBreak: tok.kind !== 'hanzi',
|
|
412
|
+
wordId: tok.wordId,
|
|
413
|
+
posInWord: tok.posInWord,
|
|
414
|
+
wordLength: tok.wordLength,
|
|
415
|
+
lockedTone: tok.lockedTone,
|
|
416
|
+
explicitHalfThird: tok.explicitHalfThird
|
|
417
|
+
}));
|
|
418
|
+
const sandhiOutputs = applyMandarinSandhi(sandhiInputs, config.sandhi);
|
|
419
|
+
const baseIpaOptions = buildIpaOptionsFromConfig(config);
|
|
420
|
+
const rawUnits = [];
|
|
421
|
+
let prevTone;
|
|
422
|
+
for (let idx = 0; idx < intermediate.length; idx++) {
|
|
423
|
+
const tok = intermediate[idx];
|
|
424
|
+
const so = sandhiOutputs[idx];
|
|
425
|
+
if (tok.kind === 'break') {
|
|
426
|
+
prevTone = undefined;
|
|
427
|
+
continue;
|
|
428
|
+
}
|
|
429
|
+
if (tok.kind === 'hanzi') {
|
|
430
|
+
const pinyinOut = formatPinyinSyllable({ bare: tok.bare, tone: so.tone, isErhua: tok.isErhua }, config.pinyinFormat, config.neutralTone.mark);
|
|
431
|
+
const baseParsed = parsePinyinSyllable(tok.bare);
|
|
432
|
+
const parsedFull = {
|
|
433
|
+
...baseParsed,
|
|
434
|
+
raw: tok.bare +
|
|
435
|
+
(tok.isErhua ? (tok.bare === 'e' ? "'r" : 'r') : '') +
|
|
436
|
+
String(so.tone),
|
|
437
|
+
tone: so.tone,
|
|
438
|
+
isErhua: tok.isErhua
|
|
439
|
+
};
|
|
440
|
+
const ipaOut = parsedFull.isValid
|
|
441
|
+
? pinyinSyllableToIpa(parsedFull, {
|
|
442
|
+
...baseIpaOptions,
|
|
443
|
+
isHalfThird: so.isHalfThird,
|
|
444
|
+
prevTone
|
|
445
|
+
})
|
|
446
|
+
: tok.sourceText;
|
|
447
|
+
prevTone = so.tone;
|
|
448
|
+
rawUnits.push({
|
|
449
|
+
kind: 'hanzi',
|
|
450
|
+
sourceText: tok.sourceText,
|
|
451
|
+
pinyin: pinyinOut,
|
|
452
|
+
ipa: ipaOut,
|
|
453
|
+
tone: so.tone,
|
|
454
|
+
isHalfThird: so.isHalfThird,
|
|
455
|
+
isErhua: tok.isErhua,
|
|
456
|
+
durationWeight: so.tone === 0 ? 0.72 : tok.isErhua ? 1.15 : 1.0,
|
|
457
|
+
acousticClass: 'syllable',
|
|
458
|
+
sourceCharStart: tok.sourceCharStart,
|
|
459
|
+
sourceCharEnd: tok.sourceCharEnd
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
else if (tok.kind === 'latin') {
|
|
463
|
+
prevTone = undefined;
|
|
464
|
+
rawUnits.push({
|
|
465
|
+
kind: 'latin',
|
|
466
|
+
sourceText: tok.sourceText,
|
|
467
|
+
pinyin: tok.sourceText,
|
|
468
|
+
ipa: config.latinInIpa === 'keep' ? tok.sourceText : '',
|
|
469
|
+
durationWeight: Math.max(0.8, Math.min(2.2, tok.sourceText.length * 0.45)),
|
|
470
|
+
acousticClass: 'syllable',
|
|
471
|
+
sourceCharStart: tok.sourceCharStart,
|
|
472
|
+
sourceCharEnd: tok.sourceCharEnd
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
else if (tok.kind === 'digit' || tok.kind === 'silence' || tok.kind === 'other') {
|
|
476
|
+
prevTone = undefined;
|
|
477
|
+
rawUnits.push({
|
|
478
|
+
kind: tok.kind,
|
|
479
|
+
sourceText: tok.sourceText,
|
|
480
|
+
pinyin: tok.sourceText,
|
|
481
|
+
ipa: tok.sourceText,
|
|
482
|
+
durationWeight: 0.9,
|
|
483
|
+
acousticClass: 'other',
|
|
484
|
+
sourceCharStart: tok.sourceCharStart,
|
|
485
|
+
sourceCharEnd: tok.sourceCharEnd
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
else if (tok.kind === 'punctuation') {
|
|
489
|
+
prevTone = undefined;
|
|
490
|
+
rawUnits.push({
|
|
491
|
+
kind: 'punctuation',
|
|
492
|
+
sourceText: tok.sourceText,
|
|
493
|
+
pinyin: tok.sourceText,
|
|
494
|
+
ipa: tok.sourceText,
|
|
495
|
+
durationWeight: 0.35,
|
|
496
|
+
acousticClass: 'other',
|
|
497
|
+
sourceCharStart: tok.sourceCharStart,
|
|
498
|
+
sourceCharEnd: tok.sourceCharEnd
|
|
499
|
+
});
|
|
500
|
+
}
|
|
501
|
+
}
|
|
502
|
+
const finalUnits = applyPunctuationModeToUnits(rawUnits, config.punctuationMode);
|
|
503
|
+
const pinyinParts = finalUnits.map((u) => u.pinyin).filter((s) => s.length > 0);
|
|
504
|
+
const ipaParts = finalUnits.map((u) => u.ipa).filter((s) => s.length > 0);
|
|
505
|
+
return {
|
|
506
|
+
sourceText: pinyinText,
|
|
507
|
+
pinyinText: pinyinParts.join(' '),
|
|
508
|
+
ipaText: ipaParts.join(' '),
|
|
509
|
+
syllables: finalUnits
|
|
510
|
+
};
|
|
511
|
+
}
|
|
512
|
+
/**
|
|
513
|
+
* Convenience zero-dependency helper returning only the converted IPA string for a Pinyin sequence.
|
|
514
|
+
*/
|
|
515
|
+
export function pinyinToIpa(pinyinText, overrides) {
|
|
516
|
+
return convertPinyinSequence(pinyinText, overrides).ipaText;
|
|
517
|
+
}
|
package/dist/sandhi.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { MandarinTone, SandhiConfig } from './types.js';
|
|
2
|
+
export interface SandhiSyllableInput {
|
|
3
|
+
char?: string;
|
|
4
|
+
bare: string;
|
|
5
|
+
tone: MandarinTone;
|
|
6
|
+
isHanzi?: boolean;
|
|
7
|
+
isDomainBreak?: boolean;
|
|
8
|
+
wordId?: number;
|
|
9
|
+
posInWord?: number;
|
|
10
|
+
wordLength?: number;
|
|
11
|
+
lockedTone?: boolean;
|
|
12
|
+
explicitHalfThird?: boolean;
|
|
13
|
+
}
|
|
14
|
+
export interface SandhiSyllableOutput {
|
|
15
|
+
tone: MandarinTone;
|
|
16
|
+
isHalfThird: boolean;
|
|
17
|
+
sandhiApplied?: 'third-tone' | 'half-third' | 'yi' | 'bu' | 'neutral-particle';
|
|
18
|
+
}
|
|
19
|
+
export declare function applyMandarinSandhi(items: readonly SandhiSyllableInput[], sandhiConfig: SandhiConfig): SandhiSyllableOutput[];
|