From 62ea37d2ec0749da65ceb9bc08536c64e96ac8d8 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 19 Jul 2022 19:04:48 +0800 Subject: [PATCH 01/11] Tekkon // v1.2.6 update. --- .../ControllerModules/SyllableComposer.swift | 51 +++++++++++++++---- 1 file changed, 41 insertions(+), 10 deletions(-) diff --git a/Source/Modules/ControllerModules/SyllableComposer.swift b/Source/Modules/ControllerModules/SyllableComposer.swift index d76d4636..abfa4e11 100644 --- a/Source/Modules/ControllerModules/SyllableComposer.swift +++ b/Source/Modules/ControllerModules/SyllableComposer.swift @@ -221,6 +221,9 @@ public struct Tekkon { /// 注音排列種類。預設情況下是大千排列(Windows / macOS 預設注音排列)。 public var parser: MandarinParser = .ofDachen + /// 是否對錯誤的注音讀音組合做出自動糾正處理。 + public var phonabetCombinationCorrectionEnabled = false + /// 內容值,會直接按照正確的順序拼裝自己的聲介韻調內容、再回傳。 /// 注意:直接取這個參數的內容的話,陰平聲調會成為一個空格。 /// 如果是要取不帶空格的注音的話,請使用「.getComposition()」而非「.value」。 @@ -285,7 +288,9 @@ public struct Tekkon { /// - Parameters: /// - input: 傳入的 String 內容,用以處理單個字符。 /// - arrange: 要使用的注音排列。 - public init(_ input: String = "", arrange parser: MandarinParser = .ofDachen) { + /// - correction: 是否對錯誤的注音讀音組合做出自動糾正處理。 + public init(_ input: String = "", arrange parser: MandarinParser = .ofDachen, correction: Bool = false) { + phonabetCombinationCorrectionEnabled = correction ensureParser(arrange: parser) receiveKey(fromString: input) } @@ -379,15 +384,24 @@ public struct Tekkon { /// - Parameters: /// - fromPhonabet: 傳入的單個注音符號字串。 public mutating func receiveKey(fromPhonabet phonabet: String = "") { - let thePhone: Phonabet = .init(phonabet) - switch phonabet { - case "ㄛ", "ㄥ": - if "ㄅㄆㄇㄈ".contains(consonant.value), semivowel.value == "ㄨ" { semivowel.clear() } - case "ㄨ": - if "ㄅㄆㄇㄈ".contains(consonant.value), "ㄛㄥ".contains(vowel.value) { vowel.clear() } - case "ㄅ", "ㄆ", "ㄇ", "ㄈ": - if ["ㄨㄛ", "ㄨㄥ"].contains(semivowel.value + vowel.value) { semivowel.clear() } - default: break + var thePhone: Phonabet = .init(phonabet) + if phonabetCombinationCorrectionEnabled { + switch phonabet { + case "ㄧ", "ㄩ": + if vowel.value == "ㄜ" { vowel = "ㄝ" } + case "ㄜ": + if "ㄧㄩ".contains(semivowel.value) { thePhone = "ㄝ" } + case "ㄛ", "ㄥ": + if "ㄅㄆㄇㄈ".contains(consonant.value), semivowel.value == "ㄨ" { semivowel.clear() } + case "ㄟ": + if "ㄋㄌ".contains(consonant.value), semivowel.value == "ㄨ" { semivowel.clear() } + case "ㄨ": + if "ㄅㄆㄇㄈ".contains(consonant.value), "ㄛㄥ".contains(vowel.value) { vowel.clear() } + if "ㄋㄌ".contains(consonant.value), "ㄟ".contains(vowel.value) { vowel.clear() } + case "ㄅ", "ㄆ", "ㄇ", "ㄈ": + if ["ㄨㄛ", "ㄨㄥ"].contains(semivowel.value + vowel.value) { semivowel.clear() } + default: break + } } switch thePhone.type { case .consonant: consonant = thePhone @@ -880,6 +894,23 @@ public struct Tekkon { return arrReturn.joined(separator: newSeparator) } + /// 該函式用來將漢語拼音轉為注音。 + /// - Parameter target: 要轉換的漢語拼音內容,要求必須帶有 12345 數字標調。 + /// - Returns: 轉換結果。 + static func cnvHanyuPinyinToPhona(target: String) -> String { + if target.contains("_") { return target } + var result = target + for key in Tekkon.mapHanyuPinyin.keys.sorted(by: { $0.count > $1.count }) { + guard let value = Tekkon.mapHanyuPinyin[key] else { continue } + result = result.replacingOccurrences(of: key, with: value) + } + for key in Tekkon.mapArayuruPinyinIntonation.keys.sorted(by: { $0.count > $1.count }) { + guard let value = Tekkon.mapArayuruPinyinIntonation[key] else { continue } + result = result.replacingOccurrences(of: key, with: value) + } + return result + } + /// 原始轉換對照表資料貯存專用佇列(數字標調格式) static let arrPhonaToHanyuPinyin = [ // 排序很重要。先處理最長的,再處理短的。不然會出亂子。 [" ", "1"], ["ˊ", "2"], ["ˇ", "3"], ["ˋ", "4"], ["˙", "5"], From 4c7ed0dc7b1b3ad87c74859b2e1ee3246acb47f0 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 19 Jul 2022 19:05:06 +0800 Subject: [PATCH 02/11] LMCoreEX // Use new Tekkon functions to handle key conversions. --- .../LangModelRelated/SubLMs/lmCoreEX.swift | 21 ++----------------- 1 file changed, 2 insertions(+), 19 deletions(-) diff --git a/Source/Modules/LangModelRelated/SubLMs/lmCoreEX.swift b/Source/Modules/LangModelRelated/SubLMs/lmCoreEX.swift index 6a91e82a..eb049aea 100644 --- a/Source/Modules/LangModelRelated/SubLMs/lmCoreEX.swift +++ b/Source/Modules/LangModelRelated/SubLMs/lmCoreEX.swift @@ -91,10 +91,9 @@ extension vChewing { let neta = strData[$0].split(separator: " ") if neta.count >= 2, String(neta[0]).first != "#" { if !neta[0].isEmpty, !neta[1].isEmpty { - var theKey = shouldReverse ? String(neta[1]) : String(neta[0]) - theKey.cnvPinyinToPhonabet() + let theKey = shouldReverse ? String(neta[1]) : String(neta[0]) let theValue = $0 - rangeMap[theKey, default: []].append(theValue) + rangeMap[Tekkon.cnvHanyuPinyinToPhona(target: theKey), default: []].append(theValue) } } } @@ -192,19 +191,3 @@ extension String { } } } - -// MARK: - 拼音轉注音 - -extension String { - fileprivate mutating func cnvPinyinToPhonabet() { - if contains("_") { return } - for key in Tekkon.mapHanyuPinyin.keys { - guard let value = Tekkon.mapHanyuPinyin[key] else { continue } - self = replacingOccurrences(of: key, with: value) - } - for key in Tekkon.mapArayuruPinyinIntonation.keys { - guard let value = Tekkon.mapArayuruPinyinIntonation[key] else { continue } - self = replacingOccurrences(of: key, with: value) - } - } -} From 478c711b0a0f4e18b4b59f3aea880387be1697ba Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 19 Jul 2022 21:10:25 +0800 Subject: [PATCH 03/11] mgrPrefs // +phonabetCombinationCorrectionEnabled. --- Source/Modules/IMEModules/mgrPrefs.swift | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/Source/Modules/IMEModules/mgrPrefs.swift b/Source/Modules/IMEModules/mgrPrefs.swift index 61ef3134..1a8b9e4d 100644 --- a/Source/Modules/IMEModules/mgrPrefs.swift +++ b/Source/Modules/IMEModules/mgrPrefs.swift @@ -58,6 +58,7 @@ struct UserDef { static let kInlineDumpPinyinInLieuOfZhuyin = "InlineDumpPinyinInLieuOfZhuyin" static let kFetchSuggestionsFromUserOverrideModel = "FetchSuggestionsFromUserOverrideModel" static let kUseFixecCandidateOrderOnSelection = "UseFixecCandidateOrderOnSelection" + static let kAutoCorrectReadingCombination = "AutoCorrectReadingCombination" static let kCandidateTextFontName = "CandidateTextFontName" static let kCandidateKeyLabelFontName = "CandidateKeyLabelFontName" @@ -278,6 +279,9 @@ public enum mgrPrefs { UserDefaults.standard.setDefault( mgrPrefs.useFixecCandidateOrderOnSelection, forKey: UserDef.kUseFixecCandidateOrderOnSelection ) + UserDefaults.standard.setDefault( + mgrPrefs.autoCorrectReadingCombination, forKey: UserDef.kAutoCorrectReadingCombination + ) UserDefaults.standard.setDefault(mgrPrefs.usingHotKeySCPC, forKey: UserDef.kUsingHotKeySCPC) UserDefaults.standard.setDefault(mgrPrefs.usingHotKeyAssociates, forKey: UserDef.kUsingHotKeyAssociates) @@ -360,6 +364,9 @@ public enum mgrPrefs { @UserDefault(key: UserDef.kUseFixecCandidateOrderOnSelection, defaultValue: false) static var useFixecCandidateOrderOnSelection: Bool + @UserDefault(key: UserDef.kAutoCorrectReadingCombination, defaultValue: true) + static var autoCorrectReadingCombination: Bool + static var minCandidateLength: Int { mgrPrefs.allowBoostingSingleKanjiAsUserPhrase ? 1 : 2 } From 3815a50e8386efff18aa75ad8339462a9e2c122b Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 19 Jul 2022 21:10:36 +0800 Subject: [PATCH 04/11] KeyHandler // Bind phonabetCombinationCorrectionEnabled. --- Source/Modules/ControllerModules/KeyHandler_Core.swift | 1 + 1 file changed, 1 insertion(+) diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index 4caf14b5..5ab32f12 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -332,6 +332,7 @@ class KeyHandler { mgrPrefs.mandarinParser = MandarinParser.ofStandard.rawValue } composer.clear() + composer.phonabetCombinationCorrectionEnabled = mgrPrefs.autoCorrectReadingCombination } // MARK: - Extracted methods and functions (Megrez). From 05ff23839d3d8d4b58586e5350ab64690803ea6f Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 19 Jul 2022 21:23:29 +0800 Subject: [PATCH 05/11] PrefUI // +phonabetCombinationCorrectionEnabled. --- Source/Resources/Base.lproj/Localizable.strings | 1 + Source/Resources/en.lproj/Localizable.strings | 1 + Source/Resources/ja.lproj/Localizable.strings | 1 + Source/Resources/zh-Hans.lproj/Localizable.strings | 1 + Source/Resources/zh-Hant.lproj/Localizable.strings | 1 + Source/UI/PrefUI/suiPrefPaneExperience.swift | 8 ++++++++ 6 files changed, 13 insertions(+) diff --git a/Source/Resources/Base.lproj/Localizable.strings b/Source/Resources/Base.lproj/Localizable.strings index e8ca421a..a1f1968f 100644 --- a/Source/Resources/Base.lproj/Localizable.strings +++ b/Source/Resources/Base.lproj/Localizable.strings @@ -95,6 +95,7 @@ "at the rear of the phrase (like Microsoft New Phonetic)" = "at the rear of the phrase (like Microsoft New Phonetic)"; "Auto-convert traditional Chinese glyphs to JIS Shinjitai characters" = "Auto-convert traditional Chinese glyphs to JIS Shinjitai characters"; "Auto-convert traditional Chinese glyphs to KangXi characters" = "Auto-convert traditional Chinese glyphs to KangXi characters"; +"Automatically correct reading combinations when typing" = "Automatically correct reading combinations when typing"; "Automatically reload user data files if changes detected" = "Automatically reload user data files if changes detected"; "Basic Keyboard Layout:" = "Basic Keyboard Layout:"; "Buffer Limit:" = "Buffer Limit:"; diff --git a/Source/Resources/en.lproj/Localizable.strings b/Source/Resources/en.lproj/Localizable.strings index e8ca421a..a1f1968f 100644 --- a/Source/Resources/en.lproj/Localizable.strings +++ b/Source/Resources/en.lproj/Localizable.strings @@ -95,6 +95,7 @@ "at the rear of the phrase (like Microsoft New Phonetic)" = "at the rear of the phrase (like Microsoft New Phonetic)"; "Auto-convert traditional Chinese glyphs to JIS Shinjitai characters" = "Auto-convert traditional Chinese glyphs to JIS Shinjitai characters"; "Auto-convert traditional Chinese glyphs to KangXi characters" = "Auto-convert traditional Chinese glyphs to KangXi characters"; +"Automatically correct reading combinations when typing" = "Automatically correct reading combinations when typing"; "Automatically reload user data files if changes detected" = "Automatically reload user data files if changes detected"; "Basic Keyboard Layout:" = "Basic Keyboard Layout:"; "Buffer Limit:" = "Buffer Limit:"; diff --git a/Source/Resources/ja.lproj/Localizable.strings b/Source/Resources/ja.lproj/Localizable.strings index 60618e9b..3c77f330 100644 --- a/Source/Resources/ja.lproj/Localizable.strings +++ b/Source/Resources/ja.lproj/Localizable.strings @@ -95,6 +95,7 @@ "at the rear of the phrase (like Microsoft New Phonetic)" = "単語の後で // Microsoft 新注音入力のやり方"; "Auto-convert traditional Chinese glyphs to JIS Shinjitai characters" = "入力した繁体字を日文 JIS 新字体と自動変換"; "Auto-convert traditional Chinese glyphs to KangXi characters" = "入力した繁体字を康熙字体と自動変換"; +"Automatically correct reading combinations when typing" = "入力中で打ち間違った発音組み合わせを自動的に訂正する"; "Automatically reload user data files if changes detected" = "ユーザー辞書データの変更を自動検出し、自動的に再読込"; "Basic Keyboard Layout:" = "基礎キーボード:"; "Buffer Limit:" = "緩衝列の容量:"; diff --git a/Source/Resources/zh-Hans.lproj/Localizable.strings b/Source/Resources/zh-Hans.lproj/Localizable.strings index a5ccdf70..653d52fa 100644 --- a/Source/Resources/zh-Hans.lproj/Localizable.strings +++ b/Source/Resources/zh-Hans.lproj/Localizable.strings @@ -95,6 +95,7 @@ "at the rear of the phrase (like Microsoft New Phonetic)" = "将游标置于词语后方 // Windows 微软新注音风格"; "Auto-convert traditional Chinese glyphs to JIS Shinjitai characters" = "自动将繁体中文字转为日文 JIS 新字体"; "Auto-convert traditional Chinese glyphs to KangXi characters" = "自动将繁体中文字转为康熙正体字"; +"Automatically correct reading combinations when typing" = "敲字时自动纠正读音组合"; "Automatically reload user data files if changes detected" = "自动检测并载入使用者语汇档案变更"; "Basic Keyboard Layout:" = "基础键盘布局:"; "Buffer Limit:" = "组字区容量:"; diff --git a/Source/Resources/zh-Hant.lproj/Localizable.strings b/Source/Resources/zh-Hant.lproj/Localizable.strings index 733e49fa..45698072 100644 --- a/Source/Resources/zh-Hant.lproj/Localizable.strings +++ b/Source/Resources/zh-Hant.lproj/Localizable.strings @@ -95,6 +95,7 @@ "at the rear of the phrase (like Microsoft New Phonetic)" = "將游標置於詞語後方 // Windows 微軟新注音風格"; "Auto-convert traditional Chinese glyphs to JIS Shinjitai characters" = "自動將繁體中文字轉為日文 JIS 新字體"; "Auto-convert traditional Chinese glyphs to KangXi characters" = "自動將繁體中文字轉為康熙正體字"; +"Automatically correct reading combinations when typing" = "敲字時自動糾正讀音組合"; "Automatically reload user data files if changes detected" = "自動檢測並載入使用者語彙檔案變更"; "Basic Keyboard Layout:" = "基礎鍵盤佈局:"; "Buffer Limit:" = "組字區容量:"; diff --git a/Source/UI/PrefUI/suiPrefPaneExperience.swift b/Source/UI/PrefUI/suiPrefPaneExperience.swift index e6273f1c..43f46668 100644 --- a/Source/UI/PrefUI/suiPrefPaneExperience.swift +++ b/Source/UI/PrefUI/suiPrefPaneExperience.swift @@ -46,6 +46,8 @@ struct suiPrefPaneExperience: View { forKey: UserDef.kEscToCleanInputBuffer) @State private var selEnableSCPCTypingMode = UserDefaults.standard.bool(forKey: UserDef.kUseSCPCTypingMode) @State private var selComposingBufferSize = UserDefaults.standard.integer(forKey: UserDef.kComposingBufferSize) + @State private var selAutoCorrectReadingCombination = UserDefaults.standard.bool( + forKey: UserDef.kAutoCorrectReadingCombination) private let contentWidth: Double = { switch mgrPrefs.appleLanguages[0] { case "ja": @@ -165,6 +167,12 @@ struct suiPrefPaneExperience: View { } } Preferences.Section(label: { Text(LocalizedStringKey("Typing Style:")) }) { + Toggle( + LocalizedStringKey("Automatically correct reading combinations when typing"), + isOn: $selAutoCorrectReadingCombination + ).onChange(of: selAutoCorrectReadingCombination) { value in + mgrPrefs.autoCorrectReadingCombination = value + } Toggle( LocalizedStringKey("Emulating select-candidate-per-character mode"), isOn: $selEnableSCPCTypingMode ).onChange(of: selEnableSCPCTypingMode) { value in From bfc94262698a79d8648e7496ea1b67c9ed092df1 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 19 Jul 2022 21:33:17 +0800 Subject: [PATCH 06/11] PrefWindow // +phonabetCombinationCorrectionEnabled. --- .../WindowNIBs/Base.lproj/frmPrefWindow.xib | 126 ++++++++++-------- .../WindowNIBs/en.lproj/frmPrefWindow.strings | 1 + .../WindowNIBs/ja.lproj/frmPrefWindow.strings | 1 + .../zh-Hans.lproj/frmPrefWindow.strings | 1 + .../zh-Hant.lproj/frmPrefWindow.strings | 1 + 5 files changed, 73 insertions(+), 57 deletions(-) diff --git a/Source/WindowNIBs/Base.lproj/frmPrefWindow.xib b/Source/WindowNIBs/Base.lproj/frmPrefWindow.xib index ae6afabc..5faf943e 100644 --- a/Source/WindowNIBs/Base.lproj/frmPrefWindow.xib +++ b/Source/WindowNIBs/Base.lproj/frmPrefWindow.xib @@ -30,16 +30,16 @@ - + - + - + @@ -229,13 +229,13 @@ - + - + - + @@ -456,7 +456,7 @@ - + @@ -464,7 +464,7 @@ - + @@ -489,7 +489,7 @@ - + @@ -507,7 +507,7 @@ - + @@ -535,7 +535,7 @@ - + @@ -591,12 +591,22 @@ + - + @@ -610,17 +620,20 @@ + + + @@ -631,6 +644,7 @@ + @@ -645,17 +659,17 @@ - + - + - + - + @@ -666,7 +680,7 @@ - + @@ -681,7 +695,7 @@