From c8b3c4ae6f8d710a98fab47f385e37259a125644 Mon Sep 17 00:00:00 2001 From: John Kao Date: Fri, 13 Nov 2026 16:51:41 +0800 Subject: [PATCH] AI candidate selection on Windows and Linux Python port of the reranker: core/lm.py runs the same CKIP gpt2-tiny-chinese model with numpy (batched per candidate length, scores match the Swift and reference implementations), Converter.kbest, core/reranker.py (modes, time budget, pause after overruns, cache), core/system_monitor.py (Windows: CPU usage and battery saver; Linux: load average, thermal zones, low-power profile and low battery) and core/ai.py (settings.json, background model loading, status line). Windows: numpy wheels for PIME's 32-bit Python 3.8 (PIME 1.3) and 3.15 (PIME 2) are bundled under autozhuyin/vendor and picked by Python version; a settings menu on the taskbar IME icon and the language bar (left click on the icon toggles English mode). Linux: install.sh uses the system numpy or downloads the matching wheel from PyPI (tools/fetch_numpy.py, SHA-256 checked); an IBus settings menu. Without numpy or the model the input method works as before. Co-Authored-By: Claude Opus 5.5 --- README.md | 27 ++++-- THIRD_PARTY_LICENSES.txt | 13 ++- linux/autozhuyin_ibus.py | 77 +++++++++++++++ linux/installer/README.txt | 10 ++ linux/installer/install.sh | 24 ++++- linux/tests/test_ibus_engine.py | 24 ++++- scripts/package-linux.sh | 5 +- scripts/package-windows.sh | 9 ++ windows/autozhuyin/autozhuyin_ime.py | 68 +++++++++++++- windows/autozhuyin/core/ai.py | 108 +++++++++++++++++++++ windows/autozhuyin/core/engine.py | 10 ++ windows/autozhuyin/core/lexicon.py | 22 +++++ windows/autozhuyin/core/lm.py | 109 ++++++++++++++++++++++ windows/autozhuyin/core/reranker.py | 102 ++++++++++++++++++++ windows/autozhuyin/core/system_monitor.py | 95 +++++++++++++++++++ windows/installer/README.txt | 9 ++ windows/tests/test_core.py | 100 ++++++++++++++++++++ windows/tests/test_pime_wrapper.py | 22 +++++ windows/tools/build_data.py | 7 ++ windows/tools/fetch_numpy.py | 79 ++++++++++++++++ 20 files changed, 900 insertions(+), 20 deletions(-) create mode 100644 windows/autozhuyin/core/ai.py create mode 100644 windows/autozhuyin/core/lm.py create mode 100644 windows/autozhuyin/core/reranker.py create mode 100644 windows/autozhuyin/core/system_monitor.py create mode 100644 windows/tools/fetch_numpy.py diff --git a/README.md b/README.md index 86b24b1..bb757a8 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ - **可省略聲調**:`jiap7`(ㄨㄛ ㄇㄣ˙)一樣能打出「我們」 - **候選字**:用 ← → 把游標移到要改的字後面,按 ↓ 選字;選過的會固定下來 - **記住選字**:從候選字選過的字詞會被記住,下次打同樣的讀音會優先出現(選越多次越優先) -- **AI 選字(macOS)**:用中研院 CKIP 的繁中小型語言模型看前後文挑同音字(他跑「得」很快、請你「再」說一次),完全在本機運算;可調整或關閉,系統忙碌時自動暫停 +- **AI 選字**:用中研院 CKIP 的繁中小型語言模型看前後文挑同音字(他跑「得」很快、請你「再」說一次),完全在本機運算;可調整或關閉,系統忙碌時自動暫停 - **中間補字、刪字**:游標移到前面後直接打注音,會插在游標處;Backspace 刪掉游標前的那個字 - **全形標點**:中文語境下 `?` `!` `<` 自動變成 `?` `!` `,`;任何時候按 `` ` `` 再按標點鍵,一定得到全形標點(例如 `` `< `` = `,`) - **完整詞庫**:約 17 萬筆,來自小麥注音 McBopomofo(MIT 授權) @@ -97,7 +97,7 @@ SmartScreen 出現警告時,按「其他資訊 → 仍要執行」。解除安 轉換用 Viterbi 演算法,在詞庫中找出整串音節機率最高的詞組組合。 -### AI 選字(macOS) +### AI 選字 詞庫只知道每個詞本身有多常見,不看前後文。AI 選字讓語言模型當裁判: @@ -105,7 +105,7 @@ SmartScreen 出現警告時,按「其他資訊 → 仍要執行」。解除安 2. 模型 [CKIP GPT2-tiny](https://huggingface.co/ckiplab/gpt2-tiny-chinese)(中研院詞庫小組,繁體中文,約 1200 萬參數,22 MB)計算每個組合出現的機率,並參考剛送出的前文。 3. 分數 = 詞庫分數 + 0.6 × 模型分數,取最高的。選過的候選字仍然固定不變。 -輸入法選單可以調整: +輸入法選單可以調整(Mac:選單列的輸入法選單;Windows:工作列輸入法圖示按右鍵或語言列的設定按鈕;Linux:IBus 選單的「設定」): | 模式 | 比較幾個組合 | 每次運算上限 | |---|---|---| @@ -116,7 +116,13 @@ SmartScreen 出現警告時,按「其他資訊 → 仍要執行」。解除安 不會拖慢打字的機制: - 每次運算超過上限就直接放棄,改用詞庫結果;連續 3 次超時就暫停一分鐘。 -- 「系統忙碌時自動暫停 AI」(預設開啟):Mac 過熱,或 CPU 負載(每核心 1 分鐘平均)超過 0.9 時暫停;低耗電模式下降到省電模式。選單最下面會顯示目前狀態。 +- 「系統忙碌時自動暫停 AI」(預設開啟),選單裡會顯示目前狀態: + +| | 暫停 AI | 降到省電模式 | +|---|---|---| +| macOS | 過熱,或 CPU 負載(每核心 1 分鐘平均)超過 0.9 | 低耗電模式 | +| Windows | CPU 使用率超過 90% | 省電模式(電池保護) | +| Linux | CPU 負載超過 0.9,或溫度達 95 °C | 低耗電設定檔,或用電池且電量 ≤ 20% | - 模型在背景載入,載入完成前照常用詞庫。 實測(`training/lm/eval_rerank.py`,標準模式): @@ -126,7 +132,12 @@ SmartScreen 出現警告時,按「其他資訊 → 仍要執行」。解除安 | 日常對話 60 句 | 字正確率 96.6%,整句全對 73% | 97.5%,80% | | 維基百科 714 句 | 92.9%,31% | 94.7%,44% | -錯字大約少了 25–30%。Windows、Linux 版目前還沒有 AI 選字。 +錯字大約少了 25–30%。 + +Windows、Linux 版用 numpy 執行同一個模型(`windows/autozhuyin/core/lm.py`),所以時間上限放寬為省電 40、標準 80、高品質 200 毫秒,超時的那次結果照用但不快取,連續 3 次超時暫停一分鐘。 +- Windows:安裝包附上 PIME 兩種 Python(1.3 版的 3.8、2.x 版的 3.15,都是 32 位元)用的 numpy,安裝檔約 40 MB。 +- Linux:系統沒有 numpy 時,`install.sh` 會從 PyPI 下載符合的版本到 `~/.local/share/autozhuyin/vendor/`。 +- 設定存在 `%APPDATA%\PIME\autozhuyin\settings.json`、`~/.config/autozhuyin/settings.json`。 ## 個人資料 @@ -311,8 +322,7 @@ THIRD_PARTY_LICENSES.txt 第三方授權聲明(會打包進兩個安裝 - [x] 用資料訓練判斷權重(合成資料) - [ ] 加入真實打字資料一起訓練 - [ ] 已送出的英文單字若判斷錯誤,可以「事後翻轉」 -- [x] AI 選字:語言模型參考前後文挑同音字(macOS) -- [ ] AI 選字移植到 Windows、Linux +- [x] AI 選字:語言模型參考前後文挑同音字(macOS、Windows、Linux) - [x] 使用者詞頻學習(選過的候選字優先) - [ ] 偏好設定視窗 @@ -321,6 +331,7 @@ 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) +- AI 選字語言模型:[CKIP GPT2 Tiny Chinese](https://github.com/ckiplab/ckip-transformers)(中研院詞庫小組,GPL-3.0,全文在 `licenses/GPL-3.0.txt`,並一起打包進三個平台的安裝檔) +- 數值運算:[NumPy](https://numpy.org)(BSD 3-Clause,Windows 版附上、Linux 版安裝時下載) 完整的第三方授權聲明(含小麥注音 MIT 授權全文)見 [THIRD_PARTY_LICENSES.txt](THIRD_PARTY_LICENSES.txt),Mac 版放在 `AutoZhuyin.app/Contents/Resources/`,Windows 版放在 zip 根目錄和安裝後的 `autozhuyin` 資料夾。 diff --git a/THIRD_PARTY_LICENSES.txt b/THIRD_PARTY_LICENSES.txt index 8ced3cc..46b2b7b 100644 --- a/THIRD_PARTY_LICENSES.txt +++ b/THIRD_PARTY_LICENSES.txt @@ -50,8 +50,8 @@ The Windows version runs as a module inside PIME, which users install separately. No PIME code is included in this software. -4. CKIP GPT2 Tiny Chinese — AI 選字語言模型 (language model, macOS) -------------------------------------------------------------------- +4. CKIP GPT2 Tiny Chinese — AI 選字語言模型 (language model) +----------------------------------------------------------- https://huggingface.co/ckiplab/gpt2-tiny-chinese https://github.com/ckiplab/ckip-transformers Author: Mu Yang at CKIP, Academia Sinica (中央研究院 詞庫小組) @@ -62,3 +62,12 @@ lm-vocab.txt is its vocabulary (from bert-base-chinese). They are used unmodifie otherwise, to rerank conversion candidates ("AI 選字"). The source code of this input method, including the conversion script and the code that runs the model, is available from the project repository. + + +5. NumPy — 數值運算 (Windows / Linux, for AI 選字) +------------------------------------------------ +https://numpy.org — BSD 3-Clause License (and the licenses of the libraries it bundles, +such as OpenBLAS). The Windows package includes unmodified NumPy wheels for 32-bit +Python 3.8 and 3.15 under autozhuyin/vendor/; the Linux installer downloads the +matching wheel from PyPI when the system has no NumPy. Each copy keeps its license +files in its numpy-*.dist-info folder. diff --git a/linux/autozhuyin_ibus.py b/linux/autozhuyin_ibus.py index 7d37916..0d56479 100644 --- a/linux/autozhuyin_ibus.py +++ b/linux/autozhuyin_ibus.py @@ -17,6 +17,10 @@ import time HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, HERE) +# numpy for "AI 選字", downloaded by install.sh when the system has none. +_VENDOR = os.path.join(HERE, "vendor", "cp%d%d" % sys.version_info[:2]) +if os.path.isdir(_VENDOR): + sys.path.insert(1, _VENDOR) import gi # noqa: E402 @@ -24,7 +28,9 @@ gi.require_version("IBus", "1.0") from gi.repository import GLib, GObject, IBus # noqa: E402 import autozhuyin # noqa: E402 +from autozhuyin.core.ai import AI, MODE_NAMES # noqa: E402 from autozhuyin.core.history import UserHistory # noqa: E402 +from autozhuyin.core.reranker import MODES # noqa: E402 from autozhuyin.core.classifier import LITERAL, Classifier, LearnedPreferences # noqa: E402 from autozhuyin.core.engine import ( # noqa: E402 BACKSPACE, DOWN, ENTER, ESCAPE, LEFT, RIGHT, SHIFT_TAP, TAB, UP, CompositionEngine, @@ -50,6 +56,15 @@ except OSError: pass _classifier = None +_ai = None + + +def shared_ai(): + """"AI 選字": settings and the language model, loaded in the background once.""" + global _ai + if _ai is None: + _ai = AI(_DATA, _USER_DIR) + return _ai def _save_json(path, data): @@ -118,6 +133,7 @@ class AutoZhuyinEngine(IBus.Engine): if getattr(self, "engine", None) is not None: return self.engine = CompositionEngine(shared_classifier()) + self.engine.reranker = shared_ai().reranker self.shift_tap = ShiftTapDetector() self.table = IBus.LookupTable.new(self.engine.page_size, 0, True, False) self.table.set_orientation(IBus.Orientation.VERTICAL) @@ -127,8 +143,43 @@ class AutoZhuyinEngine(IBus.Engine): symbol=IBus.Text.new_from_string("自"), tooltip=IBus.Text.new_from_string("單按 Shift 切換英文模式"), sensitive=True, visible=True, state=IBus.PropState.UNCHECKED) + # "AI 選字" menu: modes (radio), auto-pause (toggle), status, clear learned data. + ai = shared_ai() + self.ai_props = {} + sub = IBus.PropList() + for mode in MODES: + prop = IBus.Property( + key="ai." + mode, prop_type=IBus.PropType.RADIO, + label=IBus.Text.new_from_string("AI 選字:" + MODE_NAMES[mode]), + tooltip=IBus.Text.new_from_string(""), sensitive=True, visible=True, + state=IBus.PropState.CHECKED if ai.reranker.mode == mode else IBus.PropState.UNCHECKED) + self.ai_props[mode] = prop + sub.append(prop) + self.pause_prop = IBus.Property( + key="ai.autopause", prop_type=IBus.PropType.TOGGLE, + label=IBus.Text.new_from_string("系統忙碌時自動暫停 AI"), + tooltip=IBus.Text.new_from_string(""), sensitive=True, visible=True, + state=IBus.PropState.CHECKED if ai.auto_pause else IBus.PropState.UNCHECKED) + self.status_prop = IBus.Property( + key="ai.status", prop_type=IBus.PropType.NORMAL, + label=IBus.Text.new_from_string(ai.status_text()), + tooltip=IBus.Text.new_from_string(""), sensitive=False, visible=True, + state=IBus.PropState.UNCHECKED) + self.clear_prop = IBus.Property( + key="clear", prop_type=IBus.PropType.NORMAL, + label=IBus.Text.new_from_string("清除學習紀錄"), + tooltip=IBus.Text.new_from_string(""), sensitive=True, visible=True, + state=IBus.PropState.UNCHECKED) + for prop in (self.pause_prop, self.status_prop, self.clear_prop): + sub.append(prop) + self.ai_menu = IBus.Property( + key="ai", prop_type=IBus.PropType.MENU, + label=IBus.Text.new_from_string("設定"), + tooltip=IBus.Text.new_from_string("AI 選字與學習紀錄"), + sensitive=True, visible=True, state=IBus.PropState.UNCHECKED, sub_props=sub) self.props = IBus.PropList() self.props.append(self.mode_prop) + self.props.append(self.ai_menu) # --- keys --- @@ -211,12 +262,38 @@ class AutoZhuyinEngine(IBus.Engine): self._update(None) return True + # --- settings menu --- + + def do_property_activate(self, prop_name, state): + self._setup() + ai = shared_ai() + if prop_name.startswith("ai.") and prop_name[3:] in MODES: + if state == IBus.PropState.CHECKED: + ai.set_mode(prop_name[3:]) + elif prop_name == "ai.autopause": + ai.set_auto_pause(state == IBus.PropState.CHECKED) + elif prop_name == "clear": + shared_classifier().learned.clear() + shared_classifier().history.clear() + self._refresh_ai_props() + + def _refresh_ai_props(self): + ai = shared_ai() + for mode, prop in self.ai_props.items(): + prop.set_state(IBus.PropState.CHECKED if ai.reranker.mode == mode else IBus.PropState.UNCHECKED) + self.update_property(prop) + self.pause_prop.set_state(IBus.PropState.CHECKED if ai.auto_pause else IBus.PropState.UNCHECKED) + self.update_property(self.pause_prop) + self.status_prop.set_label(IBus.Text.new_from_string(ai.status_text())) + self.update_property(self.status_prop) + # --- lifecycle --- def do_focus_in(self): self._setup() self.register_properties(self.props) self._update_mode() + self._refresh_ai_props() def do_focus_out(self): self._setup() diff --git a/linux/installer/README.txt b/linux/installer/README.txt index 1b68f2f..dec75b3 100644 --- a/linux/installer/README.txt +++ b/linux/installer/README.txt @@ -40,6 +40,16 @@ 注音符號的順序打錯也沒關係,例如 ㄧㄒㄠˇ 會自動變成 ㄒㄧㄠˇ(小)。 +AI 選字 +------- +用中研院 CKIP 的繁體中文小型語言模型,看前後文挑同音字(他跑「得」很快、請你「再」說一次), +完全在本機運算。需要 numpy:系統沒有的話,install.sh 會從 PyPI 下載符合的版本(約 15 MB)。 +在 IBus/系統匣的輸入法選單「設定」裡可以選: + AI 選字:關閉/省電/標準(預設)/高品質 + 系統忙碌時自動暫停 AI(預設開啟:CPU 很忙或過熱時暫停,省電設定或電量低時改用省電) +設定存在 ~/.config/autozhuyin/settings.json。 + + 切換與移除 ---------- 暫時改回一般鍵盤: ibus engine xkb:us::eng diff --git a/linux/installer/install.sh b/linux/installer/install.sh index 74243a1..f01107c 100755 --- a/linux/installer/install.sh +++ b/linux/installer/install.sh @@ -23,13 +23,31 @@ fi # 2. Copy the program. pkill -f "autozhuyin/autozhuyin_ibus.py" 2>/dev/null || true +VENDOR_BACKUP="" +if [ -d "$DEST/vendor" ]; then # keep numpy downloaded by an earlier install + VENDOR_BACKUP="$(mktemp -d)" + mv "$DEST/vendor" "$VENDOR_BACKUP/" +fi rm -rf "$DEST" mkdir -p "$DEST" -cp -R autozhuyin autozhuyin_ibus.py version.txt "$DEST/" -[ -f icon.png ] && cp icon.png "$DEST/" -[ -f THIRD_PARTY_LICENSES.txt ] && cp THIRD_PARTY_LICENSES.txt "$DEST/" +cp -R autozhuyin autozhuyin_ibus.py version.txt tools "$DEST/" +for f in icon.png THIRD_PARTY_LICENSES.txt GPL-3.0.txt; do [ -f "$f" ] && cp "$f" "$DEST/"; done +[ -n "$VENDOR_BACKUP" ] && mv "$VENDOR_BACKUP/vendor" "$DEST/" && rmdir "$VENDOR_BACKUP" chmod +x "$DEST/autozhuyin_ibus.py" +# 2b. numpy for "AI 選字": use the system's, or download the matching version from PyPI. +TAG="$("$PY" -c 'import sys; print("cp%d%d" % sys.version_info[:2])')" +if "$PY" -c 'import numpy' 2>/dev/null; then + echo "AI 選字:使用系統的 numpy。" +elif [ -d "$DEST/vendor/$TAG/numpy" ]; then + echo "AI 選字:使用先前下載的 numpy。" +else + echo "AI 選字需要 numpy,正在從 PyPI 下載(約 15 MB)…" + if ! "$PY" -I "$DEST/tools/fetch_numpy.py" "$DEST/vendor" auto auto; then + echo "下載 numpy 失敗:輸入法照常可用,只是沒有 AI 選字。之後可以重新執行 install.sh。" + fi +fi + # 3. Start with the desktop session: registers the engine with IBus and selects it. mkdir -p "$AUTOSTART" cat > "$AUTOSTART/autozhuyin.desktop" </dev/null # "AI 選字" model python3 -I windows/tools/build_data.py (cd windows && python3 -m unittest discover -q tests) python3 -m unittest discover -q -s linux/tests @@ -21,7 +22,9 @@ mkdir -p "$OUT/autozhuyin" rsync -a --exclude __pycache__ --exclude '*.pyc' \ windows/autozhuyin/__init__.py windows/autozhuyin/core windows/autozhuyin/data "$OUT/autozhuyin/" cp linux/autozhuyin_ibus.py linux/installer/install.sh linux/installer/uninstall.sh linux/installer/README.txt \ - THIRD_PARTY_LICENSES.txt "$OUT/" + THIRD_PARTY_LICENSES.txt licenses/GPL-3.0.txt "$OUT/" +mkdir -p "$OUT/tools" +cp windows/tools/fetch_numpy.py "$OUT/tools/" # install.sh downloads numpy if the system has none echo "$VERSION" > "$OUT/version.txt" sips -s format png -z 64 64 build/icon.tiff --out "$OUT/icon.png" >/dev/null chmod +x "$OUT/install.sh" "$OUT/uninstall.sh" "$OUT/autozhuyin_ibus.py" diff --git a/scripts/package-windows.sh b/scripts/package-windows.sh index 92718f7..80d3cf7 100755 --- a/scripts/package-windows.sh +++ b/scripts/package-windows.sh @@ -8,6 +8,7 @@ VERSION="$(python3 -I -c 'import json,sys; print(json.load(open(sys.argv[1]))["v NAME="AutoZhuyin-Windows-$VERSION" OUT="build/$NAME" +[ -s build/lm/lm.bin ] || scripts/fetch-lm.sh >/dev/null # "AI 選字" model python3 -I windows/tools/build_data.py (cd windows && python3 -m unittest discover -q tests) @@ -29,6 +30,14 @@ done # Third-party notices: next to the installer and inside the installed folder. sed 's/$/\r/' THIRD_PARTY_LICENSES.txt > "$OUT/THIRD_PARTY_LICENSES.txt" cp "$OUT/THIRD_PARTY_LICENSES.txt" "$OUT/autozhuyin/" +sed 's/$/\r/' licenses/GPL-3.0.txt > "$OUT/autozhuyin/GPL-3.0.txt" + +# numpy for "AI 選字", for both Pythons PIME ships (1.3: 3.8, 2.x: 3.15; both 32-bit). +for tag in cp38 cp315; do + [ -d "build/numpy-vendor/$tag" ] || python3 -I windows/tools/fetch_numpy.py build/numpy-vendor "$tag" win32 +done +mkdir -p "$OUT/autozhuyin/vendor" +cp -R build/numpy-vendor/cp38 build/numpy-vendor/cp315 "$OUT/autozhuyin/vendor/" (cd build && zip -qr -X "$NAME.zip" "$NAME") echo "Built build/$NAME.zip" diff --git a/windows/autozhuyin/autozhuyin_ime.py b/windows/autozhuyin/autozhuyin_ime.py index 8c35bc7..ff6a89b 100644 --- a/windows/autozhuyin/autozhuyin_ime.py +++ b/windows/autozhuyin/autozhuyin_ime.py @@ -6,12 +6,21 @@ tests); this file only translates PIME key events and replies. """ import json import os +import sys import time -from keycodes import * # noqa: F401,F403 (VK_* constants, provided by PIME) -from textService import TextService +# numpy for "AI 選字" is bundled per Python version (PIME 1.3 has 3.8, PIME 2 has 3.15). +_VENDOR = os.path.join(os.path.dirname(os.path.abspath(__file__)), "vendor", + "cp%d%d" % sys.version_info[:2]) +if os.path.isdir(_VENDOR) and _VENDOR not in sys.path: + sys.path.insert(0, _VENDOR) +from keycodes import * # noqa: F401,F403 (VK_* constants, provided by PIME) +from textService import COMMAND_LEFT_CLICK, COMMAND_MENU, TextService # noqa: F401 + +from .core.ai import AI, MODE_NAMES from .core.history import UserHistory +from .core.reranker import MODES from .core.classifier import Classifier, LearnedPreferences from .core.engine import BACKSPACE, DOWN, ENTER, ESCAPE, LEFT, RIGHT, SHIFT_TAP, TAB, UP, CompositionEngine from .core.english import EnglishModel @@ -26,6 +35,11 @@ _USER_PHRASES = os.path.join(_USER_DIR, "user-phrases.txt") _HISTORY_FILE = os.path.join(_USER_DIR, "user-history.json") _classifier = None +_ai = None + +ID_MODE_ICON, ID_AUTO_PAUSE, ID_STATUS, ID_CLEAR = 1, 2, 3, 4 +ID_AI_MODE = {mode: 10 + i for i, mode in enumerate(MODES)} # 10..13 +_ICON = os.path.join(_HERE, "icon.ico") def _save_json(path, data): @@ -72,6 +86,14 @@ def _shared_classifier(): return _classifier +def _shared_ai(): + """"AI 選字": settings and the language model, loaded in the background once.""" + global _ai + if _ai is None: + _ai = AI(_DATA, _USER_DIR) + return _ai + + _SPECIAL = { VK_BACK: BACKSPACE, VK_RETURN: ENTER, VK_ESCAPE: ESCAPE, VK_TAB: TAB, # noqa: F405 VK_UP: UP, VK_DOWN: DOWN, VK_LEFT: LEFT, VK_RIGHT: RIGHT, # noqa: F405 @@ -83,6 +105,7 @@ class AutoZhuyinTextService(TextService): TextService.__init__(self, client) self.engine = CompositionEngine(_shared_classifier()) self.shift_tap = ShiftTapDetector() + self.engine.reranker = _shared_ai().reranker # --- lifecycle --- @@ -90,11 +113,52 @@ class AutoZhuyinTextService(TextService): TextService.onActivate(self) self.setSelKeys("123456789") self.customizeUI(candFontSize=16, candPerRow=1, candUseCursor=True) + # Taskbar IME icon (Windows 8+) and language bar button; both open the settings menu. + self.addButton("windows-mode-icon", icon=_ICON, tooltip="自動注音(右鍵:設定)", + commandId=ID_MODE_ICON) + self.addButton("autozhuyin-settings", icon=_ICON, tooltip="自動注音設定", type="menu") def onDeactivate(self): self.engine.reset() + self.removeButton("windows-mode-icon") + self.removeButton("autozhuyin-settings") TextService.onDeactivate(self) + # --- menu (AI 選字 settings) --- + + def onMenu(self, buttonId): + if buttonId not in ("windows-mode-icon", "autozhuyin-settings"): + return None + ai = _shared_ai() + items = [{"text": "AI 選字:" + MODE_NAMES[m], "id": ID_AI_MODE[m], "checked": ai.reranker.mode == m} + for m in MODES] + items += [ + {}, + {"text": "系統忙碌時自動暫停 AI", "id": ID_AUTO_PAUSE, "checked": ai.auto_pause}, + {"text": ai.status_text(), "id": ID_STATUS, "enabled": False}, + {}, + {"text": "清除學習紀錄", "id": ID_CLEAR}, + ] + return items + + def onCommand(self, commandId, commandType): + ai = _shared_ai() + if commandId == ID_MODE_ICON and commandType == COMMAND_LEFT_CLICK and not self.engine.keys: + self.engine.handle(SHIFT_TAP) # left click: English mode on / off + self.showMessage("英文" if self.engine.english_mode else "中英自動", 1) + return + for mode, cid in ID_AI_MODE.items(): + if commandId == cid: + ai.set_mode(mode) + self.showMessage("AI 選字:" + MODE_NAMES[mode], 1) + return + if commandId == ID_AUTO_PAUSE: + ai.set_auto_pause(not ai.auto_pause) + elif commandId == ID_CLEAR: + _shared_classifier().learned.clear() + _shared_classifier().history.clear() + self.showMessage("已清除學習紀錄", 1) + def onCompositionTerminated(self, forced): # The app ended the composition (focus change, mouse click): the shown text stays. self.engine.reset() diff --git a/windows/autozhuyin/core/ai.py b/windows/autozhuyin/core/ai.py new file mode 100644 index 0000000..1188740 --- /dev/null +++ b/windows/autozhuyin/core/ai.py @@ -0,0 +1,108 @@ +""""AI 選字" for the Windows and Linux shells: settings, background model loading, +system-load detection and the status line shown in the menu.""" +import json +import os +import threading + +from . import lm +from .reranker import (MODES, NORMAL, OFF, ST_ACTIVE, ST_BUSY, ST_LOADING, ST_OFF, ST_SLOW, + STANDARD, Reranker) +from .system_monitor import REDUCED, SystemMonitor + +MODE_NAMES = {OFF: "關閉", "saver": "省電", STANDARD: "標準", "quality": "高品質"} + + +class AI: + def __init__(self, data_dir, user_dir, monitor=None, background=True): + self.model_path = os.path.join(data_dir, "lm.bin") + self.vocab_path = os.path.join(data_dir, "lm-vocab.txt") + self.settings_path = os.path.join(user_dir, "settings.json") + self.monitor = monitor or SystemMonitor() + self.background = background + settings = self._load_settings() + mode = settings.get("ai_mode", STANDARD) + self.auto_pause = bool(settings.get("ai_auto_pause", True)) + self.reranker = Reranker(mode if mode in MODES else STANDARD) + self.reranker.system_state = lambda: self.monitor.state() if self.auto_pause else NORMAL + self.error = None # why the model cannot be used, if it cannot + self._loading = False + self.load_model() + + # --- settings --- + + def _load_settings(self): + try: + with open(self.settings_path, encoding="utf-8") as f: + data = json.load(f) + return data if isinstance(data, dict) else {} + except (OSError, ValueError): + return {} + + def _save_settings(self): + data = self._load_settings() + data.update(ai_mode=self.reranker.mode, ai_auto_pause=self.auto_pause) + try: + os.makedirs(os.path.dirname(self.settings_path), exist_ok=True) + with open(self.settings_path, "w", encoding="utf-8") as f: + json.dump(data, f, ensure_ascii=False, indent=1) + except OSError: + pass + + def set_mode(self, mode): + if mode not in MODES: + return + self.reranker.mode = mode + if mode == OFF: + self.reranker.model = None # free the memory + else: + self.load_model() + self._save_settings() + + def set_auto_pause(self, on): + self.auto_pause = bool(on) + self._save_settings() + + # --- model --- + + def load_model(self): + if self.reranker.mode == OFF or self.reranker.model is not None or self._loading or self.error: + return + if not lm.available(): + self.error = "需要 numpy" + return + if not (os.path.exists(self.model_path) and os.path.exists(self.vocab_path)): + self.error = "AI 模型未安裝" + return + self._loading = True + + def work(): + try: + model = lm.GPT2LanguageModel(self.model_path, self.vocab_path) + except Exception as e: # noqa: BLE001 — any failure just disables AI + self.error = f"AI 模型載入失敗:{e}" + model = None + self._loading = False + if self.reranker.mode != OFF: + self.reranker.model = model + + if self.background: + threading.Thread(target=work, daemon=True).start() + else: + work() + + def status_text(self): + if self.error: + return self.error + self.reranker.effective_mode() # refresh the status + st = self.reranker.status + if st == ST_OFF: + return "AI 選字已關閉" + if st == ST_LOADING: + return "AI 模型載入中…" + if st == ST_BUSY: + return "AI 已暫停:系統忙碌或過熱" + if st == ST_SLOW: + return "AI 已暫停:運算太慢,一分鐘後再試" + if st == ST_ACTIVE and self.auto_pause and self.monitor.state() == REDUCED: + return "AI 使用中(省電模式)" + return "AI 使用中" diff --git a/windows/autozhuyin/core/engine.py b/windows/autozhuyin/core/engine.py index da77237..7f5f427 100644 --- a/windows/autozhuyin/core/engine.py +++ b/windows/autozhuyin/core/engine.py @@ -65,6 +65,8 @@ class CompositionEngine: self.inserting = [] # Index of a held space (see HOLD_MIN_SCORE), or None. self.held = None + self.reranker = None # "AI 選字" (reranker.Reranker); None = lexicon only + self._recent_text = "" # last characters committed: context for the reranker self._previous_mode = None self._previous_time = float("-inf") self._cache_key = None @@ -159,8 +161,15 @@ class CompositionEngine: def _conversion(self, seg, final): syl, pending = self._convertible(seg, final) fx = {k: v for k, v in self.fixed.get(seg[0], {}).items() if k[1] <= len(syl)} + if self.reranker is not None: + chosen = self.reranker.choose(self.converter, syl, fx, self._rerank_context()) + if chosen is not None: + return chosen, pending return self.converter.convert(syl, fx), pending + def _rerank_context(self): + return self._recent_text if self.now() - self._previous_time < self.context_window else "" + def _render(self, seg, final): start, end, mode = seg if mode == LITERAL: @@ -556,6 +565,7 @@ class CompositionEngine: segs = segs if segs is not None else self.segments text = "".join(self._render(s, True) for s in segs) self._remember_picks(segs) + self._recent_text = (self._rerank_context() + text)[-20:] if self.forced_mode: self.classifier.learned.record("".join(self.keys), self.forced_mode) self._previous_mode = segs[-1][2] if segs else None diff --git a/windows/autozhuyin/core/lexicon.py b/windows/autozhuyin/core/lexicon.py index be88ece..66ab4d3 100644 --- a/windows/autozhuyin/core/lexicon.py +++ b/windows/autozhuyin/core/lexicon.py @@ -130,6 +130,28 @@ class Converter: j = seg[0][0] return segs[::-1] + def kbest(self, syllables, k, fixed=None): + """The k best paths [(log10 score, [((start, end), text)])], best first.""" + fixed = fixed or {} + n = len(syllables) + if n == 0 or k <= 0: + return [] + max_len = max(1, self.lexicon.max_phrase_length) + beams = [[] for _ in range(n + 1)] + beams[0] = [(0.0, [])] + for j in range(1, n + 1): + cand = [] + for length in range(1, min(max_len, j) + 1): + i = j - length + if not beams[i]: + continue + for text, score in self._options(syllables, i, j, fixed): + for s, segs in beams[i]: + cand.append((s + score, segs + [((i, j), text)])) + cand.sort(key=lambda e: -e[0]) + beams[j] = cand[:k] + return beams[n] + def candidates(self, syllables, end): """[((start, end), text)] for phrases ending at syllable `end`, longest first.""" if end <= 0 or end > len(syllables): diff --git a/windows/autozhuyin/core/lm.py b/windows/autozhuyin/core/lm.py new file mode 100644 index 0000000..acc9ef3 --- /dev/null +++ b/windows/autozhuyin/core/lm.py @@ -0,0 +1,109 @@ +"""GPT-2 language model for "AI 選字" (numpy). Port of LanguageModel.swift. + +Reads the "AZLM" file made by training/lm/export_lm.py (CKIP gpt2-tiny-chinese, GPL-3.0, +float16 weights, expanded to float32 in memory). numpy is optional: when it cannot be +imported, `available()` is False and the input method works without AI. +""" +import json +import math +import struct + +try: + import numpy as np +except Exception: # ImportError, or a numpy build for another Python / architecture + np = None + +CLS, UNK = 101, 100 + + +def available(): + return np is not None + + +class GPT2LanguageModel: + max_tokens = 64 + + def __init__(self, model_path, vocab_path): + if np is None: + raise RuntimeError("numpy is not available") + with open(model_path, "rb") as f: + raw = f.read() + if raw[:4] != b"AZLM": + raise ValueError("not an AZLM file") + n = struct.unpack("