Files
AzooKeyKanaKanjiConverter/Sources/KanaKanjiConverterModule/ConversionAlgorithms/Core/PrefixCompletionProcessing.swift

73 lines
3.2 KiB
Swift

//
// afterPartlyCompleted.swift
// Keyboard
//
// Created by ensan on 2020/09/14.
// Copyright © 2020 ensan. All rights reserved.
//
import Foundation
import SwiftUtils
extension Kana2Kanji {
/// ,
/// ###
/// (1)noderegisteredcompletedDataBOS
///
/// (2)
func kana2lattice_afterComplete(_ inputData: ComposingText, completedData: Candidate, N_best: Int, previousResult: (inputData: ComposingText, lattice: Lattice), needTypoCorrection: Bool) -> (result: LatticeNode, lattice: Lattice) {
debug("確定直後の変換、前は:", previousResult.inputData, "後は:", inputData)
let inputCount = inputData.input.count
let surfaceCount = inputData.convertTarget.count
// TODO: input/convertTargetsuffix
let convertedInputCount = previousResult.inputData.input.count - inputCount
let convertedSurfaceCount = previousResult.inputData.convertTarget.count - surfaceCount
// (1)
let start = RegisteredNode.fromLastCandidate(completedData)
let lattice = previousResult.lattice.suffix(inputCount: inputCount, surfaceCount: surfaceCount)
for (i, nodeArray) in lattice.indexedNodes() {
let prevs: [RegisteredNode] = if i.isZero {
[start]
} else {
[]
}
for node in nodeArray {
node.prevs = prevs
// inputRange
node.range = node.range.offseted(inputOffset: -convertedInputCount, surfaceOffset: -convertedSurfaceCount)
}
}
// (2)
let result = LatticeNode.EOSNode
for (i, nodeArray) in lattice.indexedNodes() {
for node in nodeArray {
if node.prevs.isEmpty {
continue
}
if self.dicdataStore.shouldBeRemoved(data: node.data) {
continue
}
//
let wValue = node.data.value()
if i.isZero {
// values
node.values = node.prevs.map {$0.totalValue + wValue + self.dicdataStore.getCCValue($0.data.rcid, node.data.lcid)}
} else {
// values
node.values = node.prevs.map {$0.totalValue + wValue}
}
//
let nextIndex = node.range.endIndex
if nextIndex == .input(inputCount) || nextIndex == .surface(surfaceCount) {
self.updateResultNode(with: node, resultNode: result)
} else {
self.updateNextNodes(with: node, nextNodes: lattice[index: nextIndex], nBest: N_best)
}
}
}
return (result: result, lattice: lattice)
}
}