LMAssembly // Store file paths of all sub-language-models.
- Also: LMPlainBPMF // Variable name fix.
This commit is contained in:
parent
cd85f821d2
commit
8f5bd48beb
|
@ -13,6 +13,7 @@ import Shared
|
|||
|
||||
extension vChewingLM {
|
||||
@frozen public struct LMAssociates {
|
||||
public private(set) var filePath: String?
|
||||
var rangeMap: [String: [(Range<String.Index>, Int)]] = [:]
|
||||
var strData: String = ""
|
||||
|
||||
|
@ -37,6 +38,8 @@ extension vChewingLM {
|
|||
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
let oldPath = filePath
|
||||
filePath = nil
|
||||
|
||||
LMConsolidator.fixEOF(path: path)
|
||||
LMConsolidator.consolidate(path: path, pragma: true)
|
||||
|
@ -45,11 +48,13 @@ extension vChewingLM {
|
|||
let rawStrData = try String(contentsOfFile: path, encoding: .utf8)
|
||||
replaceData(textData: rawStrData)
|
||||
} catch {
|
||||
filePath = oldPath
|
||||
vCLog("\(error)")
|
||||
vCLog("↑ Exception happened when reading data at: \(path).")
|
||||
return false
|
||||
}
|
||||
|
||||
filePath = path
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -74,6 +79,7 @@ extension vChewingLM {
|
|||
}
|
||||
|
||||
public mutating func clear() {
|
||||
filePath = nil
|
||||
rangeMap.removeAll()
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ import Shared
|
|||
extension vChewingLM {
|
||||
/// 磁帶模組,用來方便使用者自行擴充字根輸入法。
|
||||
@frozen public struct LMCassette {
|
||||
public private(set) var filePath: String?
|
||||
public private(set) var nameShort: String = ""
|
||||
public private(set) var nameENG: String = ""
|
||||
public private(set) var nameCJK: String = ""
|
||||
|
@ -67,6 +68,8 @@ extension vChewingLM {
|
|||
/// - Returns: 是否載入成功。
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
let oldPath = filePath
|
||||
filePath = nil
|
||||
if FileManager.default.fileExists(atPath: path) {
|
||||
do {
|
||||
guard let fileHandle = FileHandle(forReadingAtPath: path) else {
|
||||
|
@ -146,17 +149,20 @@ extension vChewingLM {
|
|||
}
|
||||
maxKeyLength = theMaxKeyLength
|
||||
keyNameMap[wildcardKey] = keyNameMap[wildcardKey] ?? "?"
|
||||
filePath = path
|
||||
return true
|
||||
} catch {
|
||||
vCLog("CIN Loading Failed: File Access Error.")
|
||||
return false
|
||||
}
|
||||
} else {
|
||||
vCLog("CIN Loading Failed: File Missing.")
|
||||
}
|
||||
vCLog("CIN Loading Failed: File Missing.")
|
||||
filePath = oldPath
|
||||
return false
|
||||
}
|
||||
|
||||
public mutating func clear() {
|
||||
filePath = nil
|
||||
keyNameMap.removeAll()
|
||||
charDefMap.removeAll()
|
||||
charDefWildcardMap.removeAll()
|
||||
|
|
|
@ -17,6 +17,7 @@ extension vChewingLM {
|
|||
/// 資料記錄原理與上游 C++ 的 ParselessLM 差不多,但用的是 Swift 原生手段。
|
||||
/// 主要時間消耗仍在 For 迴圈,但這個算法可以顯著減少記憶體佔用。
|
||||
@frozen public struct LMCoreEX {
|
||||
public private(set) var filePath: String?
|
||||
/// 資料庫辭典。索引內容為注音字串,資料內容則為字串首尾範圍、方便自 strData 取資料。
|
||||
var rangeMap: [String: [Range<String.Index>]] = [:]
|
||||
/// 資料庫追加辭典。
|
||||
|
@ -60,6 +61,10 @@ extension vChewingLM {
|
|||
/// - path: 給定路徑。
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
|
||||
let oldPath = filePath
|
||||
filePath = nil
|
||||
|
||||
var consolidated = false
|
||||
|
||||
if allowConsolidation {
|
||||
|
@ -76,11 +81,13 @@ extension vChewingLM {
|
|||
}
|
||||
replaceData(textData: rawStrData)
|
||||
} catch {
|
||||
filePath = oldPath
|
||||
vCLog("\(error)")
|
||||
vCLog("↑ Exception happened when reading data at: \(path).")
|
||||
return false
|
||||
}
|
||||
|
||||
filePath = path
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -106,6 +113,7 @@ extension vChewingLM {
|
|||
|
||||
/// 將當前語言模組的資料庫辭典自記憶體內卸除。
|
||||
public mutating func clear() {
|
||||
filePath = nil
|
||||
rangeMap.removeAll()
|
||||
}
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@ extension vChewingLM {
|
|||
/// 這樣一來可以節省在舊 mac 機種內的資料讀入速度。
|
||||
/// 目前僅針對輸入法原廠語彙資料檔案使用 plist 格式。
|
||||
@frozen public struct LMCoreNS {
|
||||
public private(set) var filePath: String?
|
||||
/// 資料庫辭典。索引內容為經過加密的注音字串,資料內容則為 UTF8 資料陣列。
|
||||
var rangeMap: [String: [Data]] = [:]
|
||||
/// 【已作廢】資料庫字串陣列。在 LMCoreNS 內沒有作用。
|
||||
|
@ -59,6 +60,8 @@ extension vChewingLM {
|
|||
/// - path: 給定路徑。
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
let oldPath = filePath
|
||||
filePath = nil
|
||||
|
||||
do {
|
||||
let rawData = try Data(contentsOf: URL(fileURLWithPath: path))
|
||||
|
@ -66,15 +69,18 @@ extension vChewingLM {
|
|||
try PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String: [Data]] ?? .init()
|
||||
rangeMap = rawPlist
|
||||
} catch {
|
||||
filePath = oldPath
|
||||
vCLog("↑ Exception happened when reading plist file at: \(path).")
|
||||
return false
|
||||
}
|
||||
|
||||
filePath = path
|
||||
return true
|
||||
}
|
||||
|
||||
/// 將當前語言模組的資料庫辭典自記憶體內卸除。
|
||||
public mutating func clear() {
|
||||
filePath = nil
|
||||
rangeMap.removeAll()
|
||||
}
|
||||
|
||||
|
|
|
@ -12,46 +12,52 @@ import Shared
|
|||
|
||||
extension vChewingLM {
|
||||
@frozen public struct LMPlainBopomofo {
|
||||
var rangeMap: [String: String] = [:]
|
||||
public private(set) var filePath: String?
|
||||
var dataMap: [String: String] = [:]
|
||||
|
||||
public var count: Int { rangeMap.count }
|
||||
public var count: Int { dataMap.count }
|
||||
|
||||
public init() {
|
||||
rangeMap = [:]
|
||||
dataMap = [:]
|
||||
}
|
||||
|
||||
public var isLoaded: Bool { !rangeMap.isEmpty }
|
||||
public var isLoaded: Bool { !dataMap.isEmpty }
|
||||
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
let oldPath = filePath
|
||||
filePath = nil
|
||||
|
||||
do {
|
||||
let rawData = try Data(contentsOf: URL(fileURLWithPath: path))
|
||||
let rawPlist: [String: String] =
|
||||
try PropertyListSerialization.propertyList(from: rawData, format: nil) as? [String: String] ?? .init()
|
||||
rangeMap = rawPlist
|
||||
dataMap = rawPlist
|
||||
} catch {
|
||||
filePath = oldPath
|
||||
vCLog("\(error)")
|
||||
vCLog("↑ Exception happened when reading data at: \(path).")
|
||||
return false
|
||||
}
|
||||
|
||||
filePath = path
|
||||
return true
|
||||
}
|
||||
|
||||
public mutating func clear() {
|
||||
rangeMap.removeAll()
|
||||
filePath = nil
|
||||
dataMap.removeAll()
|
||||
}
|
||||
|
||||
public func valuesFor(key: String) -> [String] {
|
||||
var pairs: [String] = []
|
||||
if let arrRangeRecords: String = rangeMap[key]?.trimmingCharacters(in: .newlines) {
|
||||
if let arrRangeRecords: String = dataMap[key]?.trimmingCharacters(in: .newlines) {
|
||||
pairs.append(contentsOf: arrRangeRecords.map { String($0) })
|
||||
}
|
||||
return pairs.deduplicated
|
||||
}
|
||||
|
||||
public func hasValuesFor(key: String) -> Bool { rangeMap.keys.contains(key) }
|
||||
public func hasValuesFor(key: String) -> Bool { dataMap.keys.contains(key) }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -11,6 +11,7 @@ import Shared
|
|||
|
||||
extension vChewingLM {
|
||||
@frozen public struct LMReplacements {
|
||||
public private(set) var filePath: String?
|
||||
var rangeMap: [String: Range<String.Index>] = [:]
|
||||
var strData: String = ""
|
||||
|
||||
|
@ -24,6 +25,8 @@ extension vChewingLM {
|
|||
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
let oldPath = filePath
|
||||
filePath = nil
|
||||
|
||||
LMConsolidator.fixEOF(path: path)
|
||||
LMConsolidator.consolidate(path: path, pragma: true)
|
||||
|
@ -32,11 +35,13 @@ extension vChewingLM {
|
|||
let rawStrData = try String(contentsOfFile: path, encoding: .utf8)
|
||||
replaceData(textData: rawStrData)
|
||||
} catch {
|
||||
filePath = oldPath
|
||||
vCLog("\(error)")
|
||||
vCLog("↑ Exception happened when reading data at: \(path).")
|
||||
return false
|
||||
}
|
||||
|
||||
filePath = path
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -59,6 +64,7 @@ extension vChewingLM {
|
|||
}
|
||||
|
||||
public mutating func clear() {
|
||||
filePath = nil
|
||||
rangeMap.removeAll()
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue