Skip to content

Commit 65357a5

Browse files
committed
Improve Structure Rrverse Lookup
1 parent b7470bf commit 65357a5

File tree

4 files changed

+22
-25
lines changed

4 files changed

+22
-25
lines changed

CoreIME/Sources/CoreIME/Candidate.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -191,8 +191,8 @@ extension Array where Element == Candidate {
191191
/// Returns a new Candidate by concatenating this Candidate sequence.
192192
/// - Returns: Single, concatenated Candidate.
193193
public func joined() -> Candidate? {
194-
let isNotAllCantonese: Bool = self.contains(where: \.isNotCantonese)
195-
guard !isNotAllCantonese else { return nil }
194+
let isAllCantonese: Bool = !(contains(where: \.isNotCantonese))
195+
guard isAllCantonese else { return nil }
196196
let text: String = map(\.text).joined()
197197
let lexiconText: String = map(\.lexiconText).joined()
198198
let romanization: String = map(\.romanization).joined(separator: " ")

CoreIME/Sources/CoreIME/Structure.swift

+14-17
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension Engine {
1212
public static func structureReverseLookup(text: String, input: String, segmentation: Segmentation) -> [Candidate] {
1313
let markFreeText = text.removedSpacesTonesSeparators()
1414
let matched = process(text: markFreeText, segmentation: segmentation).uniqued()
15-
guard !matched.isEmpty else { return [] }
15+
guard matched.isNotEmpty else { return [] }
1616
switch (text.hasSeparators, text.hasTones) {
1717
case (true, true):
1818
let isOneToneOnly: Bool = (text.count - markFreeText.count) == 2
@@ -44,31 +44,28 @@ extension Engine {
4444
}
4545

4646
private static func process(text: String, segmentation: Segmentation) -> [Candidate] {
47+
let matched = match(text: text)
4748
let textCount = text.count
48-
let segmentation = segmentation.filter({ $0.length == textCount })
49-
guard segmentation.maxLength > 0 else {
50-
return match(text: text)
51-
}
52-
let matches = segmentation.map({ scheme -> [Candidate] in
49+
let schemes = segmentation.filter({ $0.length == textCount })
50+
guard schemes.maxLength > 0 else { return matched }
51+
let matches = schemes.map({ scheme -> [Candidate] in
5352
let pingText = scheme.map(\.origin).joined()
5453
return match(text: pingText)
5554
})
56-
return match(text: text) + matches.flatMap({ $0 })
55+
return matched + matches.flatMap({ $0 })
5756
}
58-
5957
private static func match(text: String) -> [Candidate] {
6058
var candidates: [Candidate] = []
61-
let query: String = "SELECT word, romanization FROM composetable WHERE ping = \(text.hash);"
59+
let command: String = "SELECT word, romanization FROM structuretable WHERE ping = \(text.hash);"
6260
var statement: OpaquePointer? = nil
63-
if sqlite3_prepare_v2(Engine.database, query, -1, &statement, nil) == SQLITE_OK {
64-
while sqlite3_step(statement) == SQLITE_ROW {
65-
let word: String = String(cString: sqlite3_column_text(statement, 0))
66-
let romanization: String = String(cString: sqlite3_column_text(statement, 1))
67-
let instance = Candidate(text: word, romanization: romanization, input: text)
68-
candidates.append(instance)
69-
}
61+
defer { sqlite3_finalize(statement) }
62+
guard sqlite3_prepare_v2(Engine.database, command, -1, &statement, nil) == SQLITE_OK else { return [] }
63+
while sqlite3_step(statement) == SQLITE_ROW {
64+
let word: String = String(cString: sqlite3_column_text(statement, 0))
65+
let romanization: String = String(cString: sqlite3_column_text(statement, 1))
66+
let instance = Candidate(text: word, romanization: romanization, input: text)
67+
candidates.append(instance)
7068
}
71-
sqlite3_finalize(statement)
7269
return candidates
7370
}
7471
}

Preparing/Sources/Preparing/DatabasePreparer.swift

+6-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ struct DatabasePreparer {
1313
createQuickTable()
1414
createStrokeTable()
1515
createT2STable()
16-
createComposeTable()
16+
createStructureTable()
1717
createPinyinTable()
1818
createSymbolTable()
1919
createOtherIndies()
@@ -94,7 +94,7 @@ struct DatabasePreparer {
9494
"CREATE INDEX strokestrokeindex ON stroketable(stroke);",
9595
"CREATE INDEX strokecodeindex ON stroketable(code);",
9696

97-
"CREATE INDEX composepingindex ON composetable(ping);",
97+
"CREATE INDEX structurepingindex ON structuretable(ping);",
9898

9999
"CREATE INDEX pinyinshortcutindex ON pinyintable(shortcut);",
100100
"CREATE INDEX pinyinpingindex ON pinyintable(ping);",
@@ -232,13 +232,13 @@ struct DatabasePreparer {
232232
guard sqlite3_prepare_v2(database, insert, -1, &insertStatement, nil) == SQLITE_OK else { return }
233233
guard sqlite3_step(insertStatement) == SQLITE_DONE else { return }
234234
}
235-
private static func createComposeTable() {
236-
let createTable: String = "CREATE TABLE composetable(word TEXT NOT NULL, romanization TEXT NOT NULL, ping INTEGER NOT NULL);"
235+
private static func createStructureTable() {
236+
let createTable: String = "CREATE TABLE structuretable(word TEXT NOT NULL, romanization TEXT NOT NULL, ping INTEGER NOT NULL);"
237237
var createStatement: OpaquePointer? = nil
238238
guard sqlite3_prepare_v2(database, createTable, -1, &createStatement, nil) == SQLITE_OK else { sqlite3_finalize(createStatement); return }
239239
guard sqlite3_step(createStatement) == SQLITE_DONE else { sqlite3_finalize(createStatement); return }
240240
sqlite3_finalize(createStatement)
241-
guard let url = Bundle.module.url(forResource: "compose", withExtension: "txt") else { return }
241+
guard let url = Bundle.module.url(forResource: "structure", withExtension: "txt") else { return }
242242
guard let content = try? String(contentsOf: url) else { return }
243243
let sourceLines: [String] = content.trimmingCharacters(in: .whitespacesAndNewlines).components(separatedBy: .newlines)
244244
let entries = sourceLines.map { sourceLine -> String? in
@@ -250,7 +250,7 @@ struct DatabasePreparer {
250250
return "('\(word)', '\(romanization)', \(ping))"
251251
}
252252
let values: String = entries.compactMap({ $0 }).joined(separator: ", ")
253-
let insert: String = "INSERT INTO composetable (word, romanization, ping) VALUES \(values);"
253+
let insert: String = "INSERT INTO structuretable (word, romanization, ping) VALUES \(values);"
254254
var insertStatement: OpaquePointer? = nil
255255
defer { sqlite3_finalize(insertStatement) }
256256
guard sqlite3_prepare_v2(database, insert, -1, &insertStatement, nil) == SQLITE_OK else { return }

0 commit comments

Comments
 (0)