|
| 1 | +// Copyright (c) 2026 and onwards The McBopomofo Authors. |
| 2 | +// |
| 3 | +// Permission is hereby granted, free of charge, to any person |
| 4 | +// obtaining a copy of this software and associated documentation |
| 5 | +// files (the "Software"), to deal in the Software without |
| 6 | +// restriction, including without limitation the rights to use, |
| 7 | +// copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 8 | +// copies of the Software, and to permit persons to whom the |
| 9 | +// Software is furnished to do so, subject to the following |
| 10 | +// conditions: |
| 11 | +// |
| 12 | +// The above copyright notice and this permission notice shall be |
| 13 | +// included in all copies or substantial portions of the Software. |
| 14 | +// |
| 15 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 16 | +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 17 | +// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 18 | +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 19 | +// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, |
| 20 | +// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 21 | +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR |
| 22 | +// OTHER DEALINGS IN THE SOFTWARE. |
| 23 | + |
| 24 | + |
| 25 | +import SQLite |
| 26 | +import Foundation |
| 27 | + |
| 28 | +private let path = "/System/Library/Input Methods/CharacterPalette.app/Contents/Resources/CharacterDB.sqlite3" |
| 29 | + |
| 30 | +public struct UnihanEntry { |
| 31 | + public var name: String |
| 32 | + public var japanese: String |
| 33 | + /// 日文訓讀 |
| 34 | + public var japaneseKun: String |
| 35 | + /// 日文音讀 |
| 36 | + public var japaneseOn: String |
| 37 | + public var korean: String |
| 38 | + public var pinyinPrc: String |
| 39 | + public var pinyinRoc: String |
| 40 | + public var pinyinPrimary: String |
| 41 | + public var canjie: String |
| 42 | + public var canjieKeys: String |
| 43 | + public var components: String |
| 44 | + public var phonetic: String |
| 45 | + /// 五筆形 |
| 46 | + public var wubiXing: String |
| 47 | + /// 五筆劃 |
| 48 | + public var wubiHua: String |
| 49 | +} |
| 50 | + |
| 51 | +public class UnihanDictionary { |
| 52 | + public static let shared: UnihanDictionary? = try? UnihanDictionary() |
| 53 | + |
| 54 | + class UnihanEntryParser { |
| 55 | + static func parse(from string: String) -> UnihanEntry { |
| 56 | + let components = string.split(separator: "|", omittingEmptySubsequences: false) |
| 57 | + func get(_ index: Int) -> String { |
| 58 | + return index < components.count ? String(components[index]) : "" |
| 59 | + } |
| 60 | + return UnihanEntry( |
| 61 | + name: get(0), |
| 62 | + japanese: get(1), |
| 63 | + japaneseKun: get(12), |
| 64 | + japaneseOn: get(13), |
| 65 | + korean: get(2), |
| 66 | + pinyinPrc: get(3), |
| 67 | + pinyinRoc: get(10), |
| 68 | + pinyinPrimary: get(14), |
| 69 | + canjie: get(8), |
| 70 | + canjieKeys: get(15), |
| 71 | + components: get(7), |
| 72 | + phonetic: get(11), |
| 73 | + wubiXing: get(4), |
| 74 | + wubiHua: get(5) |
| 75 | + ) |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + private var db: Connection |
| 80 | + |
| 81 | + public init() throws { |
| 82 | + self.db = try Connection(path) |
| 83 | + } |
| 84 | + |
| 85 | + public func read(string: String) throws -> UnihanEntry { |
| 86 | + let table = Table("unihan_dict") |
| 87 | + let uchr = Expression<String>("uchr") |
| 88 | + let query = table.filter(uchr == string).limit(1) |
| 89 | + let result = try db.prepare(query) |
| 90 | + let rows = Array(result) |
| 91 | + guard let row = rows.first else { |
| 92 | + throw SystemCharacterInfoError.notFound |
| 93 | + } |
| 94 | + let info = SQLite.Expression<String?>("info") |
| 95 | + let infoString = try? row.get(info) |
| 96 | + guard let infoString else { |
| 97 | + throw SystemCharacterInfoError.notFound |
| 98 | + } |
| 99 | + let entry = UnihanEntryParser.parse(from: infoString) |
| 100 | + return entry |
| 101 | + } |
| 102 | +} |
0 commit comments