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.
@@ -0,0 +1,2 @@
1
+ export * from './pinyin.js';
2
+ export { convertMandarinText, extractPolyphones, loadPinyinPro, mandarinToIpa, mandarinToPinyin, parseCustomDictWithDiagnostics, type CustomDictDiagnostic, type PinyinProModule } from './converter.js';
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ export * from './pinyin.js';
2
+ export { convertMandarinText, extractPolyphones, loadPinyinPro, mandarinToIpa, mandarinToPinyin, parseCustomDictWithDiagnostics } from './converter.js';
@@ -0,0 +1,10 @@
1
+ import { type IpaConversionOptions, type IpaToneStyle, type MandarinIpaConfigOverrides, type MandarinTone, type NeutralToneMark, type ParsedPinyinSyllable, type PinyinFormat } from './types.js';
2
+ export declare function countPinyinToneDiacritics(input: string): number;
3
+ export declare function parsePinyinSyllable(input: string): ParsedPinyinSyllable;
4
+ export declare function formatPinyinSyllable(parsed: Pick<ParsedPinyinSyllable, 'bare' | 'tone' | 'isErhua'>, format: PinyinFormat, neutralMark?: NeutralToneMark): string;
5
+ export declare function formatIpaTone(tone: MandarinTone, style: IpaToneStyle, neutralMark?: NeutralToneMark, isHalfThird?: boolean, prevTone?: MandarinTone): string;
6
+ export declare function pinyinSyllableToIpa(input: string | ParsedPinyinSyllable, options: IpaConversionOptions): string;
7
+ export declare function buildIpaOptionsFromConfig(config?: MandarinIpaConfigOverrides, extra?: {
8
+ isHalfThird?: boolean;
9
+ prevTone?: MandarinTone;
10
+ }): IpaConversionOptions;
@@ -0,0 +1,412 @@
1
+ import { createDefaultMandarinConfig } from './types.js';
2
+ // ---------------------------------------------------------------------------
3
+ // Tone Diacritic Tables
4
+ // ---------------------------------------------------------------------------
5
+ const TONE_CHAR_INFO = {
6
+ ā: { base: 'a', tone: 1 }, á: { base: 'a', tone: 2 }, ǎ: { base: 'a', tone: 3 }, à: { base: 'a', tone: 4 },
7
+ ō: { base: 'o', tone: 1 }, ó: { base: 'o', tone: 2 }, ǒ: { base: 'o', tone: 3 }, ò: { base: 'o', tone: 4 },
8
+ ē: { base: 'e', tone: 1 }, é: { base: 'e', tone: 2 }, ě: { base: 'e', tone: 3 }, è: { base: 'e', tone: 4 },
9
+ ī: { base: 'i', tone: 1 }, í: { base: 'i', tone: 2 }, ǐ: { base: 'i', tone: 3 }, ì: { base: 'i', tone: 4 },
10
+ ū: { base: 'u', tone: 1 }, ú: { base: 'u', tone: 2 }, ǔ: { base: 'u', tone: 3 }, ù: { base: 'u', tone: 4 },
11
+ ǖ: { base: 'v', tone: 1 }, ǘ: { base: 'v', tone: 2 }, ǚ: { base: 'v', tone: 3 }, ǜ: { base: 'v', tone: 4 },
12
+ ü: { base: 'v', tone: 0 },
13
+ ê: { base: 'ê', tone: 0 }, ế: { base: 'ê', tone: 2 }, ề: { base: 'ê', tone: 4 },
14
+ ń: { base: 'n', tone: 2 }, ň: { base: 'n', tone: 3 }, ǹ: { base: 'n', tone: 4 },
15
+ ḿ: { base: 'm', tone: 2 }
16
+ };
17
+ const VOWEL_TONE_MARKS = {
18
+ a: ['a', 'ā', 'á', 'ǎ', 'à'],
19
+ o: ['o', 'ō', 'ó', 'ǒ', 'ò'],
20
+ e: ['e', 'ē', 'é', 'ě', 'è'],
21
+ i: ['i', 'ī', 'í', 'ǐ', 'ì'],
22
+ u: ['u', 'ū', 'ú', 'ǔ', 'ù'],
23
+ v: ['ü', 'ǖ', 'ǘ', 'ǚ', 'ǜ'],
24
+ ê: ['ê', 'ê̄', 'ế', 'ê̌', 'ề'],
25
+ n: ['n', 'n', 'ń', 'ň', 'ǹ'],
26
+ m: ['m', 'm', 'ḿ', 'm', 'm']
27
+ };
28
+ const ONE_CHAR_INITIALS = new Set('bpmfdtnlgkhjqxrzcs'.split(''));
29
+ const LABIALS = new Set(['b', 'p', 'm', 'f']);
30
+ const PALATALS = new Set(['j', 'q', 'x']);
31
+ const ALVEOLARS = new Set(['z', 'c', 's']);
32
+ const RETROFLEXES = new Set(['zh', 'ch', 'sh', 'r']);
33
+ const INTERJECTIONS = new Set(['m', 'n', 'ng', 'hm', 'hng', 'ê']);
34
+ const VALID_FINALS = new Set([
35
+ 'a', 'o', 'e', 'i', 'u', 'v', 'ê', 'ai', 'ei', 'ao', 'ou', 'an', 'en', 'ang', 'eng', 'ong',
36
+ 'ia', 'ie', 'iao', 'iu', 'iou', 'ian', 'in', 'iang', 'ing', 'iong',
37
+ 'ua', 'uo', 'uai', 'ui', 'uei', 'uan', 'un', 'uen', 'uang', 'ueng',
38
+ 've', 'van', 'vn', 'er',
39
+ 'yi', 'ya', 'ye', 'yao', 'you', 'yan', 'yin', 'yang', 'ying', 'yong', 'yo',
40
+ 'wu', 'wa', 'wo', 'wai', 'wei', 'wan', 'wen', 'wang', 'weng',
41
+ 'yu', 'yue', 'yuan', 'yun'
42
+ ]);
43
+ const ZERO_YW_FINALS = {
44
+ yi: ['i', 'i', 'i'], ya: ['ja', 'ja', 'ja'], ye: ['jɛ', 'je', 'jɛ'],
45
+ yao: ['jau', 'jau', 'jɑʊ'], you: ['jou', 'jou', 'joʊ'], yan: ['jɛn', 'jan', 'jɛn'],
46
+ yin: ['in', 'in', 'in'], yang: ['jaŋ', 'jaŋ', 'jɑŋ'], ying: ['iŋ', 'iŋ', 'iŋ'],
47
+ yong: ['juŋ', 'yŋ', 'jʊŋ'], yo: ['jo', 'jo', 'jo'],
48
+ yu: ['y', 'y', 'y'], yue: ['ɥɛ', 'ɥe', 'ɥɛ'], yuan: ['ɥɛn', 'ɥan', 'ɥɛn'], yun: ['yn', 'yn', 'yn'],
49
+ wu: ['u', 'u', 'u'], wa: ['wa', 'wa', 'wa'], wo: ['wo', 'wo', 'wɔ'],
50
+ wai: ['wai', 'wai', 'waɪ'], wei: ['wei', 'wei', 'weɪ'], wan: ['wan', 'wan', 'wan'],
51
+ wen: ['wən', 'wən', 'wən'], wang: ['waŋ', 'waŋ', 'wɑŋ'], weng: ['wəŋ', 'wəŋ', 'wəŋ'],
52
+ er: ['ɚ', 'ɚ', 'ɑɻ']
53
+ };
54
+ const CORE_FINALS = {
55
+ a: ['a', 'a', 'a'], o: ['o', 'o', 'ɔ'], e: ['ɤ', 'ɤ', 'ɤ'],
56
+ i: ['i', 'i', 'i'], u: ['u', 'u', 'u'], v: ['y', 'y', 'y'],
57
+ ai: ['ai', 'ai', 'aɪ'], ei: ['ei', 'ei', 'eɪ'], ao: ['au', 'au', 'ɑʊ'], ou: ['ou', 'ou', 'oʊ'],
58
+ an: ['an', 'an', 'an'], en: ['ən', 'ən', 'ən'], ang: ['aŋ', 'aŋ', 'ɑŋ'], eng: ['əŋ', 'əŋ', 'əŋ'],
59
+ ong: ['uŋ', 'uŋ', 'ʊŋ'],
60
+ ia: ['ia', 'ia', 'ia'], ie: ['iɛ', 'ie', 'iɛ'], iao: ['iau', 'iau', 'iɑʊ'],
61
+ iu: ['iou', 'iou', 'ioʊ'], iou: ['iou', 'iou', 'ioʊ'],
62
+ ian: ['iɛn', 'ian', 'iɛn'], in: ['in', 'in', 'in'], iang: ['iaŋ', 'iaŋ', 'iɑŋ'],
63
+ ing: ['iŋ', 'iŋ', 'iŋ'], iong: ['yuŋ', 'yŋ', 'iʊŋ'],
64
+ ua: ['ua', 'ua', 'ua'], uo: ['uo', 'uo', 'uɔ'], uai: ['uai', 'uai', 'uaɪ'],
65
+ ui: ['uei', 'uei', 'ueɪ'], uei: ['uei', 'uei', 'ueɪ'],
66
+ uan: ['uan', 'uan', 'uan'], un: ['uən', 'uən', 'uən'], uen: ['uən', 'uən', 'uən'],
67
+ uang: ['uaŋ', 'uaŋ', 'uɑŋ'], ueng: ['uəŋ', 'uəŋ', 'uəŋ'],
68
+ ve: ['yɛ', 'ye', 'yɛ'], van: ['yɛn', 'yan', 'yɛn'], vn: ['yn', 'yn', 'yn'],
69
+ er: ['ɚ', 'ɚ', 'ɑɻ']
70
+ };
71
+ // Finals taking optional glottal onset ʔ in zero-initial position
72
+ const GLOTTAL_ELIGIBLE_FINALS = new Set([
73
+ 'a', 'o', 'e', 'ê', 'ai', 'ei', 'ao', 'ou', 'an', 'en', 'ang', 'eng'
74
+ ]);
75
+ // Erhua final table: [sinological, broad, narrow]
76
+ const ERHUA_YW_FINALS = {
77
+ yi: ['iə˞', 'iə˞', 'iə˞'], ya: ['ja˞', 'ja˞', 'ja˞'], ye: ['jɛ˞', 'je˞', 'jɛ˞'],
78
+ yao: ['jau˞', 'jau˞', 'jɑʊ˞'], you: ['jou˞', 'jou˞', 'joʊ˞'], yan: ['ja˞', 'ja˞', 'ja˞'],
79
+ yin: ['iə˞', 'iə˞', 'iə˞'], yang: ['ja\u0303˞', 'ja\u0303˞', 'jɑ\u0303˞'],
80
+ ying: ['iə\u0303˞', 'iə\u0303˞', 'iə\u0303˞'], yong: ['ju\u0303˞', 'ju\u0303˞', 'jʊ\u0303˞'],
81
+ yu: ['yə˞', 'yə˞', 'yə˞'], yue: ['ɥɛ˞', 'ɥe˞', 'ɥɛ˞'], yuan: ['ɥa˞', 'ɥa˞', 'ɥa˞'],
82
+ yun: ['yə˞', 'yə˞', 'yə˞'], wu: ['u˞', 'u˞', 'u˞'], wa: ['wa˞', 'wa˞', 'wa˞'],
83
+ wo: ['wo˞', 'wo˞', 'wɔ˞'], wai: ['wa˞', 'wa˞', 'wa˞'], wei: ['wə˞', 'wə˞', 'wə˞'],
84
+ wan: ['wa˞', 'wa˞', 'wa˞'], wen: ['wə˞', 'wə˞', 'wə˞'],
85
+ wang: ['wa\u0303˞', 'wa\u0303˞', 'wɑ\u0303˞'], weng: ['wə\u0303˞', 'wə\u0303˞', 'wə\u0303˞']
86
+ };
87
+ const ERHUA_CORE_FINALS = {
88
+ a: ['a˞', 'a˞', 'a˞'], o: ['o˞', 'o˞', 'ɔ˞'], e: ['ɤ˞', 'ɤ˞', 'ɤ˞'], u: ['u˞', 'u˞', 'u˞'],
89
+ i: ['iə˞', 'iə˞', 'iə˞'], v: ['yə˞', 'yə˞', 'yə˞'],
90
+ ia: ['ia˞', 'ia˞', 'ia˞'], ua: ['ua˞', 'ua˞', 'ua˞'], uo: ['uo˞', 'uo˞', 'uɔ˞'],
91
+ ie: ['iɛ˞', 'ie˞', 'iɛ˞'], ve: ['yɛ˞', 'ye˞', 'yɛ˞'],
92
+ ao: ['au˞', 'au˞', 'ɑʊ˞'], iao: ['iau˞', 'iau˞', 'iɑʊ˞'],
93
+ ou: ['ou˞', 'ou˞', 'oʊ˞'], iu: ['iou˞', 'iou˞', 'ioʊ˞'], iou: ['iou˞', 'iou˞', 'ioʊ˞'],
94
+ ai: ['a˞', 'a˞', 'a˞'], an: ['a˞', 'a˞', 'a˞'],
95
+ uai: ['ua˞', 'ua˞', 'ua˞'], uan: ['ua˞', 'ua˞', 'ua˞'],
96
+ ian: ['ia˞', 'ia˞', 'ia˞'], van: ['ya˞', 'ya˞', 'ya˞'],
97
+ ei: ['ə˞', 'ə˞', 'ə˞'], en: ['ə˞', 'ə˞', 'ə˞'],
98
+ ui: ['uə˞', 'uə˞', 'uə˞'], uei: ['uə˞', 'uə˞', 'uə˞'],
99
+ un: ['uə˞', 'uə˞', 'uə˞'], uen: ['uə˞', 'uə˞', 'uə˞'],
100
+ in: ['iə˞', 'iə˞', 'iə˞'], vn: ['yə˞', 'yə˞', 'yə˞'],
101
+ ang: ['a\u0303˞', 'a\u0303˞', 'ɑ\u0303˞'], iang: ['ia\u0303˞', 'ia\u0303˞', 'iɑ\u0303˞'],
102
+ uang: ['ua\u0303˞', 'ua\u0303˞', 'uɑ\u0303˞'], eng: ['ə\u0303˞', 'ə\u0303˞', 'ə\u0303˞'],
103
+ ing: ['iə\u0303˞', 'iə\u0303˞', 'iə\u0303˞'], ueng: ['uə\u0303˞', 'uə\u0303˞', 'uə\u0303˞'],
104
+ ong: ['u\u0303˞', 'u\u0303˞', 'ʊ\u0303˞'], iong: ['yu\u0303˞', 'yu\u0303˞', 'iʊ\u0303˞'],
105
+ er: ['ɚ˞', 'ɚ˞', 'ɚ˞']
106
+ };
107
+ function pickTriplet(t, preset, narrow) {
108
+ return narrow ? t[2] : preset === 'broad' ? t[1] : t[0];
109
+ }
110
+ // ---------------------------------------------------------------------------
111
+ // Syllable Parsing & Formatting
112
+ // ---------------------------------------------------------------------------
113
+ export function countPinyinToneDiacritics(input) {
114
+ const s = input.normalize('NFC').toLowerCase();
115
+ let count = 0;
116
+ for (let i = 0; i < s.length; i++) {
117
+ const ch = s[i];
118
+ if (ch === '\u0304' || ch === '\u030c') {
119
+ count++;
120
+ continue;
121
+ }
122
+ const info = TONE_CHAR_INFO[ch];
123
+ if (info && info.tone !== 0) {
124
+ count++;
125
+ }
126
+ }
127
+ return count;
128
+ }
129
+ export function parsePinyinSyllable(input) {
130
+ const raw = input.trim();
131
+ if (!raw) {
132
+ return { raw, initial: '', final: '', bare: '', tone: 0, isErhua: false, isValid: false };
133
+ }
134
+ let s = raw.normalize('NFC').toLowerCase().replace(/u:/g, 'v');
135
+ let tone = 0;
136
+ let trailingR = false;
137
+ if (s.length > 3 && /[0-5]'r$/.test(s)) {
138
+ trailingR = true;
139
+ const d = Number(s[s.length - 3]);
140
+ tone = (d >= 1 && d <= 4 ? d : 0);
141
+ s = s.slice(0, -3);
142
+ }
143
+ else if (s.length > 2 && /[0-5]r$/.test(s)) {
144
+ trailingR = true;
145
+ const d = Number(s[s.length - 2]);
146
+ tone = (d >= 1 && d <= 4 ? d : 0);
147
+ s = s.slice(0, -2);
148
+ }
149
+ else if (/[0-5]$/.test(s)) {
150
+ const d = Number(s[s.length - 1]);
151
+ tone = (d >= 1 && d <= 4 ? d : 0);
152
+ s = s.slice(0, -1);
153
+ }
154
+ s = s.replace(/ê\u0304/g, () => ((tone = 1), 'ê')).replace(/ê\u030c/g, () => ((tone = 3), 'ê'));
155
+ let decoded = '';
156
+ for (const ch of s) {
157
+ const info = TONE_CHAR_INFO[ch];
158
+ if (info) {
159
+ decoded += info.base;
160
+ if (info.tone !== 0 && tone === 0)
161
+ tone = info.tone;
162
+ }
163
+ else {
164
+ decoded += ch;
165
+ }
166
+ }
167
+ decoded = decoded.replace(/^'+|'+$/g, '');
168
+ let isErhua = trailingR;
169
+ if (!isErhua && decoded.length > 2 && decoded.endsWith("'r")) {
170
+ isErhua = true;
171
+ decoded = decoded.slice(0, -2);
172
+ }
173
+ else if (!isErhua && decoded.length > 1 && decoded !== 'er' && decoded.endsWith('r')) {
174
+ isErhua = true;
175
+ decoded = decoded.slice(0, -1);
176
+ }
177
+ if (!decoded) {
178
+ return { raw, initial: '', final: '', bare: '', tone, isErhua, isValid: false };
179
+ }
180
+ if (INTERJECTIONS.has(decoded)) {
181
+ return {
182
+ raw,
183
+ initial: decoded.startsWith('h') ? 'h' : '',
184
+ final: decoded.startsWith('h') ? decoded.slice(1) : decoded,
185
+ bare: decoded,
186
+ tone,
187
+ isErhua,
188
+ isValid: true
189
+ };
190
+ }
191
+ let initial = '';
192
+ let final = decoded;
193
+ if (decoded.startsWith('zh') || decoded.startsWith('ch') || decoded.startsWith('sh')) {
194
+ initial = decoded.slice(0, 2);
195
+ final = decoded.slice(2);
196
+ }
197
+ else if (decoded.length >= 2 && ONE_CHAR_INITIALS.has(decoded[0])) {
198
+ initial = decoded[0];
199
+ final = decoded.slice(1);
200
+ }
201
+ if (PALATALS.has(initial) && final.startsWith('u')) {
202
+ final = 'v' + final.slice(1);
203
+ }
204
+ return { raw, initial, final, bare: decoded, tone, isErhua, isValid: VALID_FINALS.has(final) };
205
+ }
206
+ export function formatPinyinSyllable(parsed, format, neutralMark = 'none') {
207
+ const { bare, tone, isErhua } = parsed;
208
+ if (!bare)
209
+ return '';
210
+ const orthoBare = /^[jqx]v/.test(bare) ? bare[0] + 'u' + bare.slice(2) : bare;
211
+ // Distinguish zero-initial e + erhua (e.g. 鹅儿 -> é'r) from independent syllable er (儿 -> ér)
212
+ const rSuffix = isErhua ? (orthoBare === 'e' ? "'r" : 'r') : '';
213
+ if (format === 'toneless')
214
+ return orthoBare.replace(/v/g, 'ü') + rSuffix;
215
+ if (format === 'tone-numbers' || format === 'tone-numbers-v') {
216
+ const base = format === 'tone-numbers-v' ? orthoBare : orthoBare.replace(/v/g, 'ü');
217
+ const digit = tone >= 1 && tone <= 4 ? String(tone) : neutralMark === 'zero' ? '0' : '';
218
+ return base + rSuffix + digit;
219
+ }
220
+ if (tone === 0)
221
+ return orthoBare.replace(/v/g, 'ü') + rSuffix;
222
+ const chars = Array.from(orthoBare);
223
+ let targetIdx = -1;
224
+ for (const v of ['a', 'o', 'e', 'ê']) {
225
+ const idx = chars.indexOf(v);
226
+ if (idx !== -1) {
227
+ targetIdx = idx;
228
+ break;
229
+ }
230
+ }
231
+ if (targetIdx === -1) {
232
+ const iu = orthoBare.indexOf('iu');
233
+ const ui = orthoBare.indexOf('ui');
234
+ if (iu !== -1)
235
+ targetIdx = iu + 1;
236
+ else if (ui !== -1)
237
+ targetIdx = ui + 1;
238
+ }
239
+ if (targetIdx === -1) {
240
+ for (const v of ['i', 'u', 'v', 'n', 'm']) {
241
+ const idx = chars.indexOf(v);
242
+ if (idx !== -1) {
243
+ targetIdx = idx;
244
+ break;
245
+ }
246
+ }
247
+ }
248
+ if (targetIdx !== -1 && VOWEL_TONE_MARKS[chars[targetIdx]]) {
249
+ chars[targetIdx] = VOWEL_TONE_MARKS[chars[targetIdx]][tone];
250
+ }
251
+ return chars.join('').replace(/v/g, 'ü') + rSuffix;
252
+ }
253
+ // ---------------------------------------------------------------------------
254
+ // IPA Tone Suffixes, Initials & Finals
255
+ // ---------------------------------------------------------------------------
256
+ const TONE_TABLES = {
257
+ 'chao-contour': ['', '˥˥', '˧˥', '˨˩˦', '˥˩'],
258
+ 'chao-single': ['', '˥', '˧˥', '˨˩˦', '˥˩'],
259
+ superscript: ['', '⁵⁵', '³⁵', '²¹⁴', '⁵¹'],
260
+ digits: ['', '55', '35', '214', '51']
261
+ };
262
+ const CHAO_NEUTRAL_DOTS = { 0: '꜋', 1: '꜋', 2: '꜊', 3: '꜉', 4: '꜌' };
263
+ export function formatIpaTone(tone, style, neutralMark = 'none', isHalfThird = false, prevTone) {
264
+ if (style === 'none')
265
+ return '';
266
+ if (tone === 0) {
267
+ if (neutralMark === 'none')
268
+ return '';
269
+ if (neutralMark === 'zero')
270
+ return style === 'superscript' ? '⁰' : '0';
271
+ return style === 'chao-contour' || style === 'chao-single'
272
+ ? CHAO_NEUTRAL_DOTS[prevTone ?? 0]
273
+ : '·';
274
+ }
275
+ if (tone === 3 && isHalfThird) {
276
+ return style === 'superscript' ? '²¹' : style === 'digits' ? '21' : '˨˩';
277
+ }
278
+ return TONE_TABLES[style][tone];
279
+ }
280
+ const SIMPLE_INITIAL_IPA = {
281
+ b: 'p', p: 'pʰ', m: 'm', f: 'f', d: 't', t: 'tʰ', n: 'n', l: 'l',
282
+ g: 'k', k: 'kʰ', h: 'x', x: 'ɕ', s: 's', sh: 'ʂ'
283
+ };
284
+ const VOICED_NEUTRAL_SIMPLE_INITIAL_IPA = {
285
+ b: 'b',
286
+ d: 'd',
287
+ g: 'ɡ'
288
+ };
289
+ function mapInitialToIpa(initial, g, voiceInNeutral = false) {
290
+ if (!initial)
291
+ return '';
292
+ if (voiceInNeutral && VOICED_NEUTRAL_SIMPLE_INITIAL_IPA[initial]) {
293
+ return VOICED_NEUTRAL_SIMPLE_INITIAL_IPA[initial];
294
+ }
295
+ if (SIMPLE_INITIAL_IPA[initial])
296
+ return SIMPLE_INITIAL_IPA[initial];
297
+ const tie = g.tieBar ? '\u0361' : '';
298
+ const retro = g.retroflex === 'ʈʂ' ? 'ʈ' : 't';
299
+ const voicedRetro = g.retroflex === 'ʈʂ' ? 'ɖ' : 'd';
300
+ switch (initial) {
301
+ case 'j': return voiceInNeutral ? `d${tie}ʑ` : `t${tie}ɕ`;
302
+ case 'q': return `t${tie}ɕʰ`;
303
+ case 'z': return voiceInNeutral ? `d${tie}z` : `t${tie}s`;
304
+ case 'c': return `t${tie}sʰ`;
305
+ case 'zh': return voiceInNeutral ? `${voicedRetro}${tie}ʐ` : `${retro}${tie}ʂ`;
306
+ case 'ch': return `${retro}${tie}ʂʰ`;
307
+ case 'r': return g.rInitial;
308
+ default: return initial;
309
+ }
310
+ }
311
+ function mapApicalVowel(isRetroflex, style) {
312
+ if (style === 'sinological')
313
+ return isRetroflex ? 'ʅ' : 'ɿ';
314
+ if (style === 'strict-fricative')
315
+ return isRetroflex ? 'ʐ\u0329' : 'z\u0329';
316
+ return isRetroflex ? 'ɻ\u0329' : 'ɹ\u0329';
317
+ }
318
+ function mapFinalToIpa(parsed, options) {
319
+ const { initial, final, tone, isErhua } = parsed;
320
+ const { preset, granular, zeroInitial } = options;
321
+ const narrow = granular.narrowVowels;
322
+ const glottal = initial === '' && zeroInitial === 'glottal' && GLOTTAL_ELIGIBLE_FINALS.has(final) ? 'ʔ' : '';
323
+ if (final === 'm')
324
+ return 'm\u0329';
325
+ if (final === 'n')
326
+ return 'n\u0329';
327
+ if (final === 'ng')
328
+ return 'ŋ\u030d';
329
+ if (final === 'ê')
330
+ return glottal + 'ɛ';
331
+ // Apical vowels
332
+ const isAlvApical = final === 'i' && ALVEOLARS.has(initial);
333
+ const isRetApical = final === 'i' && RETROFLEXES.has(initial);
334
+ if (isAlvApical || isRetApical) {
335
+ return isErhua ? 'ə˞' : mapApicalVowel(isRetApical, granular.apicalVowel);
336
+ }
337
+ // Labial b/p/m/f + o
338
+ if (final === 'o' && LABIALS.has(initial)) {
339
+ const base = pickTriplet(['uo', 'o', 'uɔ'], preset, narrow);
340
+ return isErhua ? base + '˞' : base;
341
+ }
342
+ // Erhua rhotacization
343
+ if (isErhua) {
344
+ if (initial === '' && ERHUA_YW_FINALS[final]) {
345
+ return pickTriplet(ERHUA_YW_FINALS[final], preset, narrow);
346
+ }
347
+ if (ERHUA_CORE_FINALS[final]) {
348
+ return glottal + pickTriplet(ERHUA_CORE_FINALS[final], preset, narrow);
349
+ }
350
+ return glottal + final + '˞';
351
+ }
352
+ // Tone-conditioned mid-vowel compression in strict narrow mode (T1/T2 compress; T3/T4/T0 preserve)
353
+ if (narrow && (tone === 1 || tone === 2)) {
354
+ if (initial === '' && final === 'you')
355
+ return 'ju';
356
+ if (initial === '' && final === 'wei')
357
+ return 'wi';
358
+ if (initial === '' && final === 'wen')
359
+ return 'wun';
360
+ if (final === 'iu' || final === 'iou')
361
+ return 'iu';
362
+ if (final === 'ui' || final === 'uei')
363
+ return 'ui';
364
+ if (final === 'un' || final === 'uen')
365
+ return 'un';
366
+ }
367
+ if (initial === '' && ZERO_YW_FINALS[final]) {
368
+ return pickTriplet(ZERO_YW_FINALS[final], preset, narrow);
369
+ }
370
+ if (CORE_FINALS[final]) {
371
+ return glottal + pickTriplet(CORE_FINALS[final], preset, narrow);
372
+ }
373
+ return glottal + final;
374
+ }
375
+ export function pinyinSyllableToIpa(input, options) {
376
+ const parsed = typeof input === 'string' ? parsePinyinSyllable(input) : input;
377
+ if (!parsed.isValid)
378
+ return typeof input === 'string' ? input : parsed.raw;
379
+ // For ri in strict-fricative / strict-approximant modes, coordinate initial + apical vowel
380
+ // into a single syllabic segment (ʐ̩ or ɻ̩) to avoid duplicate ʐʐ̩/ɻɻ̩ or contradictory ɻʐ̩/ʐɻ̩.
381
+ const isCoordinatedStrictRi = parsed.initial === 'r' &&
382
+ parsed.final === 'i' &&
383
+ !parsed.isErhua &&
384
+ options.granular.apicalVowel !== 'sinological';
385
+ const voiceInNeutral = parsed.tone === 0 && Boolean(options.neutralTone.voicingInNeutral);
386
+ const initialIpa = isCoordinatedStrictRi
387
+ ? ''
388
+ : mapInitialToIpa(parsed.initial, options.granular, voiceInNeutral);
389
+ let finalIpa = mapFinalToIpa(parsed, options);
390
+ if (parsed.tone === 0 && options.neutralTone.reduceVowel && !parsed.isErhua) {
391
+ if (parsed.final === 'e')
392
+ finalIpa = finalIpa.replace('ɤ', 'ə');
393
+ else if (parsed.final === 'a' || parsed.final === 'ya' || parsed.final === 'wa') {
394
+ finalIpa = finalIpa.replace(/a$/, 'ɐ');
395
+ }
396
+ }
397
+ const toneSuffix = formatIpaTone(parsed.tone, options.toneStyle, options.neutralTone.mark, Boolean(options.isHalfThird), options.prevTone);
398
+ const out = initialIpa + finalIpa + toneSuffix;
399
+ return options.granular.scriptG ? out.replace(/g/g, 'ɡ') : out.replace(/ɡ/g, 'g');
400
+ }
401
+ export function buildIpaOptionsFromConfig(config, extra) {
402
+ const resolved = createDefaultMandarinConfig(config);
403
+ return {
404
+ preset: resolved.ipaPreset,
405
+ toneStyle: resolved.ipaToneStyle,
406
+ neutralTone: resolved.neutralTone,
407
+ zeroInitial: resolved.zeroInitial,
408
+ granular: resolved.ipaGranular,
409
+ isHalfThird: extra?.isHalfThird,
410
+ prevTone: extra?.prevTone
411
+ };
412
+ }
@@ -0,0 +1,23 @@
1
+ import { buildIpaOptionsFromConfig, formatIpaTone, formatPinyinSyllable, parsePinyinSyllable, pinyinSyllableToIpa } from './ipaRules.js';
2
+ import { applyMandarinSandhi, type SandhiSyllableInput, type SandhiSyllableOutput } from './sandhi.js';
3
+ import { createDefaultMandarinConfig, DEFAULT_MANDARIN_CONFIG, getPresetIpaGranular, type AcousticSegmentClass, type ApicalVowelStyle, type ConvertedSyllableUnit, type ConvertPinyinResult, type ConvertTextOptions, type ConvertTextResult, type DigitMode, type ErhuaMode, type IpaConversionOptions, type IpaGranularConfig, type IpaPreset, type IpaToneStyle, type LatinInIpaMode, type MandarinConvertOptions, type MandarinIpaConfig, type MandarinIpaConfigOverrides, type MandarinTone, type NeutralToneConfig, type NeutralToneMark, type ParsedPinyinSyllable, type PinyinFormat, type PolyphoneCandidate, type PolyphoneOverrideMap, type PunctuationMode, type RetroflexStyle, type RInitialStyle, type SandhiConfig, type SyllableUnitKind, type ZeroInitialMode } from './types.js';
4
+ export { applyMandarinSandhi, buildIpaOptionsFromConfig, createDefaultMandarinConfig, DEFAULT_MANDARIN_CONFIG, formatIpaTone, formatPinyinSyllable, getPresetIpaGranular, parsePinyinSyllable, pinyinSyllableToIpa };
5
+ export type { AcousticSegmentClass, ApicalVowelStyle, ConvertedSyllableUnit, ConvertPinyinResult, ConvertTextOptions, ConvertTextResult, DigitMode, ErhuaMode, IpaConversionOptions, IpaGranularConfig, IpaPreset, IpaToneStyle, LatinInIpaMode, MandarinConvertOptions, MandarinIpaConfig, MandarinIpaConfigOverrides, MandarinTone, NeutralToneConfig, NeutralToneMark, ParsedPinyinSyllable, PinyinFormat, PolyphoneCandidate, PolyphoneOverrideMap, PunctuationMode, RetroflexStyle, RInitialStyle, SandhiConfig, SandhiSyllableInput, SandhiSyllableOutput, SyllableUnitKind, ZeroInitialMode };
6
+ export declare function isWholeSilenceTag(text: string): boolean;
7
+ export declare function applyPunctuationModeToUnits(units: readonly ConvertedSyllableUnit[], mode: PunctuationMode): ConvertedSyllableUnit[];
8
+ export interface ExplicitToneParse {
9
+ tone: MandarinTone;
10
+ isHalfThird: boolean;
11
+ }
12
+ export declare function parseExplicitChaoTone(str: string): ExplicitToneParse | null;
13
+ export declare function parseExplicitMultiDigitChao(str: string): ExplicitToneParse | null;
14
+ /**
15
+ * Zero-dependency multi-syllable Pinyin phrase/sentence converter.
16
+ * Parses space-, apostrophe-, or connected multi-syllable Pinyin strings,
17
+ * runs prosodic tone sandhi (`applyMandarinSandhi`), and formats both Pinyin and IPA.
18
+ */
19
+ export declare function convertPinyinSequence(pinyinText: string, overrides?: MandarinIpaConfigOverrides): ConvertPinyinResult;
20
+ /**
21
+ * Convenience zero-dependency helper returning only the converted IPA string for a Pinyin sequence.
22
+ */
23
+ export declare function pinyinToIpa(pinyinText: string, overrides?: MandarinIpaConfigOverrides): string;