Remember picked candidates (user history)
Candidates chosen with the arrow-down list are recorded on commit as toneless reading -> phrase with a count. The converter lifts a picked phrase just above the best lexicon entry for that reading (more for more picks), so it comes first next time without overriding longer phrases. Up to 2000 pairs, least recently used evicted. Stored as user-history.json on every platform; the Mac menu's clear item clears it too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
+23
-10
@@ -24,6 +24,7 @@ gi.require_version("IBus", "1.0")
|
||||
from gi.repository import GLib, GObject, IBus # noqa: E402
|
||||
|
||||
import autozhuyin # noqa: E402
|
||||
from autozhuyin.core.history import UserHistory # 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,
|
||||
@@ -40,6 +41,7 @@ _DATA = os.path.join(os.path.dirname(os.path.abspath(autozhuyin.__file__)), "dat
|
||||
_USER_DIR = os.path.join(os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser("~/.config"), "autozhuyin")
|
||||
_LEARNED_FILE = os.path.join(_USER_DIR, "learned.json")
|
||||
_USER_PHRASES = os.path.join(_USER_DIR, "user-phrases.txt")
|
||||
_HISTORY_FILE = os.path.join(_USER_DIR, "user-history.json")
|
||||
|
||||
try:
|
||||
with open(os.path.join(HERE, "version.txt"), encoding="utf-8") as _f:
|
||||
@@ -50,15 +52,31 @@ except OSError:
|
||||
_classifier = None
|
||||
|
||||
|
||||
def _save_learned(weights):
|
||||
def _save_json(path, data):
|
||||
try:
|
||||
os.makedirs(_USER_DIR, exist_ok=True)
|
||||
with open(_LEARNED_FILE, "w", encoding="utf-8") as f:
|
||||
json.dump(weights, f, ensure_ascii=False)
|
||||
with open(path, "w", encoding="utf-8") as f:
|
||||
json.dump(data, f, ensure_ascii=False)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
||||
def _load_json(path):
|
||||
try:
|
||||
with open(path, encoding="utf-8") as f:
|
||||
return json.load(f)
|
||||
except (OSError, ValueError):
|
||||
return {}
|
||||
|
||||
|
||||
def _save_learned(weights):
|
||||
_save_json(_LEARNED_FILE, weights)
|
||||
|
||||
|
||||
def _save_history(data):
|
||||
_save_json(_HISTORY_FILE, data)
|
||||
|
||||
|
||||
def shared_classifier():
|
||||
"""Loads the lexicon and English model once per process."""
|
||||
global _classifier
|
||||
@@ -72,14 +90,9 @@ def shared_classifier():
|
||||
os.path.join(_DATA, "english-dict.txt"),
|
||||
os.path.join(_DATA, "english-bigram.json"),
|
||||
)
|
||||
weights = {}
|
||||
try:
|
||||
with open(_LEARNED_FILE, encoding="utf-8") as f:
|
||||
weights = json.load(f)
|
||||
except (OSError, ValueError):
|
||||
pass
|
||||
trained = Classifier.load_weights(os.path.join(_DATA, "classifier-weights.json"))
|
||||
_classifier = Classifier(lexicon, english, LearnedPreferences(weights, _save_learned), trained)
|
||||
_classifier = Classifier(lexicon, english, LearnedPreferences(_load_json(_LEARNED_FILE), _save_learned),
|
||||
trained, UserHistory(_load_json(_HISTORY_FILE), _save_history))
|
||||
return _classifier
|
||||
|
||||
|
||||
|
||||
@@ -30,7 +30,8 @@
|
||||
Tab 判斷錯了?按 Tab 在中/英之間翻轉,輸入法會記住這次修正
|
||||
← → 在組字區移動游標;游標在中間時直接打注音會插入新字,
|
||||
Backspace 刪掉游標前的字
|
||||
↓ 開啟游標前那個字或詞的候選字,按數字鍵 1–9 選字
|
||||
↓ 開啟游標前那個字或詞的候選字,按數字鍵 1–9 選字;
|
||||
選過的字會被記住,下次優先出現
|
||||
Esc 取消組字
|
||||
單按 Shift 切換英文模式(按鍵直接送出),再單按一次回到中英自動判斷
|
||||
Shift+字母 打大寫字母(也會強制英文)
|
||||
@@ -47,6 +48,7 @@
|
||||
|
||||
個人資料(學習紀錄、自訂詞)在 ~/.config/autozhuyin/:
|
||||
learned.json 按 Tab 修正的學習紀錄
|
||||
user-history.json 選字紀錄(選過的候選字)
|
||||
user-phrases.txt 自訂詞,一行一個,例如:ㄗˋ-ㄉㄨㄥˋ 自動 -1
|
||||
|
||||
|
||||
|
||||
@@ -8,6 +8,9 @@ import types
|
||||
import unittest
|
||||
|
||||
LINUX = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
|
||||
import tempfile # noqa: E402
|
||||
|
||||
os.environ["XDG_CONFIG_HOME"] = tempfile.mkdtemp(prefix="autozhuyin-test-") # learned / history files
|
||||
sys.path.insert(0, os.path.join(os.path.dirname(LINUX), "windows")) # autozhuyin package
|
||||
sys.path.insert(0, LINUX)
|
||||
|
||||
@@ -107,6 +110,8 @@ def press(e, keys):
|
||||
|
||||
class IBusEngineTests(unittest.TestCase):
|
||||
def setUp(self):
|
||||
autozhuyin_ibus.shared_classifier().history.clear()
|
||||
autozhuyin_ibus.shared_classifier().learned.clear()
|
||||
self.e = autozhuyin_ibus.AutoZhuyinEngine()
|
||||
|
||||
def test_mixed_sentence(self):
|
||||
@@ -157,6 +162,17 @@ class IBusEngineTests(unittest.TestCase):
|
||||
press(self.e, "rup fu4←wu0 \n")
|
||||
self.assertEqual(self.e.committed, "今天氣")
|
||||
|
||||
def test_picked_candidate_saved(self):
|
||||
press(self.e, "su3↓")
|
||||
second = self.e.shown[1]
|
||||
self.e.do_candidate_clicked(1, 1, 0)
|
||||
press(self.e, "\n")
|
||||
path = os.path.join(os.environ["XDG_CONFIG_HOME"], "autozhuyin", "user-history.json")
|
||||
with open(path, encoding="utf-8") as f:
|
||||
self.assertIn(second, f.read())
|
||||
press(self.e, "su3 \n")
|
||||
self.assertTrue(self.e.committed.endswith(second))
|
||||
|
||||
def test_focus_out_resets(self):
|
||||
press(self.e, "su3")
|
||||
self.e.do_focus_out()
|
||||
|
||||
Reference in New Issue
Block a user