From 78a950e355973b89ba3a6e7c88fe603e3220c412 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Thu, 23 Mar 2023 12:07:27 +0800 Subject: [PATCH] LMAssembly & LMMgr // Add countPhrasePairs(), etc. --- .../LangModelAssembly/LMInstantiator.swift | 11 ++++++++++ .../PhraseEditorUI/PhraseEditorDelegate.swift | 2 +- .../PhraseEditorUI/PhraseEditorUI.swift | 2 +- Source/Modules/IMEStateData.swift | 2 +- Source/Modules/LMMgr_Core.swift | 20 ++++++++++++++++++- .../Modules/LMMgr_PhraseEditorDelegate.swift | 6 +++--- .../Modules/LMMgr_UserPhraseStructure.swift | 2 +- .../CtlPrefWindow_PhraseEditor.swift | 2 +- 8 files changed, 38 insertions(+), 9 deletions(-) diff --git a/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift index d2b5f0eb..467a4deb 100644 --- a/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift +++ b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift @@ -305,6 +305,17 @@ public extension vChewingLM { : unigramsFor(keyArray: keyArray).map(\.value).contains(value) } + /// 根據給定的索引鍵,確認有多少筆資料值在庫。 + /// - Parameters: + /// - keyArray: 索引鍵陣列。 + /// - factoryDictionaryOnly: 是否僅統計原廠辭典。 + /// - Returns: 是否在庫。 + public func countKeyValuePairs(keyArray: [String], factoryDictionaryOnly: Bool = false) -> Int { + factoryDictionaryOnly + ? lmCore.unigramsFor(key: keyArray.joined(separator: "-")).count + : unigramsFor(keyArray: keyArray).count + } + /// 給定讀音字串,讓 LMI 給出對應的經過處理的單元圖陣列。 /// - Parameter key: 給定的讀音字串。 /// - Returns: 對應的經過處理的單元圖陣列。 diff --git a/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorDelegate.swift b/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorDelegate.swift index e614eff1..6569cd53 100644 --- a/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorDelegate.swift +++ b/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorDelegate.swift @@ -15,7 +15,7 @@ public protocol PhraseEditorDelegate { func retrieveData(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType) -> String @discardableResult func saveData(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType, data: String) -> String - func checkIfUserPhraseExist(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool + func checkIfPhrasePairExists(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool func consolidate(text strProcessed: inout String, pragma shouldCheckPragma: Bool) func openPhraseFile(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType, app: String) func tagOverrides(in strProcessed: inout String, mode: Shared.InputMode) diff --git a/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorUI.swift b/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorUI.swift index 3593d066..da785a6c 100644 --- a/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorUI.swift +++ b/Packages/vChewing_PhraseEditorUI/Sources/PhraseEditorUI/PhraseEditorUI.swift @@ -140,7 +140,7 @@ public struct VwrPhraseEditorUI: View { } if !txtAddPhraseField4.isEmpty { arrResult.append("#" + txtAddPhraseField4) } if let delegate = delegate, - delegate.checkIfUserPhraseExist( + delegate.checkIfPhrasePairExists( userPhrase: txtAddPhraseField1, mode: selInputMode, key: txtAddPhraseField2 ) { diff --git a/Source/Modules/IMEStateData.swift b/Source/Modules/IMEStateData.swift index a4618756..496cb996 100644 --- a/Source/Modules/IMEStateData.swift +++ b/Source/Modules/IMEStateData.swift @@ -77,7 +77,7 @@ public struct IMEStateData: IMEStateDataProtocol { public var markedTargetExists: Bool { let pair = userPhraseKVPair - return LMMgr.checkIfUserPhraseExist( + return LMMgr.checkIfPhrasePairExists( userPhrase: pair.value, mode: IMEApp.currentInputMode, keyArray: pair.keyArray ) } diff --git a/Source/Modules/LMMgr_Core.swift b/Source/Modules/LMMgr_Core.swift index 69a0aee1..3142f993 100644 --- a/Source/Modules/LMMgr_Core.swift +++ b/Source/Modules/LMMgr_Core.swift @@ -274,7 +274,7 @@ public class LMMgr { ) } - public static func checkIfUserPhraseExist( + public static func checkIfPhrasePairExists( userPhrase: String, mode: Shared.InputMode, keyArray: [String], @@ -293,6 +293,24 @@ public class LMMgr { } } + public static func countPhrasePairs( + keyArray: [String], + mode: Shared.InputMode, + factoryDictionaryOnly: Bool = false + ) -> Int { + switch mode { + case .imeModeCHS: + return lmCHS.countKeyValuePairs( + keyArray: keyArray, factoryDictionaryOnly: factoryDictionaryOnly + ) + case .imeModeCHT: + return lmCHT.countKeyValuePairs( + keyArray: keyArray, factoryDictionaryOnly: factoryDictionaryOnly + ) + case .imeModeNULL: return 0 + } + } + public static func setPhraseReplacementEnabled(_ state: Bool) { Self.lmCHT.isPhraseReplacementEnabled = state Self.lmCHS.isPhraseReplacementEnabled = state diff --git a/Source/Modules/LMMgr_PhraseEditorDelegate.swift b/Source/Modules/LMMgr_PhraseEditorDelegate.swift index 61bc596f..39a7aaee 100644 --- a/Source/Modules/LMMgr_PhraseEditorDelegate.swift +++ b/Source/Modules/LMMgr_PhraseEditorDelegate.swift @@ -25,8 +25,8 @@ extension LMMgr: PhraseEditorDelegate { vChewingLM.LMConsolidator.consolidate(text: &strProcessed, pragma: shouldCheckPragma) } - public func checkIfUserPhraseExist(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool { - Self.checkIfUserPhraseExist(userPhrase: userPhrase, mode: mode, keyArray: [unigramKey]) + public func checkIfPhrasePairExists(userPhrase: String, mode: Shared.InputMode, key unigramKey: String) -> Bool { + Self.checkIfPhrasePairExists(userPhrase: userPhrase, mode: mode, keyArray: [unigramKey]) } public func retrieveData(mode: Shared.InputMode, type: vChewingLM.ReplacableUserDataType) -> String { @@ -95,7 +95,7 @@ extension LMMgr: PhraseEditorDelegate { for currentLine in strProcessed.split(separator: "\n") { let arr = currentLine.split(separator: " ") guard arr.count >= 2 else { continue } - let exists = Self.checkIfUserPhraseExist( + let exists = Self.checkIfPhrasePairExists( userPhrase: arr[0].description, mode: mode, keyArray: arr[1].split(separator: "-").map(\.description), factoryDictionaryOnly: true diff --git a/Source/Modules/LMMgr_UserPhraseStructure.swift b/Source/Modules/LMMgr_UserPhraseStructure.swift index 137e6eca..e835e786 100644 --- a/Source/Modules/LMMgr_UserPhraseStructure.swift +++ b/Source/Modules/LMMgr_UserPhraseStructure.swift @@ -21,7 +21,7 @@ public extension LMMgr { public var weight: Double? private var isDuplicated: Bool { - LMMgr.checkIfUserPhraseExist(userPhrase: value, mode: inputMode, keyArray: keyArray) + LMMgr.checkIfPhrasePairExists(userPhrase: value, mode: inputMode, keyArray: keyArray) } public var description: String { diff --git a/Source/Modules/WindowControllers/CtlPrefWindow_PhraseEditor.swift b/Source/Modules/WindowControllers/CtlPrefWindow_PhraseEditor.swift index 213ff67b..b876ec4d 100644 --- a/Source/Modules/WindowControllers/CtlPrefWindow_PhraseEditor.swift +++ b/Source/Modules/WindowControllers/CtlPrefWindow_PhraseEditor.swift @@ -214,7 +214,7 @@ extension CtlPrefWindow: NSTextViewDelegate, NSTextFieldDelegate { arrResult.append(weightVal.description) } if !txtPECommentField.stringValue.isEmpty { arrResult.append("#" + txtPECommentField.stringValue) } - if LMMgr.shared.checkIfUserPhraseExist( + if LMMgr.shared.checkIfPhrasePairExists( userPhrase: txtPEField1.stringValue, mode: selInputMode, key: txtPEField2.stringValue ) { arrResult.append(" #𝙾𝚟𝚎𝚛𝚛𝚒𝚍𝚎")