diff --git a/README.md b/README.md index 4dee945..1350d61 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ - **注音順序打錯也能修正**:`ㄧㄒㄠˇ`、`ㄒㄠㄧˇ` 都會變成 `ㄒㄧㄠˇ`(小) - **可省略聲調**:`jiap7`(ㄨㄛ ㄇㄣ˙)一樣能打出「我們」 - **候選字**:用 ← → 把游標移到要改的字後面,按 ↓ 選字;選過的會固定下來 -- **全形標點**:中文語境下 `?` `!` `<` 自動變成 `?` `!` `,` +- **全形標點**:中文語境下 `?` `!` `<` 自動變成 `?` `!` `,`;任何時候按 `` ` `` 再按標點鍵,一定得到全形標點(例如 `` `< `` = `,`) - **完整詞庫**:約 17 萬筆,來自小麥注音 McBopomofo(MIT 授權) ## 下載與安裝 @@ -58,6 +58,7 @@ SmartScreen 出現警告時,按「其他資訊 → 仍要執行」。解除安 | Shift + 字母 | 打大寫字母(也會強制英文) | | `< > ? ! : ( ) [ ]` | 中文語境下自動轉成全形 `,。?!:()「」` | | `, . / ; -` | 英文單字後是標點;在注音中是 ㄝ ㄡ ㄥ ㄤ ㄦ(例如 `2;3` = 檔) | +| `` ` `` + 標點鍵 | **強制全形標點**,不管前後是中文還是英文:`` `, `` `` `< `` = `,`、`` `. `` `` `> `` = `。`、`` `/ `` = `?`、`` `; `` = `;`、`` `: `` = `:`、`` `' `` `` `\ `` = `、`、`` `[ `` `` `] `` = `「」`、`` `{ `` `` `} `` = `『』`、`` `( `` `` `) `` = `()`、`` `! `` = `!`、`` `~ `` = `~`、`` `- `` = `——`、`` `= `` = `=`。`` ` `` 後面接其他鍵就是普通的反引號(` ``` ` 照常可用) | ## 運作方式 @@ -217,7 +218,7 @@ training/ 合成資料、訓練權重、Swift/Python 一致 - Windows 版還沒在實機上測試過。 - 聲調鍵打在注音符號中間(例如 `uv3l`)無法自動修正。 -- 很短的字串(如 `up` 是英文還是 ㄧㄣ)本質上就有歧義,可能需要按 Tab 修正;修正後會記住。 +- 很短的字串本質上就有歧義。像 `up`(也是 ㄧㄣ 因)這種常用的,按空白後會先保留,看下一個詞再決定(`up jo4` = 因為、`up the` = up the);其他判斷錯的可以按 Tab 修正,修正後會記住。 - 安裝檔沒有簽章(macOS 沒有 Developer ID,Windows 沒有程式碼簽章憑證)。 ## 下一步(Roadmap) diff --git a/Sources/AutoZhuyinCore/CompositionEngine.swift b/Sources/AutoZhuyinCore/CompositionEngine.swift index 9d94b1e..ade483b 100644 --- a/Sources/AutoZhuyinCore/CompositionEngine.swift +++ b/Sources/AutoZhuyinCore/CompositionEngine.swift @@ -55,6 +55,9 @@ public final class CompositionEngine { /// reading ("up " = ㄧㄣ 因) is not committed on space; the next word decides /// ("up jo4" = 因為, "up the" = up the). 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). + public private(set) var pendingBacktick = false static let holdMinScore = -3.5 private var previousMode: InputMode? @@ -115,9 +118,11 @@ public final class CompositionEngine { public var markedPieces: [MarkedPiece] { let segs = segments - return segs.enumerated().map { i, seg in + var pieces = segs.enumerated().map { i, seg in MarkedPiece(text: render(seg, final: i < segs.count - 1), mode: seg.mode) } + if pendingBacktick { pieces.append(MarkedPiece(text: "`", mode: .literal)) } + return pieces } /// Text for one segment. A non-final Zhuyin segment shows its unfinished syllable as Bopomofo. @@ -186,6 +191,7 @@ public final class CompositionEngine { public func handle(_ key: KeyInput) -> KeyResult { if isShowingCandidates { return handleCandidateKey(key) } + if pendingBacktick { return afterBacktick(key) } switch key { case .character(let c): @@ -245,6 +251,10 @@ public final class CompositionEngine { cursor = nil // typing always continues at the end // English mode: every key goes to the app as typed. if isEnglishMode && keys.isEmpty { return .passThrough } + if c == "`" { + pendingBacktick = true + return .consumed + } if c == " " { guard !keys.isEmpty else { return .passThrough } let segs = segments(of: keys, finishing: true) @@ -290,6 +300,26 @@ public final class CompositionEngine { return ZhuyinParser.parse(Array(keys[last.range])) } + private func afterBacktick(_ key: KeyInput) -> KeyResult { + pendingBacktick = false + if key == .escape || key == .backspace { return .consumed } + let committed = keys.isEmpty ? "" : commitCurrent() + if case .character(let c) = key, let symbol = CompositionEngine.symbols[c] { + return KeyResult(handled: true, commit: committed + symbol) + } + // Not a symbol: the backtick was meant literally; then handle the key as usual. + var r = handle(key) + if !r.handled, case .character(let c) = key { r = KeyResult(handled: true, commit: String(c)) } + return KeyResult(handled: r.handled, commit: committed + "`" + (r.commit ?? "")) + } + + /// "`" + key → full-width symbol. + static let symbols: [Character: String] = [ + ",": ",", "<": ",", ".": "。", ">": "。", "/": "?", "?": "?", ";": ";", ":": ":", + "'": "、", "\\": "、", "\"": "「", "[": "「", "]": "」", "{": "『", "}": "』", + "(": "(", ")": ")", "!": "!", "~": "~", "-": "——", "_": "——", "=": "=", + ] + static let fullwidth: [Character: String] = [ "<": ",", ">": "。", "?": "?", "!": "!", ":": ":", "\"": "「", "(": "(", ")": ")", "{": "『", "}": "』", "[": "「", "]": "」", "~": "~", "_": "——", @@ -363,6 +393,7 @@ public final class CompositionEngine { public func reset() { keys = [] held = nil + pendingBacktick = false cursor = nil forcedMode = nil fixed = [:] diff --git a/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift b/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift index 90b1e49..f4300c4 100644 --- a/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift +++ b/Tests/AutoZhuyinCoreTests/AutoZhuyinCoreTests.swift @@ -462,6 +462,44 @@ final class HeldWordTests: XCTestCase { } } +final class BacktickSymbolTests: 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 key: KeyInput = c == "\n" ? .enter : .character(c) + let r = e.handle(key) + if let t = r.commit { out += t } else if !r.handled { out.append(c) } + } + return out + } + + func testForcedSymbols() { + let cases = ["`<": ",", "`.": "。", "`?": "?", "`;": ";", "`[": "「", + "hello`,": "hello,", "su3cl3`.": "你好。", "```\n": "```"] + for (keys, want) in cases { + XCTAssertEqual(type(keys, into: makeEngine()), want, keys) + } + } + + func testPlainBacktick() { + let e = makeEngine() + XCTAssertEqual(type("`", into: e), "") + XCTAssertEqual(e.markedText, "`") + XCTAssertEqual(type("ls` ", into: e), "`ls` ") + } + + func testBackspaceCancels() { + let e = makeEngine() + _ = type("su3`", into: e) + _ = e.handle(.backspace) + XCTAssertEqual(e.markedText, "你") + } +} + 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 fcaaf61..e2351f4 100644 --- a/windows/autozhuyin/core/engine.py +++ b/windows/autozhuyin/core/engine.py @@ -24,6 +24,15 @@ FULLWIDTH = { # is not committed on space; the next word decides ("up jo4" = 因為, "up the" = up the). HOLD_MIN_SCORE = -3.5 +# "`" then one of these keys always gives the full-width symbol, whatever the context. +# "`" followed by anything else is just a backtick (so ``` still works). +BACKTICK = "`" +SYMBOLS = { + ",": ",", "<": ",", ".": "。", ">": "。", "/": "?", "?": "?", ";": ";", ":": ":", + "'": "、", "\\": "、", '"': "「", "[": "「", "]": "」", "{": "『", "}": "』", + "(": "(", ")": ")", "!": "!", "~": "~", "-": "——", "_": "——", "=": "=", +} + PASS = (False, None) CONSUMED = (True, None) @@ -41,6 +50,7 @@ class CompositionEngine: # 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 + self.backtick = False # "`" typed; the next key may be a forced symbol self.fixed = {} # zhuyin segment start -> {(syl_start, syl_end): text} self.candidates = [] self.candidate_index = 0 @@ -171,7 +181,10 @@ class CompositionEngine: @property def marked_pieces(self): segs = self.segments - return [(self._render(s, i < len(segs) - 1), s[2]) for i, s in enumerate(segs)] + pieces = [(self._render(s, i < len(segs) - 1), s[2]) for i, s in enumerate(segs)] + if self.backtick: + pieces.append((BACKTICK, LITERAL)) + return pieces @property def marked_text(self): @@ -185,7 +198,7 @@ class CompositionEngine: def wants(self, key): """Whether handle(key) would consume the key, without changing state.""" - if self.candidates or key == SHIFT_TAP: + if self.candidates or key == SHIFT_TAP or self.backtick: return True if self.english_mode and not self.keys: return False # English mode: everything goes to the app as typed @@ -202,6 +215,8 @@ class CompositionEngine: def handle(self, key): if self.candidates: return self._handle_candidate_key(key) + if self.backtick: + return self._after_backtick(key) if key == BACKSPACE: if not self.keys: return PASS @@ -251,6 +266,19 @@ class CompositionEngine: return CONSUMED if self.keys else PASS return self._handle_character(key) + def _after_backtick(self, key): + self.backtick = False + if key in (ESCAPE, BACKSPACE): + return CONSUMED + committed = self._commit() if self.keys else "" + if key in SYMBOLS: + return True, committed + SYMBOLS[key] + # Not a symbol: the backtick was meant literally; then handle the key as usual. + handled, more = self.handle(key) + if not handled and len(key) == 1: + handled, more = True, key + return handled, committed + BACKTICK + (more or "") + def _last_parse(self, segs): if not segs or segs[-1][2] != ZHUYIN: return None @@ -261,6 +289,9 @@ class CompositionEngine: self.cursor = None # typing always continues at the end if self.english_mode and not self.keys: return PASS # English mode: every key goes to the app as typed + if c == BACKTICK: + self.backtick = True + return CONSUMED if c == " ": if not self.keys: return PASS @@ -356,6 +387,7 @@ class CompositionEngine: def reset(self): self.keys = [] self.held = None + self.backtick = False self.cursor = None self.forced_mode = None self.fixed = {} diff --git a/windows/installer/README.txt b/windows/installer/README.txt index f9b1289..b1a4a24 100644 --- a/windows/installer/README.txt +++ b/windows/installer/README.txt @@ -34,6 +34,9 @@ 打到一半時按,同 Tab 整段翻轉 Shift+字母 打大寫字母(也會強制英文) < > ? ! : 中文語境下自動變成全形 ,。?!: + ` + 標點 強制全形標點,例如 `< 或 `, = , `. = 。 `/ = ? `; = ; + `' = 、 `[ `] = 「」 `( `) = () `! = ! + ` 後面接其他鍵就是普通的反引號 注音符號的順序打錯也沒關係,例如 ㄧㄒㄠˇ 會自動變成 ㄒㄧㄠˇ(小)。 diff --git a/windows/tests/test_core.py b/windows/tests/test_core.py index 5d6db82..3f0ea30 100644 --- a/windows/tests/test_core.py +++ b/windows/tests/test_core.py @@ -295,5 +295,25 @@ class HeldWordTests(unittest.TestCase): self.assertEqual(type_keys(" the ", e), "up the ") +class BacktickSymbolTests(unittest.TestCase): + def test_forced_symbols(self): + cases = {"`<": ",", "`.": "。", "`?": "?", "`;": ";", "`[": "「", + "hello`,": "hello,", "su3cl3`.": "你好。", "```\n": "```"} + for keys, want in cases.items(): + self.assertEqual(type_keys(keys, make_engine()), want, keys) + + def test_plain_backtick(self): + e = make_engine() + self.assertEqual(type_keys("`", e), "") + self.assertEqual(e.marked_text, "`") + self.assertEqual(type_keys("ls` ", e), "`ls` ") + + def test_escape_cancels(self): + e = make_engine() + type_keys("su3`", e) + e.handle(BACKSPACE) + self.assertEqual(e.marked_text, "你") + + if __name__ == "__main__": unittest.main()