LMAssembly // Use class in lieu of struct.
This commit is contained in:
parent
da891266f5
commit
5ca4138b63
|
@ -56,40 +56,40 @@ extension vChewingLM {
|
|||
// 聲明原廠語言模組:
|
||||
// Reverse 的話,第一欄是注音,第二欄是對應的漢字,第三欄是可能的權重。
|
||||
// 不 Reverse 的話,第一欄是漢字,第二欄是對應的注音,第三欄是可能的權重。
|
||||
var lmCore = LMCoreNS(
|
||||
let lmCore = LMCoreNS(
|
||||
reverse: false, consolidate: false, defaultScore: -9.9, forceDefaultScore: false
|
||||
)
|
||||
var lmMisc = LMCoreNS(
|
||||
let lmMisc = LMCoreNS(
|
||||
reverse: true, consolidate: false, defaultScore: -1.0, forceDefaultScore: false
|
||||
)
|
||||
|
||||
// 簡體中文模式與繁體中文模式共用全字庫擴展模組,故靜態處理。
|
||||
// 不然,每個模式都會讀入一份全字庫,會多佔用 100MB 記憶體。
|
||||
static var lmCNS = vChewingLM.LMCoreNS(
|
||||
static let lmCNS = vChewingLM.LMCoreNS(
|
||||
reverse: true, consolidate: false, defaultScore: -11.0, forceDefaultScore: false
|
||||
)
|
||||
static var lmSymbols = vChewingLM.LMCoreNS(
|
||||
static let lmSymbols = vChewingLM.LMCoreNS(
|
||||
reverse: true, consolidate: false, defaultScore: -13.0, forceDefaultScore: false
|
||||
)
|
||||
|
||||
// 磁帶資料模組。「currentCassette」對外唯讀,僅用來讀取磁帶本身的中繼資料(Metadata)。
|
||||
static var lmCassette = LMCassette()
|
||||
static let lmCassette = LMCassette()
|
||||
public var currentCassette: LMCassette { Self.lmCassette }
|
||||
|
||||
// 聲明使用者語言模組。
|
||||
// 使用者語言模組使用多執行緒的話,可能會導致一些問題。有時間再仔細排查看看。
|
||||
var lmUserPhrases = LMCoreEX(
|
||||
let lmUserPhrases = LMCoreEX(
|
||||
reverse: true, consolidate: true, defaultScore: 0, forceDefaultScore: false
|
||||
)
|
||||
var lmFiltered = LMCoreEX(
|
||||
let lmFiltered = LMCoreEX(
|
||||
reverse: true, consolidate: true, defaultScore: 0, forceDefaultScore: true
|
||||
)
|
||||
var lmUserSymbols = LMCoreEX(
|
||||
let lmUserSymbols = LMCoreEX(
|
||||
reverse: true, consolidate: true, defaultScore: -12.0, forceDefaultScore: true
|
||||
)
|
||||
var lmReplacements = LMReplacements()
|
||||
var lmAssociates = LMAssociates()
|
||||
var lmPlainBopomofo = LMPlainBopomofo()
|
||||
let lmReplacements = LMReplacements()
|
||||
let lmAssociates = LMAssociates()
|
||||
let lmPlainBopomofo = LMPlainBopomofo()
|
||||
|
||||
// MARK: - 工具函式
|
||||
|
||||
|
@ -139,7 +139,7 @@ extension vChewingLM {
|
|||
public func loadUserPhrasesData(path: String, filterPath: String) {
|
||||
DispatchQueue.main.async {
|
||||
if FileManager.default.isReadableFile(atPath: path) {
|
||||
self.lmUserPhrases.close()
|
||||
self.lmUserPhrases.clear()
|
||||
self.lmUserPhrases.open(path)
|
||||
vCLog("lmUserPhrases: \(self.lmUserPhrases.count) entries of data loaded from: \(path)")
|
||||
} else {
|
||||
|
@ -148,7 +148,7 @@ extension vChewingLM {
|
|||
}
|
||||
DispatchQueue.main.async {
|
||||
if FileManager.default.isReadableFile(atPath: filterPath) {
|
||||
self.lmFiltered.close()
|
||||
self.lmFiltered.clear()
|
||||
self.lmFiltered.open(filterPath)
|
||||
vCLog("lmFiltered: \(self.lmFiltered.count) entries of data loaded from: \(path)")
|
||||
} else {
|
||||
|
@ -160,7 +160,7 @@ extension vChewingLM {
|
|||
public func loadUserSymbolData(path: String) {
|
||||
DispatchQueue.main.async {
|
||||
if FileManager.default.isReadableFile(atPath: path) {
|
||||
self.lmUserSymbols.close()
|
||||
self.lmUserSymbols.clear()
|
||||
self.lmUserSymbols.open(path)
|
||||
vCLog("lmUserSymbol: \(self.lmUserSymbols.count) entries of data loaded from: \(path)")
|
||||
} else {
|
||||
|
@ -172,7 +172,7 @@ extension vChewingLM {
|
|||
public func loadUserAssociatesData(path: String) {
|
||||
DispatchQueue.main.async {
|
||||
if FileManager.default.isReadableFile(atPath: path) {
|
||||
self.lmAssociates.close()
|
||||
self.lmAssociates.clear()
|
||||
self.lmAssociates.open(path)
|
||||
vCLog("lmAssociates: \(self.lmAssociates.count) entries of data loaded from: \(path)")
|
||||
} else {
|
||||
|
@ -184,7 +184,7 @@ extension vChewingLM {
|
|||
public func loadReplacementsData(path: String) {
|
||||
DispatchQueue.main.async {
|
||||
if FileManager.default.isReadableFile(atPath: path) {
|
||||
self.lmReplacements.close()
|
||||
self.lmReplacements.clear()
|
||||
self.lmReplacements.open(path)
|
||||
vCLog("lmReplacements: \(self.lmReplacements.count) entries of data loaded from: \(path)")
|
||||
} else {
|
||||
|
@ -196,7 +196,7 @@ extension vChewingLM {
|
|||
public func loadUserSCPCSequencesData(path: String) {
|
||||
DispatchQueue.main.async {
|
||||
if FileManager.default.isReadableFile(atPath: path) {
|
||||
self.lmPlainBopomofo.close()
|
||||
self.lmPlainBopomofo.clear()
|
||||
self.lmPlainBopomofo.open(path)
|
||||
vCLog("lmPlainBopomofo: \(self.lmPlainBopomofo.count) entries of data loaded from: \(path)")
|
||||
} else {
|
||||
|
@ -208,7 +208,7 @@ extension vChewingLM {
|
|||
public static func loadCassetteData(path: String) {
|
||||
DispatchQueue.main.async {
|
||||
if FileManager.default.isReadableFile(atPath: path) {
|
||||
Self.lmCassette.close()
|
||||
Self.lmCassette.clear()
|
||||
Self.lmCassette.open(path)
|
||||
vCLog("lmCassette: \(Self.lmCassette.count) entries of data loaded from: \(path)")
|
||||
} else {
|
||||
|
|
|
@ -12,7 +12,7 @@ import PinyinPhonaConverter
|
|||
import Shared
|
||||
|
||||
extension vChewingLM {
|
||||
@frozen public struct LMAssociates {
|
||||
public class LMAssociates {
|
||||
var rangeMap: [String: [(Range<String.Index>, Int)]] = [:]
|
||||
var strData: String = ""
|
||||
|
||||
|
@ -35,7 +35,7 @@ extension vChewingLM {
|
|||
return "(\(arrTarget0),\(arrTarget[1]))"
|
||||
}
|
||||
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
@discardableResult public func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
|
||||
LMConsolidator.fixEOF(path: path)
|
||||
|
@ -65,7 +65,7 @@ extension vChewingLM {
|
|||
return true
|
||||
}
|
||||
|
||||
public mutating func close() {
|
||||
public func clear() {
|
||||
rangeMap.removeAll()
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ import Shared
|
|||
|
||||
extension vChewingLM {
|
||||
/// 磁帶模組,用來方便使用者自行擴充字根輸入法。
|
||||
@frozen public struct LMCassette {
|
||||
public class LMCassette {
|
||||
public private(set) var nameENG: String = ""
|
||||
public private(set) var nameCJK: String = ""
|
||||
/// 一個漢字可能最多要用到多少碼。
|
||||
|
@ -46,7 +46,7 @@ extension vChewingLM {
|
|||
/// - `%chardef begin` 至 `%chardef end` 之間則是詞庫資料。
|
||||
/// - Parameter path: 檔案路徑。
|
||||
/// - Returns: 是否載入成功。
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
@discardableResult public func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
if FileManager.default.fileExists(atPath: path) {
|
||||
do {
|
||||
|
@ -111,7 +111,7 @@ extension vChewingLM {
|
|||
return false
|
||||
}
|
||||
|
||||
public mutating func close() {
|
||||
public func clear() {
|
||||
keyNameMap.removeAll()
|
||||
charDefMap.removeAll()
|
||||
nameENG.removeAll()
|
||||
|
|
|
@ -16,7 +16,7 @@ extension vChewingLM {
|
|||
/// 需要資料的時候,直接拿 range 去 strData 取資料。
|
||||
/// 資料記錄原理與上游 C++ 的 ParselessLM 差不多,但用的是 Swift 原生手段。
|
||||
/// 主要時間消耗仍在 For 迴圈,但這個算法可以顯著減少記憶體佔用。
|
||||
@frozen public struct LMCoreEX {
|
||||
public class LMCoreEX {
|
||||
/// 資料庫辭典。索引內容為注音字串,資料內容則為字串首尾範圍、方便自 strData 取資料。
|
||||
var rangeMap: [String: [Range<String.Index>]] = [:]
|
||||
/// 資料庫字串陣列。
|
||||
|
@ -56,7 +56,7 @@ extension vChewingLM {
|
|||
/// 將資料從檔案讀入至資料庫辭典內。
|
||||
/// - parameters:
|
||||
/// - path: 給定路徑。
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
@discardableResult public func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
|
||||
if allowConsolidation {
|
||||
|
@ -88,7 +88,7 @@ extension vChewingLM {
|
|||
}
|
||||
|
||||
/// 將當前語言模組的資料庫辭典自記憶體內卸除。
|
||||
public mutating func close() {
|
||||
public func clear() {
|
||||
rangeMap.removeAll()
|
||||
}
|
||||
|
||||
|
|
|
@ -14,7 +14,7 @@ extension vChewingLM {
|
|||
/// 與之前的 LMCore 不同,LMCoreNS 直接讀取 plist。
|
||||
/// 這樣一來可以節省在舊 mac 機種內的資料讀入速度。
|
||||
/// 目前僅針對輸入法原廠語彙資料檔案使用 plist 格式。
|
||||
@frozen public struct LMCoreNS {
|
||||
public class LMCoreNS {
|
||||
/// 資料庫辭典。索引內容為經過加密的注音字串,資料內容則為 UTF8 資料陣列。
|
||||
var rangeMap: [String: [Data]] = [:]
|
||||
/// 【已作廢】資料庫字串陣列。在 LMCoreNS 內沒有作用。
|
||||
|
@ -57,7 +57,7 @@ extension vChewingLM {
|
|||
/// 將資料從檔案讀入至資料庫辭典內。
|
||||
/// - parameters:
|
||||
/// - path: 給定路徑。
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
@discardableResult public func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
|
||||
do {
|
||||
|
@ -74,7 +74,7 @@ extension vChewingLM {
|
|||
}
|
||||
|
||||
/// 將當前語言模組的資料庫辭典自記憶體內卸除。
|
||||
public mutating func close() {
|
||||
public func clear() {
|
||||
rangeMap.removeAll()
|
||||
}
|
||||
|
||||
|
|
|
@ -11,7 +11,7 @@ import Foundation
|
|||
import Shared
|
||||
|
||||
extension vChewingLM {
|
||||
@frozen public struct LMPlainBopomofo {
|
||||
public class LMPlainBopomofo {
|
||||
var rangeMap: [String: String] = [:]
|
||||
|
||||
public var count: Int { rangeMap.count }
|
||||
|
@ -22,7 +22,7 @@ extension vChewingLM {
|
|||
|
||||
public var isLoaded: Bool { !rangeMap.isEmpty }
|
||||
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
@discardableResult public func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
|
||||
do {
|
||||
|
@ -39,7 +39,7 @@ extension vChewingLM {
|
|||
return true
|
||||
}
|
||||
|
||||
public mutating func close() {
|
||||
public func clear() {
|
||||
rangeMap.removeAll()
|
||||
}
|
||||
|
||||
|
|
|
@ -10,7 +10,7 @@
|
|||
import Shared
|
||||
|
||||
extension vChewingLM {
|
||||
@frozen public struct LMReplacements {
|
||||
public class LMReplacements {
|
||||
var rangeMap: [String: Range<String.Index>] = [:]
|
||||
var strData: String = ""
|
||||
|
||||
|
@ -22,7 +22,7 @@ extension vChewingLM {
|
|||
|
||||
public var isLoaded: Bool { !rangeMap.isEmpty }
|
||||
|
||||
@discardableResult public mutating func open(_ path: String) -> Bool {
|
||||
@discardableResult public func open(_ path: String) -> Bool {
|
||||
if isLoaded { return false }
|
||||
|
||||
LMConsolidator.fixEOF(path: path)
|
||||
|
@ -50,7 +50,7 @@ extension vChewingLM {
|
|||
return true
|
||||
}
|
||||
|
||||
public mutating func close() {
|
||||
public func clear() {
|
||||
rangeMap.removeAll()
|
||||
}
|
||||
|
||||
|
|
|
@ -21,7 +21,7 @@ private let testDataPath: String = packageRootPath + "/Tests/TestCINData/"
|
|||
final class LMCassetteTests: XCTestCase {
|
||||
func testCassetteLoadWubi98() throws {
|
||||
let pathCINFile = testDataPath + "wubi98.cin"
|
||||
var lmCassette = vChewingLM.LMCassette()
|
||||
let lmCassette98 = vChewingLM.LMCassette()
|
||||
NSLog("LMCassette: Start loading CIN.")
|
||||
lmCassette98.open(pathCINFile)
|
||||
NSLog("LMCassette: Finished loading CIN. Entries: \(lmCassette98.count)")
|
||||
|
@ -36,7 +36,7 @@ final class LMCassetteTests: XCTestCase {
|
|||
|
||||
func testCassetteLoadWubi86() throws {
|
||||
let pathCINFile = testDataPath + "wubi86.cin"
|
||||
var lmCassette = vChewingLM.LMCassette()
|
||||
let lmCassette86 = vChewingLM.LMCassette()
|
||||
NSLog("LMCassette: Start loading CIN.")
|
||||
lmCassette86.open(pathCINFile)
|
||||
NSLog("LMCassette: Finished loading CIN. Entries: \(lmCassette86.count)")
|
||||
|
|
Loading…
Reference in New Issue