From 7c3d90dc809c8bcd6df0e79ca0320e722df18bcf Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Sat, 24 Sep 2022 09:28:14 +0800 Subject: [PATCH] LMI // Introducing hasKeyValuePairFor(), using it in LMMgr. --- .../Sources/LangModelAssembly/LMInstantiator.swift | 9 +++++++++ Source/Modules/LMMgr.swift | 13 ++++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift index 90a5c0f7..0efc45ae 100644 --- a/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift +++ b/Packages/vChewing_LangModelAssembly/Sources/LangModelAssembly/LMInstantiator.swift @@ -221,6 +221,15 @@ extension vChewingLM { return !unigramsFor(key: key).isEmpty } + /// 根據給定的索引鍵和資料值,確認是否有該具體的資料值在庫。 + /// - Parameters: + /// - key: 索引鍵。 + /// - value: 資料值。 + /// - Returns: 是否在庫。 + public func hasKeyValuePairFor(key: String, value: String) -> Bool { + unigramsFor(key: key).map(\.value).contains(value) + } + /// 給定讀音字串,讓 LMI 給出對應的經過處理的單元圖陣列。 /// - Parameter key: 給定的讀音字串。 /// - Returns: 對應的經過處理的單元圖陣列。 diff --git a/Source/Modules/LMMgr.swift b/Source/Modules/LMMgr.swift index 04897042..426818c2 100644 --- a/Source/Modules/LMMgr.swift +++ b/Source/Modules/LMMgr.swift @@ -8,7 +8,6 @@ import BookmarkManager import LangModelAssembly -import Megrez import NotifierUI import Shared @@ -245,15 +244,11 @@ public enum LMMgr { mode: Shared.InputMode, key unigramKey: String ) -> Bool { - let unigrams: [Megrez.Unigram] = - (mode == .imeModeCHT) - ? Self.lmCHT.unigramsFor(key: unigramKey) : Self.lmCHS.unigramsFor(key: unigramKey) - for unigram in unigrams { - if unigram.value == userPhrase { - return true - } + switch mode { + case .imeModeCHS: return lmCHS.hasKeyValuePairFor(key: unigramKey, value: userPhrase) + case .imeModeCHT: return lmCHT.hasKeyValuePairFor(key: unigramKey, value: userPhrase) + case .imeModeNULL: return false } - return false } public static func setPhraseReplacementEnabled(_ state: Bool) {