LMAssembly // Adapting LMCassette to LMInstantiator.

- Also using "Self." when necessary.
This commit is contained in:
ShikiSuen 2022-10-15 10:40:25 +08:00
parent f74d3c174d
commit 9411f4c7b8
1 changed files with 28 additions and 8 deletions

View File

@ -30,6 +30,7 @@ extension vChewingLM {
/// ///
public class LMInstantiator: LangModelProtocol { public class LMInstantiator: LangModelProtocol {
// //
public var isCassetteEnabled = false
public var isPhraseReplacementEnabled = false public var isPhraseReplacementEnabled = false
public var isCNSEnabled = false public var isCNSEnabled = false
public var isSymbolEnabled = false public var isSymbolEnabled = false
@ -71,6 +72,10 @@ extension vChewingLM {
reverse: true, consolidate: false, defaultScore: -13.0, forceDefaultScore: false reverse: true, consolidate: false, defaultScore: -13.0, forceDefaultScore: false
) )
// currentCassetteMetadata
static var lmCassette = LMCassette()
public var currentCassette: LMCassette { Self.lmCassette }
// 使 // 使
// 使使 // 使使
var lmUserPhrases = LMCoreEX( var lmUserPhrases = LMCoreEX(
@ -98,11 +103,11 @@ extension vChewingLM {
} }
} }
public var isCNSDataLoaded: Bool { vChewingLM.LMInstantiator.lmCNS.isLoaded } public var isCNSDataLoaded: Bool { Self.lmCNS.isLoaded }
public func loadCNSData(path: String) { public func loadCNSData(path: String) {
if FileManager.default.isReadableFile(atPath: path) { if FileManager.default.isReadableFile(atPath: path) {
vChewingLM.LMInstantiator.lmCNS.open(path) Self.lmCNS.open(path)
vCLog("lmCNS: \(vChewingLM.LMInstantiator.lmCNS.count) entries of data loaded from: \(path)") vCLog("lmCNS: \(Self.lmCNS.count) entries of data loaded from: \(path)")
} else { } else {
vCLog("lmCNS: File access failure: \(path)") vCLog("lmCNS: File access failure: \(path)")
} }
@ -118,12 +123,12 @@ extension vChewingLM {
} }
} }
public var isSymbolDataLoaded: Bool { vChewingLM.LMInstantiator.lmSymbols.isLoaded } public var isSymbolDataLoaded: Bool { Self.lmSymbols.isLoaded }
public func loadSymbolData(path: String) { public func loadSymbolData(path: String) {
if FileManager.default.isReadableFile(atPath: path) { if FileManager.default.isReadableFile(atPath: path) {
vChewingLM.LMInstantiator.lmSymbols.open(path) Self.lmSymbols.open(path)
vCLog( vCLog(
"lmSymbol: \(vChewingLM.LMInstantiator.lmSymbols.count) entries of data loaded from: \(path)") "lmSymbol: \(Self.lmSymbols.count) entries of data loaded from: \(path)")
} else { } else {
vCLog("lmSymbols: File access failure: \(path)") vCLog("lmSymbols: File access failure: \(path)")
} }
@ -200,6 +205,18 @@ extension vChewingLM {
} }
} }
public func loadCassetteData(path: String) {
DispatchQueue.main.async {
if FileManager.default.isReadableFile(atPath: path) {
Self.lmCassette.close()
Self.lmCassette.open(path)
vCLog("lmCassette: \(Self.lmCassette.count) entries of data loaded from: \(path)")
} else {
vCLog("lmCassette: File access failure: \(path)")
}
}
}
// MARK: - // MARK: -
public func hasAssociatedPhrasesFor(pair: Megrez.Compositor.KeyValuePaired) -> Bool { public func hasAssociatedPhrasesFor(pair: Megrez.Compositor.KeyValuePaired) -> Bool {
@ -217,6 +234,7 @@ extension vChewingLM {
if key == " " { return true } if key == " " { return true }
if !lmFiltered.hasUnigramsFor(key: key) { if !lmFiltered.hasUnigramsFor(key: key) {
return lmUserPhrases.hasUnigramsFor(key: key) || lmCore.hasUnigramsFor(key: key) return lmUserPhrases.hasUnigramsFor(key: key) || lmCore.hasUnigramsFor(key: key)
|| Self.lmCassette.hasUnigramsFor(key: key) || (Self.lmCNS.hasUnigramsFor(key: key) && isCNSEnabled)
} }
return !unigramsFor(key: key).isEmpty return !unigramsFor(key: key).isEmpty
} }
@ -240,6 +258,8 @@ extension vChewingLM {
/// ///
var rawAllUnigrams: [Megrez.Unigram] = [] var rawAllUnigrams: [Megrez.Unigram] = []
if isCassetteEnabled { rawAllUnigrams += Self.lmCassette.unigramsFor(key: key) }
// 使 // 使
if isSCPCEnabled { if isSCPCEnabled {
rawAllUnigrams += lmPlainBopomofo.valuesFor(key: key).map { Megrez.Unigram(value: $0, score: 0) } rawAllUnigrams += lmPlainBopomofo.valuesFor(key: key).map { Megrez.Unigram(value: $0, score: 0) }
@ -255,12 +275,12 @@ extension vChewingLM {
rawAllUnigrams += lmCore.unigramsFor(key: key) rawAllUnigrams += lmCore.unigramsFor(key: key)
if isCNSEnabled { if isCNSEnabled {
rawAllUnigrams += vChewingLM.LMInstantiator.lmCNS.unigramsFor(key: key) rawAllUnigrams += Self.lmCNS.unigramsFor(key: key)
} }
if isSymbolEnabled { if isSymbolEnabled {
rawAllUnigrams += lmUserSymbols.unigramsFor(key: key) rawAllUnigrams += lmUserSymbols.unigramsFor(key: key)
rawAllUnigrams += vChewingLM.LMInstantiator.lmSymbols.unigramsFor(key: key) rawAllUnigrams += Self.lmSymbols.unigramsFor(key: key)
} }
// //