LMCassette // Supply generated %quick results when needed.

* Partial results are only supplied if  the line `%flag_disp_partial_match` presents in the cassette.
This commit is contained in:
ShikiSuen 2023-08-31 10:42:24 +08:00
parent e19aa9e83a
commit 480b362bf4
1 changed files with 20 additions and 12 deletions

View File

@ -38,6 +38,7 @@ public extension vChewingLM {
public private(set) var octagramDividedMap: [String: (Int, String)] = [:]
public private(set) var areCandidateKeysShiftHeld: Bool = false
public private(set) var supplyQuickResults: Bool = false
public private(set) var supplyPartiallyMatchedResults: Bool = false
/// 西
private static let fscale = 2.7
@ -96,9 +97,14 @@ public extension vChewingLM {
if !loadingKeys, strLine.contains("begin") { loadingKeys = true }
if loadingKeys, strLine.contains("end") { loadingKeys = false }
}
// %flag_disp_partial_match
if strLine == "%flag_disp_partial_match" {
supplyPartiallyMatchedResults = true
supplyQuickResults = true
}
// %quick
if strLine.starts(with: "%quick") {
if strLine == "%quick", !supplyQuickResults { supplyQuickResults = true }
supplyQuickResults = true
if !loadingQuickSets, strLine.contains("begin") {
loadingQuickSets = true
}
@ -202,9 +208,6 @@ public extension vChewingLM {
if wildcardKey.isEmpty, strLine.starts(with: "%wildcardkey ") {
wildcardKey = cells[1].first?.description ?? ""
}
if strLine == "%flag_disp_partial_match", !supplyQuickResults {
supplyQuickResults = true
}
}
// Post process.
if CandidateKey.validate(keys: selectionKeys) != nil { selectionKeys = "1234567890" }
@ -252,7 +255,8 @@ public extension vChewingLM {
if let specifiedResult = quickDefMap[key], !specifiedResult.isEmpty {
result.append(contentsOf: specifiedResult.map(\.description))
}
if supplyQuickResults, quickDefMap.isEmpty {
if supplyQuickResults, result.isEmpty {
if supplyPartiallyMatchedResults {
let fetched = charDefMap.compactMap {
$0.key.starts(with: key) ? $0 : nil
}.stableSort {
@ -261,6 +265,10 @@ public extension vChewingLM {
$0.count == 1
}
result.append(contentsOf: fetched.deduplicated.prefix(selectionKeys.count * 6))
} else {
let fetched = (charDefMap[key] ?? [String]()).filter { $0.count == 1 }
result.append(contentsOf: fetched.deduplicated.prefix(selectionKeys.count * 6))
}
}
return result.isEmpty ? nil : result.joined(separator: "\t")
}