| import TshetUinh from "tshet-uinh"; |
| import * as Examples from "tshet-uinh-examples"; |
|
|
| function tokensFromAnnotatedText(text, derivePron) { |
| const tokens = []; |
| const re = /([\s\S])\(([^)]+)\)/g; |
| let lastIndex = 0; |
| for (let m; (m = re.exec(text)); ) { |
| const start = m.index; |
| if (start > lastIndex) { |
| tokens.push({ type: "raw", text: text.slice(lastIndex, start) }); |
| } |
| const ch = m[1]; |
| const desc = m[2]; |
| let pron = ""; |
| try { |
| const pos = TshetUinh.音韻地位.from描述(desc); |
| pron = derivePron(pos, ch) ?? ""; |
| } catch { |
| |
| pron = ""; |
| } |
| tokens.push({ type: "annotated", ch, desc, pron }); |
| lastIndex = start + m[0].length; |
| } |
| if (lastIndex < text.length) tokens.push({ type: "raw", text: text.slice(lastIndex) }); |
| return tokens; |
| } |
|
|
| function choosePositionByChar(ch) { |
| const entries = TshetUinh.資料.query字頭(ch); |
| if (!entries || !entries.length) return null; |
| |
| const guangyun = entries.find(e => e?.來源?.文獻 === "廣韻"); |
| return (guangyun || entries[0]).音韻地位 || null; |
| } |
|
|
| function tokensFromCharText(text, derivePron) { |
| const tokens = []; |
| let rawBuf = ""; |
|
|
| const flushRaw = () => { |
| if (!rawBuf) return; |
| tokens.push({ type: "raw", text: rawBuf }); |
| rawBuf = ""; |
| }; |
|
|
| for (const ch of Array.from(text)) { |
| const pos = choosePositionByChar(ch); |
| if (!pos) { |
| rawBuf += ch; |
| continue; |
| } |
| flushRaw(); |
| let pron = ""; |
| try { |
| pron = derivePron(pos, ch) ?? ""; |
| } catch { |
| pron = ""; |
| } |
| tokens.push({ type: "annotated", ch, desc: pos.描述, pron }); |
| } |
| flushRaw(); |
| return tokens; |
| } |
|
|
| function getDeriver(scheme) { |
| return getDeriverWithOptions(scheme, undefined); |
| } |
|
|
| function getDeriverWithOptions(scheme, options) { |
| if (scheme === "tupa") return Examples.tupa(options ?? {}); |
| if (scheme === "high_tang") return Examples.high_tang(options ?? {}); |
| if (scheme === "unt") return Examples.unt(options ?? {}); |
| if (scheme === "unt_legacy") return Examples.unt_legacy(options ?? {}); |
| if (scheme === "putonghua") return Examples.putonghua(options ?? {}); |
| if (scheme === "gwongzau") return Examples.gwongzau(options ?? {}); |
| if (scheme === "zaonhe") return Examples.zaonhe(options ?? {}); |
| throw new Error(`Unsupported scheme: ${scheme}`); |
| } |
|
|
| export function derive({ scheme = "tupa", text = "", options = undefined } = {}) { |
| const s = scheme.toString(); |
| const t = text.toString(); |
| if (s === "tupa_high_tang") { |
| const deriveTupa = getDeriverWithOptions("tupa", undefined); |
| const deriveHighTang = getDeriverWithOptions("high_tang", undefined); |
| const tokens = /[\s\S]\([^)]+\)/.test(t) |
| ? |
| (() => { |
| const base = tokensFromAnnotatedText(t, () => ""); |
| return base.map(tok => { |
| if (tok.type !== "annotated") return tok; |
| const pos = TshetUinh.音韻地位.from描述(tok.desc); |
| const tupa = (deriveTupa(pos, tok.ch) ?? "").trim(); |
| const ipa = (deriveHighTang(pos, tok.ch) ?? "").trim(); |
| return { ...tok, pron: `${tupa} /${ipa}/` }; |
| }); |
| })() |
| : |
| (() => { |
| const tokens = []; |
| let rawBuf = ""; |
| const flushRaw = () => { |
| if (!rawBuf) return; |
| tokens.push({ type: "raw", text: rawBuf }); |
| rawBuf = ""; |
| }; |
| for (const ch of Array.from(t)) { |
| const pos = choosePositionByChar(ch); |
| if (!pos) { |
| rawBuf += ch; |
| continue; |
| } |
| flushRaw(); |
| const tupa = (deriveTupa(pos, ch) ?? "").trim(); |
| const ipa = (deriveHighTang(pos, ch) ?? "").trim(); |
| tokens.push({ type: "annotated", ch, desc: pos.描述, pron: `${tupa} /${ipa}/` }); |
| } |
| flushRaw(); |
| return tokens; |
| })(); |
| return { scheme: s, tokens }; |
| } |
|
|
| const derivePron = getDeriverWithOptions(s, options); |
| |
| |
| |
| const tokens = /[\s\S]\([^)]+\)/.test(t) ? tokensFromAnnotatedText(t, derivePron) : tokensFromCharText(t, derivePron); |
| return { scheme: s, tokens }; |
| } |
|
|