Files
AzooKeyKanaKanjiConverter/Sources/KanaKanjiConverterModule/ConversionAlgorithms/Core/SuffixReplacementProcessing.swift
2025-07-15 14:05:44 -07:00

138 lines
5.7 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// changed_last_n_character.swift
// Keyboard
//
// Created by ensan on 2020/10/14.
// Copyright © 2020 ensan. All rights reserved.
//
import Algorithms
import Foundation
import SwiftUtils
extension Kana2Kanji {
/// ,
/// ###
/// (0)
///
/// (1)
///
/// (2)
///
/// (3)(1)(2)register
///
/// (4)registerresult
///
/// (5)
func kana2lattice_changed(
_ inputData: ComposingText,
N_best: Int,
counts: (deletedInput: Int, addedInput: Int, deletedSurface: Int, addedSurface: Int),
previousResult: (inputData: ComposingText, lattice: Lattice),
needTypoCorrection: Bool
) -> (result: LatticeNode, lattice: Lattice) {
// (0)
let inputCount = inputData.input.count
let surfaceCount = inputData.convertTarget.count
let commonInputCount = previousResult.inputData.input.count - counts.deletedInput
let commonSurfaceCount = previousResult.inputData.convertTarget.count - counts.deletedSurface
debug("kana2lattice_changed", inputData, counts, previousResult.inputData, inputCount, commonInputCount)
// (1)
let indexMap = LatticeDualIndexMap(inputData)
let latticeIndices = indexMap.indices(inputCount: inputCount, surfaceCount: surfaceCount)
var lattice = previousResult.lattice.prefix(inputCount: commonInputCount, surfaceCount: commonSurfaceCount)
let terminalNodes: Lattice
if counts.addedInput == 0 && counts.addedSurface == 0 {
terminalNodes = Lattice(
inputCount: inputCount,
surfaceCount: surfaceCount,
rawNodes: lattice.map {
$0.filter {
$0.range.endIndex == .input(inputCount) || $0.range.endIndex == .surface(inputCount)
}
}
)
} else {
// (2)
let rawNodes = latticeIndices.map { index in
let inputRange: (startIndex: Int, endIndexRange: Range<Int>?)? = if let iIndex = index.inputIndex, max(commonInputCount, iIndex) < inputCount {
(iIndex, max(commonInputCount, iIndex) ..< inputCount)
} else {
nil
}
let surfaceRange: (startIndex: Int, endIndexRange: Range<Int>?)? = if let sIndex = index.surfaceIndex, max(commonSurfaceCount, sIndex) < surfaceCount {
(sIndex, max(commonSurfaceCount, sIndex) ..< surfaceCount)
} else {
nil
}
return self.dicdataStore.lookupDicdata(
composingText: inputData,
inputRange: inputRange,
surfaceRange: surfaceRange,
needTypoCorrection: needTypoCorrection
)
}
let addedNodes: Lattice = Lattice(
inputCount: inputCount,
surfaceCount: surfaceCount,
rawNodes: rawNodes
)
// (3)
for nodeArray in lattice {
for node in nodeArray {
if node.prevs.isEmpty {
continue
}
if self.dicdataStore.shouldBeRemoved(data: node.data) {
continue
}
//
let nextIndex = indexMap.dualIndex(for: node.range.endIndex)
if nextIndex != .bothIndex(inputIndex: inputCount, surfaceIndex: surfaceCount) {
self.updateNextNodes(with: node, nextNodes: addedNodes[index: nextIndex], nBest: N_best)
}
}
}
lattice.merge(addedNodes)
terminalNodes = addedNodes
}
// (3)
// terminalNodes
let result = LatticeNode.EOSNode
for (i, nodes) in terminalNodes.enumerated() {
for node in nodes {
if node.prevs.isEmpty {
continue
}
// node.registered.isEmpty
if self.dicdataStore.shouldBeRemoved(data: node.data) {
continue
}
//
let wValue = node.data.value()
if i == 0 {
// 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 = indexMap.dualIndex(for: node.range.endIndex)
if nextIndex.inputIndex == inputCount && nextIndex.surfaceIndex == surfaceCount {
self.updateResultNode(with: node, resultNode: result)
} else {
self.updateNextNodes(with: node, nextNodes: terminalNodes[index: nextIndex], nBest: N_best)
}
}
}
return (result: result, lattice: lattice)
}
}