diff --git a/README.md b/README.md index 3974f22..c3185d2 100644 --- a/README.md +++ b/README.md @@ -247,7 +247,7 @@ THIRD_PARTY_LICENSES.txt 第三方授權聲明(會打包進兩個安裝 - Windows 版、Linux 版還沒在實機上測試過。 - Linux 版只支援 IBus。KDE 上有些 Flatpak 程式(例如 Flatpak 版 Chrome)在 IBus Wayland 下可能打不出字。 - 聲調鍵打在注音符號中間(例如 `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 沒有程式碼簽章憑證)。 ## 下一步(Roadmap) diff --git a/Sources/AutoZhuyinCore/CompositionEngine.swift b/Sources/AutoZhuyinCore/CompositionEngine.swift index 83a4c37..6332123 100644 --- a/Sources/AutoZhuyinCore/CompositionEngine.swift +++ b/Sources/AutoZhuyinCore/CompositionEngine.swift @@ -53,9 +53,10 @@ public final class CompositionEngine { public var candidateIndex = 0 public var isShowingCandidates: Bool { !candidates.isEmpty } - /// Index of a held space: an English word that is also a common one-syllable Chinese - /// reading ("up " = ㄧㄣ 因) is not committed on space; the next word decides - /// ("up jo4" = 因為, "up the" = up the). + /// Index of a held space: the first word of a composition that also reads as Zhuyin is + /// not committed as English 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 English typing isn't delayed. private var held: Int? /// "`" 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). @@ -94,19 +95,38 @@ public final class CompositionEngine { 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) } - 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.. Bool { + let c = classifier, w = classifier.weights, ctx = context + let isLast = end == keys.count + guard let zhWhole = c.segmentFeatures(Array(keys[..= Features.dot(w, word) + Features.dot(w, zhRest) + } + /// Whether keys + space also read as a common Chinese syllable (see `held`). private func isAmbiguousWord(_ keys: [Character]) -> Bool { guard keys.allSatisfy({ $0.isASCII && $0.isLetter }) else { return false } let p = ZhuyinParser.parse(keys + [" "]) - guard p.pending == nil, p.syllables.count == 1 else { return false } - guard let best = classifier.lexicon.entries(for: p.syllables.map(\.syllable)).first else { return false } - return best.score >= CompositionEngine.holdMinScore + guard p.pending == nil, !p.syllables.isEmpty, p.strayTones == 0, p.foreignKeys == 0 else { return false } + var best: [Double] = [] + 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. diff --git a/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift b/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift index e9cc86c..b95f8d1 100644 --- a/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift +++ b/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift @@ -459,8 +459,10 @@ final class FirstToneAfterEnglishTests: XCTestCase { let e = CompositionEngine(classifier: Classifier(lexicon: sharedLexicon, english: sharedEnglish, weights: sharedWeights)) var out = "" for c in "hello " + keys + " " { if let t = e.handle(.character(c)).commit { out += t } } - XCTAssertEqual(out, "hello ", keys) - XCTAssertEqual(e.markedText.count, 1, keys) // still composing one Chinese character + out += e.handle(.enter).commit ?? "" + 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 { func testStartupTiming() throws { var t = Date() diff --git a/windows/autozhuyin/core/engine.py b/windows/autozhuyin/core/engine.py index 8dff359..1684458 100644 --- a/windows/autozhuyin/core/engine.py +++ b/windows/autozhuyin/core/engine.py @@ -8,6 +8,7 @@ import time from . import bopomofo from .classifier import LITERAL, ZHUYIN +from .features import dot from .lexicon import Converter BACKSPACE, ENTER, ESCAPE, TAB = "", "", "", "" @@ -20,8 +21,10 @@ FULLWIDTH = { "{": "『", "}": "』", "[": "「", "]": "」", "~": "~", "_": "——", } -# An English word that is also a common one-syllable Chinese reading ("up " = ㄧㄣ 因) -# is not committed on space; the next word decides ("up jo4" = 因為, "up the" = up the). +# The first word of a composition that also reads as Zhuyin is not committed as English +# 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 # "`" then one of these keys always gives the full-width symbol, whatever the context. @@ -94,19 +97,39 @@ class CompositionEngine: rest = keys[held + 1:] base = held + 1 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, 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): """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): return False 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 - entries = self.classifier.lexicon.entries([s.syllable for s in p.syllables]) - return bool(entries) and entries[0][1] >= HOLD_MIN_SCORE + best = [] + 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 def segments(self): diff --git a/windows/tests/test_core.py b/windows/tests/test_core.py index d10ac02..d1f1682 100644 --- a/windows/tests/test_core.py +++ b/windows/tests/test_core.py @@ -308,8 +308,10 @@ class FirstToneAfterEnglishTests(unittest.TestCase): def test_first_tone_after_english(self): for keys in ["ru", "t", "gj", "vup", "tj", "el"]: e = make_engine() - self.assertEqual(type_keys("hello " + keys + " ", e), "hello ", keys) - self.assertEqual(len(e.marked_text), 1, keys) + out = type_keys("hello " + keys + " \n", e) + 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): @@ -348,5 +350,19 @@ class BacktickSymbolTests(unittest.TestCase): 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__": unittest.main()