From ebde0d088eac09f2fbffa2cffbd56b345f23cbf9 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 16 Jul 2022 17:58:23 +0800 Subject: [PATCH] LMAssoc. // Allow querying with KeyValuePaired.toNGramKey. --- .../ControllerModules/KeyHandler_Core.swift | 6 ++--- .../KeyHandler_HandleInput.swift | 3 ++- .../ControllerModules/KeyHandler_States.swift | 4 +-- .../ctlInputMethod_Delegates.swift | 6 +++-- .../LangModelRelated/LMInstantiator.swift | 10 +++++++- .../SubLMs/lmAssociates.swift | 25 ++++++++++++++++++- 6 files changed, 44 insertions(+), 10 deletions(-) diff --git a/Source/Modules/ControllerModules/KeyHandler_Core.swift b/Source/Modules/ControllerModules/KeyHandler_Core.swift index 93217d84..4caf14b5 100644 --- a/Source/Modules/ControllerModules/KeyHandler_Core.swift +++ b/Source/Modules/ControllerModules/KeyHandler_Core.swift @@ -149,10 +149,10 @@ class KeyHandler { /// - Parameter key: 給定的聯想詞的開頭字。 /// - Returns: 抓取到的聯想詞陣列。 /// 不會是 nil,但那些負責接收結果的函式會對空白陣列結果做出正確的處理。 - func buildAssociatePhraseArray(withKey key: String) -> [(String, String)] { + func buildAssociatePhraseArray(withPair pair: Megrez.KeyValuePaired) -> [(String, String)] { var arrResult: [(String, String)] = [] - if currentLM.hasAssociatedPhrasesFor(key: key) { - arrResult = currentLM.associatedPhrasesFor(key: key).map { ("", $0) } + if currentLM.hasAssociatedPhrasesFor(pair: pair) { + arrResult = currentLM.associatedPhrasesFor(pair: pair).map { ("", $0) } } return arrResult } diff --git a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift index b984167d..cd16f283 100644 --- a/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift +++ b/Source/Modules/ControllerModules/KeyHandler_HandleInput.swift @@ -225,6 +225,7 @@ extension KeyHandler { ) if choosingCandidates.candidates.count == 1 { clear() + let reading: String = choosingCandidates.candidates.first?.0 ?? "" let text: String = choosingCandidates.candidates.first?.1 ?? "" stateCallback(InputState.Committing(textToCommit: text)) @@ -233,7 +234,7 @@ extension KeyHandler { } else { if let associatedPhrases = buildAssociatePhraseState( - withKey: text, + withPair: .init(key: reading, value: text), isTypingVertical: input.isTypingVertical ), !associatedPhrases.candidates.isEmpty { diff --git a/Source/Modules/ControllerModules/KeyHandler_States.swift b/Source/Modules/ControllerModules/KeyHandler_States.swift index 40e2a73d..bc45cb13 100644 --- a/Source/Modules/ControllerModules/KeyHandler_States.swift +++ b/Source/Modules/ControllerModules/KeyHandler_States.swift @@ -198,12 +198,12 @@ extension KeyHandler { /// - isTypingVertical: 是否縱排輸入? /// - Returns: 回呼一個新的聯想詞狀態,來就給定的聯想詞陣列資料內容顯示選字窗。 func buildAssociatePhraseState( - withKey key: String!, + withPair pair: Megrez.KeyValuePaired, isTypingVertical: Bool ) -> InputState.AssociatedPhrases! { // 上一行必須要用驚嘆號,否則 Xcode 會誤導你砍掉某些實際上必需的語句。 InputState.AssociatedPhrases( - candidates: buildAssociatePhraseArray(withKey: key), isTypingVertical: isTypingVertical + candidates: buildAssociatePhraseArray(withPair: pair), isTypingVertical: isTypingVertical ) } diff --git a/Source/Modules/ControllerModules/ctlInputMethod_Delegates.swift b/Source/Modules/ControllerModules/ctlInputMethod_Delegates.swift index 7b0f6f92..d3f60594 100644 --- a/Source/Modules/ControllerModules/ctlInputMethod_Delegates.swift +++ b/Source/Modules/ControllerModules/ctlInputMethod_Delegates.swift @@ -122,7 +122,8 @@ extension ctlInputMethod: ctlCandidateDelegate { handle(state: InputState.Committing(textToCommit: composingBuffer)) if mgrPrefs.associatedPhrasesEnabled, let associatePhrases = keyHandler.buildAssociatePhraseState( - withKey: composingBuffer, isTypingVertical: state.isTypingVertical + withPair: .init(key: selectedValue.0, value: selectedValue.1), + isTypingVertical: state.isTypingVertical ), !associatePhrases.candidates.isEmpty { handle(state: associatePhrases) @@ -140,7 +141,8 @@ extension ctlInputMethod: ctlCandidateDelegate { handle(state: InputState.Committing(textToCommit: selectedValue.1)) if mgrPrefs.associatedPhrasesEnabled, let associatePhrases = keyHandler.buildAssociatePhraseState( - withKey: selectedValue.1, isTypingVertical: state.isTypingVertical + withPair: .init(key: selectedValue.0, value: selectedValue.1), + isTypingVertical: state.isTypingVertical ), !associatePhrases.candidates.isEmpty { handle(state: associatePhrases) diff --git a/Source/Modules/LangModelRelated/LMInstantiator.swift b/Source/Modules/LangModelRelated/LMInstantiator.swift index 69b9c73f..39c10112 100644 --- a/Source/Modules/LangModelRelated/LMInstantiator.swift +++ b/Source/Modules/LangModelRelated/LMInstantiator.swift @@ -249,13 +249,21 @@ extension vChewing { } public func associatedPhrasesFor(key: String) -> [String] { - lmAssociates.valuesFor(key: key) ?? [] + lmAssociates.valuesFor(key: key) } public func hasAssociatedPhrasesFor(key: String) -> Bool { lmAssociates.hasValuesFor(key: key) } + public func associatedPhrasesFor(pair: Megrez.KeyValuePaired) -> [String] { + lmAssociates.valuesFor(pair: pair) + } + + public func hasAssociatedPhrasesFor(pair: Megrez.KeyValuePaired) -> Bool { + lmAssociates.hasValuesFor(pair: pair) + } + /// 該函式不起作用,僅用來滿足 LangModelProtocol 協定的要求。 public func bigramsFor(precedingKey _: String, key _: String) -> [Megrez.Bigram] { .init() } diff --git a/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift b/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift index 2740e293..958da8bd 100644 --- a/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift +++ b/Source/Modules/LangModelRelated/SubLMs/lmAssociates.swift @@ -90,7 +90,7 @@ extension vChewing { IME.prtDebugIntel(strDump) } - public func valuesFor(key: String) -> [String]? { + public func valuesFor(key: String) -> [String] { var pairs: [String] = [] if let arrRangeRecords: [Range] = rangeMap[key] { for netaRange in arrRangeRecords { @@ -105,6 +105,29 @@ extension vChewing { public func hasValuesFor(key: String) -> Bool { rangeMap[key] != nil } + + public func valuesFor(pair: Megrez.KeyValuePaired) -> [String] { + var pairs: [String] = [] + if let arrRangeRecords: [Range] = rangeMap[pair.toNGramKey] { + for netaRange in arrRangeRecords { + let neta = strData[netaRange].split(separator: " ") + let theValue: String = .init(neta[1]) + pairs.append(theValue) + } + } else if let arrRangeRecords: [Range] = rangeMap[pair.value] { + for netaRange in arrRangeRecords { + let neta = strData[netaRange].split(separator: " ") + let theValue: String = .init(neta[1]) + pairs.append(theValue) + } + } + return pairs + } + + public func hasValuesFor(pair: Megrez.KeyValuePaired) -> Bool { + if rangeMap[pair.toNGramKey] != nil { return true } + return rangeMap[pair.value] != nil + } } }