AI candidate selection: rerank with a Traditional Chinese language model (macOS)

The converter's K best paths are rescored with CKIP gpt2-tiny-chinese (GPL-3.0,
character-level, ~12M parameters, 22 MB as float16):
score = lexicon log-prob + 0.6 x LM log-prob, with the recently committed text
as context. Inference is plain Accelerate (Apple Silicon and Intel).

Modes off / saver (K=3, 15 ms) / standard (K=10, 40 ms) / quality (K=30, 120 ms)
in the input menu. A rerank over its time budget falls back to the lexicon; three
in a row pause AI for a minute. With auto-pause on, AI pauses when the Mac is hot
or the CPU is busy and drops to saver in Low Power Mode. The model loads in the
background and is not loaded at all when off.

Evaluation (training/lm/eval_rerank.py): chat 96.6% -> 97.5% chars,
Wikipedia 92.9% -> 94.7% chars (about 25-30% fewer wrong characters).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 16:33:58 +08:00
co-authored by Claude Opus 5.5
parent acafd8f96e
commit fd87da8061
16 changed files with 1653 additions and 2 deletions
+51 -1
View File
@@ -17,6 +17,7 @@
- **可省略聲調**:`jiap7`(ㄨㄛ ㄇㄣ˙)一樣能打出「我們」
- **候選字**:用 ← → 把游標移到要改的字後面,按 ↓ 選字;選過的會固定下來
- **記住選字**:從候選字選過的字詞會被記住,下次打同樣的讀音會優先出現(選越多次越優先)
- **AI 選字(macOS)**:用中研院 CKIP 的繁中小型語言模型看前後文挑同音字(他跑「得」很快、請你「再」說一次),完全在本機運算;可調整或關閉,系統忙碌時自動暫停
- **中間補字、刪字**:游標移到前面後直接打注音,會插在游標處;Backspace 刪掉游標前的那個字
- **全形標點**:中文語境下 `?` `!` `<` 自動變成 `?` `!` `,`;任何時候按 `` ` `` 再按標點鍵,一定得到全形標點(例如 `` `< `` = `,`)
- **完整詞庫**:約 17 萬筆,來自小麥注音 McBopomofo(MIT 授權)
@@ -96,6 +97,37 @@ SmartScreen 出現警告時,按「其他資訊 → 仍要執行」。解除安
轉換用 Viterbi 演算法,在詞庫中找出整串音節機率最高的詞組組合。
### AI 選字(macOS)
詞庫只知道每個詞本身有多常見,不看前後文。AI 選字讓語言模型當裁判:
1. 詞庫先找出前 K 名的整句組合(例如「請你在說一次」「請你再說一次」)。
2. 模型 [CKIP GPT2-tiny](https://huggingface.co/ckiplab/gpt2-tiny-chinese)(中研院詞庫小組,繁體中文,約 1200 萬參數,22 MB)計算每個組合出現的機率,並參考剛送出的前文。
3. 分數 = 詞庫分數 + 0.6 × 模型分數,取最高的。選過的候選字仍然固定不變。
輸入法選單可以調整:
| 模式 | 比較幾個組合 | 每次運算上限 |
|---|---|---|
| 關閉 | — | 不載入模型、不佔記憶體 |
| 省電 | 3 | 15 毫秒 |
| 標準(預設) | 10 | 40 毫秒 |
| 高品質 | 30 | 120 毫秒 |
不會拖慢打字的機制:
- 每次運算超過上限就直接放棄,改用詞庫結果;連續 3 次超時就暫停一分鐘。
- 「系統忙碌時自動暫停 AI」(預設開啟):Mac 過熱,或 CPU 負載(每核心 1 分鐘平均)超過 0.9 時暫停;低耗電模式下降到省電模式。選單最下面會顯示目前狀態。
- 模型在背景載入,載入完成前照常用詞庫。
實測(`training/lm/eval_rerank.py`,標準模式):
| 測試資料 | 只用詞庫 | AI 選字 |
|---|---|---|
| 日常對話 60 句 | 字正確率 96.6%,整句全對 73% | 97.5%,80% |
| 維基百科 714 句 | 92.9%,31% | 94.7%,44% |
錯字大約少了 25–30%。Windows、Linux 版目前還沒有 AI 選字。
## 個人資料
| | macOS | Windows | Linux |
@@ -190,6 +222,22 @@ python3 training/make_parity.py # 權重或特徵改了之後都要重
scripts/import-mcbopomofo.sh
```
### AI 選字模型
```bash
scripts/fetch-lm.sh # 下載 CKIP gpt2-tiny-chinese,轉成 build/lm/lm.bin(float16)+ lm-vocab.txt
```
`build.sh` 會自動執行並把模型打包進 app。模型推論寫在 `Sources/AutoZhuyinCore/LanguageModel.swift`(Accelerate,Apple Silicon 和 Intel 都可用),重排在 `Reranker.swift`。
評估效果(需要 numpy;`build/lm-eval/` 放測試句子,一行一句):
```bash
python training/lm/eval_rerank.py build/lm-eval/chat.txt build/lm-eval/wiki.txt
```
`training/lm/gpt2.py` 是 numpy 版的參考實作,Swift 版的測試會比對它算出的分數。
### 發布新版本
先 commit 所有修改,再執行:
@@ -263,7 +311,8 @@ THIRD_PARTY_LICENSES.txt 第三方授權聲明(會打包進兩個安裝
- [x] 用資料訓練判斷權重(合成資料)
- [ ] 加入真實打字資料一起訓練
- [ ] 已送出的英文單字若判斷錯誤,可以「事後翻轉」
- [ ] 用 Core ML 小模型取代手調分數,參考整句語境
- [x] AI 選字:語言模型參考前後文挑同音字(macOS)
- [ ] AI 選字移植到 Windows、Linux
- [x] 使用者詞頻學習(選過的候選字優先)
- [ ] 偏好設定視窗
@@ -272,5 +321,6 @@ THIRD_PARTY_LICENSES.txt 第三方授權聲明(會打包進兩個安裝
- 詞庫資料:[小麥注音 McBopomofo](https://github.com/openvanilla/McBopomofo)(MIT License)
- Windows 輸入法框架:[PIME](https://github.com/EasyIME/PIME)(LGPL 2.1,使用者自行安裝,本專案不包含其程式碼)
- 英文字典:Webster's Second International Dictionary(公有領域,macOS 的 `/usr/share/dict/words`)
- AI 選字語言模型:[CKIP GPT2 Tiny Chinese](https://github.com/ckiplab/ckip-transformers)(中研院詞庫小組,GPL-3.0,全文在 `licenses/GPL-3.0.txt`,Mac 版一併放在 app 的 Resources)
完整的第三方授權聲明(含小麥注音 MIT 授權全文)見 [THIRD_PARTY_LICENSES.txt](THIRD_PARTY_LICENSES.txt),Mac 版放在 `AutoZhuyin.app/Contents/Resources/`,Windows 版放在 zip 根目錄和安裝後的 `autozhuyin` 資料夾。