Skip to content

Commit

Permalink
reverseWalk // Make score-balancing optional. (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen authored May 12, 2022
1 parent cd68bf0 commit 244037e
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions Sources/Megrez/1_Walker.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,23 @@ extension Megrez {
mutGrid = grid
}

public func reverseWalk(at location: Int, score accumulatedScore: Double = 0.0, nodesLimit: Int = 0)
-> [NodeAnchor]
{
public func reverseWalk(
at location: Int,
score accumulatedScore: Double = 0.0,
nodesLimit: Int = 0,
balanced: Bool = false
) -> [NodeAnchor] {
if location == 0 || location > mutGrid.width() {
return [] as [NodeAnchor]
}

var paths: [[NodeAnchor]] = []
var nodes: [NodeAnchor] = mutGrid.nodesEndingAt(location: location)

nodes.sort {
$0.balancedScore > $1.balancedScore // 排序規則已經在 NodeAnchor 內定義了。
if balanced {
nodes.sort {
$0.balancedScore > $1.balancedScore // 排序規則已經在 NodeAnchor 內定義了。
}
}

// 只檢查前 X 個 NodeAnchor 是否有 node。
Expand All @@ -58,10 +63,13 @@ extension Megrez {
continue
}

n.accumulatedScore = accumulatedScore + nNode.score()
// 利用 Spanning Length 來決定權重。
// 這樣一來,例:「再見」比「在」與「見」的權重更高。
let weightedScore: Double = (Double(n.spanningLength) - 1) * 2
n.accumulatedScore = accumulatedScore + nNode.score() + weightedScore
if balanced {
let weightedScore: Double = (Double(n.spanningLength) - 1) * 2
n.accumulatedScore += weightedScore
}

var path: [NodeAnchor] = reverseWalk(
at: location - n.spanningLength,
Expand All @@ -72,7 +80,7 @@ extension Megrez {
paths.append(path)

// 始終使用固定的候選字
if nNode.score() >= 0 {
if balanced, nNode.score() >= 0 {
break
}
}
Expand Down

0 comments on commit 244037e

Please sign in to comment.