LMInstantiator // Stop duplicating LMCassette.

This commit is contained in:
ShikiSuen 2022-11-27 00:15:02 +08:00
parent 383a3dd658
commit 15188f0eed
7 changed files with 64 additions and 24 deletions

View File

@ -74,10 +74,6 @@ extension vChewingLM {
// currentCassetteMetadata
static var lmCassette = LMCassette()
public var currentCassette: LMCassette {
get { Self.lmCassette }
set { Self.lmCassette = newValue }
}
// 使
// 使使
@ -208,6 +204,7 @@ extension vChewingLM {
}
}
public var isCassetteDataLoaded: Bool { Self.lmCassette.isLoaded }
public static func loadCassetteData(path: String) {
DispatchQueue.main.async {
if FileManager.default.isReadableFile(atPath: path) {

View File

@ -0,0 +1,52 @@
// (c) 2021 and onwards The vChewing Project (MIT-NTL License).
// ====================
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
// ... with NTL restriction stating that:
// No trademark license is granted to use the trade names, trademarks, service
// marks, or product names of Contributor, except as required to fulfill notice
// requirements defined in MIT License.
import Foundation
import Megrez
import Shared
extension vChewingLM.LMInstantiator {
///
public var cassetteWildcardKey: String { Self.lmCassette.wildcardKey }
///
public var maxCassetteKeyLength: Int { Self.lmCassette.maxKeyLength }
///
/// - Parameter char:
/// - Returns:
public func convertCassetteKeyToDisplay(char: String) -> String {
Self.lmCassette.convertKeyToDisplay(char: char)
}
///
/// - Parameter key:
/// - Returns:
public func isThisCassetteKeyAllowed(key: String) -> Bool {
Self.lmCassette.allowedKeys.contains(key)
}
///
/// - Parameter key:
/// - Returns:
public func hasCassetteWildcardResultsFor(key: String) -> Bool {
Self.lmCassette.hasUnigramsFor(key: key + Self.lmCassette.wildcard)
}
///
/// - Parameter value:
/// - Returns:
public func cassetteReverseLookup(for value: String) -> [String] {
var lookupResult = Self.lmCassette.reverseLookupMap[value] ?? []
guard !lookupResult.isEmpty else { return [] }
lookupResult = lookupResult.map { $0.trimmingCharacters(in: .newlines) }
return lookupResult.stableSort(by: { $0.count < $1.count }).stableSort {
Self.lmCassette.unigramsFor(key: $0).count
< Self.lmCassette.unigramsFor(key: $1).count
}
}
}

View File

@ -428,11 +428,9 @@ public class InputHandler: InputHandlerProtocol {
return composer.getInlineCompositionForDisplay(isHanyuPinyin: prefs.showHanyuPinyinInCompositionBuffer)
}
if !prefs.showTranslatedStrokesInCompositionBuffer { return calligrapher }
var result = calligrapher.charComponents
for idx in 0..<result.count {
result[idx] = currentLM.currentCassette.convertKeyToDisplay(char: result[idx])
}
return result.joined()
return calligrapher.charComponents.map {
currentLM.convertCassetteKeyToDisplay(char: $0)
}.joined()
}
// MARK: - Extracted methods and functions (Megrez).

View File

@ -170,7 +170,7 @@ extension InputHandler {
let isInputValid: Bool =
prefs.cassetteEnabled
? currentLM.currentCassette.allowedKeys.contains(input.text) : composer.inputValidityCheck(key: input.charCode)
? currentLM.isThisCassetteKeyAllowed(key: input.text) : composer.inputValidityCheck(key: input.charCode)
var shouldAutoSelectCandidate: Bool =
isInputValid || currentLM.hasUnigramsFor(key: customPunctuation)

View File

@ -181,8 +181,7 @@ extension InputHandler {
/// - Returns: IMK
private func handleCassetteComposition(input: InputSignalProtocol) -> Bool? {
guard let delegate = delegate else { return nil }
var wildcardKey: String { currentLM.currentCassette.wildcardKey } //
var wildcard: String { currentLM.currentCassette.wildcard } //
var wildcardKey: String { currentLM.cassetteWildcardKey } //
let isWildcardKeyInput: Bool = (input.text == wildcardKey && !wildcardKey.isEmpty)
var keyConsumedByStrokes = false
@ -193,14 +192,14 @@ extension InputHandler {
var isLongestPossibleKeyFormed: Bool {
guard !isWildcardKeyInput, prefs.autoCompositeWithLongestPossibleCassetteKey else { return false }
return !currentLM.currentCassette.hasUnigramsFor(key: calligrapher + wildcard) && !calligrapher.isEmpty
return !currentLM.hasCassetteWildcardResultsFor(key: calligrapher) && !calligrapher.isEmpty
}
var isStrokesFull: Bool {
calligrapher.count >= currentLM.currentCassette.maxKeyLength || isLongestPossibleKeyFormed
calligrapher.count >= currentLM.maxCassetteKeyLength || isLongestPossibleKeyFormed
}
prehandling: if !skipStrokeHandling && currentLM.currentCassette.allowedKeys.contains(input.text) {
prehandling: if !skipStrokeHandling && currentLM.isThisCassetteKeyAllowed(key: input.text) {
if calligrapher.isEmpty, isWildcardKeyInput {
delegate.callError("3606B9C0")
var newEmptyState = compositor.isEmpty ? IMEState.ofEmpty() : generateStateOfInputting()

View File

@ -69,14 +69,8 @@ extension SessionCtl: CtlCandidateDelegate {
if isVerticalTyping { return blankResult } //
if value.isEmpty { return blankResult } // 西
if value.contains("_") { return blankResult }
guard var lookupResult = LMMgr.currentLM.currentCassette.reverseLookupMap[value] else { return blankResult }
for i in 0..<lookupResult.count {
lookupResult[i] = lookupResult[i].trimmingCharacters(in: .newlines)
}
return lookupResult.stableSort(by: { $0.count < $1.count }).stableSort {
LMMgr.currentLM.currentCassette.unigramsFor(key: $0).count
< LMMgr.currentLM.currentCassette.unigramsFor(key: $1).count
}
// LMInstantiator
return LMMgr.currentLM.cassetteReverseLookup(for: value)
}
public var selectionKeys: String {

View File

@ -247,7 +247,7 @@ extension SessionCtl {
? NSLocalizedString("NotificationSwitchON", comment: "")
: NSLocalizedString("NotificationSwitchOFF", comment: ""))
)
if !LMMgr.currentLM.currentCassette.isLoaded {
if !LMMgr.currentLM.isCassetteDataLoaded {
LMMgr.loadCassetteData()
}
}