Repo // Megrez v2.1.0 update.

This commit is contained in:
ShikiSuen 2022-09-04 08:37:01 +08:00
parent 97062ab731
commit a5fa288f97
10 changed files with 64 additions and 45 deletions

View File

@ -120,7 +120,7 @@ public class KeyHandler {
/// - Parameter key:
/// - Returns:
/// nil
func buildAssociatePhraseArray(withPair pair: Megrez.Compositor.Candidate) -> [(String, String)] {
func buildAssociatePhraseArray(withPair pair: Megrez.Compositor.KeyValuePaired) -> [(String, String)] {
var arrResult: [(String, String)] = []
if currentLM.hasAssociatedPhrasesFor(pair: pair) {
arrResult = currentLM.associatedPhrasesFor(pair: pair).map { ("", $0) }
@ -142,7 +142,7 @@ public class KeyHandler {
/// v1.9.3 SP2 Bug v1.9.4
/// v2.0.2
/// - Parameter theCandidate:
func consolidateCursorContext(with theCandidate: Megrez.Compositor.Candidate) {
func consolidateCursorContext(with theCandidate: Megrez.Compositor.KeyValuePaired) {
var grid = compositor
var frontBoundaryEX = actualCandidateCursor + 1
var rearBoundaryEX = actualCandidateCursor
@ -193,7 +193,7 @@ public class KeyHandler {
let values = currentNode.currentPair.value.charComponents
for (subPosition, key) in currentNode.keyArray.enumerated() {
guard values.count > subPosition else { break } //
let thePair = Megrez.Compositor.Candidate(
let thePair = Megrez.Compositor.KeyValuePaired(
key: key, value: values[subPosition]
)
compositor.overrideCandidate(thePair, at: position)
@ -212,7 +212,7 @@ public class KeyHandler {
/// - respectCursorPushing: true
/// - consolidate:
func fixNode(candidate: (String, String), respectCursorPushing: Bool = true, preConsolidate: Bool = false) {
let theCandidate: Megrez.Compositor.Candidate = .init(key: candidate.0, value: candidate.1)
let theCandidate: Megrez.Compositor.KeyValuePaired = .init(key: candidate.0, value: candidate.1)
///
if preConsolidate { consolidateCursorContext(with: theCandidate) }
@ -256,7 +256,7 @@ public class KeyHandler {
func getCandidatesArray(fixOrder: Bool = true) -> [(String, String)] {
/// 使 nodesCrossing macOS
/// nodeCrossing
var arrCandidates: [Megrez.Compositor.Candidate] = {
var arrCandidates: [Megrez.Compositor.KeyValuePaired] = {
switch mgrPrefs.useRearCursorMode {
case false:
return compositor.fetchCandidates(at: actualCandidateCursor, filter: .endAt)
@ -276,8 +276,8 @@ public class KeyHandler {
}
let arrSuggestedUnigrams: [(String, Megrez.Unigram)] = fetchSuggestionsFromUOM(apply: false)
let arrSuggestedCandidates: [Megrez.Compositor.Candidate] = arrSuggestedUnigrams.map {
Megrez.Compositor.Candidate(key: $0.0, value: $0.1.value)
let arrSuggestedCandidates: [Megrez.Compositor.KeyValuePaired] = arrSuggestedUnigrams.map {
Megrez.Compositor.KeyValuePaired(key: $0.0, value: $0.1.value)
}
arrCandidates = arrSuggestedCandidates.filter { arrCandidates.contains($0) } + arrCandidates
arrCandidates = arrCandidates.deduplicate
@ -302,7 +302,7 @@ public class KeyHandler {
if !suggestion.isEmpty, let newestSuggestedCandidate = suggestion.candidates.last {
let overrideBehavior: Megrez.Compositor.Node.OverrideType =
suggestion.forceHighScoreOverride ? .withHighScore : .withTopUnigramScore
let suggestedPair: Megrez.Compositor.Candidate = .init(
let suggestedPair: Megrez.Compositor.KeyValuePaired = .init(
key: newestSuggestedCandidate.0, value: newestSuggestedCandidate.1.value
)
IME.prtDebugIntel(

View File

@ -148,7 +148,7 @@ extension KeyHandler {
/// - key:
/// - Returns:
func buildAssociatePhraseState(
withPair pair: Megrez.Compositor.Candidate
withPair pair: Megrez.Compositor.KeyValuePaired
) -> InputState.Associates! {
//  Xcode
InputState.Associates(

View File

@ -245,11 +245,11 @@ extension vChewing {
return !unigramsFor(key: key).isEmpty
}
public func associatedPhrasesFor(pair: Megrez.Compositor.Candidate) -> [String] {
public func associatedPhrasesFor(pair: Megrez.Compositor.KeyValuePaired) -> [String] {
lmAssociates.valuesFor(pair: pair)
}
public func hasAssociatedPhrasesFor(pair: Megrez.Compositor.Candidate) -> Bool {
public func hasAssociatedPhrasesFor(pair: Megrez.Compositor.KeyValuePaired) -> Bool {
lmAssociates.hasValuesFor(pair: pair)
}

View File

@ -78,7 +78,7 @@ extension vChewing {
// This function will be implemented only if further hard-necessity comes.
}
public func valuesFor(pair: Megrez.Compositor.Candidate) -> [String] {
public func valuesFor(pair: Megrez.Compositor.KeyValuePaired) -> [String] {
var pairs: [String] = []
if let arrRangeRecords: [(Range<String.Index>, Int)] = rangeMap[pair.toNGramKey] {
for (netaRange, index) in arrRangeRecords {
@ -98,7 +98,7 @@ extension vChewing {
return pairs.filter { set.insert($0).inserted }
}
public func hasValuesFor(pair: Megrez.Compositor.Candidate) -> Bool {
public func hasValuesFor(pair: Megrez.Compositor.KeyValuePaired) -> Bool {
if rangeMap[pair.toNGramKey] != nil { return true }
return rangeMap[pair.value] != nil
}

View File

@ -319,8 +319,8 @@ extension vChewing.LMUserOverride {
//
let strCurrent = kvCurrent.key
var kvPrevious = Megrez.Compositor.Candidate()
var kvAnterior = Megrez.Compositor.Candidate()
var kvPrevious = Megrez.Compositor.KeyValuePaired()
var kvAnterior = Megrez.Compositor.KeyValuePaired()
var readingStack = ""
var trigramKey: String { "(\(kvAnterior.toNGramKey),\(kvPrevious.toNGramKey),\(strCurrent))" }
var result: String {

View File

@ -4,9 +4,7 @@
// This code is released under the MIT license (SPDX-License-Identifier: MIT)
/// The namespace for this package.
public enum Megrez {
public typealias KeyValuePaired = Compositor.Candidate //
}
public enum Megrez {}
//
// Megrez Gramambular Lukhnos Liu

View File

@ -23,7 +23,15 @@ extension Megrez {
/// -
public static let kDefaultSeparator: String = "-"
///
public var cursor: Int = 0 { didSet { cursor = max(0, min(cursor, length)) } }
public var cursor: Int = 0 {
didSet {
cursor = max(0, min(cursor, length))
marker = cursor
}
}
///
public var marker: Int = 0 { didSet { marker = max(0, min(marker, length)) } }
/// -
public var separator = kDefaultSeparator
///
@ -88,37 +96,46 @@ extension Megrez {
}
///
/// - Parameter direction:
/// - Parameters:
/// - direction:
/// - isMarker:
/// - Returns:
@discardableResult public mutating func jumpCursorBySpan(to direction: TypingDirection) -> Bool {
@discardableResult public mutating func jumpCursorBySpan(to direction: TypingDirection, isMarker: Bool = false)
-> Bool
{
var target = isMarker ? marker : cursor
switch direction {
case .front:
if cursor == width { return false }
if target == width { return false }
case .rear:
if cursor == 0 { return false }
if target == 0 { return false }
}
guard let currentRegion = cursorRegionMap[cursor] else { return false }
guard let currentRegion = cursorRegionMap[target] else { return false }
let aRegionForward = max(currentRegion - 1, 0)
let currentRegionBorderRear: Int = walkedNodes[0..<currentRegion].map(\.spanLength).reduce(0, +)
switch cursor {
switch target {
case currentRegionBorderRear:
switch direction {
case .front:
cursor =
target =
(currentRegion > walkedNodes.count)
? keys.count : walkedNodes[0...currentRegion].map(\.spanLength).reduce(0, +)
case .rear:
cursor = walkedNodes[0..<aRegionForward].map(\.spanLength).reduce(0, +)
target = walkedNodes[0..<aRegionForward].map(\.spanLength).reduce(0, +)
}
default:
switch direction {
case .front:
cursor = currentRegionBorderRear + walkedNodes[currentRegion].spanLength
target = currentRegionBorderRear + walkedNodes[currentRegion].spanLength
case .rear:
cursor = currentRegionBorderRear
target = currentRegionBorderRear
}
}
switch isMarker {
case false: cursor = target
case true: marker = target
}
return true
}

View File

@ -6,7 +6,7 @@
import Foundation
extension Megrez.Compositor {
public struct Candidate: Equatable, Hashable, Comparable, CustomStringConvertible {
public struct KeyValuePaired: Equatable, Hashable, Comparable, CustomStringConvertible {
///
public var key: String
///
@ -32,23 +32,23 @@ extension Megrez.Compositor {
hasher.combine(value)
}
public static func == (lhs: Candidate, rhs: Candidate) -> Bool {
public static func == (lhs: KeyValuePaired, rhs: KeyValuePaired) -> Bool {
lhs.key == rhs.key && lhs.value == rhs.value
}
public static func < (lhs: Candidate, rhs: Candidate) -> Bool {
public static func < (lhs: KeyValuePaired, rhs: KeyValuePaired) -> Bool {
(lhs.key.count < rhs.key.count) || (lhs.key.count == rhs.key.count && lhs.value < rhs.value)
}
public static func > (lhs: Candidate, rhs: Candidate) -> Bool {
public static func > (lhs: KeyValuePaired, rhs: KeyValuePaired) -> Bool {
(lhs.key.count > rhs.key.count) || (lhs.key.count == rhs.key.count && lhs.value > rhs.value)
}
public static func <= (lhs: Candidate, rhs: Candidate) -> Bool {
public static func <= (lhs: KeyValuePaired, rhs: KeyValuePaired) -> Bool {
(lhs.key.count <= rhs.key.count) || (lhs.key.count == rhs.key.count && lhs.value <= rhs.value)
}
public static func >= (lhs: Candidate, rhs: Candidate) -> Bool {
public static func >= (lhs: KeyValuePaired, rhs: KeyValuePaired) -> Bool {
(lhs.key.count >= rhs.key.count) || (lhs.key.count == rhs.key.count && lhs.value >= rhs.value)
}
}
@ -60,8 +60,8 @@ extension Megrez.Compositor {
/// location - 1
/// - Parameter location:
/// - Returns:
public func fetchCandidates(at location: Int, filter: CandidateFetchFilter = .all) -> [Candidate] {
var result = [Candidate]()
public func fetchCandidates(at location: Int, filter: CandidateFetchFilter = .all) -> [KeyValuePaired] {
var result = [KeyValuePaired]()
guard !keys.isEmpty else { return result }
let location = max(min(location, keys.count - 1), 0) //
let anchors: [NodeAnchor] = fetchOverlappingNodes(at: location).stableSorted {
@ -96,7 +96,7 @@ extension Megrez.Compositor {
/// - overrideType:
/// - Returns:
@discardableResult public func overrideCandidate(
_ candidate: Candidate, at location: Int, overrideType: Node.OverrideType = .withHighScore
_ candidate: KeyValuePaired, at location: Int, overrideType: Node.OverrideType = .withHighScore
)
-> Bool
{
@ -154,7 +154,7 @@ extension Megrez.Compositor {
anchor.node.reset()
continue
}
anchor.node.overridingScore /= 2
anchor.node.overridingScore /= 4
}
}
return true

View File

@ -42,7 +42,7 @@ extension Megrez.Compositor {
didSet { currentUnigramIndex = min(max(0, currentUnigramIndex), unigrams.count - 1) }
}
public var currentPair: Megrez.Compositor.Candidate { .init(key: key, value: value) }
public var currentPair: Megrez.Compositor.KeyValuePaired { .init(key: key, value: value) }
public func hash(into hasher: inout Hasher) {
hasher.combine(key)
@ -70,8 +70,12 @@ extension Megrez.Compositor {
overrideType = .withNoOverrides
}
///
public var isReadingMismatched: Bool {
keyArray.count != value.count
}
///
/// - Returns:
public var currentUnigram: Megrez.Unigram {
unigrams.isEmpty ? .init() : unigrams[currentUnigramIndex]
}

View File

@ -17,7 +17,7 @@
5B2170E0289FACAD00BE7304 /* 7_LangModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2170D7289FACAC00BE7304 /* 7_LangModel.swift */; };
5B2170E1289FACAD00BE7304 /* 0_Megrez.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2170D8289FACAC00BE7304 /* 0_Megrez.swift */; };
5B2170E2289FACAD00BE7304 /* 8_Unigram.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2170D9289FACAC00BE7304 /* 8_Unigram.swift */; };
5B2170E3289FACAD00BE7304 /* 3_Candidate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2170DA289FACAC00BE7304 /* 3_Candidate.swift */; };
5B2170E3289FACAD00BE7304 /* 3_KeyValuePaired.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2170DA289FACAC00BE7304 /* 3_KeyValuePaired.swift */; };
5B2170E4289FACAD00BE7304 /* 2_Walker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2170DB289FACAC00BE7304 /* 2_Walker.swift */; };
5B2170E5289FACAD00BE7304 /* 6_Node.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2170DC289FACAC00BE7304 /* 6_Node.swift */; };
5B2170E6289FACAD00BE7304 /* 4_Span.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5B2170DD289FACAC00BE7304 /* 4_Span.swift */; };
@ -231,7 +231,7 @@
5B2170D7289FACAC00BE7304 /* 7_LangModel.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 7_LangModel.swift; sourceTree = "<group>"; };
5B2170D8289FACAC00BE7304 /* 0_Megrez.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 0_Megrez.swift; sourceTree = "<group>"; };
5B2170D9289FACAC00BE7304 /* 8_Unigram.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 8_Unigram.swift; sourceTree = "<group>"; };
5B2170DA289FACAC00BE7304 /* 3_Candidate.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 3_Candidate.swift; sourceTree = "<group>"; };
5B2170DA289FACAC00BE7304 /* 3_KeyValuePaired.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 3_KeyValuePaired.swift; sourceTree = "<group>"; };
5B2170DB289FACAC00BE7304 /* 2_Walker.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 2_Walker.swift; sourceTree = "<group>"; };
5B2170DC289FACAC00BE7304 /* 6_Node.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 6_Node.swift; sourceTree = "<group>"; };
5B2170DD289FACAC00BE7304 /* 4_Span.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = 4_Span.swift; sourceTree = "<group>"; };
@ -873,7 +873,7 @@
5B2170D8289FACAC00BE7304 /* 0_Megrez.swift */,
5B2170DE289FACAC00BE7304 /* 1_Compositor.swift */,
5B2170DB289FACAC00BE7304 /* 2_Walker.swift */,
5B2170DA289FACAC00BE7304 /* 3_Candidate.swift */,
5B2170DA289FACAC00BE7304 /* 3_KeyValuePaired.swift */,
5B2170DD289FACAC00BE7304 /* 4_Span.swift */,
5B2170DF289FACAC00BE7304 /* 5_Vertex.swift */,
5B2170DC289FACAC00BE7304 /* 6_Node.swift */,
@ -1255,7 +1255,7 @@
5BAEFAD028012565001F42C9 /* mgrLangModel.swift in Sources */,
5B782EC4280C243C007276DE /* KeyHandler_HandleCandidate.swift in Sources */,
5B62A33827AE79CD00A19448 /* StringUtils.swift in Sources */,
5B2170E3289FACAD00BE7304 /* 3_Candidate.swift in Sources */,
5B2170E3289FACAD00BE7304 /* 3_KeyValuePaired.swift in Sources */,
5BA9FD0F27FEDB6B002DE248 /* suiPrefPaneGeneral.swift in Sources */,
5B2170E6289FACAD00BE7304 /* 4_Span.swift in Sources */,
5BA9FD4927FEF3C9002DE248 /* Section.swift in Sources */,