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 <noreply@anthropic.com>
This commit is contained in:
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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" <<DESKTOP
|
||||
|
||||
@@ -54,9 +54,10 @@ class LookupTable:
|
||||
|
||||
|
||||
class Property:
|
||||
def __init__(self, **kw): self.label = kw["label"].text
|
||||
def __init__(self, **kw): self.key, self.label, self.state = kw["key"], kw["label"].text, kw.get("state", 0)
|
||||
def set_label(self, t): self.label = t.text
|
||||
def set_symbol(self, t): pass
|
||||
def set_state(self, s): self.state = s
|
||||
|
||||
|
||||
class Engine:
|
||||
@@ -71,7 +72,9 @@ class Engine:
|
||||
def update_lookup_table(self, table, visible): self.shown = list(table.items)
|
||||
def hide_lookup_table(self): self.shown = None
|
||||
def register_properties(self, p): pass
|
||||
def update_property(self, p): self.mode_label = p.label
|
||||
def update_property(self, p):
|
||||
if p.key == "mode":
|
||||
self.mode_label = p.label
|
||||
|
||||
|
||||
IBus.Text, IBus.AttrList, IBus.LookupTable, IBus.Property, IBus.Engine = Text, AttrList, LookupTable, Property, Engine
|
||||
@@ -80,8 +83,8 @@ IBus.attr_underline_new = lambda kind, a, b: ("underline", a, b)
|
||||
IBus.attr_foreground_new = lambda color, a, b: ("color", a, b)
|
||||
IBus.AttrUnderline = types.SimpleNamespace(SINGLE=1)
|
||||
IBus.Orientation = types.SimpleNamespace(VERTICAL=1)
|
||||
IBus.PropType = types.SimpleNamespace(NORMAL=0)
|
||||
IBus.PropState = types.SimpleNamespace(UNCHECKED=0)
|
||||
IBus.PropType = types.SimpleNamespace(NORMAL=0, TOGGLE=1, RADIO=2, MENU=3)
|
||||
IBus.PropState = types.SimpleNamespace(UNCHECKED=0, CHECKED=1)
|
||||
IBus.PreeditFocusMode = types.SimpleNamespace(COMMIT=1)
|
||||
|
||||
gi = types.ModuleType("gi")
|
||||
@@ -173,6 +176,19 @@ class IBusEngineTests(unittest.TestCase):
|
||||
press(self.e, "su3 \n")
|
||||
self.assertTrue(self.e.committed.endswith(second))
|
||||
|
||||
def test_ai_menu(self):
|
||||
e = self.e
|
||||
self.assertEqual(e.ai_props["standard"].state, 1)
|
||||
e.do_property_activate("ai.saver", 1)
|
||||
self.assertEqual(autozhuyin_ibus.shared_ai().reranker.mode, "saver")
|
||||
self.assertEqual(e.ai_props["saver"].state, 1)
|
||||
self.assertEqual(e.ai_props["standard"].state, 0)
|
||||
e.do_property_activate("ai.autopause", 0)
|
||||
self.assertFalse(autozhuyin_ibus.shared_ai().auto_pause)
|
||||
self.assertTrue(e.status_prop.label)
|
||||
e.do_property_activate("ai.standard", 1)
|
||||
e.do_property_activate("ai.autopause", 1)
|
||||
|
||||
def test_focus_out_resets(self):
|
||||
press(self.e, "su3")
|
||||
self.e.do_focus_out()
|
||||
|
||||
Reference in New Issue
Block a user