diff --git a/Source/Modules/LangModelRelated/SubLMs/lmUserOverride.swift b/Source/Modules/LangModelRelated/SubLMs/lmUserOverride.swift index c70638f2..01921a10 100644 --- a/Source/Modules/LangModelRelated/SubLMs/lmUserOverride.swift +++ b/Source/Modules/LangModelRelated/SubLMs/lmUserOverride.swift @@ -56,7 +56,7 @@ extension vChewing { mutLRUList.insert(koPair, at: 0) if mutLRUList.count > mutCapacity { - mutLRUMap[mutLRUList[mutLRUList.endIndex].key] = nil + mutLRUMap.removeValue(forKey: mutLRUList[mutLRUList.endIndex].key) mutLRUList.removeLast() } IME.prtDebugIntel("UOM: Observation finished with new observation: \(key)") @@ -256,3 +256,38 @@ extension vChewing.LMUserOverride { } } } + +// MARK: - Hash and Dehash the entire UOM data + +extension vChewing.LMUserOverride { + public func saveData(toURL fileURL: URL) { + let encoder = JSONEncoder() + do { + if let jsonData = try? encoder.encode(mutLRUMap) { + try jsonData.write(to: fileURL, options: .atomic) + } + } catch { + IME.prtDebugIntel("UOM Error: Unable to save data, abort saving. Details: \(error)") + return + } + } + + public func loadData(fromURL fileURL: URL) { + let decoder = JSONDecoder() + do { + let data = try Data(contentsOf: fileURL, options: .mappedIfSafe) + guard let jsonResult = try? decoder.decode(Dictionary.self, from: data) else { + IME.prtDebugIntel("UOM Error: Read file content type invalid, abort loading.") + return + } + mutLRUMap = jsonResult + mutLRUList.removeAll() + for neta in mutLRUMap.reversed() { + mutLRUList.append(neta.value) + } + } catch { + IME.prtDebugIntel("UOM Error: Unable to read file or parse the data, abort loading. Details: \(error)") + return + } + } +}