Compare commits

...
2 Commits
Author SHA1 Message Date
ays953 e98103b1f8 Release v0.14.1 2026-11-17 10:49:48 +08:00
ays953andClaude Opus 5.5 ec3ee4ba11 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>
2026-11-17 10:49:28 +08:00
8 changed files with 59 additions and 12 deletions
+2 -2
View File
@@ -15,9 +15,9 @@
<key>CFBundlePackageType</key> <key>CFBundlePackageType</key>
<string>APPL</string> <string>APPL</string>
<key>CFBundleShortVersionString</key> <key>CFBundleShortVersionString</key>
<string>0.14.0</string> <string>0.14.1</string>
<key>CFBundleVersion</key> <key>CFBundleVersion</key>
<string>24</string> <string>25</string>
<key>InputMethodConnectionName</key> <key>InputMethodConnectionName</key>
<string>com.autozhuyin.inputmethod.AutoZhuyin_Connection</string> <string>com.autozhuyin.inputmethod.AutoZhuyin_Connection</string>
<key>InputMethodServerControllerClass</key> <key>InputMethodServerControllerClass</key>
+3 -1
View File
@@ -37,7 +37,9 @@ public final class LearnedPreferences {
public func record(_ keys: String, as mode: InputMode) { public func record(_ keys: String, as mode: InputMode) {
let delta = mode == .zhuyin ? 1 : -1 let delta = mode == .zhuyin ? 1 : -1
weights[keys] = max(-3, min(3, (weights[keys] ?? 0) + delta)) let old = weights[keys] ?? 0
// A correction against what was learned replaces it, so one Tab is enough to undo it.
weights[keys] = old * delta < 0 ? delta : max(-3, min(3, old + delta))
onChange(weights) onChange(weights)
} }
@@ -38,6 +38,9 @@ public final class CompositionEngine {
/// Set by Tab / a Shift tap while typing: overrides the classifier for the whole /// Set by Tab / a Shift tap while typing: overrides the classifier for the whole
/// buffer. It is a correction, learned on commit. /// buffer. It is a correction, learned on commit.
public private(set) var forcedMode: InputMode? public private(set) var forcedMode: InputMode?
/// How many keys there were when the user flipped: that word is what gets learned, not
/// whatever was typed after it.
private var forcedCount = 0
/// English mode, toggled by a Shift tap when nothing is being typed. Keys go straight /// 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. /// to the app (no composition, no underline) until Shift is tapped again.
public private(set) var isEnglishMode = false public private(set) var isEnglishMode = false
@@ -120,9 +123,10 @@ public final class CompositionEngine {
return Features.dot(w, zhWhole) >= Features.dot(w, word) + Features.dot(w, zhRest) return Features.dot(w, zhWhole) >= Features.dot(w, word) + Features.dot(w, zhRest)
} }
/// Whether keys + space also read as a common Chinese syllable (see `held`). /// Whether keys + space also read as a common Chinese syllable (see `held`). Any Dachen
/// keys count, not only letters: "0" is ㄢ (安), "5j;" is ㄓㄨㄤ (裝).
private func isAmbiguousWord(_ keys: [Character]) -> Bool { private func isAmbiguousWord(_ keys: [Character]) -> Bool {
guard keys.allSatisfy({ $0.isASCII && $0.isLetter }) else { return false } guard keys.allSatisfy(DachenLayout.isZhuyinKey) else { return false }
let p = ZhuyinParser.parse(keys + [" "]) let p = ZhuyinParser.parse(keys + [" "])
guard p.pending == nil, !p.syllables.isEmpty, p.strayTones == 0, p.foreignKeys == 0 else { return false } guard p.pending == nil, !p.syllables.isEmpty, p.strayTones == 0, p.foreignKeys == 0 else { return false }
var best: [Double] = [] var best: [Double] = []
@@ -430,6 +434,7 @@ public final class CompositionEngine {
/// Forces the whole buffer into the other mode (a correction that is learned). /// Forces the whole buffer into the other mode (a correction that is learned).
private func flip() { private func flip() {
forcedMode = (mode == .zhuyin) ? .literal : .zhuyin forcedMode = (mode == .zhuyin) ? .literal : .zhuyin
forcedCount = keys.count
} }
private func handleCharacter(_ c: Character) -> KeyResult { private func handleCharacter(_ c: Character) -> KeyResult {
@@ -566,7 +571,7 @@ public final class CompositionEngine {
rememberPicks(segs) rememberPicks(segs)
recentText = String((rerankContext + text).suffix(20)) recentText = String((rerankContext + text).suffix(20))
if let forced = forcedMode { if let forced = forcedMode {
classifier.learned.record(String(keys), as: forced) classifier.learned.record(String(keys.prefix(forcedCount)), as: forced)
} }
previousMode = segs.last?.mode previousMode = segs.last?.mode
previousCommitTime = now() previousCommitTime = now()
@@ -487,7 +487,7 @@ final class HeldWordTests: XCTestCase {
func type(_ s: String, into e: CompositionEngine) -> String { func type(_ s: String, into e: CompositionEngine) -> String {
var out = "" var out = ""
for c in s { for c in s {
let key: KeyInput = c == "\n" ? .enter : .character(c) let key: KeyInput = c == "\n" ? .enter : c == "\t" ? .tab : .character(c)
let r = e.handle(key) let r = e.handle(key)
if let t = r.commit { out += t } else if !r.handled { out.append(c) } if let t = r.commit { out += t } else if !r.handled { out.append(c) }
} }
@@ -511,6 +511,24 @@ final class HeldWordTests: XCTestCase {
XCTAssertEqual(e.markedText, "up") XCTAssertEqual(e.markedText, "up")
XCTAssertEqual(type(" the ", into: e), "up the ") XCTAssertEqual(type(" the ", into: e), "up the ")
} }
/// Words with digits / punctuation keys are held too: "0 " is 安, "5j; " is 裝.
func testHeldWordWithDigitKeys() {
let learned = LearnedPreferences(weights: ["0": -2]) // "0" once learned as a number
let c = Classifier(lexicon: sharedLexicon, english: sharedEnglish, learned: learned, weights: sharedWeights)
XCTAssertEqual(type("0 5j; \n", into: CompositionEngine(classifier: c)), "安裝")
XCTAssertEqual(type("0 \n", into: CompositionEngine(classifier: c)), "0 ")
}
/// One Tab undoes a wrong learned preference, and only the word flipped is learned.
func testTabOverridesLearned() {
let learned = LearnedPreferences(weights: ["0": -2])
let c = Classifier(lexicon: sharedLexicon, english: sharedEnglish, learned: learned, weights: sharedWeights)
XCTAssertEqual(type("0\t 5j; \n", into: CompositionEngine(classifier: c)), "安裝")
XCTAssertEqual(learned.weights["0"], 1)
XCTAssertNil(learned.weights["0 5j; "])
XCTAssertEqual(type("hello 0 5j; \n", into: CompositionEngine(classifier: c)), "hello 安裝")
}
} }
final class BacktickSymbolTests: XCTestCase { final class BacktickSymbolTests: XCTestCase {
+3 -1
View File
@@ -19,7 +19,9 @@ class LearnedPreferences:
def record(self, keys, mode): def record(self, keys, mode):
delta = 1 if mode == ZHUYIN else -1 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) self._on_change(self.weights)
def clear(self): def clear(self):
+6 -3
View File
@@ -50,6 +50,7 @@ class CompositionEngine:
self.now = now self.now = now
self.keys = [] self.keys = []
self.forced_mode = None self.forced_mode = None
self.forced_count = 0
# English mode, toggled by a Shift tap when nothing is being typed: keys go straight # 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. # to the app (no composition, no underline) until Shift is tapped again.
self.english_mode = False self.english_mode = False
@@ -117,8 +118,9 @@ class CompositionEngine:
return dot(w, zh_whole) >= dot(w, word) + dot(w, zh_rest) return dot(w, zh_whole) >= dot(w, word) + dot(w, zh_rest)
def _is_ambiguous_word(self, keys): def _is_ambiguous_word(self, keys):
"""Whether keys + space also read as a common Chinese syllable (see HOLD_MIN_SCORE).""" """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): 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 return False
p = bopomofo.parse(list(keys) + [" "]) p = bopomofo.parse(list(keys) + [" "])
if p.pending is not None or not p.syllables or p.stray_tones or p.foreign_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): def _flip(self):
"""Forces the whole buffer into the other mode (a correction that is learned).""" """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_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): def wants(self, key):
"""Whether handle(key) would consume the key, without changing state.""" """Whether handle(key) would consume the key, without changing state."""
@@ -567,7 +570,7 @@ class CompositionEngine:
self._remember_picks(segs) self._remember_picks(segs)
self._recent_text = (self._rerank_context() + text)[-20:] self._recent_text = (self._rerank_context() + text)[-20:]
if self.forced_mode: 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_mode = segs[-1][2] if segs else None
self._previous_time = self.now() self._previous_time = self.now()
self.reset() self.reset()
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "自動注音 AutoZhuyin", "name": "自動注音 AutoZhuyin",
"version": "0.14.0", "version": "0.14.1",
"guid": "{3895C6E3-6D40-4B4B-B4EA-CEEC75382D23}", "guid": "{3895C6E3-6D40-4B4B-B4EA-CEEC75382D23}",
"locale": "zh-Hant-TW", "locale": "zh-Hant-TW",
"fallbackLocale": "zh-TW", "fallbackLocale": "zh-TW",
+17
View File
@@ -340,6 +340,23 @@ class HeldWordTests(unittest.TestCase):
self.assertEqual(e.marked_text, "up") self.assertEqual(e.marked_text, "up")
self.assertEqual(type_keys(" the ", e), "up the ") 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): class BacktickSymbolTests(unittest.TestCase):
def test_forced_symbols(self): def test_forced_symbols(self):