Skip to content

Commit

Permalink
build() // Fix wrong range. (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
ShikiSuen authored Apr 30, 2022
1 parent b396cdd commit 23fe3aa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 29 deletions.
8 changes: 2 additions & 6 deletions Sources/Megrez/1_BlockReadingBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -125,15 +125,11 @@ extension Megrez {

for p in itrBegin..<itrEnd {
var q = 1
while q <= kMaximumBuildSpanLength {
if p + q > itrEnd {
break
}
let strSlice = (p == itrEnd) ? [mutReadings[itrEnd]] : mutReadings[p..<itrEnd]
while q <= kMaximumBuildSpanLength, p + q <= itrEnd {
let strSlice = mutReadings[p..<(p + q)]
let combinedReading: String = join(slice: strSlice, separator: mutJoinSeparator)
if !mutGrid.hasMatchedNode(location: p, spanningLength: q, key: combinedReading) {
let unigrams: [Unigram] = mutLM.unigramsFor(key: combinedReading)

if !unigrams.isEmpty {
let n = Node(key: combinedReading, unigrams: unigrams)
mutGrid.insertNode(node: n, location: p, spanningLength: q)
Expand Down
44 changes: 25 additions & 19 deletions Sources/Megrez/2_Grid.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ extension Megrez {
}

public func insertNode(node: Node, location: Int, spanningLength: Int) {
if mutSpans.count <= location {
if location >= mutSpans.count {
let diff = location - mutSpans.count + 1
for _ in 0..<diff {
mutSpans.append(Span())
Expand All @@ -54,10 +54,8 @@ extension Megrez {
}

public func expandGridByOneAt(location: Int) {
if location == 0 || location == mutSpans.count {
mutSpans.insert(Span(), at: location)
} else {
mutSpans.insert(Span(), at: location)
mutSpans.append(Span())
if location > 0, location < mutSpans.count {
for i in 0..<location {
// zaps overlapping spans
mutSpans[i].removeNodeOfLengthGreaterThan(location - i)
Expand Down Expand Up @@ -88,11 +86,13 @@ extension Megrez {
let span = mutSpans[i]
if i + span.maximumLength >= location {
if let np = span.node(length: location - i) {
var na = NodeAnchor()
na.node = np
na.location = i
na.spanningLength = location - i
results.append(na)
results.append(
NodeAnchor(
node: np,
location: i,
spanningLength: location - i
)
)
}
}
}
Expand All @@ -111,11 +111,13 @@ extension Megrez {
continue
}
if let np = span.node(length: j) {
var na = NodeAnchor()
na.node = np
na.location = i
na.spanningLength = location - i
results.append(na)
results.append(
NodeAnchor(
node: np,
location: i,
spanningLength: location - i
)
)
}
}
}
Expand All @@ -125,17 +127,19 @@ extension Megrez {
}

public func fixNodeSelectedCandidate(location: Int, value: String) -> NodeAnchor {
let nodes = nodesCrossingOrEndingAt(location: location)
var node = NodeAnchor()
for nodeAnchor in nodes {
for nodeAnchor in nodesCrossingOrEndingAt(location: location) {
var nodeAnchor = nodeAnchor
if let theNode = nodeAnchor.node {
let candidates = theNode.candidates()
// Reset the candidate-fixed state of every node at the location.
theNode.resetCandidate()
nodeAnchor.node = theNode

for (i, candidate) in candidates.enumerated() {
if candidate.value == value {
theNode.selectCandidateAt(index: i)
nodeAnchor.node = theNode
node = nodeAnchor
break
}
Expand All @@ -146,16 +150,18 @@ extension Megrez {
}

public func overrideNodeScoreForSelectedCandidate(location: Int, value: inout String, overridingScore: Float) {
let nodes = nodesCrossingOrEndingAt(location: location)
for nodeAnchor in nodes {
for nodeAnchor in nodesCrossingOrEndingAt(location: location) {
var nodeAnchor = nodeAnchor
if let theNode = nodeAnchor.node {
let candidates = theNode.candidates()
// Reset the candidate-fixed state of every node at the location.
theNode.resetCandidate()
nodeAnchor.node = theNode

for (i, candidate) in candidates.enumerated() {
if candidate.value == value {
theNode.selectFloatingCandidateAt(index: i, score: Double(overridingScore))
nodeAnchor.node = theNode
break
}
}
Expand Down
8 changes: 4 additions & 4 deletions Sources/Megrez/4_Node.swift
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ extension Megrez {
var index = 0
for gram in unigrams {
mutValueUnigramIndexMap[gram.keyValue.value] = index
mutCandidates.append(contentsOf: [gram.keyValue])
mutCandidates.append(gram.keyValue)
index += 1
}

for gram in bigrams {
mutPrecedingBigramMap[gram.precedingKeyValue]?.append(contentsOf: [gram])
mutPrecedingBigramMap[gram.precedingKeyValue]?.append(gram)
}
}

Expand All @@ -89,8 +89,8 @@ extension Megrez {

if !isCandidateFixed() {
for (index, _) in precedingKeyValues.enumerated() {
let bigrams = mutPrecedingBigramMap.elements[index].1
for (_, bigram) in bigrams.enumerated() {
let bigrams = mutPrecedingBigramMap.elements[index].value
for bigram in bigrams {
if bigram.score > max {
if let valRetrieved = mutValueUnigramIndexMap[bigram.keyValue.value] {
newIndex = valRetrieved as Int
Expand Down
3 changes: 3 additions & 0 deletions main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ func testInput() {
builder.insertReadingAtCursor(reading: "zhong1")
builder.insertReadingAtCursor(reading: "jiang3")
builder.insertReadingAtCursor(reading: "jin1")
builder.insertReadingAtCursor(reading: "ni3")
builder.insertReadingAtCursor(reading: "zhe4")
builder.insertReadingAtCursor(reading: "yang4")

let walker = Megrez.Walker(grid: builder.grid())

Expand Down

0 comments on commit 23fe3aa

Please sign in to comment.