DataCompiler // Only compile JSON when `--json` argument is given.

This commit is contained in:
ShikiSuen 2023-12-02 13:21:26 +08:00
parent a4e301beef
commit 33bcbff3dc
1 changed files with 73 additions and 46 deletions

View File

@ -190,7 +190,6 @@ private var mapReverseLookupForCheck: [String: [String]] = [:]
private var exceptedChars: Set<String> = .init()
private var ptrSQL: OpaquePointer?
private var sharedJSONEncoder: JSONEncoder = .init()
var rangeMapJSONCHS: [String: [String]] = [:]
var rangeMapJSONCHT: [String: [String]] = [:]
@ -427,8 +426,10 @@ func rawDictForKanjis(isCHS: Bool) -> [Unigram] {
if !isReverseLookupDictionaryProcessed {
do {
isReverseLookupDictionaryProcessed = true
if compileJSON {
try JSONSerialization.data(withJSONObject: mapReverseLookupJSON, options: .sortedKeys).write(
to: URL(fileURLWithPath: urlJSONBPMFReverseLookup))
}
mapReverseLookupForCheck = mapReverseLookupUnencrypted
} catch {
NSLog(" - Core Reverse Lookup Data Generation Failed.")
@ -638,7 +639,9 @@ func fileOutput(isCHS: Bool) {
NSLog(" - \(i18n): 要寫入檔案的 txt 內容編譯完畢。")
do {
try strPrintLine.write(to: pathOutput, atomically: true, encoding: .utf8)
if compileJSON {
try JSONSerialization.data(withJSONObject: rangeMapJSON, options: .sortedKeys).write(to: jsonURL)
}
if isCHS {
rangeMapJSONCHS = rangeMapJSON
} else {
@ -748,6 +751,7 @@ func commonFileOutput() {
}
NSLog(" - \(i18n): 要寫入檔案的內容編譯完畢。")
do {
if compileJSON {
try JSONSerialization.data(withJSONObject: mapSymbols, options: .sortedKeys).write(
to: URL(fileURLWithPath: urlJSONSymbols))
try JSONSerialization.data(withJSONObject: mapZhuyinwen, options: .sortedKeys).write(
@ -766,6 +770,7 @@ func commonFileOutput() {
to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS5))
try JSONSerialization.data(withJSONObject: mapReverseLookupCNS6, options: .sortedKeys).write(
to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS6))
}
} catch {
NSLog(" - \(i18n): Error on writing strings to file: \(error)")
}
@ -1068,11 +1073,28 @@ func healthCheck(_ data: [Unigram]) -> String {
// MARK: -
var compileJSON = false
var compileSQLite = true
func main() {
let arguments = CommandLine.arguments.compactMap { $0.lowercased() }
let conditionMet = arguments.contains(where: { $0 == "--json" || $0 == "json" })
if conditionMet {
NSLog("// 接下來準備建置 JSON 格式的原廠辭典,同時生成用來偵錯的 TXT 副產物。")
compileJSON = true
compileSQLite = false
} else {
NSLog("// 接下來準備建置 SQLite 格式的原廠辭典,同時生成用來偵錯的 TXT 副產物。")
compileJSON = false
compileSQLite = true
}
if compileSQLite {
guard prepareDatabase() else {
NSLog("// SQLite 資料庫初期化失敗。")
exit(-1)
}
}
let globalQueue = DispatchQueue.global(qos: .default)
let group = DispatchGroup()
group.enter()
@ -1095,7 +1117,11 @@ func main() {
}
//
_ = group.wait(timeout: .distantFuture)
NSLog("// 全部 TXT & JSON 辭典檔案建置完畢。")
NSLog("// 全部 TXT 辭典檔案建置完畢。")
if compileJSON {
NSLog("// 全部 JSON 辭典檔案建置完畢。")
}
if compileSQLite {
NSLog("// 開始整合反查資料。")
mapReverseLookupForCheck.forEach { key, values in
values.reversed().forEach { valueLiteral in
@ -1120,5 +1146,6 @@ func main() {
sqlite3_close_v2(ptrSQL)
NSLog("// 全部 SQLite 辭典檔案建置完畢。")
}
}
main()