Hold the first word on space when it also reads as Zhuyin

At the start of a composition (not right after English), a word that reads as
valid Zhuyin is no longer committed as English on space. The next word decides,
by comparing "Chinese all the way" with "English word + Chinese" using the
classifier's features: el ji3 = 高我, no su3cl3 = no 你好, up the = up the.
After English text only common one-syllable readings (up, url) are held.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 12:06:10 +08:00
co-authored by Claude Opus 5.5
parent 9cac3467a0
commit 8e82105af9
5 changed files with 109 additions and 18 deletions
+1 -1
View File
@@ -247,7 +247,7 @@ THIRD_PARTY_LICENSES.txt 第三方授權聲明(會打包進兩個安裝
- Windows 版、Linux 版還沒在實機上測試過。 - Windows 版、Linux 版還沒在實機上測試過。
- Linux 版只支援 IBus。KDE 上有些 Flatpak 程式(例如 Flatpak 版 Chrome)在 IBus Wayland 下可能打不出字。 - Linux 版只支援 IBus。KDE 上有些 Flatpak 程式(例如 Flatpak 版 Chrome)在 IBus Wayland 下可能打不出字。
- 聲調鍵打在注音符號中間(例如 `uv3l`)無法自動修正。 - 聲調鍵打在注音符號中間(例如 `uv3l`)無法自動修正。
- 很短的字串本質上就有歧義。像 `up`(也是 ㄧㄣ 因)這種常用的,按空白後會先保留,看下一個詞再決定(`up jo4` = 因為、`up the` = up the);其他判斷錯的可以按 Tab 修正,修正後會記住。 - 很短的字串本質上就有歧義。開頭第一個詞如果也能讀成注音,按空白後會先保留(顯示成英文),看下一個詞再決定:`up jo4` = 因為、`el ji3` = 高我、`no su3cl3` = no 你好、`up the` = up the。接在英文後面時只保留 `up`(因)這種很常見的讀音,不會拖慢打英文。還是判斷錯的話按 Tab 修正,修正後會記住。
- 安裝檔沒有簽章(macOS 沒有 Developer ID,Windows 沒有程式碼簽章憑證)。 - 安裝檔沒有簽章(macOS 沒有 Developer ID,Windows 沒有程式碼簽章憑證)。
## 下一步(Roadmap) ## 下一步(Roadmap)
+27 -7
View File
@@ -53,9 +53,10 @@ public final class CompositionEngine {
public var candidateIndex = 0 public var candidateIndex = 0
public var isShowingCandidates: Bool { !candidates.isEmpty } public var isShowingCandidates: Bool { !candidates.isEmpty }
/// Index of a held space: an English word that is also a common one-syllable Chinese /// Index of a held space: the first word of a composition that also reads as Zhuyin is
/// reading ("up " = ㄧㄣ 因) is not committed on space; the next word decides /// not committed as English on space; the next word decides ("up jo4" = 因為,
/// ("up jo4" = 因為, "up the" = up the). /// "up the" = up the, "no su3" = no 你). Right after English text only common
/// one-syllable readings are held (up = 因, url = 教), so English typing isn't delayed.
private var held: Int? private var held: Int?
/// "`" was typed: if the next key is in `symbols` it gives that full-width symbol /// "`" was typed: if the next key is in `symbols` it gives that full-width symbol
/// whatever the context; anything else makes it a plain backtick (so ``` still works). /// whatever the context; anything else makes it a plain backtick (so ``` still works).
@@ -94,19 +95,38 @@ public final class CompositionEngine {
let rest = classifier.segment(Array(keys[base...]), finishing: finishing, previous: nil).map { let rest = classifier.segment(Array(keys[base...]), finishing: finishing, previous: nil).map {
Segment(range: ($0.range.lowerBound + base)..<($0.range.upperBound + base), mode: $0.mode) Segment(range: ($0.range.lowerBound + base)..<($0.range.upperBound + base), mode: $0.mode)
} }
if let first = rest.first, first.mode == .zhuyin { if let first = rest.first, first.mode == .zhuyin,
heldReadsAsChinese(keys, held: h, end: first.range.upperBound, finishing: finishing) {
return [Segment(range: 0..<first.range.upperBound, mode: .zhuyin)] + rest.dropFirst() return [Segment(range: 0..<first.range.upperBound, mode: .zhuyin)] + rest.dropFirst()
} }
return [Segment(range: 0..<base, mode: .literal)] + rest return [Segment(range: 0..<base, mode: .literal)] + rest
} }
/// Chinese all the way to `end` vs. the held English word followed by Chinese.
private func heldReadsAsChinese(_ keys: [Character], held h: Int, end: Int, finishing: Bool) -> Bool {
let c = classifier, w = classifier.weights, ctx = context
let isLast = end == keys.count
guard let zhWhole = c.segmentFeatures(Array(keys[..<end]), isLast: isLast, afterOther: false,
finishing: finishing, previous: ctx).0 else { return false }
guard let word = c.segmentFeatures(Array(keys[..<h]), isLast: false, afterOther: false,
finishing: true, previous: ctx).1,
let zhRest = c.segmentFeatures(Array(keys[(h + 1)..<end]), isLast: isLast, afterOther: true,
finishing: finishing, previous: nil).0 else { return true }
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`).
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({ $0.isASCII && $0.isLetter }) else { return false }
let p = ZhuyinParser.parse(keys + [" "]) let p = ZhuyinParser.parse(keys + [" "])
guard p.pending == nil, p.syllables.count == 1 else { return false } guard p.pending == nil, !p.syllables.isEmpty, p.strayTones == 0, p.foreignKeys == 0 else { return false }
guard let best = classifier.lexicon.entries(for: p.syllables.map(\.syllable)).first else { return false } var best: [Double] = []
return best.score >= CompositionEngine.holdMinScore for ps in p.syllables {
guard let e = classifier.lexicon.entries(for: [ps.syllable]).first else { return false }
best.append(e.score)
}
if context != .literal { return true } // first word: hold anything that reads as Zhuyin
return best.count == 1 && best[0] >= CompositionEngine.holdMinScore
} }
/// Current split of the buffer into Zhuyin / literal runs. /// Current split of the buffer into Zhuyin / literal runs.
@@ -459,8 +459,10 @@ final class FirstToneAfterEnglishTests: XCTestCase {
let e = CompositionEngine(classifier: Classifier(lexicon: sharedLexicon, english: sharedEnglish, weights: sharedWeights)) let e = CompositionEngine(classifier: Classifier(lexicon: sharedLexicon, english: sharedEnglish, weights: sharedWeights))
var out = "" var out = ""
for c in "hello " + keys + " " { if let t = e.handle(.character(c)).commit { out += t } } for c in "hello " + keys + " " { if let t = e.handle(.character(c)).commit { out += t } }
XCTAssertEqual(out, "hello ", keys) out += e.handle(.enter).commit ?? ""
XCTAssertEqual(e.markedText.count, 1, keys) // still composing one Chinese character XCTAssertEqual(String(out.prefix(6)), "hello ", keys)
XCTAssertEqual(out.count, 7, keys) // one Chinese character after it
XCTAssertFalse(out.last!.isASCII, keys)
} }
} }
} }
@@ -537,6 +539,36 @@ final class BacktickSymbolTests: XCTestCase {
} }
} }
final class FirstWordHoldTests: XCTestCase {
func makeEngine() -> CompositionEngine {
CompositionEngine(classifier: Classifier(lexicon: sharedLexicon, english: sharedEnglish, weights: sharedWeights))
}
func type(_ s: String, into e: CompositionEngine) -> String {
var out = ""
for c in s {
let r = e.handle(c == "\n" ? .enter : .character(c))
if let t = r.commit { out += t } else if !r.handled { out.append(c) }
}
return out
}
/// The first word is not committed as English on space if it also reads as Zhuyin.
func testNextWordDecides() {
let cases = ["el ji3 ": "高我", "no su3cl3 ": "no 你好", "ok ji3 ": "ok 我",
"i love you ": "i love you ", "up the ": "up the "]
for (keys, want) in cases {
XCTAssertEqual(type(keys, into: makeEngine()), want, keys)
}
}
func testNotHeldAfterEnglish() {
let e = makeEngine()
XCTAssertEqual(type("hello world ", into: e), "hello world ")
XCTAssertEqual(type("so ", into: e), "so ") // right after English: commits at once
}
}
final class StartupTimingTests: XCTestCase { final class StartupTimingTests: XCTestCase {
func testStartupTiming() throws { func testStartupTiming() throws {
var t = Date() var t = Date()
+29 -6
View File
@@ -8,6 +8,7 @@ import time
from . import bopomofo from . import bopomofo
from .classifier import LITERAL, ZHUYIN from .classifier import LITERAL, ZHUYIN
from .features import dot
from .lexicon import Converter from .lexicon import Converter
BACKSPACE, ENTER, ESCAPE, TAB = "<backspace>", "<enter>", "<escape>", "<tab>" BACKSPACE, ENTER, ESCAPE, TAB = "<backspace>", "<enter>", "<escape>", "<tab>"
@@ -20,8 +21,10 @@ FULLWIDTH = {
"{": "『", "}": "』", "[": "「", "]": "」", "~": "~", "_": "——", "{": "『", "}": "』", "[": "「", "]": "」", "~": "~", "_": "——",
} }
# An English word that is also a common one-syllable Chinese reading ("up " = ㄧㄣ 因) # The first word of a composition that also reads as Zhuyin is not committed as English
# is not committed on space; the next word decides ("up jo4" = 因為, "up the" = up the). # on space; the next word decides ("up jo4" = 因為, "up the" = up the, "no su3" = no 你).
# Right after English text only common one-syllable readings are held (up = 因, url = 教),
# so normal English typing is not delayed.
HOLD_MIN_SCORE = -3.5 HOLD_MIN_SCORE = -3.5
# "`" then one of these keys always gives the full-width symbol, whatever the context. # "`" then one of these keys always gives the full-width symbol, whatever the context.
@@ -94,19 +97,39 @@ class CompositionEngine:
rest = keys[held + 1:] rest = keys[held + 1:]
base = held + 1 base = held + 1
rest_segs = [(a + base, b + base, m) for a, b, m in self.classifier.segment(rest, finishing, None)] rest_segs = [(a + base, b + base, m) for a, b, m in self.classifier.segment(rest, finishing, None)]
if rest_segs and rest_segs[0][2] == ZHUYIN: if rest_segs and rest_segs[0][2] == ZHUYIN and self._held_reads_as_chinese(keys, held, rest_segs[0][1], finishing):
return [(0, rest_segs[0][1], ZHUYIN)] + rest_segs[1:] return [(0, rest_segs[0][1], ZHUYIN)] + rest_segs[1:]
return [(0, base, LITERAL)] + rest_segs return [(0, base, LITERAL)] + rest_segs
def _held_reads_as_chinese(self, keys, held, end, finishing):
"""Chinese all the way to `end` vs. the held English word followed by Chinese."""
c, w, ctx = self.classifier, self.classifier.weights, self.context
is_last = end == len(keys)
zh_whole = c.segment_features(keys[:end], is_last, False, finishing, ctx)[0]
word = c.segment_features(keys[:held], False, False, True, ctx)[1]
zh_rest = c.segment_features(keys[held + 1:end], is_last, True, finishing, None)[0]
if zh_whole is None:
return False
if word is None or zh_rest is None:
return True
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): if not all(k.isascii() and k.isalpha() 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 len(p.syllables) != 1: if p.pending is not None or not p.syllables or p.stray_tones or p.foreign_keys:
return False return False
entries = self.classifier.lexicon.entries([s.syllable for s in p.syllables]) best = []
return bool(entries) and entries[0][1] >= HOLD_MIN_SCORE for ps in p.syllables:
entries = self.classifier.lexicon.entries([ps.syllable])
if not entries:
return False
best.append(entries[0][1])
if self.context != LITERAL:
return True # first word: hold anything that reads as Zhuyin
return len(best) == 1 and best[0] >= HOLD_MIN_SCORE
@property @property
def segments(self): def segments(self):
+18 -2
View File
@@ -308,8 +308,10 @@ class FirstToneAfterEnglishTests(unittest.TestCase):
def test_first_tone_after_english(self): def test_first_tone_after_english(self):
for keys in ["ru", "t", "gj", "vup", "tj", "el"]: for keys in ["ru", "t", "gj", "vup", "tj", "el"]:
e = make_engine() e = make_engine()
self.assertEqual(type_keys("hello " + keys + " ", e), "hello ", keys) out = type_keys("hello " + keys + " \n", e)
self.assertEqual(len(e.marked_text), 1, keys) self.assertEqual(out[:6], "hello ", keys)
self.assertEqual(len(out), 7, keys) # one Chinese character after it
self.assertFalse(out[6].isascii(), keys)
class HeldWordTests(unittest.TestCase): class HeldWordTests(unittest.TestCase):
@@ -348,5 +350,19 @@ class BacktickSymbolTests(unittest.TestCase):
self.assertEqual(e.marked_text, "你") self.assertEqual(e.marked_text, "你")
class FirstWordHoldTests(unittest.TestCase):
"""The first word is not committed as English on space if it also reads as Zhuyin."""
def test_next_word_decides(self):
cases = {"el ji3 ": "高我", "no su3cl3 ": "no 你好", "ok ji3 ": "ok 我",
"i love you ": "i love you ", "up the ": "up the "}
for keys, want in cases.items():
self.assertEqual(type_keys(keys, make_engine()), want, keys)
def test_not_held_after_english(self):
e = make_engine()
self.assertEqual(type_keys("hello world ", e), "hello world ")
self.assertEqual(type_keys("so ", e), "so ") # right after English: commits at once
if __name__ == "__main__": if __name__ == "__main__":
unittest.main() unittest.main()