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 exceptedChars: Set<String> = .init()
private var ptrSQL: OpaquePointer? private var ptrSQL: OpaquePointer?
private var sharedJSONEncoder: JSONEncoder = .init()
var rangeMapJSONCHS: [String: [String]] = [:] var rangeMapJSONCHS: [String: [String]] = [:]
var rangeMapJSONCHT: [String: [String]] = [:] var rangeMapJSONCHT: [String: [String]] = [:]
@ -427,8 +426,10 @@ func rawDictForKanjis(isCHS: Bool) -> [Unigram] {
if !isReverseLookupDictionaryProcessed { if !isReverseLookupDictionaryProcessed {
do { do {
isReverseLookupDictionaryProcessed = true isReverseLookupDictionaryProcessed = true
try JSONSerialization.data(withJSONObject: mapReverseLookupJSON, options: .sortedKeys).write( if compileJSON {
to: URL(fileURLWithPath: urlJSONBPMFReverseLookup)) try JSONSerialization.data(withJSONObject: mapReverseLookupJSON, options: .sortedKeys).write(
to: URL(fileURLWithPath: urlJSONBPMFReverseLookup))
}
mapReverseLookupForCheck = mapReverseLookupUnencrypted mapReverseLookupForCheck = mapReverseLookupUnencrypted
} catch { } catch {
NSLog(" - Core Reverse Lookup Data Generation Failed.") NSLog(" - Core Reverse Lookup Data Generation Failed.")
@ -638,7 +639,9 @@ func fileOutput(isCHS: Bool) {
NSLog(" - \(i18n): 要寫入檔案的 txt 內容編譯完畢。") NSLog(" - \(i18n): 要寫入檔案的 txt 內容編譯完畢。")
do { do {
try strPrintLine.write(to: pathOutput, atomically: true, encoding: .utf8) try strPrintLine.write(to: pathOutput, atomically: true, encoding: .utf8)
try JSONSerialization.data(withJSONObject: rangeMapJSON, options: .sortedKeys).write(to: jsonURL) if compileJSON {
try JSONSerialization.data(withJSONObject: rangeMapJSON, options: .sortedKeys).write(to: jsonURL)
}
if isCHS { if isCHS {
rangeMapJSONCHS = rangeMapJSON rangeMapJSONCHS = rangeMapJSON
} else { } else {
@ -748,24 +751,26 @@ func commonFileOutput() {
} }
NSLog(" - \(i18n): 要寫入檔案的內容編譯完畢。") NSLog(" - \(i18n): 要寫入檔案的內容編譯完畢。")
do { do {
try JSONSerialization.data(withJSONObject: mapSymbols, options: .sortedKeys).write( if compileJSON {
to: URL(fileURLWithPath: urlJSONSymbols)) try JSONSerialization.data(withJSONObject: mapSymbols, options: .sortedKeys).write(
try JSONSerialization.data(withJSONObject: mapZhuyinwen, options: .sortedKeys).write( to: URL(fileURLWithPath: urlJSONSymbols))
to: URL(fileURLWithPath: urlJSONZhuyinwen)) try JSONSerialization.data(withJSONObject: mapZhuyinwen, options: .sortedKeys).write(
try JSONSerialization.data(withJSONObject: mapCNS, options: .sortedKeys).write( to: URL(fileURLWithPath: urlJSONZhuyinwen))
to: URL(fileURLWithPath: urlJSONCNS)) try JSONSerialization.data(withJSONObject: mapCNS, options: .sortedKeys).write(
try JSONSerialization.data(withJSONObject: mapReverseLookupCNS1, options: .sortedKeys).write( to: URL(fileURLWithPath: urlJSONCNS))
to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS1)) try JSONSerialization.data(withJSONObject: mapReverseLookupCNS1, options: .sortedKeys).write(
try JSONSerialization.data(withJSONObject: mapReverseLookupCNS2, options: .sortedKeys).write( to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS1))
to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS2)) try JSONSerialization.data(withJSONObject: mapReverseLookupCNS2, options: .sortedKeys).write(
try JSONSerialization.data(withJSONObject: mapReverseLookupCNS3, options: .sortedKeys).write( to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS2))
to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS3)) try JSONSerialization.data(withJSONObject: mapReverseLookupCNS3, options: .sortedKeys).write(
try JSONSerialization.data(withJSONObject: mapReverseLookupCNS4, options: .sortedKeys).write( to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS3))
to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS4)) try JSONSerialization.data(withJSONObject: mapReverseLookupCNS4, options: .sortedKeys).write(
try JSONSerialization.data(withJSONObject: mapReverseLookupCNS5, options: .sortedKeys).write( to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS4))
to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS5)) try JSONSerialization.data(withJSONObject: mapReverseLookupCNS5, options: .sortedKeys).write(
try JSONSerialization.data(withJSONObject: mapReverseLookupCNS6, options: .sortedKeys).write( to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS5))
to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS6)) try JSONSerialization.data(withJSONObject: mapReverseLookupCNS6, options: .sortedKeys).write(
to: URL(fileURLWithPath: urlJSONBPMFReverseLookupCNS6))
}
} catch { } catch {
NSLog(" - \(i18n): Error on writing strings to file: \(error)") NSLog(" - \(i18n): Error on writing strings to file: \(error)")
} }
@ -1068,10 +1073,27 @@ func healthCheck(_ data: [Unigram]) -> String {
// MARK: - // MARK: -
var compileJSON = false
var compileSQLite = true
func main() { func main() {
guard prepareDatabase() else { let arguments = CommandLine.arguments.compactMap { $0.lowercased() }
NSLog("// SQLite 資料庫初期化失敗。") let conditionMet = arguments.contains(where: { $0 == "--json" || $0 == "json" })
exit(-1) 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 globalQueue = DispatchQueue.global(qos: .default)
let group = DispatchGroup() let group = DispatchGroup()
@ -1095,30 +1117,35 @@ func main() {
} }
// //
_ = group.wait(timeout: .distantFuture) _ = group.wait(timeout: .distantFuture)
NSLog("// 全部 TXT & JSON 辭典檔案建置完畢。") NSLog("// 全部 TXT 辭典檔案建置完畢。")
NSLog("// 開始整合反查資料。") if compileJSON {
mapReverseLookupForCheck.forEach { key, values in NSLog("// 全部 JSON 辭典檔案建置完畢。")
values.reversed().forEach { valueLiteral in }
let value = cnvPhonabetToASCII(valueLiteral) if compileSQLite {
if !rangeMapReverseLookup[key, default: []].contains(value) { NSLog("// 開始整合反查資料。")
rangeMapReverseLookup[key, default: []].insert(value, at: 0) mapReverseLookupForCheck.forEach { key, values in
values.reversed().forEach { valueLiteral in
let value = cnvPhonabetToASCII(valueLiteral)
if !rangeMapReverseLookup[key, default: []].contains(value) {
rangeMapReverseLookup[key, default: []].insert(value, at: 0)
}
} }
} }
NSLog("// 反查資料整合完畢。")
NSLog("// 準備建置 SQL 資料庫。")
writeMainMapToSQL(rangeMapJSONCHS, column: "theDataCHS")
writeMainMapToSQL(rangeMapJSONCHT, column: "theDataCHT")
writeMainMapToSQL(rangeMapSymbols, column: "theDataSYMB")
writeMainMapToSQL(rangeMapZhuyinwen, column: "theDataCHEW")
writeMainMapToSQL(rangeMapCNS, column: "theDataCNS")
writeRevLookupMapToSQL(rangeMapReverseLookup)
let committed = "commit;".runAsSQLExec(dbPointer: &ptrSQL)
assert(committed)
let compressed = "VACUUM;".runAsSQLExec(dbPointer: &ptrSQL)
assert(compressed)
sqlite3_close_v2(ptrSQL)
NSLog("// 全部 SQLite 辭典檔案建置完畢。")
} }
NSLog("// 反查資料整合完畢。")
NSLog("// 準備建置 SQL 資料庫。")
writeMainMapToSQL(rangeMapJSONCHS, column: "theDataCHS")
writeMainMapToSQL(rangeMapJSONCHT, column: "theDataCHT")
writeMainMapToSQL(rangeMapSymbols, column: "theDataSYMB")
writeMainMapToSQL(rangeMapZhuyinwen, column: "theDataCHEW")
writeMainMapToSQL(rangeMapCNS, column: "theDataCNS")
writeRevLookupMapToSQL(rangeMapReverseLookup)
let committed = "commit;".runAsSQLExec(dbPointer: &ptrSQL)
assert(committed)
let compressed = "VACUUM;".runAsSQLExec(dbPointer: &ptrSQL)
assert(compressed)
sqlite3_close_v2(ptrSQL)
NSLog("// 全部 SQLite 辭典檔案建置完畢。")
} }
main() main()