LMCassette // Add areCandidateKeysShiftPressed(), etc.

This commit is contained in:
ShikiSuen 2023-06-20 15:54:39 +08:00
parent 7d899b4af1
commit fa0e98ca4a
4 changed files with 914 additions and 939 deletions

View File

@ -17,6 +17,8 @@ public extension vChewingLM.LMInstantiator {
var maxCassetteKeyLength: Int { Self.lmCassette.maxKeyLength } var maxCassetteKeyLength: Int { Self.lmCassette.maxKeyLength }
/// `%quick` /// `%quick`
var nullCandidateInCassette: String { Self.lmCassette.nullCandidate } var nullCandidateInCassette: String { Self.lmCassette.nullCandidate }
/// Shift
var areCassetteCandidateKeysShiftPressed: Bool { Self.lmCassette.areCandidateKeysShiftPressed }
/// ///
var cassetteSelectionKey: String? { var cassetteSelectionKey: String? {
let result = Self.lmCassette.selectionKeys let result = Self.lmCassette.selectionKeys

View File

@ -36,6 +36,7 @@ public extension vChewingLM {
public private(set) var octagramMap: [String: Int] = [:] public private(set) var octagramMap: [String: Int] = [:]
/// [:(, )] /// [:(, )]
public private(set) var octagramDividedMap: [String: (Int, String)] = [:] public private(set) var octagramDividedMap: [String: (Int, String)] = [:]
public private(set) var areCandidateKeysShiftPressed: Bool = false
/// 西 /// 西
private static let fscale = 2.7 private static let fscale = 2.7
@ -88,6 +89,7 @@ public extension vChewingLM {
var loadingCharDefinitions = false var loadingCharDefinitions = false
var loadingSymbolDefinitions = false var loadingSymbolDefinitions = false
var loadingOctagramData = false var loadingOctagramData = false
var keysUsedInCharDef: Set<String> = .init()
for strLine in lineReader { for strLine in lineReader {
if !loadingKeys, strLine.contains("%keyname"), strLine.contains("begin") { loadingKeys = true } if !loadingKeys, strLine.contains("%keyname"), strLine.contains("begin") { loadingKeys = true }
if loadingKeys, strLine.contains("%keyname"), strLine.contains("end") { loadingKeys = false } if loadingKeys, strLine.contains("%keyname"), strLine.contains("end") { loadingKeys = false }
@ -138,6 +140,11 @@ public extension vChewingLM {
{ {
theMaxKeyLength = max(theMaxKeyLength, cells[0].count) theMaxKeyLength = max(theMaxKeyLength, cells[0].count)
charDefMap[strFirstCell, default: []].append(strSecondCell) charDefMap[strFirstCell, default: []].append(strSecondCell)
if strFirstCell.count > 1 {
strFirstCell.map(\.description).forEach { keyChar in
keysUsedInCharDef.insert(keyChar.description)
}
}
reverseLookupMap[strSecondCell, default: []].append(strFirstCell) reverseLookupMap[strSecondCell, default: []].append(strFirstCell)
var keyComps = strFirstCell.map(\.description) var keyComps = strFirstCell.map(\.description)
while !keyComps.isEmpty { while !keyComps.isEmpty {
@ -175,7 +182,7 @@ public extension vChewingLM {
if nameShort.isEmpty, strLine.contains("%sname ") { nameShort = strSecondCell } if nameShort.isEmpty, strLine.contains("%sname ") { nameShort = strSecondCell }
if nullCandidate.isEmpty, strLine.contains("%nullcandidate ") { nullCandidate = strSecondCell } if nullCandidate.isEmpty, strLine.contains("%nullcandidate ") { nullCandidate = strSecondCell }
if selectionKeys.isEmpty, strLine.contains("%selkey ") { if selectionKeys.isEmpty, strLine.contains("%selkey ") {
selectionKeys = cells[1].description selectionKeys = cells[1].map(\.description).deduplicated.joined()
} }
if endKeys.isEmpty, strLine.contains("%endkey ") { if endKeys.isEmpty, strLine.contains("%endkey ") {
endKeys = cells[1].map(\.description).deduplicated endKeys = cells[1].map(\.description).deduplicated
@ -184,6 +191,11 @@ public extension vChewingLM {
wildcardKey = cells[1].first?.description ?? "" wildcardKey = cells[1].first?.description ?? ""
} }
} }
// Post process.
if CandidateKey.validate(keys: selectionKeys) != nil { selectionKeys = "1234567890" }
if !keysUsedInCharDef.intersection(selectionKeys.map(\.description)).isEmpty {
areCandidateKeysShiftPressed = true
}
maxKeyLength = theMaxKeyLength maxKeyLength = theMaxKeyLength
keyNameMap[wildcardKey] = keyNameMap[wildcardKey] ?? "" keyNameMap[wildcardKey] = keyNameMap[wildcardKey] ?? ""
filePath = path filePath = path

View File

@ -41,7 +41,7 @@ final class LMCassetteTests: XCTestCase {
} }
func testCassetteLoadArray30() throws { func testCassetteLoadArray30() throws {
let pathCINFile = testDataPath + "array30.cin" let pathCINFile = testDataPath + "array30.cin2"
var lmCassette = vChewingLM.LMCassette() var lmCassette = vChewingLM.LMCassette()
NSLog("LMCassette: Start loading CIN.") NSLog("LMCassette: Start loading CIN.")
lmCassette.open(pathCINFile) lmCassette.open(pathCINFile)
@ -49,8 +49,8 @@ final class LMCassetteTests: XCTestCase {
XCTAssertFalse(lmCassette.quickDefMap.isEmpty) XCTAssertFalse(lmCassette.quickDefMap.isEmpty)
print(lmCassette.quickSetsFor(key: ",.") ?? "") print(lmCassette.quickSetsFor(key: ",.") ?? "")
XCTAssertEqual(lmCassette.keyNameMap.count, 41) XCTAssertEqual(lmCassette.keyNameMap.count, 41)
XCTAssertEqual(lmCassette.charDefMap.count, 29537) XCTAssertEqual(lmCassette.charDefMap.count, 29491)
XCTAssertEqual(lmCassette.charDefWildcardMap.count, 11973) XCTAssertEqual(lmCassette.charDefWildcardMap.count, 11946)
XCTAssertEqual(lmCassette.octagramMap.count, 0) XCTAssertEqual(lmCassette.octagramMap.count, 0)
XCTAssertEqual(lmCassette.octagramDividedMap.count, 0) XCTAssertEqual(lmCassette.octagramDividedMap.count, 0)
XCTAssertEqual(lmCassette.nameShort, "AR30") XCTAssertEqual(lmCassette.nameShort, "AR30")