Megrez v1.1.9 // Real forwardWalker().

- Also, Grid: Get [NodeAnchor] using given ClosedRange.
This commit is contained in:
ShikiSuen 2022-05-31 23:43:06 +08:00
parent 9cde097c59
commit ba4654f850
2 changed files with 152 additions and 12 deletions

View File

@ -147,12 +147,101 @@ extension Megrez {
joinedPhrase: String = "",
longPhrases: [String] = .init()
) -> [NodeAnchor] {
let newLocation = (mutGrid.width) - abs(location) //
return Array(
reverseWalk(
at: newLocation, score: accumulatedScore,
joinedPhrase: joinedPhrase, longPhrases: longPhrases
).reversed())
let location = abs(location) //
if location >= mutGrid.width {
return .init()
}
var paths = [[NodeAnchor]]()
var nodes = mutGrid.nodesBeginningAt(location: location)
nodes = nodes.stableSorted {
$0.scoreForSort > $1.scoreForSort
}
if let nodeOfNodeZero = nodes[0].node, nodeOfNodeZero.score >= nodeOfNodeZero.kSelectedCandidateScore {
// 使
var nodeZero = nodes[0]
nodeZero.accumulatedScore = accumulatedScore + nodeOfNodeZero.score
var path: [NodeAnchor] = walk(at: location + nodeZero.spanningLength, score: nodeZero.accumulatedScore)
path.insert(nodeZero, at: 0)
paths.append(path)
} else if !longPhrases.isEmpty {
var path = [NodeAnchor]()
for theAnchor in nodes {
guard let theNode = theAnchor.node else { continue }
var theAnchor = theAnchor
let joinedValue = joinedPhrase + theNode.currentKeyValue.value
//
// /////////使
//
if longPhrases.contains(joinedValue) {
theAnchor.accumulatedScore = kDroppedPathScore
path.insert(theAnchor, at: 0)
paths.append(path)
continue
}
theAnchor.accumulatedScore = accumulatedScore + theNode.score
if joinedValue.count >= longPhrases[0].count {
path = walk(
at: location + theAnchor.spanningLength, score: theAnchor.accumulatedScore, joinedPhrase: "",
longPhrases: .init()
)
} else {
path = walk(
at: location + theAnchor.spanningLength, score: theAnchor.accumulatedScore, joinedPhrase: joinedValue,
longPhrases: longPhrases
)
}
path.insert(theAnchor, at: 0)
paths.append(path)
}
} else {
//
var longPhrases = [String]()
for theAnchor in nodes {
guard let theNode = theAnchor.node else { continue }
if theAnchor.spanningLength > 1 {
longPhrases.append(theNode.currentKeyValue.value)
}
}
longPhrases = longPhrases.stableSorted {
$0.count > $1.count
}
for theAnchor in nodes {
var theAnchor = theAnchor
guard let theNode = theAnchor.node else { continue }
theAnchor.accumulatedScore = accumulatedScore + theNode.score
var path = [NodeAnchor]()
if theAnchor.spanningLength > 1 {
path = walk(
at: location + theAnchor.spanningLength, score: theAnchor.accumulatedScore, joinedPhrase: "",
longPhrases: .init()
)
} else {
path = walk(
at: location + 1, score: theAnchor.accumulatedScore,
joinedPhrase: theNode.currentKeyValue.value, longPhrases: longPhrases
)
}
path.insert(theAnchor, at: 0)
paths.append(path)
}
}
guard !paths.isEmpty else {
return .init()
}
var result: [NodeAnchor] = paths[0]
for neta in paths {
if neta.last!.accumulatedScore > result.last!.accumulatedScore {
result = neta
}
}
return result
}
///

View File

@ -189,14 +189,43 @@ extension Megrez {
return results
}
///
/// - Parameters:
/// - range:
func nodes(in range: ClosedRange<Int>) -> [NodeAnchor] {
var result = [NodeAnchor]()
if !mutSpans.isEmpty, range.upperBound <= mutSpans.count {
for i in 0..<range.upperBound {
let span = mutSpans[i]
if i + span.maximumLength > range.lowerBound {
for j in 1...span.maximumLength {
guard let np = span.node(length: j),
(i + j) > range.lowerBound
else { continue }
result.append(
NodeAnchor(
node: np,
location: i,
spanningLength: j
)
)
}
}
}
}
return result
}
///
/// - Parameters:
/// - location:
/// - value:
@discardableResult public func fixNodeSelectedCandidate(location: Int, value: String) -> NodeAnchor {
let location = abs(location) //
var node = NodeAnchor()
for nodeAnchor in nodesCrossingOrEndingAt(location: location) {
var anchor = NodeAnchor()
var selectedIndex = 0
var anchors = nodesCrossingOrEndingAt(location: location)
for nodeAnchor in anchors {
guard let theNode = nodeAnchor.node else {
continue
}
@ -205,13 +234,24 @@ extension Megrez {
theNode.resetCandidate()
for (i, candidate) in candidates.enumerated() {
if candidate.value == value {
theNode.selectCandidateAt(index: i)
node = nodeAnchor
selectedIndex = i
anchor = nodeAnchor
break
}
}
}
return node
guard let node = anchor.node else {
return anchor
}
anchors = nodes(in: (location - anchor.spanningLength)...location)
for nodeAnchor in anchors {
if let theNode = nodeAnchor.node {
theNode.resetCandidate()
}
}
node.selectCandidateAt(index: selectedIndex)
anchor.node = node
return anchor
}
///
@ -221,7 +261,10 @@ extension Megrez {
/// - overridingScore:
public func overrideNodeScoreForSelectedCandidate(location: Int, value: String, overridingScore: Double) {
let location = abs(location) //
for nodeAnchor in nodesCrossingOrEndingAt(location: location) {
let anchor = NodeAnchor()
let selectedIndex = 0
var anchors = nodesCrossingOrEndingAt(location: location)
for nodeAnchor in anchors {
guard let theNode = nodeAnchor.node else {
continue
}
@ -235,6 +278,14 @@ extension Megrez {
}
}
}
guard let node = anchor.node else { return }
anchors = nodes(in: (location - anchor.spanningLength)...location)
for nodeAnchor in anchors {
if let theNode = nodeAnchor.node {
theNode.resetCandidate()
}
}
node.selectFloatingCandidateAt(index: selectedIndex, score: overridingScore)
}
}
}