From 67a6bb5f872d5ab68a1ca267fabb95cee14ec1d7 Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 2 Apr 2024 16:38:10 +0800 Subject: [PATCH] LMMgr && Main // Allow dumping user dicts through terminal. --- .../LangModelManager/LMMgr_Utilities.swift | 23 +++++++++++++++++++ Source/Modules/main.swift | 15 ++++++++++++ 2 files changed, 38 insertions(+) diff --git a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_Utilities.swift b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_Utilities.swift index c211e44b..5f74653d 100644 --- a/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_Utilities.swift +++ b/Packages/vChewing_MainAssembly/Sources/MainAssembly/LangModelManager/LMMgr_Utilities.swift @@ -345,3 +345,26 @@ public extension LMMgr { } } } + +// MARK: - 將當前輸入法的所有使用者辭典數據傾印成 JSON + +// 威注音輸入法並非可永續的專案。用單個 JSON 檔案遷移資料的話,可方便其他程式開發者們實作相關功能。 + +public extension LMMgr { + @discardableResult static func dumpUserDictDataToJSON(print: Bool = false, all: Bool) -> String? { + var summarizedDict = [LMAssembly.UserDictionarySummarized]() + Shared.InputMode.allCases.forEach { mode in + guard mode != .imeModeNULL else { return } + summarizedDict.append(mode.langModel.summarize(all: all)) + } + let encoder = JSONEncoder() + encoder.outputFormatting = .prettyPrinted + if #available(macOS 10.13, *) { + encoder.outputFormatting.insert(.sortedKeys) + } + guard let data = try? encoder.encode(summarizedDict) else { return nil } + guard let outputStr = String(data: data, encoding: .utf8) else { return nil } + if print { Swift.print(outputStr) } + return outputStr + } +} diff --git a/Source/Modules/main.swift b/Source/Modules/main.swift index a7a972d7..8902830a 100644 --- a/Source/Modules/main.swift +++ b/Source/Modules/main.swift @@ -9,6 +9,7 @@ import AppKit import IMKUtils import InputMethodKit +import LangModelAssembly import MainAssembly import Shared import Uninstaller @@ -24,6 +25,19 @@ case 1: print(strDumpedPrefs) } exit(0) + case "--dump-user-dict": + LMAssembly.LMInstantiator.asyncLoadingUserData = false + LMMgr.initUserLangModels() + LMMgr.loadUserPhraseReplacement() + LMMgr.dumpUserDictDataToJSON(print: true, all: false) + exit(0) + case "--dump-user-dict-all": + LMAssembly.LMInstantiator.asyncLoadingUserData = false + LMMgr.initUserLangModels() + LMMgr.loadUserPhraseReplacement() + LMMgr.loadUserAssociatesData() + LMMgr.dumpUserDictDataToJSON(print: true, all: true) + exit(0) case "install": let exitCode = IMKHelper.registerInputMethod() exit(exitCode) @@ -60,6 +74,7 @@ case 2: exit(0) default: break } + exit(0) default: exit(0) }