LMI // Introducing hasKeyValuePairFor(), using it in LMMgr.

This commit is contained in:
ShikiSuen 2022-09-24 09:28:14 +08:00
parent 45f595920a
commit 7c3d90dc80
2 changed files with 13 additions and 9 deletions

View File

@ -221,6 +221,15 @@ extension vChewingLM {
return !unigramsFor(key: key).isEmpty 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 /// LMI
/// - Parameter key: /// - Parameter key:
/// - Returns: /// - Returns:

View File

@ -8,7 +8,6 @@
import BookmarkManager import BookmarkManager
import LangModelAssembly import LangModelAssembly
import Megrez
import NotifierUI import NotifierUI
import Shared import Shared
@ -245,15 +244,11 @@ public enum LMMgr {
mode: Shared.InputMode, mode: Shared.InputMode,
key unigramKey: String key unigramKey: String
) -> Bool { ) -> Bool {
let unigrams: [Megrez.Unigram] = switch mode {
(mode == .imeModeCHT) case .imeModeCHS: return lmCHS.hasKeyValuePairFor(key: unigramKey, value: userPhrase)
? Self.lmCHT.unigramsFor(key: unigramKey) : Self.lmCHS.unigramsFor(key: unigramKey) case .imeModeCHT: return lmCHT.hasKeyValuePairFor(key: unigramKey, value: userPhrase)
for unigram in unigrams { case .imeModeNULL: return false
if unigram.value == userPhrase {
return true
}
} }
return false
} }
public static func setPhraseReplacementEnabled(_ state: Bool) { public static func setPhraseReplacementEnabled(_ state: Bool) {