Hold words typed with digit keys; one Tab undoes a wrong learned preference
"0 5j; " (安裝) committed "0 " at once: "0" had been learned as literal, and the space hold only considered words made of letters. Any Dachen keys now count (0 = ㄢ, 5j; = ㄓㄨㄤ). A Tab correction against a learned preference replaces it instead of stepping by one, and only the keys present at the Tab are learned, not everything typed after them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -19,7 +19,9 @@ class LearnedPreferences:
|
||||
|
||||
def record(self, keys, mode):
|
||||
delta = 1 if mode == ZHUYIN else -1
|
||||
self.weights[keys] = max(-3, min(3, self.weights.get(keys, 0) + delta))
|
||||
old = self.weights.get(keys, 0)
|
||||
# A correction against what was learned replaces it, so one Tab is enough to undo it.
|
||||
self.weights[keys] = delta if old * delta < 0 else max(-3, min(3, old + delta))
|
||||
self._on_change(self.weights)
|
||||
|
||||
def clear(self):
|
||||
|
||||
@@ -50,6 +50,7 @@ class CompositionEngine:
|
||||
self.now = now
|
||||
self.keys = []
|
||||
self.forced_mode = None
|
||||
self.forced_count = 0
|
||||
# English mode, toggled by a Shift tap when nothing is being typed: keys go straight
|
||||
# to the app (no composition, no underline) until Shift is tapped again.
|
||||
self.english_mode = False
|
||||
@@ -117,8 +118,9 @@ class CompositionEngine:
|
||||
return dot(w, zh_whole) >= dot(w, word) + dot(w, zh_rest)
|
||||
|
||||
def _is_ambiguous_word(self, keys):
|
||||
"""Whether keys + space also read as a common Chinese syllable (see HOLD_MIN_SCORE)."""
|
||||
if not all(k.isascii() and k.isalpha() for k in keys):
|
||||
"""Whether keys + space also read as a common Chinese syllable (see HOLD_MIN_SCORE).
|
||||
Any Dachen keys count, not only letters: "0" is ㄢ (安), "5j;" is ㄓㄨㄤ (裝)."""
|
||||
if not all(bopomofo.is_zhuyin_key(k) for k in keys):
|
||||
return False
|
||||
p = bopomofo.parse(list(keys) + [" "])
|
||||
if p.pending is not None or not p.syllables or p.stray_tones or p.foreign_keys:
|
||||
@@ -365,6 +367,7 @@ class CompositionEngine:
|
||||
def _flip(self):
|
||||
"""Forces the whole buffer into the other mode (a correction that is learned)."""
|
||||
self.forced_mode = LITERAL if self.mode == ZHUYIN else ZHUYIN
|
||||
self.forced_count = len(self.keys) # that word is learned, not what is typed after it
|
||||
|
||||
def wants(self, key):
|
||||
"""Whether handle(key) would consume the key, without changing state."""
|
||||
@@ -567,7 +570,7 @@ class CompositionEngine:
|
||||
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.classifier.learned.record("".join(self.keys[:self.forced_count]), self.forced_mode)
|
||||
self._previous_mode = segs[-1][2] if segs else None
|
||||
self._previous_time = self.now()
|
||||
self.reset()
|
||||
|
||||
@@ -340,6 +340,23 @@ class HeldWordTests(unittest.TestCase):
|
||||
self.assertEqual(e.marked_text, "up")
|
||||
self.assertEqual(type_keys(" the ", e), "up the ")
|
||||
|
||||
def _engine(self, learned):
|
||||
return CompositionEngine(Classifier(LEX, ENG, learned, weights=WEIGHTS))
|
||||
|
||||
def test_held_word_with_digit_keys(self):
|
||||
"""Words with digits / punctuation keys are held too: "0 " is 安, "5j; " is 裝."""
|
||||
learned = LearnedPreferences({"0": -2}) # "0" once learned as a number
|
||||
self.assertEqual(type_keys("0 5j; \n", self._engine(learned)), "安裝")
|
||||
self.assertEqual(type_keys("0 \n", self._engine(learned)), "0 ")
|
||||
|
||||
def test_tab_overrides_learned(self):
|
||||
"""One Tab undoes a wrong learned preference, and only the word flipped is learned."""
|
||||
learned = LearnedPreferences({"0": -2})
|
||||
self.assertEqual(type_keys("0\t 5j; \n", self._engine(learned)), "安裝")
|
||||
self.assertEqual(learned.weights["0"], 1)
|
||||
self.assertNotIn("0 5j; ", learned.weights)
|
||||
self.assertEqual(type_keys("hello 0 5j; \n", self._engine(learned)), "hello 安裝")
|
||||
|
||||
|
||||
class BacktickSymbolTests(unittest.TestCase):
|
||||
def test_forced_symbols(self):
|
||||
|
||||
Reference in New Issue
Block a user