cortico-world-agent-speed 0.2.0 → 0.2.2
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/CHANGELOG.md +64 -0
- package/LICENSE +0 -14
- package/README.md +29 -11
- package/THIRD_PARTY_NOTICES.md +69 -0
- package/activity.d.ts +15 -0
- package/activity.js +88 -3
- package/describe.json +806 -0
- package/dist/webui.css +2 -0
- package/dist/webui.css.map +7 -0
- package/dist/webui.js +2 -0
- package/dist/webui.js.map +7 -0
- package/host/app-store.cjs +68 -0
- package/host/cursor.cjs +67 -0
- package/host/electron-main.cjs +666 -0
- package/host/input-shape.cjs +63 -0
- package/host/mouse-lease.cjs +77 -0
- package/host/overlay.cjs +139 -0
- package/host/portrait.cjs +52 -0
- package/host/preload.cjs +60 -14
- package/host/renderer-gone.cjs +24 -0
- package/host/windows.cjs +251 -0
- package/package.json +27 -7
- package/scripts/build-webui.mjs +40 -0
- package/scripts/speech-bank/build.py +166 -0
- package/scripts/speech-bank/dicts.py +76 -0
- package/src/ENV_PROMPT.en.md +51 -0
- package/src/ENV_PROMPT.md +51 -0
- package/src/asr/address.ts +25 -0
- package/src/asr/hotkey.ts +335 -0
- package/src/asr/result.ts +51 -0
- package/src/asr/segmenter.ts +269 -0
- package/src/asr/sherpa.ts +280 -0
- package/src/asr/simplify.ts +38 -0
- package/src/asr/system-recognizer.ts +292 -0
- package/src/asr/system-sapi.ps1 +173 -0
- package/src/asr/transcription-api.ts +176 -0
- package/src/chat.ts +212 -0
- package/src/config.ts +481 -0
- package/src/cua/ENV_PROMPT.en.md +29 -0
- package/src/cua/ENV_PROMPT.md +29 -0
- package/src/cua/config.ts +88 -0
- package/src/cua/cua.ts +712 -0
- package/src/cua/engine/darwin.ts +410 -0
- package/src/cua/engine/display.ts +55 -0
- package/src/cua/engine/fail.ts +15 -0
- package/src/cua/engine/foreground.ts +8 -0
- package/src/cua/engine/image.ts +57 -0
- package/src/cua/engine/keys.ts +70 -0
- package/src/cua/engine/linux-keys.ts +29 -0
- package/src/cua/engine/linux.ts +343 -0
- package/src/cua/engine/mac-keys.ts +57 -0
- package/src/cua/engine/user-activity.ts +54 -0
- package/src/cua/engine/wait-idle.ts +31 -0
- package/src/cua/engine/win32.ts +299 -0
- package/src/cua/engine-child.ts +274 -0
- package/src/cua/engine-ipc.ts +140 -0
- package/src/cua/i18n/de.ts +53 -0
- package/src/cua/i18n/en.ts +63 -0
- package/src/cua/i18n/es-419.ts +53 -0
- package/src/cua/i18n/fr.ts +53 -0
- package/src/cua/i18n/index.ts +26 -0
- package/src/cua/i18n/it.ts +53 -0
- package/src/cua/i18n/ja.ts +53 -0
- package/src/cua/i18n/ko.ts +53 -0
- package/src/cua/i18n/pt-BR.ts +53 -0
- package/src/cua/i18n/ru.ts +59 -0
- package/src/cua/i18n/zh-Hant.ts +53 -0
- package/src/cua/i18n/zh.ts +59 -0
- package/src/cua/model-text.ts +252 -0
- package/src/cua/terminal.ts +31 -0
- package/src/cua/tools.ts +153 -0
- package/src/definition.ts +146 -0
- package/src/host-reason.ts +32 -0
- package/src/i18n/de.ts +409 -0
- package/src/i18n/en.ts +443 -0
- package/src/i18n/es-419.ts +409 -0
- package/src/i18n/fr.ts +409 -0
- package/src/i18n/index.ts +56 -0
- package/src/i18n/it.ts +409 -0
- package/src/i18n/ja.ts +409 -0
- package/src/i18n/ko.ts +409 -0
- package/src/i18n/pt-BR.ts +409 -0
- package/src/i18n/ru.ts +415 -0
- package/src/i18n/zh-Hant.ts +409 -0
- package/src/i18n/zh.ts +440 -0
- package/src/index.ts +15 -0
- package/src/model-text.ts +344 -0
- package/src/moves.ts +101 -0
- package/src/pack-import.ts +361 -0
- package/src/packs.ts +449 -0
- package/src/runtime/download.ts +29 -0
- package/src/runtime/store.ts +293 -0
- package/src/script.ts +202 -0
- package/src/self.ts +247 -0
- package/src/server.ts +479 -0
- package/src/speech/data/CMUDICT-LICENSE +33 -0
- package/src/speech/data/IPADIC-COPYING +71 -0
- package/src/speech/data/UNICODE-LICENSE +39 -0
- package/src/speech/data/en.tsv.gz +0 -0
- package/src/speech/data/ja-ipadic.tsv.gz +0 -0
- package/src/speech/data/ja-unihan.tsv.gz +0 -0
- package/src/speech/data/zh.json +1 -0
- package/src/speech/index.ts +204 -0
- package/src/speech/ipa.ts +83 -0
- package/src/speech/japanese.ts +105 -0
- package/src/speech/jukujikun.ts +22 -0
- package/src/speech/korean.ts +35 -0
- package/src/speech/lang.ts +151 -0
- package/src/speech/rules.ts +72 -0
- package/src/speech/syllable.ts +76 -0
- package/src/status.ts +164 -0
- package/src/tools.ts +110 -0
- package/src/webui/api-rows.css +2 -0
- package/src/webui/api-rows.ts +44 -0
- package/src/webui/client.ts +451 -0
- package/src/webui/dress-dialog.css +14 -0
- package/src/webui/dress-dialog.ts +117 -0
- package/src/webui/dress-panel.ts +160 -0
- package/src/webui/key-capture.ts +60 -0
- package/src/webui/shared/modal.css +13 -0
- package/src/webui/shared/modal.ts +77 -0
- package/src/webui/shared/problem.css +15 -0
- package/src/webui/shared/problem.ts +217 -0
- package/src/webui/shared/strings.ts +5 -0
- package/src/webui/style.css +50 -0
- package/src/window-host.ts +116 -0
- package/src/world.ts +2926 -0
- package/web/activity-motion.js +117 -0
- package/web/appearance.js +60 -0
- package/web/body-host.js +257 -0
- package/web/coo/coo.js +384 -0
- package/web/coo/figure.js +15 -0
- package/web/coo/figure.json +1224 -0
- package/web/dist/dress-dialog.css +1 -0
- package/web/dist/dress-dialog.js +1 -0
- package/web/drag-row.js +49 -0
- package/web/drag.js +149 -0
- package/web/dress.css +170 -0
- package/web/dress.html +37 -0
- package/web/dress.js +786 -0
- package/web/figure-frame.html +16 -0
- package/web/figure-frame.js +128 -0
- package/web/frame-loop.js +55 -0
- package/web/hints.js +77 -0
- package/web/i18n/de.js +170 -0
- package/web/i18n/en.js +177 -0
- package/web/i18n/es-419.js +170 -0
- package/web/i18n/fr.js +170 -0
- package/web/i18n/it.js +170 -0
- package/web/i18n/ja.js +170 -0
- package/web/i18n/ko.js +170 -0
- package/web/i18n/pt-BR.js +170 -0
- package/web/i18n/ru.js +170 -0
- package/web/i18n/zh-Hant.js +170 -0
- package/web/i18n/zh.js +175 -0
- package/web/i18n.js +54 -0
- package/{renderer/vendor → web}/kit/body.js +31 -58
- package/web/looks.js +70 -0
- package/web/mic-worklet.js +36 -0
- package/web/mic.js +117 -0
- package/web/motion-frames.js +80 -0
- package/web/motion-level.js +40 -0
- package/web/new-year-2027.svg +238 -0
- package/web/overlay.html +16 -0
- package/web/overlay.js +100 -0
- package/web/pet-app.js +3000 -0
- package/web/pet.css +642 -0
- package/web/pet.html +21 -0
- package/web/scale-slider.js +259 -0
- package/web/show-hide.js +49 -0
- package/web/sound.js +245 -0
- package/web/speech/zh.json +1 -0
- package/web/speech/zh.ogg +0 -0
- package/web/speech.js +197 -0
- package/web/stage.js +160 -0
- package/web/swap-motion.js +119 -0
- package/web/ui.js +195 -0
- package/host/launch.js +0 -187
- package/host/library.cjs +0 -638
- package/host/main.cjs +0 -389
- package/index.js +0 -93
- package/renderer/index.html +0 -22
- package/renderer/pet-sandbox.html +0 -11
- package/renderer/pet.css +0 -38
- package/renderer/pet.js +0 -251
- package/renderer/vendor/activity-motion.js +0 -101
- package/renderer/vendor/figure-frame.js +0 -115
- package/renderer/vendor/pack.css +0 -2
- /package/{renderer/vendor → web}/kit/rig.js +0 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Japanese to IPA. Kanji are read by the longest word that starts at them in `JapaneseReadings.word` (the Jōyō
|
|
3
|
+
* appendix's jukujikun, then IPADIC's words); a kanji in no word takes its own reading (Unihan's): next to another
|
|
4
|
+
* kanji its first on'yomi, alone its kun'yomi cut where the kana after it (okurigana) begin to match it, else its
|
|
5
|
+
* first kun'yomi. Kana are read by mora: small ゃゅょ palatalize the mora before them, っ adds no syllable, and
|
|
6
|
+
* う or ー after an o-mora and い or ー after an e-mora lengthen it into ou and ei.
|
|
7
|
+
*/
|
|
8
|
+
import type { Ph } from './rules.ts';
|
|
9
|
+
|
|
10
|
+
const ROWS: Array<[string, string[]]> = [
|
|
11
|
+
['あいうえお', ['a', 'i', 'ɯ', 'e', 'o']],
|
|
12
|
+
['かきくけこ', ['k a', 'k i', 'k ɯ', 'k e', 'k o']], ['がぎぐげご', ['ɡ a', 'ɡ i', 'ɡ ɯ', 'ɡ e', 'ɡ o']],
|
|
13
|
+
['さしすせそ', ['s a', 'ɕ i', 's ɯ', 's e', 's o']], ['ざじずぜぞ', ['z a', 'dʑ i', 'z ɯ', 'z e', 'z o']],
|
|
14
|
+
['たちつてと', ['t a', 'tɕ i', 'ts ɯ', 't e', 't o']], ['だぢづでど', ['d a', 'dʑ i', 'z ɯ', 'd e', 'd o']],
|
|
15
|
+
['なにぬねの', ['n a', 'ɲ i', 'n ɯ', 'n e', 'n o']], ['はひふへほ', ['h a', 'ç i', 'ɸ ɯ', 'h e', 'h o']],
|
|
16
|
+
['ばびぶべぼ', ['b a', 'b i', 'b ɯ', 'b e', 'b o']], ['ぱぴぷぺぽ', ['p a', 'p i', 'p ɯ', 'p e', 'p o']],
|
|
17
|
+
['まみむめも', ['m a', 'm i', 'm ɯ', 'm e', 'm o']], ['らりるれろ', ['ɾ a', 'ɾ i', 'ɾ ɯ', 'ɾ e', 'ɾ o']],
|
|
18
|
+
['やゆよ', ['j a', 'j ɯ', 'j o']], ['わゐゑを', ['w a', 'i', 'e', 'o']], ['ゔ', ['v ɯ']], ['ん', ['N']],
|
|
19
|
+
];
|
|
20
|
+
const MORA = new Map<string, string[]>();
|
|
21
|
+
for (const [kana, ps] of ROWS) [...kana].forEach((k, i) => MORA.set(k, ps[i]!.split(' ')));
|
|
22
|
+
const SMALL_Y: Record<string, string> = { 'ゃ': 'a', 'ゅ': 'ɯ', 'ょ': 'o' };
|
|
23
|
+
const SMALL_V: Record<string, string> = { 'ぁ': 'a', 'ぃ': 'i', 'ぅ': 'ɯ', 'ぇ': 'e', 'ぉ': 'o' };
|
|
24
|
+
const PALATAL = new Set(['ɕ', 'dʑ', 'tɕ', 'ç', 'ɲ']);
|
|
25
|
+
|
|
26
|
+
/** Katakana to hiragana; anything else unchanged. */
|
|
27
|
+
export const hira = (ch: string) => {
|
|
28
|
+
const cp = ch.codePointAt(0)!;
|
|
29
|
+
return cp >= 0x30a1 && cp <= 0x30f6 ? String.fromCodePoint(cp - 0x60) : ch;
|
|
30
|
+
};
|
|
31
|
+
export const isKana = (ch: string) => /[ぁ-ゖァ-ヺー]/.test(ch);
|
|
32
|
+
export const isKanji = (ch: string) => /[㐀-䶿一-鿿々]/.test(ch);
|
|
33
|
+
|
|
34
|
+
/** Kana (each with the text index it came from) to phonemes. */
|
|
35
|
+
export function kanaPhonemes(kana: ReadonlyArray<readonly [string, number]>): Ph[] {
|
|
36
|
+
const out: Ph[] = [];
|
|
37
|
+
let lastVowel = -1;
|
|
38
|
+
for (const [raw, at] of kana) {
|
|
39
|
+
const k = hira(raw);
|
|
40
|
+
if (k === 'っ') continue;
|
|
41
|
+
if (k === 'ー') {
|
|
42
|
+
const v = lastVowel >= 0 ? out[lastVowel]!.p : '';
|
|
43
|
+
if (v === 'o' || v === 'e') out[lastVowel]!.p = v === 'o' ? 'ou' : 'ei';
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (SMALL_Y[k] && lastVowel >= 0) {
|
|
47
|
+
const v = out[lastVowel]!;
|
|
48
|
+
const cons = out[lastVowel - 1];
|
|
49
|
+
v.p = SMALL_Y[k]!;
|
|
50
|
+
if (cons && !PALATAL.has(cons.p)) { out.splice(lastVowel, 0, { p: 'j', at: cons.at }); lastVowel++; }
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
if (SMALL_V[k] && lastVowel >= 0) { out[lastVowel]!.p = SMALL_V[k]!; continue; }
|
|
54
|
+
const prev = lastVowel >= 0 ? out[lastVowel]!.p : '';
|
|
55
|
+
if ((k === 'う' && (prev === 'o' || prev === 'ɯ')) || (k === 'い' && prev === 'e')) {
|
|
56
|
+
out[lastVowel]!.p = prev === 'o' ? 'ou' : prev === 'e' ? 'ei' : 'ɯː';
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
const m = MORA.get(k);
|
|
60
|
+
if (!m) continue;
|
|
61
|
+
for (const p of m) out.push({ p, at });
|
|
62
|
+
lastVowel = m.at(-1) === 'N' ? -1 : out.length - 1;
|
|
63
|
+
}
|
|
64
|
+
return out;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/** Where kanji readings come from, all in hiragana. */
|
|
68
|
+
export interface JapaneseReadings {
|
|
69
|
+
/** A word's reading; words up to `longest` characters are tried. */
|
|
70
|
+
word(w: string): string | undefined;
|
|
71
|
+
longest: number;
|
|
72
|
+
/** A kanji's own readings, the commonest first. */
|
|
73
|
+
kanji(k: string): { on: string[]; kun: string[] } | undefined;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** A kanji in no word: see the module header. */
|
|
77
|
+
function ownReading(run: string, i: number, r: { on: string[]; kun: string[] }): string | undefined {
|
|
78
|
+
const inWord = (i > 0 && isKanji(run[i - 1]!)) || (i + 1 < run.length && isKanji(run[i + 1]!));
|
|
79
|
+
if (inWord && r.on[0]) return r.on[0];
|
|
80
|
+
const tail = run.slice(i + 1).match(/^[ぁ-ゟ]+/)?.[0] ?? '';
|
|
81
|
+
for (const k of r.kun) for (let cut = 1; cut < k.length; cut++) if (tail.startsWith(k.slice(cut))) return k.slice(0, cut);
|
|
82
|
+
return r.kun[0] ?? r.on[0];
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Japanese text (kana and kanji) to phonemes. */
|
|
86
|
+
export function japanesePhonemes(run: string, start: number, readings: JapaneseReadings): Ph[] {
|
|
87
|
+
const kana: Array<[string, number]> = [];
|
|
88
|
+
// a reading's kana spread over the characters it reads
|
|
89
|
+
const spread = (reading: string, i: number, len: number) => {
|
|
90
|
+
const chars = [...reading];
|
|
91
|
+
chars.forEach((k, n) => kana.push([k, start + i + Math.floor((n * len) / chars.length)]));
|
|
92
|
+
};
|
|
93
|
+
let i = 0;
|
|
94
|
+
while (i < run.length) {
|
|
95
|
+
if (!isKanji(run[i]!)) { kana.push([run[i]!, start + i]); i++; continue; }
|
|
96
|
+
let len = Math.min(readings.longest, run.length - i), reading: string | undefined;
|
|
97
|
+
for (; len > 0; len--) { reading = readings.word(run.slice(i, i + len)); if (reading) break; }
|
|
98
|
+
if (reading) { spread(reading, i, len); i += len; continue; }
|
|
99
|
+
const own = readings.kanji(run[i] === '々' && i > 0 ? run[i - 1]! : run[i]!);
|
|
100
|
+
const said = own && ownReading(run, i, own);
|
|
101
|
+
if (said) spread(said, i, 1);
|
|
102
|
+
i++;
|
|
103
|
+
}
|
|
104
|
+
return kanaPhonemes(kana);
|
|
105
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Words whose kanji are not read one by one: the jukujikun and ateji of the appendix (付表) to the Jōyō kanji
|
|
3
|
+
* list (常用漢字表, a notice of the Agency for Cultural Affairs), with the appendix's readings.
|
|
4
|
+
*/
|
|
5
|
+
export const JUKUJIKUN: Readonly<Record<string, string>> = {
|
|
6
|
+
明日: 'あす', 小豆: 'あずき', 海女: 'あま', 海士: 'あま', 硫黄: 'いおう', 意気地: 'いくじ', 田舎: 'いなか', 息吹: 'いぶき',
|
|
7
|
+
海原: 'うなばら', 乳母: 'うば', 浮気: 'うわき', 浮つく: 'うわつく', 笑顔: 'えがお', 叔父: 'おじ', 伯父: 'おじ', 大人: 'おとな',
|
|
8
|
+
乙女: 'おとめ', 叔母: 'おば', 伯母: 'おば', お巡りさん: 'おまわりさん', お神酒: 'おみき', 母屋: 'おもや', 母家: 'おもや',
|
|
9
|
+
母さん: 'かあさん', 神楽: 'かぐら', 河岸: 'かし', 鍛冶: 'かじ', 風邪: 'かぜ', 固唾: 'かたず', 仮名: 'かな', 蚊帳: 'かや',
|
|
10
|
+
為替: 'かわせ', 河原: 'かわら', 川原: 'かわら', 昨日: 'きのう', 今日: 'きょう', 果物: 'くだもの', 玄人: 'くろうと', 今朝: 'けさ',
|
|
11
|
+
景色: 'けしき', 心地: 'ここち', 居士: 'こじ', 今年: 'ことし', 早乙女: 'さおとめ', 雑魚: 'ざこ', 桟敷: 'さじき',
|
|
12
|
+
差し支える: 'さしつかえる', 五月: 'さつき', 早苗: 'さなえ', 五月雨: 'さみだれ', 時雨: 'しぐれ', 尻尾: 'しっぽ', 竹刀: 'しない',
|
|
13
|
+
老舗: 'しにせ', 芝生: 'しばふ', 清水: 'しみず', 三味線: 'しゃみせん', 砂利: 'じゃり', 数珠: 'じゅず', 上手: 'じょうず', 白髪: 'しらが',
|
|
14
|
+
素人: 'しろうと', 師走: 'しわす', 数寄屋: 'すきや', 数奇屋: 'すきや', 相撲: 'すもう', 草履: 'ぞうり', 山車: 'だし', 太刀: 'たち',
|
|
15
|
+
立ち退く: 'たちのく', 七夕: 'たなばた', 足袋: 'たび', 稚児: 'ちご', 一日: 'ついたち', 築山: 'つきやま', 梅雨: 'つゆ', 凸凹: 'でこぼこ',
|
|
16
|
+
手伝う: 'てつだう', 伝馬船: 'てんません', 投網: 'とあみ', 父さん: 'とうさん', 十重二十重: 'とえはたえ', 読経: 'どきょう', 時計: 'とけい',
|
|
17
|
+
友達: 'ともだち', 仲人: 'なこうど', 名残: 'なごり', 雪崩: 'なだれ', 兄さん: 'にいさん', 姉さん: 'ねえさん', 野良: 'のら',
|
|
18
|
+
祝詞: 'のりと', 博士: 'はかせ', 二十歳: 'はたち', 二十日: 'はつか', 波止場: 'はとば', 一人: 'ひとり', 日和: 'ひより', 二人: 'ふたり',
|
|
19
|
+
二日: 'ふつか', 吹雪: 'ふぶき', 下手: 'へた', 部屋: 'へや', 迷子: 'まいご', 真面目: 'まじめ', 真っ赤: 'まっか', 真っ青: 'まっさお',
|
|
20
|
+
土産: 'みやげ', 息子: 'むすこ', 眼鏡: 'めがね', 猛者: 'もさ', 紅葉: 'もみじ', 木綿: 'もめん', 最寄り: 'もより', 八百長: 'やおちょう',
|
|
21
|
+
八百屋: 'やおや', 大和: 'やまと', 弥生: 'やよい', 浴衣: 'ゆかた', 行方: 'ゆくえ', 寄席: 'よせ', 若人: 'わこうど',
|
|
22
|
+
};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Hangul to IPA: each syllable block's initial, vowel and final consonant. A final before a syllable that starts
|
|
3
|
+
* with ㅇ moves over to start it (국어 ku-gʌ); finals are otherwise read as the seven final sounds.
|
|
4
|
+
*/
|
|
5
|
+
import type { Ph } from './rules.ts';
|
|
6
|
+
|
|
7
|
+
const INITIAL = ['k', 'k͈', 'n', 't', 't͈', 'ɾ', 'm', 'p', 'p͈', 's', 's͈', '', 'tɕ', 'tɕ͈', 'tɕʰ', 'kʰ', 'tʰ', 'pʰ', 'h'];
|
|
8
|
+
const MEDIAL = ['a', 'ɛ', 'j a', 'j ɛ', 'ʌ', 'e', 'j ʌ', 'j e', 'o', 'w a', 'w ɛ', 'w e', 'j o', 'u', 'w ʌ', 'w e', 'w i', 'j u', 'ɯ', 'ɰ i', 'i'];
|
|
9
|
+
/** Each final as said before a consonant or at the end. */
|
|
10
|
+
const FINAL = ['', 'k', 'k', 'k', 'n', 'n', 'n', 't', 'l', 'k', 'm', 'l', 'l', 'l', 'p', 'l', 'm', 'p', 'p', 't', 't', 'ŋ', 't', 't', 'k', 't', 'p', 't'];
|
|
11
|
+
/** Each final as the start of a following ㅇ syllable; '' = it does not move (ㅇ, ㅎ). */
|
|
12
|
+
const LINKED = ['', 'ɡ', 'k͈', 'k s', 'n', 'n dʑ', 'n', 'd', 'ɾ', 'l ɡ', 'l m', 'l b', 'l s', 'l tʰ', 'l pʰ', 'ɾ', 'm', 'b', 'p s', 's', 's͈', '', 'dʑ', 'tɕʰ', 'kʰ', 'tʰ', 'pʰ', ''];
|
|
13
|
+
const SILENT_INITIAL = 11;
|
|
14
|
+
|
|
15
|
+
/** A run of Hangul syllables starting at `start` in the text. */
|
|
16
|
+
export function hangulPhonemes(run: string, start: number): Ph[] {
|
|
17
|
+
const out: Ph[] = [];
|
|
18
|
+
const blocks = [...run].map((ch) => ch.codePointAt(0)! - 0xac00);
|
|
19
|
+
let carried = '';
|
|
20
|
+
blocks.forEach((s, k) => {
|
|
21
|
+
const at = start + k;
|
|
22
|
+
if (s < 0 || s > 11171) return;
|
|
23
|
+
const ini = Math.floor(s / 588), med = Math.floor((s % 588) / 28), fin = s % 28;
|
|
24
|
+
const onset = carried || INITIAL[ini]!;
|
|
25
|
+
carried = '';
|
|
26
|
+
for (const p of [onset, ...MEDIAL[med]!.split(' ')]) if (p) out.push({ p, at });
|
|
27
|
+
const next = blocks[k + 1];
|
|
28
|
+
if (fin && next !== undefined && Math.floor(next / 588) === SILENT_INITIAL && LINKED[fin]) {
|
|
29
|
+
const parts = LINKED[fin]!.split(' ');
|
|
30
|
+
carried = parts.pop()!;
|
|
31
|
+
for (const p of parts) out.push({ p, at });
|
|
32
|
+
} else if (fin) out.push({ p: FINAL[fin]!, at });
|
|
33
|
+
});
|
|
34
|
+
return out;
|
|
35
|
+
}
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spelling rules (rules.ts) of the languages written in Latin or Cyrillic letters, and where each puts the stress
|
|
3
|
+
* of a word the rules leave unmarked. English reads its dictionary first (index.ts); its rules here read the words
|
|
4
|
+
* the dictionary lacks.
|
|
5
|
+
*/
|
|
6
|
+
import { applyRules, type Ph, type RuleSet } from './rules.ts';
|
|
7
|
+
import { isVowel } from './ipa.ts';
|
|
8
|
+
|
|
9
|
+
const EN: RuleSet = {
|
|
10
|
+
vowels: 'aeiouy',
|
|
11
|
+
rules: [
|
|
12
|
+
['tion', 'ʃ ə n'], ['sion', 'ʒ ə n'], ['ture', 't ʃ ə'], ['igh', 'aɪ'], ['ough', 'ɔ'], ['augh', 'ɔ'], ['eigh', 'eɪ'],
|
|
13
|
+
['ee', 'i'], ['ea', 'i'], ['oo', 'u'], ['ou', 'aʊ'], ['ow', 'oʊ'], ['oa', 'oʊ'], ['ai', 'eɪ'], ['ay', 'eɪ'], ['oi', 'ɔɪ'], ['oy', 'ɔɪ'],
|
|
14
|
+
['au', 'ɔ'], ['aw', 'ɔ'], ['ew', 'j u'], ['ie(#', 'i'],
|
|
15
|
+
['ch', 'tʃ'], ['sh', 'ʃ'], ['th', 'θ'], ['ph', 'f'], ['wh', 'w'], ['ck', 'k'], ['ng(#', 'ŋ'], ['qu', 'k w'], ['#)kn', 'n'], ['#)wr', 'ɹ'],
|
|
16
|
+
['c([eiy]', 's'], ['c', 'k'], ['g([eiy]', 'dʒ'], ['g', 'ɡ'], ['x', 'k s'], ['j', 'dʒ'], ['y(A', 'j'], ['C)y(#', 'i'], ['y', 'ɪ'],
|
|
17
|
+
['a(Ce#', 'eɪ'], ['i(Ce#', 'aɪ'], ['o(Ce#', 'oʊ'], ['u(Ce#', 'j u'], ['e(Ce#', 'i'], ['AC)e(#', ''], ['AC)es(#', 'z'],
|
|
18
|
+
['a', 'æ'], ['e', 'ɛ'], ['i', 'ɪ'], ['o', 'ɑ'], ['u', 'ʌ'],
|
|
19
|
+
['b', 'b'], ['d', 'd'], ['f', 'f'], ['h', 'h'], ['k', 'k'], ['l', 'l'], ['m', 'm'], ['n', 'n'], ['p', 'p'], ['r', 'ɹ'], ['s', 's'],
|
|
20
|
+
['t', 't'], ['v', 'v'], ['w', 'w'], ['z', 'z'],
|
|
21
|
+
],
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
const ES: RuleSet = {
|
|
25
|
+
vowels: 'aeiouáéíóúü',
|
|
26
|
+
rules: [
|
|
27
|
+
['ch', 'tʃ'], ['ll', 'ʝ'], ['ñ', 'ɲ'], ['rr', 'r'], ['#)r', 'r'], ['r', 'ɾ'], ['qu([eiéí]', 'k'], ['gu([eiéí]', 'ɡ'], ['gü', 'ɡ w'],
|
|
28
|
+
['c([eiéí]', 's'], ['c', 'k'], ['z', 's'], ['g([eiéí]', 'x'], ['g', 'ɡ'], ['j', 'x'], ['h', ''], ['v', 'b'], ['x', 'k s'], ['y(#', 'i'], ['y', 'ʝ'],
|
|
29
|
+
['á', 'ˈ a'], ['é', 'ˈ e'], ['í', 'ˈ i'], ['ó', 'ˈ o'], ['ú', 'ˈ u'], ['a', 'a'], ['e', 'e'], ['i', 'i'], ['o', 'o'], ['u', 'u'], ['ü', 'u'],
|
|
30
|
+
['b', 'b'], ['d', 'd'], ['f', 'f'], ['k', 'k'], ['l', 'l'], ['m', 'm'], ['n', 'n'], ['p', 'p'], ['s', 's'], ['t', 't'], ['w', 'w'],
|
|
31
|
+
],
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
const IT: RuleSet = {
|
|
35
|
+
vowels: 'aeiouàèéìíòóùú',
|
|
36
|
+
rules: [
|
|
37
|
+
['ci([aouàòóù]', 'tʃ'], ['c([eièéì]', 'tʃ'], ['ch', 'k'], ['c', 'k'], ['gi([aouàòóù]', 'dʒ'], ['g([eièéì]', 'dʒ'], ['gh', 'ɡ'], ['gn', 'ɲ'],
|
|
38
|
+
['gli', 'ʎ'], ['g', 'ɡ'], ['sci([aouàòóù]', 'ʃ'], ['sc([eièéì]', 'ʃ'], ['qu', 'k w'], ['z', 'ts'], ['h', ''], ['r', 'r'], ['j', 'j'],
|
|
39
|
+
['à', 'ˈ a'], ['è', 'ˈ ɛ'], ['é', 'ˈ e'], ['ì', 'ˈ i'], ['í', 'ˈ i'], ['ò', 'ˈ ɔ'], ['ó', 'ˈ o'], ['ù', 'ˈ u'], ['ú', 'ˈ u'],
|
|
40
|
+
['a', 'a'], ['e', 'e'], ['i', 'i'], ['o', 'o'], ['u', 'u'],
|
|
41
|
+
['b', 'b'], ['d', 'd'], ['f', 'f'], ['k', 'k'], ['l', 'l'], ['m', 'm'], ['n', 'n'], ['p', 'p'], ['s', 's'], ['t', 't'], ['v', 'v'], ['w', 'w'],
|
|
42
|
+
['x', 'k s'], ['y', 'i'],
|
|
43
|
+
],
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
const PT: RuleSet = {
|
|
47
|
+
vowels: 'aeiouáàâãéêíóôõú',
|
|
48
|
+
rules: [
|
|
49
|
+
['lh', 'ʎ'], ['nh', 'ɲ'], ['ch', 'ʃ'], ['ão', 'ɐ̃'], ['ãe', 'ɐ̃'], ['õe', 'õ'], ['ã', 'ɐ̃'], ['õ', 'õ'], ['ç', 's'],
|
|
50
|
+
['c([eiéêí]', 's'], ['c', 'k'], ['qu([eiéêí]', 'k'], ['qu', 'k w'], ['gu([eiéêí]', 'ɡ'], ['g([eiéêí]', 'ʒ'], ['g', 'ɡ'], ['j', 'ʒ'],
|
|
51
|
+
['#)r', 'h'], ['rr', 'h'], ['r(#', 'h'], ['r', 'ɾ'], ['A)s(A', 'z'], ['x', 'ʃ'], ['h', ''], ['l(#', 'w'],
|
|
52
|
+
['d(i', 'dʒ'], ['d(es?#', 'dʒ'], ['t(i', 'tʃ'], ['t(es?#', 'tʃ'],
|
|
53
|
+
['am(#', 'ɐ̃'], ['an(C', 'ɐ̃'], ['em(#', 'ẽ'], ['en(C', 'ẽ'], ['im(#', 'ĩ'], ['in(C', 'ĩ'], ['om(#', 'õ'], ['on(C', 'õ'],
|
|
54
|
+
['um(#', 'ũ'], ['un(C', 'ũ'], ['e(s?#', 'i'], ['o(s?#', 'u'],
|
|
55
|
+
['á', 'ˈ a'], ['à', 'a'], ['â', 'ˈ ɐ'], ['é', 'ˈ ɛ'], ['ê', 'ˈ e'], ['í', 'ˈ i'], ['ó', 'ˈ ɔ'], ['ô', 'ˈ o'], ['ú', 'ˈ u'],
|
|
56
|
+
['a', 'a'], ['e', 'e'], ['i', 'i'], ['o', 'o'], ['u', 'u'],
|
|
57
|
+
['b', 'b'], ['d', 'd'], ['f', 'f'], ['k', 'k'], ['l', 'l'], ['m', 'm'], ['n', 'n'], ['p', 'p'], ['s', 's'], ['t', 't'], ['v', 'v'],
|
|
58
|
+
['w', 'w'], ['y', 'i'], ['z', 'z'],
|
|
59
|
+
],
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
const DE: RuleSet = {
|
|
63
|
+
vowels: 'aeiouäöüy',
|
|
64
|
+
rules: [
|
|
65
|
+
['tsch', 'tʃ'], ['sch', 'ʃ'], ['#)sp', 'ʃ p'], ['#)st', 'ʃ t'], ['chs', 'k s'], ['[aou])ch', 'x'], ['au)ch', 'x'], ['ch', 'ç'],
|
|
66
|
+
['ck', 'k'], ['qu', 'k v'], ['ph', 'f'], ['pf', 'pf'], ['th', 't'], ['tz', 'ts'], ['z', 'ts'], ['ß', 's'], ['v', 'f'], ['w', 'v'], ['j', 'j'],
|
|
67
|
+
['#)s(A', 'z'], ['A)s(A', 'z'], ['ei', 'aɪ'], ['ai', 'aɪ'], ['ey', 'aɪ'], ['ay', 'aɪ'], ['ie', 'iː'], ['eu', 'ɔʏ'], ['äu', 'ɔʏ'], ['au', 'aʊ'],
|
|
68
|
+
['ä', 'ɛ'], ['ö', 'ø'], ['ü', 'y'], ['y', 'y'], ['er(#', 'ɐ'], ['e(#', 'ə'], ['e(n#', 'ə'], ['A)h(C', ''], ['A)h(#', ''], ['h', 'h'],
|
|
69
|
+
['ig(#', 'ɪ ç'], ['b(#', 'p'], ['d(#', 't'], ['g(#', 'k'], ['g', 'ɡ'], ['A)r(#', 'ɐ'], ['r', 'ʁ'],
|
|
70
|
+
['a', 'a'], ['e', 'e'], ['i', 'ɪ'], ['o', 'o'], ['u', 'u'],
|
|
71
|
+
['b', 'b'], ['c', 'k'], ['d', 'd'], ['f', 'f'], ['k', 'k'], ['l', 'l'], ['m', 'm'], ['n', 'n'], ['p', 'p'], ['s', 's'], ['t', 't'], ['x', 'k s'],
|
|
72
|
+
],
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
const FR: RuleSet = {
|
|
76
|
+
vowels: 'aeiouyàâäéèêëîïôöùûüÿœæ',
|
|
77
|
+
rules: [
|
|
78
|
+
['eaux(#', 'o'], ['eau', 'o'], ['aux(#', 'o'], ['au', 'o'], ['oin', 'w ɛ̃'], ['oi', 'w a'], ['oy', 'w a j'], ['ou', 'u'], ['où', 'u'], ['oû', 'u'],
|
|
79
|
+
['aill', 'a j'], ['eill', 'ɛ j'], ['ouill', 'u j'], ['A)ill', 'j'], ['ill(e', 'i j'],
|
|
80
|
+
['ai', 'ɛ'], ['aî', 'ɛ'], ['ei', 'ɛ'], ['ay', 'ɛ j'], ['œu', 'ø'], ['eu', 'ø'], ['œ', 'œ'],
|
|
81
|
+
['ien([^aeiouyn]', 'j ɛ̃'], ['ien(#', 'j ɛ̃'],
|
|
82
|
+
['an([^aeiouyhnm]', 'ɑ̃'], ['an(#', 'ɑ̃'], ['am([^aeiouyhnm]', 'ɑ̃'], ['am(#', 'ɑ̃'], ['en([^aeiouyhnm]', 'ɑ̃'], ['en(#', 'ɑ̃'],
|
|
83
|
+
['em([^aeiouyhnm]', 'ɑ̃'], ['em(#', 'ɑ̃'], ['on([^aeiouyhnm]', 'ɔ̃'], ['on(#', 'ɔ̃'], ['om([^aeiouyhnm]', 'ɔ̃'], ['om(#', 'ɔ̃'],
|
|
84
|
+
['ain([^aeiouyhnm]', 'ɛ̃'], ['ain(#', 'ɛ̃'], ['ein([^aeiouyhnm]', 'ɛ̃'], ['ein(#', 'ɛ̃'], ['in([^aeiouyhnm]', 'ɛ̃'], ['in(#', 'ɛ̃'],
|
|
85
|
+
['im([^aeiouyhnm]', 'ɛ̃'], ['im(#', 'ɛ̃'], ['un([^aeiouyhnm]', 'œ̃'], ['un(#', 'œ̃'], ['um(#', 'œ̃'],
|
|
86
|
+
['tion', 's j ɔ̃'], ['ui', 'ɥ i'], ['ch', 'ʃ'], ['gn', 'ɲ'], ['qu', 'k'], ['gu([eiyéèê]', 'ɡ'], ['ç', 's'], ['c([eiyéèê]', 's'], ['c', 'k'],
|
|
87
|
+
['g([eiyéèê]', 'ʒ'], ['g', 'ɡ'], ['j', 'ʒ'], ['ph', 'f'], ['th', 't'], ['h', ''], ['A)s(A', 'z'], ['x(#', ''], ['x', 'k s'], ['r', 'ʁ'], ['w', 'w'],
|
|
88
|
+
['#C)e(#', 'ə'], ['qu)e(#', 'ə'], ['e(#', ''], ['e(s#', ''], ['er(#', 'e'], ['ez(#', 'e'], ['et(#', 'ɛ'],
|
|
89
|
+
['d(#', ''], ['t(#', ''], ['s(#', ''], ['z(#', ''], ['p(#', ''], ['g(#', ''],
|
|
90
|
+
['é', 'e'], ['è', 'ɛ'], ['ê', 'ɛ'], ['ë', 'ɛ'], ['e(CC', 'ɛ'], ['e', 'ə'], ['a', 'a'], ['à', 'a'], ['â', 'a'], ['i', 'i'], ['î', 'i'], ['ï', 'i'],
|
|
91
|
+
['o', 'o'], ['ô', 'o'], ['u', 'y'], ['û', 'y'], ['ù', 'y'], ['y', 'i'],
|
|
92
|
+
['b', 'b'], ['d', 'd'], ['f', 'f'], ['k', 'k'], ['l', 'l'], ['m', 'm'], ['n', 'n'], ['p', 'p'], ['s', 's'], ['t', 't'], ['v', 'v'], ['z', 'z'],
|
|
93
|
+
],
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
const RU: RuleSet = {
|
|
97
|
+
vowels: 'аеёиоуыэюя',
|
|
98
|
+
rules: [
|
|
99
|
+
['#)е', 'j e'], ['[аеёиоуыэюяьъ])е', 'j e'], ['е', 'ʲ e'], ['#)ё', 'j o'], ['[аеёиоуыэюяьъ])ё', 'j o'], ['ё', 'ʲ o'],
|
|
100
|
+
['#)ю', 'j u'], ['[аеёиоуыэюяьъ])ю', 'j u'], ['ю', 'ʲ u'], ['#)я', 'j a'], ['[аеёиоуыэюяьъ])я', 'j a'], ['я', 'ʲ a'],
|
|
101
|
+
['а', 'a'], ['о', 'o'], ['у', 'u'], ['ы', 'ɨ'], ['э', 'e'], ['и', 'i'],
|
|
102
|
+
['б', 'b'], ['в', 'v'], ['г', 'ɡ'], ['д', 'd'], ['ж', 'ʐ'], ['з', 'z'], ['й', 'j'], ['к', 'k'], ['л', 'l'], ['м', 'm'], ['н', 'n'],
|
|
103
|
+
['п', 'p'], ['р', 'r'], ['с', 's'], ['т', 't'], ['ф', 'f'], ['х', 'x'], ['ц', 'ts'], ['ч', 'tɕ'], ['ш', 'ʂ'], ['щ', 'ɕː'], ['ь(A', 'ʲ'],
|
|
104
|
+
['ь', ''], ['ъ', ''],
|
|
105
|
+
],
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** Which vowel of a word without a marked stress carries it. */
|
|
109
|
+
type StressRule = (vowels: number, phs: readonly Ph[]) => number | null;
|
|
110
|
+
const PENULT: StressRule = (n) => (n > 1 ? n - 2 : 0);
|
|
111
|
+
const STRESS: Record<string, StressRule> = {
|
|
112
|
+
en: () => 0,
|
|
113
|
+
de: () => 0,
|
|
114
|
+
fr: (n) => n - 1,
|
|
115
|
+
it: PENULT,
|
|
116
|
+
// Spanish: the last syllable when the word ends in a consonant other than n or s
|
|
117
|
+
es: (n, phs) => { const last = phs.at(-1)?.p ?? ''; return isVowel(last) || last === 'n' || last === 's' ? PENULT(n, phs) : n - 1; },
|
|
118
|
+
// Portuguese: the last syllable when the word ends in r, l, z, i, u or a nasal vowel
|
|
119
|
+
pt: (n, phs) => (/^(ɾ|h|w|z|i|u)$|̃/.test(phs.at(-1)?.p ?? '') ? n - 1 : PENULT(n, phs)),
|
|
120
|
+
ru: () => null,
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
export const RULES: Record<string, RuleSet> = { en: EN, es: ES, it: IT, pt: PT, de: DE, fr: FR, ru: RU };
|
|
124
|
+
|
|
125
|
+
/** Languages whose unstressed i and u next to another vowel are glides: before it j and w, after it a diphthong. */
|
|
126
|
+
const GLIDING = new Set(['es', 'it', 'pt']);
|
|
127
|
+
|
|
128
|
+
function glides(phs: Ph[]): Ph[] {
|
|
129
|
+
const out: Ph[] = [];
|
|
130
|
+
for (let i = 0; i < phs.length; i++) {
|
|
131
|
+
const { p, at } = phs[i]!;
|
|
132
|
+
const weak = (p === 'i' || p === 'u') && phs[i - 1]?.p !== 'ˈ';
|
|
133
|
+
const prev = out.at(-1), next = phs[i + 1];
|
|
134
|
+
if (weak && next && isVowel(next.p)) { out.push({ p: p === 'i' ? 'j' : 'w', at }); continue; }
|
|
135
|
+
if (weak && prev && isVowel(prev.p) && prev.p.length === 1) { prev.p += p; continue; }
|
|
136
|
+
out.push({ p, at });
|
|
137
|
+
}
|
|
138
|
+
return out;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/** `word` (lower case) in `lang` (a key of RULES) to phonemes, its stress marked. */
|
|
142
|
+
export function spell(word: string, lang: string): Ph[] {
|
|
143
|
+
let phs = applyRules(word, RULES[lang] ?? EN);
|
|
144
|
+
if (GLIDING.has(lang)) phs = glides(phs);
|
|
145
|
+
if (phs.some((p) => p.p === 'ˈ')) return phs;
|
|
146
|
+
const vowels = phs.map((p, i) => (isVowel(p.p) ? i : -1)).filter((i) => i >= 0);
|
|
147
|
+
const k = (STRESS[lang] ?? STRESS.en!)(vowels.length, phs);
|
|
148
|
+
if (k === null || vowels[k] === undefined) return phs;
|
|
149
|
+
const at = vowels[k]!;
|
|
150
|
+
return [...phs.slice(0, at), { p: 'ˈ', at: phs[at]!.at }, ...phs.slice(at)];
|
|
151
|
+
}
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Spelling to phonemes by rules, the way eSpeak NG's `<lang>_rules` work: a rule reads one group of letters in
|
|
3
|
+
* its left and right context and gives the phonemes they stand for. At each letter the longest group that has a
|
|
4
|
+
* rule matching there wins; among rules of the same group the first listed that matches. A rule is written
|
|
5
|
+
* `left)group(right`, either context optional:
|
|
6
|
+
*
|
|
7
|
+
* - `#` the word's edge, `A` a vowel letter, `C` a consonant letter, `[...]` one of those letters, any other letter itself.
|
|
8
|
+
* - The phonemes are IPA, one per space-separated token; `ˈ` before a vowel marks it stressed; no token = silent.
|
|
9
|
+
*
|
|
10
|
+
* A consonant letter written twice reads once unless a rule takes the pair.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** A phoneme with the index (in the word) of the first letter it came from. */
|
|
14
|
+
export interface Ph { p: string; at: number }
|
|
15
|
+
|
|
16
|
+
export interface RuleSet {
|
|
17
|
+
vowels: string;
|
|
18
|
+
rules: ReadonlyArray<readonly [pattern: string, phonemes: string]>;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
interface Compiled { group: string; re: RegExp; out: string[] }
|
|
22
|
+
|
|
23
|
+
const cache = new WeakMap<RuleSet, { byFirst: Map<string, Compiled[]>; vowels: Set<string> }>();
|
|
24
|
+
|
|
25
|
+
function compile(set: RuleSet) {
|
|
26
|
+
let hit = cache.get(set);
|
|
27
|
+
if (hit) return hit;
|
|
28
|
+
const v = escapeClass(set.vowels);
|
|
29
|
+
const ctx = (s: string, left: boolean) => s.replace(/\[[^\]]*\]|./gu, (t) =>
|
|
30
|
+
t === '#' ? (left ? '^' : '$') : t === 'A' ? `[${v}]` : t === 'C' ? `[^${v}\\P{L}]` : t.startsWith('[') ? t : escapeChar(t));
|
|
31
|
+
const byFirst = new Map<string, Compiled[]>();
|
|
32
|
+
for (const [pattern, phonemes] of set.rules) {
|
|
33
|
+
const m = /^(?:(.*)\))?([^()]+)(?:\((.*))?$/u.exec(pattern);
|
|
34
|
+
if (!m) throw new Error(`bad rule ${pattern}`);
|
|
35
|
+
const [, left = '', group = '', right = ''] = m;
|
|
36
|
+
const re = new RegExp(`${left ? `(?<=${ctx(left, true)})` : ''}${escapeChar(group)}${right ? `(?=${ctx(right, false)})` : ''}`, 'uy');
|
|
37
|
+
const first = group[0]!;
|
|
38
|
+
if (!byFirst.has(first)) byFirst.set(first, []);
|
|
39
|
+
byFirst.get(first)!.push({ group, re, out: phonemes.split(' ').filter(Boolean) });
|
|
40
|
+
}
|
|
41
|
+
// longest group first; a stable sort keeps the listed order within a length
|
|
42
|
+
for (const list of byFirst.values()) list.sort((a, b) => b.group.length - a.group.length);
|
|
43
|
+
hit = { byFirst, vowels: new Set(set.vowels) };
|
|
44
|
+
cache.set(set, hit);
|
|
45
|
+
return hit;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const escapeChar = (s: string) => s.replace(/[\\^$.*+?()[\]{}|/-]/g, '\\$&');
|
|
49
|
+
const escapeClass = (s: string) => s.replace(/[\\\]^-]/g, '\\$&');
|
|
50
|
+
|
|
51
|
+
/** `word` (lower case) to phonemes. A letter no rule reads is skipped. */
|
|
52
|
+
export function applyRules(word: string, set: RuleSet): Ph[] {
|
|
53
|
+
const { byFirst, vowels } = compile(set);
|
|
54
|
+
const out: Ph[] = [];
|
|
55
|
+
let i = 0, prev = '';
|
|
56
|
+
while (i < word.length) {
|
|
57
|
+
const ch = word[i]!;
|
|
58
|
+
if (ch === prev && !vowels.has(ch) && /\p{L}/u.test(ch)) { i++; prev = ''; continue; }
|
|
59
|
+
let done = false;
|
|
60
|
+
for (const r of byFirst.get(ch) ?? []) {
|
|
61
|
+
r.re.lastIndex = i;
|
|
62
|
+
if (!r.re.test(word)) continue;
|
|
63
|
+
for (const p of r.out) out.push({ p, at: i });
|
|
64
|
+
prev = r.group.length === 1 ? ch : '';
|
|
65
|
+
i += r.group.length;
|
|
66
|
+
done = true;
|
|
67
|
+
break;
|
|
68
|
+
}
|
|
69
|
+
if (!done) { i++; prev = ''; }
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Syllables as the speech bank holds them: every language's syllable (from its IPA, ipa.ts) is spoken as one
|
|
3
|
+
* toneless Mandarin syllable of web/speech/zh.json, spelled from pinyin's pieces: an initial (`b`..`s` or none),
|
|
4
|
+
* a glide (`i`, `u` or none), a nucleus and a nasal coda.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/** A vowel nucleus, by pinyin final: `e` is a central vowel (ə, ʌ), `ei` a front mid one (e, ɛ), `v` is ü. */
|
|
8
|
+
export type Nucleus = 'a' | 'ai' | 'ao' | 'e' | 'ei' | 'i' | 'o' | 'ou' | 'u' | 'v';
|
|
9
|
+
export type Glide = '' | 'i' | 'u';
|
|
10
|
+
export type Coda = '' | 'n' | 'ng';
|
|
11
|
+
|
|
12
|
+
/** A syllable to speak: `at` is the index of the character whose reveal starts it. */
|
|
13
|
+
export interface Syllable {
|
|
14
|
+
at: number;
|
|
15
|
+
initial: string;
|
|
16
|
+
glide: Glide;
|
|
17
|
+
nucleus: Nucleus;
|
|
18
|
+
coda: Coda;
|
|
19
|
+
stress: boolean;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const RHYME: Record<Nucleus, Record<Coda, string>> = {
|
|
23
|
+
// a diphthong keeps its glide over a nasal after it: I'm is ai, not an
|
|
24
|
+
a: { '': 'a', n: 'an', ng: 'ang' }, ai: { '': 'ai', n: 'ai', ng: 'ai' }, ao: { '': 'ao', n: 'ao', ng: 'ao' },
|
|
25
|
+
e: { '': 'e', n: 'en', ng: 'eng' }, ei: { '': 'ei', n: 'en', ng: 'eng' }, i: { '': 'i', n: 'in', ng: 'ing' },
|
|
26
|
+
o: { '': 'o', n: 'ong', ng: 'ong' }, ou: { '': 'ou', n: 'ou', ng: 'ou' }, u: { '': 'u', n: 'un', ng: 'ong' },
|
|
27
|
+
v: { '': 'v', n: 'vn', ng: 'iong' },
|
|
28
|
+
};
|
|
29
|
+
/** A rhyme after a glide, in pinyin's spelling after an initial. */
|
|
30
|
+
const AFTER_I: Record<string, string> = {
|
|
31
|
+
a: 'ia', an: 'ian', ang: 'iang', ao: 'iao', ai: 'ia', e: 'ie', ei: 'ie', en: 'in', eng: 'ing', o: 'iu', ou: 'iu', ong: 'iong',
|
|
32
|
+
u: 'iu', un: 'in', i: 'i', in: 'in', ing: 'ing', v: 'v', vn: 'vn', iong: 'iong',
|
|
33
|
+
};
|
|
34
|
+
const AFTER_U: Record<string, string> = {
|
|
35
|
+
a: 'ua', an: 'uan', ang: 'uang', ai: 'uai', ao: 'uo', e: 'uo', ei: 'ui', en: 'un', eng: 'ong', o: 'uo', ou: 'uo', ong: 'ong',
|
|
36
|
+
i: 'ui', in: 'un', ing: 'ong', u: 'u', un: 'un', v: 'v', vn: 'vn', iong: 'iong',
|
|
37
|
+
};
|
|
38
|
+
/** Initials that cannot take an i-final, and the palatal one each sounds like before i. */
|
|
39
|
+
const PALATAL: Record<string, string> = { k: 'q', g: 'j', h: 'x', s: 'x', sh: 'x', z: 'j', zh: 'j', c: 'q', ch: 'q' };
|
|
40
|
+
|
|
41
|
+
/** A final with no initial, as pinyin writes it. */
|
|
42
|
+
function bare(f: string): string {
|
|
43
|
+
if (f === 'i' || f === 'in' || f === 'ing') return 'y' + f;
|
|
44
|
+
if (f === 'u') return 'wu';
|
|
45
|
+
if (f === 'v') return 'yu';
|
|
46
|
+
if (f === 'vn') return 'yun';
|
|
47
|
+
if (f === 'iu') return 'you';
|
|
48
|
+
if (f === 'ui') return 'wei';
|
|
49
|
+
if (f === 'un') return 'wen';
|
|
50
|
+
if (f === 'ong') return 'weng';
|
|
51
|
+
if (f.startsWith('i')) return 'y' + f.slice(1);
|
|
52
|
+
if (f.startsWith('u')) return 'w' + f.slice(1);
|
|
53
|
+
return f;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/** The bank syllable nearest `s`; `has` says which syllables the bank holds. */
|
|
57
|
+
export function bankKey(s: Pick<Syllable, 'initial' | 'glide' | 'nucleus' | 'coda'>, has: (key: string) => boolean): string | null {
|
|
58
|
+
const rhyme = RHYME[s.nucleus][s.coda];
|
|
59
|
+
let c = s.initial, glide = s.glide;
|
|
60
|
+
// w and y are glides in pinyin, not initials
|
|
61
|
+
if ((c === 'w' || c === 'y') && !glide) { glide = c === 'w' ? 'u' : 'i'; c = ''; }
|
|
62
|
+
let f = glide === 'i' ? AFTER_I[rhyme] ?? rhyme : glide === 'u' ? AFTER_U[rhyme] ?? rhyme : rhyme;
|
|
63
|
+
if (f.startsWith('i') && PALATAL[c]) c = PALATAL[c]!;
|
|
64
|
+
// ü is written u after j, q, x; after any other initial but n and l it is u
|
|
65
|
+
if (f.startsWith('v') && c && c !== 'n' && c !== 'l') f = 'u' + f.slice(1);
|
|
66
|
+
// j, q, x take only i- and ü-finals
|
|
67
|
+
if ((c === 'j' || c === 'q' || c === 'x') && !f.startsWith('i') && !f.startsWith('u')) f = AFTER_I[f] ?? 'i' + f;
|
|
68
|
+
const join = (i: string, fin: string) => (i ? i + fin : bare(fin));
|
|
69
|
+
const plain = RHYME[s.nucleus][''];
|
|
70
|
+
// nearest first: the same rhyme (ko as kuo, de as dei), then the same coda after another vowel (bong as beng),
|
|
71
|
+
// then the vowel alone
|
|
72
|
+
const nasal = s.coda ? [RHYME.e[s.coda], RHYME.a[s.coda], RHYME.u[s.coda]] : [];
|
|
73
|
+
const tries = [join(c, f), join(c, rhyme), c && join(c, 'u' + plain), c && join(c, plain + 'i'), ...nasal.map((r) => c && join(c, r)),
|
|
74
|
+
join(c, plain), c && join(c, 'e'), c && join(c, 'u'), c && join(c, 'a'), bare(f), bare(rhyme), 'a'];
|
|
75
|
+
return tries.find((k) => k && has(k)) ?? null;
|
|
76
|
+
}
|