Repo // Sync changes from Megrez v2.6.0 update.

This commit is contained in:
ShikiSuen 2023-03-09 22:21:05 +08:00
parent 490a646f88
commit 8535fc1711
5 changed files with 24 additions and 26 deletions

View File

@ -225,11 +225,11 @@ public extension vChewingLM {
// MARK: -
public func hasAssociatedPhrasesFor(pair: Megrez.Compositor.KeyValuePaired) -> Bool {
public func hasAssociatedPhrasesFor(pair: Megrez.KeyValuePaired) -> Bool {
lmAssociates.hasValuesFor(pair: pair)
}
public func associatedPhrasesFor(pair: Megrez.Compositor.KeyValuePaired) -> [String] {
public func associatedPhrasesFor(pair: Megrez.KeyValuePaired) -> [String] {
lmAssociates.valuesFor(pair: pair)
}

View File

@ -97,7 +97,7 @@ public extension vChewingLM {
}
}
public func valuesFor(pair: Megrez.Compositor.KeyValuePaired) -> [String] {
public func valuesFor(pair: Megrez.KeyValuePaired) -> [String] {
var pairs: [String] = []
if let arrRangeRecords: [(Range<String.Index>, Int)] = rangeMap[pair.toNGramKey] {
for (netaRange, index) in arrRangeRecords {
@ -117,7 +117,7 @@ public extension vChewingLM {
return pairs.filter { set.insert($0).inserted }
}
public func hasValuesFor(pair: Megrez.Compositor.KeyValuePaired) -> Bool {
public func hasValuesFor(pair: Megrez.KeyValuePaired) -> Bool {
if rangeMap[pair.toNGramKey] != nil { return true }
return rangeMap[pair.value] != nil
}

View File

@ -31,7 +31,7 @@ public extension vChewingLM {
}
public func performObservation(
walkedBefore: [Megrez.Compositor.Node], walkedAfter: [Megrez.Compositor.Node],
walkedBefore: [Megrez.Node], walkedAfter: [Megrez.Node],
cursor: Int, timestamp: Double, saveCallback: @escaping () -> Void
) {
//
@ -64,7 +64,7 @@ public extension vChewingLM {
}
public func fetchSuggestion(
currentWalk: [Megrez.Compositor.Node], cursor: Int, timestamp: Double
currentWalk: [Megrez.Node], cursor: Int, timestamp: Double
) -> Suggestion {
var headIndex = 0
guard let nodeIter = currentWalk.findNode(at: cursor, target: &headIndex) else { return .init() }
@ -307,7 +307,7 @@ extension vChewingLM.LMUserOverride {
return prob * decay
}
private static func isPunctuation(_ node: Megrez.Compositor.Node) -> Bool {
private static func isPunctuation(_ node: Megrez.Node) -> Bool {
for key in node.keyArray {
guard let firstChar = key.first else { continue }
return String(firstChar) == "_"
@ -316,10 +316,10 @@ extension vChewingLM.LMUserOverride {
}
private static func formObservationKey(
walkedNodes: [Megrez.Compositor.Node], headIndex cursorIndex: Int, readingOnly: Bool = false
walkedNodes: [Megrez.Node], headIndex cursorIndex: Int, readingOnly: Bool = false
) -> String {
// let whiteList = ""
var arrNodes: [Megrez.Compositor.Node] = []
var arrNodes: [Megrez.Node] = []
var intLength = 0
for theNodeAnchor in walkedNodes {
arrNodes.append(theNodeAnchor)
@ -343,8 +343,8 @@ extension vChewingLM.LMUserOverride {
//
let strCurrent = kvCurrent.joinedKey()
var kvPrevious = Megrez.Compositor.KeyValuePaired(keyArray: [""], value: "")
var kvAnterior = Megrez.Compositor.KeyValuePaired(keyArray: [""], value: "")
var kvPrevious = Megrez.KeyValuePaired(keyArray: [""], value: "")
var kvAnterior = Megrez.KeyValuePaired(keyArray: [""], value: "")
var readingStack = ""
var trigramKey: String { "(\(kvAnterior.toNGramKey),\(kvPrevious.toNGramKey),\(strCurrent))" }
var result: String {

View File

@ -32,7 +32,7 @@ public protocol InputHandlerProtocol {
func handleEvent(_ event: NSEvent) -> Bool
func generateStateOfCandidates() -> IMEStateProtocol
func generateStateOfInputting(sansReading: Bool) -> IMEStateProtocol
func generateStateOfAssociates(withPair pair: Megrez.Compositor.KeyValuePaired) -> IMEStateProtocol
func generateStateOfAssociates(withPair pair: Megrez.KeyValuePaired) -> IMEStateProtocol
func consolidateNode(
candidate: (keyArray: [String], value: String), respectCursorPushing: Bool, preConsolidate: Bool, skipObservation: Bool
)
@ -196,7 +196,7 @@ public class InputHandler: InputHandlerProtocol {
/// - Parameter key:
/// - Returns:
/// nil
func generateArrayOfAssociates(withPair pair: Megrez.Compositor.KeyValuePaired) -> [(keyArray: [String], value: String)] {
func generateArrayOfAssociates(withPair pair: Megrez.KeyValuePaired) -> [(keyArray: [String], value: String)] {
var arrResult: [(keyArray: [String], value: String)] = []
if currentLM.hasAssociatedPhrasesFor(pair: pair) {
arrResult = currentLM.associatedPhrasesFor(pair: pair).map { ([""], $0) }
@ -209,7 +209,7 @@ public class InputHandler: InputHandlerProtocol {
/// - Returns:
func getStepsToNearbyNodeBorder(direction: Megrez.Compositor.TypingDirection) -> Int {
let currentCursor = compositor.cursor
var testCompositor = compositor
var testCompositor = compositor // Compositor hardCopy
testCompositor.jumpCursorBySpan(to: direction)
return abs(testCompositor.cursor - currentCursor)
}
@ -228,8 +228,8 @@ public class InputHandler: InputHandlerProtocol {
/// v1.9.3 SP2 Bug v1.9.4
/// v2.0.2
/// - Parameter theCandidate:
func consolidateCursorContext(with theCandidate: Megrez.Compositor.KeyValuePaired) {
var grid = compositor
func consolidateCursorContext(with theCandidate: Megrez.KeyValuePaired) {
var grid = compositor.hardCopy // Node hardCopy
var frontBoundaryEX = cursorForCandidate + 1
var rearBoundaryEX = cursorForCandidate
var debugIntelToPrint = ""
@ -279,7 +279,7 @@ public class InputHandler: InputHandlerProtocol {
let values = currentNode.currentPair.value.map(\.description)
for (subPosition, key) in currentNode.keyArray.enumerated() {
guard values.count > subPosition else { break } //
let thePair = Megrez.Compositor.KeyValuePaired(
let thePair = Megrez.KeyValuePaired(
keyArray: [key], value: values[subPosition]
)
compositor.overrideCandidate(thePair, at: position)
@ -302,9 +302,7 @@ public class InputHandler: InputHandlerProtocol {
candidate: (keyArray: [String], value: String), respectCursorPushing: Bool = true,
preConsolidate: Bool = false, skipObservation: Bool = false
) {
let theCandidate: Megrez.Compositor.KeyValuePaired = .init(
keyArray: candidate.keyArray, value: candidate.value
)
let theCandidate: Megrez.KeyValuePaired = .init(candidate)
///
if preConsolidate { consolidateCursorContext(with: theCandidate) }
@ -349,7 +347,7 @@ public class InputHandler: InputHandlerProtocol {
func generateArrayOfCandidates(fixOrder: Bool = true) -> [(keyArray: [String], value: String)] {
/// 使 nodesCrossing macOS
/// nodeCrossing
var arrCandidates: [Megrez.Compositor.KeyValuePaired] = {
var arrCandidates: [Megrez.KeyValuePaired] = {
switch prefs.useRearCursorMode {
case false: return compositor.fetchCandidates(at: cursorForCandidate, filter: .endAt)
case true: return compositor.fetchCandidates(at: cursorForCandidate, filter: .beginAt)
@ -367,8 +365,8 @@ public class InputHandler: InputHandlerProtocol {
}
let arrSuggestedUnigrams: [(String, Megrez.Unigram)] = retrieveUOMSuggestions(apply: false)
let arrSuggestedCandidates: [Megrez.Compositor.KeyValuePaired] = arrSuggestedUnigrams.map {
Megrez.Compositor.KeyValuePaired(key: $0.0, value: $0.1.value)
let arrSuggestedCandidates: [Megrez.KeyValuePaired] = arrSuggestedUnigrams.map {
Megrez.KeyValuePaired(key: $0.0, value: $0.1.value)
}
arrCandidates = arrSuggestedCandidates.filter { arrCandidates.contains($0) } + arrCandidates
arrCandidates = arrCandidates.deduplicated
@ -391,9 +389,9 @@ public class InputHandler: InputHandlerProtocol {
if apply {
///
if !suggestion.isEmpty, let newestSuggestedCandidate = suggestion.candidates.last {
let overrideBehavior: Megrez.Compositor.Node.OverrideType =
let overrideBehavior: Megrez.Node.OverrideType =
suggestion.forceHighScoreOverride ? .withHighScore : .withTopUnigramScore
let suggestedPair: Megrez.Compositor.KeyValuePaired = .init(
let suggestedPair: Megrez.KeyValuePaired = .init(
key: newestSuggestedCandidate.0, value: newestSuggestedCandidate.1.value
)
vCLog(

View File

@ -118,7 +118,7 @@ extension InputHandler {
/// - Parameters:
/// - key:
/// - Returns:
public func generateStateOfAssociates(withPair pair: Megrez.Compositor.KeyValuePaired) -> IMEStateProtocol {
public func generateStateOfAssociates(withPair pair: Megrez.KeyValuePaired) -> IMEStateProtocol {
IMEState.ofAssociates(
candidates: generateArrayOfAssociates(withPair: pair))
}