Repo // Change the method of reading factory plists.
This commit is contained in:
parent
45b16f1618
commit
5ef515fa23
|
@ -92,45 +92,51 @@ extension vChewingLM {
|
||||||
|
|
||||||
// MARK: - 工具函式
|
// MARK: - 工具函式
|
||||||
|
|
||||||
|
public func resetFactoryPlistModels() {
|
||||||
|
lmCore.clear()
|
||||||
|
lmMisc.clear()
|
||||||
|
Self.lmCNS.clear()
|
||||||
|
Self.lmSymbols.clear()
|
||||||
|
}
|
||||||
|
|
||||||
public var isCoreLMLoaded: Bool { lmCore.isLoaded }
|
public var isCoreLMLoaded: Bool { lmCore.isLoaded }
|
||||||
public func loadLanguageModel(path: String) {
|
public func loadLanguageModel(plist: (dict: [String: [Data]]?, path: String)) {
|
||||||
if FileManager.default.isReadableFile(atPath: path) {
|
guard let plistDict = plist.dict else {
|
||||||
lmCore.open(path)
|
vCLog("lmCore: File access failure: \(plist.path)")
|
||||||
vCLog("lmCore: \(lmCore.count) entries of data loaded from: \(path)")
|
return
|
||||||
} else {
|
|
||||||
vCLog("lmCore: File access failure: \(path)")
|
|
||||||
}
|
}
|
||||||
|
lmCore.load((dict: plistDict, path: plist.path))
|
||||||
|
vCLog("lmCore: \(lmCore.count) entries of data loaded from: \(plist.path)")
|
||||||
}
|
}
|
||||||
|
|
||||||
public var isCNSDataLoaded: Bool { Self.lmCNS.isLoaded }
|
public var isCNSDataLoaded: Bool { Self.lmCNS.isLoaded }
|
||||||
public func loadCNSData(path: String) {
|
public func loadCNSData(plist: (dict: [String: [Data]]?, path: String)) {
|
||||||
if FileManager.default.isReadableFile(atPath: path) {
|
guard let plistDict = plist.dict else {
|
||||||
Self.lmCNS.open(path)
|
vCLog("lmCNS: File access failure: \(plist.path)")
|
||||||
vCLog("lmCNS: \(Self.lmCNS.count) entries of data loaded from: \(path)")
|
return
|
||||||
} else {
|
|
||||||
vCLog("lmCNS: File access failure: \(path)")
|
|
||||||
}
|
}
|
||||||
|
Self.lmCNS.load((dict: plistDict, path: plist.path))
|
||||||
|
vCLog("lmCNS: \(Self.lmCNS.count) entries of data loaded from: \(plist.path)")
|
||||||
}
|
}
|
||||||
|
|
||||||
public var isMiscDataLoaded: Bool { lmMisc.isLoaded }
|
public var isMiscDataLoaded: Bool { lmMisc.isLoaded }
|
||||||
public func loadMiscData(path: String) {
|
public func loadMiscData(plist: (dict: [String: [Data]]?, path: String)) {
|
||||||
if FileManager.default.isReadableFile(atPath: path) {
|
guard let plistDict = plist.dict else {
|
||||||
lmMisc.open(path)
|
vCLog("lmCore: File access failure: \(plist.path)")
|
||||||
vCLog("lmMisc: \(lmMisc.count) entries of data loaded from: \(path)")
|
return
|
||||||
} else {
|
|
||||||
vCLog("lmMisc: File access failure: \(path)")
|
|
||||||
}
|
}
|
||||||
|
lmMisc.load((dict: plistDict, path: plist.path))
|
||||||
|
vCLog("lmMisc: \(lmMisc.count) entries of data loaded from: \(plist.path)")
|
||||||
}
|
}
|
||||||
|
|
||||||
public var isSymbolDataLoaded: Bool { Self.lmSymbols.isLoaded }
|
public var isSymbolDataLoaded: Bool { Self.lmSymbols.isLoaded }
|
||||||
public func loadSymbolData(path: String) {
|
public func loadSymbolData(plist: (dict: [String: [Data]]?, path: String)) {
|
||||||
if FileManager.default.isReadableFile(atPath: path) {
|
guard let plistDict = plist.dict else {
|
||||||
Self.lmSymbols.open(path)
|
vCLog("lmCore: File access failure: \(plist.path)")
|
||||||
vCLog(
|
return
|
||||||
"lmSymbol: \(Self.lmSymbols.count) entries of data loaded from: \(path)")
|
|
||||||
} else {
|
|
||||||
vCLog("lmSymbols: File access failure: \(path)")
|
|
||||||
}
|
}
|
||||||
|
Self.lmSymbols.load((dict: plistDict, path: plist.path))
|
||||||
|
vCLog("lmSymbols: \(Self.lmSymbols.count) entries of data loaded from: \(plist.path)")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 上述幾個函式不要加 Async,因為這些內容都被 LMMgr 負責用別的方法 Async 了、用 GCD 的多任務並行共結來完成。
|
// 上述幾個函式不要加 Async,因為這些內容都被 LMMgr 負責用別的方法 Async 了、用 GCD 的多任務並行共結來完成。
|
||||||
|
|
|
@ -55,6 +55,15 @@ extension vChewingLM {
|
||||||
/// 檢測資料庫辭典內是否已經有載入的資料。
|
/// 檢測資料庫辭典內是否已經有載入的資料。
|
||||||
public var isLoaded: Bool { !dataMap.isEmpty }
|
public var isLoaded: Bool { !dataMap.isEmpty }
|
||||||
|
|
||||||
|
/// 讀入資料辭典。
|
||||||
|
/// - parameters:
|
||||||
|
/// - dictData: 辭典資料及對應的 URL 位置。
|
||||||
|
public mutating func load(_ dictData: (dict: [String: [Data]], path: String)) {
|
||||||
|
if isLoaded { return }
|
||||||
|
filePath = dictData.path
|
||||||
|
dataMap = dictData.dict
|
||||||
|
}
|
||||||
|
|
||||||
/// 將資料從檔案讀入至資料庫辭典內。
|
/// 將資料從檔案讀入至資料庫辭典內。
|
||||||
/// - parameters:
|
/// - parameters:
|
||||||
/// - path: 給定路徑。
|
/// - path: 給定路徑。
|
||||||
|
|
|
@ -14,8 +14,13 @@ extension vChewingLM {
|
||||||
public private(set) var dataMap: [String: [Data]] = [:]
|
public private(set) var dataMap: [String: [Data]] = [:]
|
||||||
public private(set) var filePath: String = ""
|
public private(set) var filePath: String = ""
|
||||||
|
|
||||||
public init(dataMap: [String: [Data]]) {
|
public init(data dictData: (dict: [String: [Data]]?, path: String)) {
|
||||||
self.dataMap = dataMap
|
guard let theDict = dictData.dict else {
|
||||||
|
vCLog("↑ Exception happened when reading plist file at: \(dictData.path).")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
filePath = dictData.path
|
||||||
|
dataMap = theDict
|
||||||
}
|
}
|
||||||
|
|
||||||
public init(path: String) {
|
public init(path: String) {
|
||||||
|
|
|
@ -66,8 +66,7 @@ public class LMMgr {
|
||||||
public static func loadCoreLanguageModelFile(
|
public static func loadCoreLanguageModelFile(
|
||||||
filenameSansExtension: String, langModel lm: inout vChewingLM.LMInstantiator
|
filenameSansExtension: String, langModel lm: inout vChewingLM.LMInstantiator
|
||||||
) {
|
) {
|
||||||
let dataPath: String = Self.getBundleDataPath(filenameSansExtension)
|
lm.loadLanguageModel(plist: Self.getDictionaryData(filenameSansExtension))
|
||||||
lm.loadLanguageModel(path: dataPath)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static func loadDataModelsOnAppDelegate() {
|
public static func loadDataModelsOnAppDelegate() {
|
||||||
|
@ -77,22 +76,22 @@ public class LMMgr {
|
||||||
group.enter()
|
group.enter()
|
||||||
globalQueue.async {
|
globalQueue.async {
|
||||||
if !Self.lmCHT.isCNSDataLoaded {
|
if !Self.lmCHT.isCNSDataLoaded {
|
||||||
Self.lmCHT.loadCNSData(path: getBundleDataPath("data-cns"))
|
Self.lmCHT.loadCNSData(plist: Self.getDictionaryData("data-cns"))
|
||||||
}
|
}
|
||||||
if !Self.lmCHT.isMiscDataLoaded {
|
if !Self.lmCHT.isMiscDataLoaded {
|
||||||
Self.lmCHT.loadMiscData(path: getBundleDataPath("data-zhuyinwen"))
|
Self.lmCHT.loadMiscData(plist: Self.getDictionaryData("data-zhuyinwen"))
|
||||||
}
|
}
|
||||||
if !Self.lmCHT.isSymbolDataLoaded {
|
if !Self.lmCHT.isSymbolDataLoaded {
|
||||||
Self.lmCHT.loadSymbolData(path: getBundleDataPath("data-symbols"))
|
Self.lmCHT.loadSymbolData(plist: Self.getDictionaryData("data-symbols"))
|
||||||
}
|
}
|
||||||
if !Self.lmCHS.isCNSDataLoaded {
|
if !Self.lmCHS.isCNSDataLoaded {
|
||||||
Self.lmCHS.loadCNSData(path: getBundleDataPath("data-cns"))
|
Self.lmCHS.loadCNSData(plist: Self.getDictionaryData("data-cns"))
|
||||||
}
|
}
|
||||||
if !Self.lmCHS.isMiscDataLoaded {
|
if !Self.lmCHS.isMiscDataLoaded {
|
||||||
Self.lmCHS.loadMiscData(path: getBundleDataPath("data-zhuyinwen"))
|
Self.lmCHS.loadMiscData(plist: Self.getDictionaryData("data-zhuyinwen"))
|
||||||
}
|
}
|
||||||
if !Self.lmCHS.isSymbolDataLoaded {
|
if !Self.lmCHS.isSymbolDataLoaded {
|
||||||
Self.lmCHS.loadSymbolData(path: getBundleDataPath("data-symbols"))
|
Self.lmCHS.loadSymbolData(plist: Self.getDictionaryData("data-symbols"))
|
||||||
}
|
}
|
||||||
group.leave()
|
group.leave()
|
||||||
}
|
}
|
||||||
|
@ -136,23 +135,23 @@ public class LMMgr {
|
||||||
switch mode {
|
switch mode {
|
||||||
case .imeModeCHS:
|
case .imeModeCHS:
|
||||||
if !Self.lmCHS.isCNSDataLoaded {
|
if !Self.lmCHS.isCNSDataLoaded {
|
||||||
Self.lmCHS.loadCNSData(path: getBundleDataPath("data-cns"))
|
Self.lmCHS.loadCNSData(plist: Self.getDictionaryData("data-cns"))
|
||||||
}
|
}
|
||||||
if !Self.lmCHS.isMiscDataLoaded {
|
if !Self.lmCHS.isMiscDataLoaded {
|
||||||
Self.lmCHS.loadMiscData(path: getBundleDataPath("data-zhuyinwen"))
|
Self.lmCHS.loadMiscData(plist: Self.getDictionaryData("data-zhuyinwen"))
|
||||||
}
|
}
|
||||||
if !Self.lmCHS.isSymbolDataLoaded {
|
if !Self.lmCHS.isSymbolDataLoaded {
|
||||||
Self.lmCHS.loadSymbolData(path: getBundleDataPath("data-symbols"))
|
Self.lmCHS.loadSymbolData(plist: Self.getDictionaryData("data-symbols"))
|
||||||
}
|
}
|
||||||
case .imeModeCHT:
|
case .imeModeCHT:
|
||||||
if !Self.lmCHT.isCNSDataLoaded {
|
if !Self.lmCHT.isCNSDataLoaded {
|
||||||
Self.lmCHT.loadCNSData(path: getBundleDataPath("data-cns"))
|
Self.lmCHT.loadCNSData(plist: Self.getDictionaryData("data-cns"))
|
||||||
}
|
}
|
||||||
if !Self.lmCHT.isMiscDataLoaded {
|
if !Self.lmCHT.isMiscDataLoaded {
|
||||||
Self.lmCHT.loadMiscData(path: getBundleDataPath("data-zhuyinwen"))
|
Self.lmCHT.loadMiscData(plist: Self.getDictionaryData("data-zhuyinwen"))
|
||||||
}
|
}
|
||||||
if !Self.lmCHT.isSymbolDataLoaded {
|
if !Self.lmCHT.isSymbolDataLoaded {
|
||||||
Self.lmCHT.loadSymbolData(path: getBundleDataPath("data-symbols"))
|
Self.lmCHT.loadSymbolData(plist: Self.getDictionaryData("data-symbols"))
|
||||||
}
|
}
|
||||||
default: break
|
default: break
|
||||||
}
|
}
|
||||||
|
@ -194,6 +193,17 @@ public class LMMgr {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static func reloadFactoryDictionaryPlists() {
|
||||||
|
FrmRevLookupWindow.reloadData()
|
||||||
|
LMMgr.lmCHS.resetFactoryPlistModels()
|
||||||
|
LMMgr.lmCHT.resetFactoryPlistModels()
|
||||||
|
if PrefMgr.shared.onlyLoadFactoryLangModelsIfNeeded {
|
||||||
|
LMMgr.loadDataModel(IMEApp.currentInputMode)
|
||||||
|
} else {
|
||||||
|
LMMgr.loadDataModelsOnAppDelegate()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// 載入磁帶資料。
|
/// 載入磁帶資料。
|
||||||
/// - Remark: cassettePath() 會在輸入法停用磁帶時直接返回
|
/// - Remark: cassettePath() 會在輸入法停用磁帶時直接返回
|
||||||
public static func loadCassetteData() {
|
public static func loadCassetteData() {
|
||||||
|
|
|
@ -29,15 +29,15 @@ class CtlRevLookupWindow: NSWindowController, NSWindowDelegate {
|
||||||
class FrmRevLookupWindow: NSWindow {
|
class FrmRevLookupWindow: NSWindow {
|
||||||
typealias LMRevLookup = vChewingLM.LMRevLookup
|
typealias LMRevLookup = vChewingLM.LMRevLookup
|
||||||
|
|
||||||
static let lmRevLookupCore = LMRevLookup(path: LMMgr.getBundleDataPath("data-bpmf-reverse-lookup"))
|
static var lmRevLookupCore = LMRevLookup(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup"))
|
||||||
|
|
||||||
// 全字庫資料接近十萬筆索引,只放到單個 Dictionary 內的話、每次查詢時都會把輸入法搞崩潰。只能分卷處理。
|
// 全字庫資料接近十萬筆索引,只放到單個 Dictionary 內的話、每次查詢時都會把輸入法搞崩潰。只能分卷處理。
|
||||||
static let lmRevLookupCNS1 = LMRevLookup(path: LMMgr.getBundleDataPath("data-bpmf-reverse-lookup-CNS1"))
|
static var lmRevLookupCNS1 = LMRevLookup(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS1"))
|
||||||
static let lmRevLookupCNS2 = LMRevLookup(path: LMMgr.getBundleDataPath("data-bpmf-reverse-lookup-CNS2"))
|
static var lmRevLookupCNS2 = LMRevLookup(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS2"))
|
||||||
static let lmRevLookupCNS3 = LMRevLookup(path: LMMgr.getBundleDataPath("data-bpmf-reverse-lookup-CNS3"))
|
static var lmRevLookupCNS3 = LMRevLookup(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS3"))
|
||||||
static let lmRevLookupCNS4 = LMRevLookup(path: LMMgr.getBundleDataPath("data-bpmf-reverse-lookup-CNS4"))
|
static var lmRevLookupCNS4 = LMRevLookup(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS4"))
|
||||||
static let lmRevLookupCNS5 = LMRevLookup(path: LMMgr.getBundleDataPath("data-bpmf-reverse-lookup-CNS5"))
|
static var lmRevLookupCNS5 = LMRevLookup(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS5"))
|
||||||
static let lmRevLookupCNS6 = LMRevLookup(path: LMMgr.getBundleDataPath("data-bpmf-reverse-lookup-CNS6"))
|
static var lmRevLookupCNS6 = LMRevLookup(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS6"))
|
||||||
|
|
||||||
public lazy var inputField = NSTextField()
|
public lazy var inputField = NSTextField()
|
||||||
public lazy var resultView = NSTextView()
|
public lazy var resultView = NSTextView()
|
||||||
|
@ -46,6 +46,16 @@ class FrmRevLookupWindow: NSWindow {
|
||||||
private lazy var button = NSButton()
|
private lazy var button = NSButton()
|
||||||
private lazy var view = NSView()
|
private lazy var view = NSView()
|
||||||
|
|
||||||
|
static func reloadData() {
|
||||||
|
DispatchQueue.main.async { lmRevLookupCore = .init(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup")) }
|
||||||
|
DispatchQueue.main.async { lmRevLookupCNS1 = .init(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS1")) }
|
||||||
|
DispatchQueue.main.async { lmRevLookupCNS2 = .init(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS2")) }
|
||||||
|
DispatchQueue.main.async { lmRevLookupCNS3 = .init(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS3")) }
|
||||||
|
DispatchQueue.main.async { lmRevLookupCNS4 = .init(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS4")) }
|
||||||
|
DispatchQueue.main.async { lmRevLookupCNS5 = .init(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS5")) }
|
||||||
|
DispatchQueue.main.async { lmRevLookupCNS6 = .init(data: LMMgr.getDictionaryData("data-bpmf-reverse-lookup-CNS6")) }
|
||||||
|
}
|
||||||
|
|
||||||
init() {
|
init() {
|
||||||
super.init(
|
super.init(
|
||||||
contentRect: CGRect(x: 196, y: 240, width: 480, height: 340),
|
contentRect: CGRect(x: 196, y: 240, width: 480, height: 340),
|
||||||
|
|
Loading…
Reference in New Issue