Files
AzooKeyKanaKanjiConverter/Sources/KanaKanjiConverterModule/ConversionAlgorithms/Core/SuffixReplacementProcessing.swift
2025-07-09 00:01:25 +09:00

101 lines
3.9 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 Foundation
import SwiftUtils
extension Kana2Kanji {
/// ,
/// ###
/// (0)
///
/// (1)
///
/// (2)
///
/// (3)(1)(2)register
///
/// (4)registerresult
///
/// (5)
func kana2lattice_changed(_ inputData: ComposingText, N_best: Int, counts: (deleted: Int, added: Int), previousResult: (inputData: ComposingText, lattice: Lattice), needTypoCorrection: Bool) -> (result: LatticeNode, lattice: Lattice) {
// (0)
let count = inputData.input.count
let commonCount = previousResult.inputData.input.count - counts.deleted
debug("kana2lattice_changed", inputData, counts, previousResult.inputData, count, commonCount)
// (1)
var lattice = previousResult.lattice.prefix(commonCount)
let terminalNodes: Lattice
if counts.added == 0 {
terminalNodes = Lattice(nodes: lattice.nodes.map {
$0.filter {
$0.inputRange.endIndex == count
}
})
} else {
// (2)
let addedNodes: Lattice = Lattice(nodes: (0..<count).map {(i: Int) in
self.dicdataStore.getLOUDSDataInRange(inputData: inputData, from: i, toIndexRange: max(commonCount, i) ..< count, needTypoCorrection: needTypoCorrection)
})
// (3)
for nodeArray in lattice.nodes {
for node in nodeArray {
if node.prevs.isEmpty {
continue
}
if self.dicdataStore.shouldBeRemoved(data: node.data) {
continue
}
//
let nextIndex = node.inputRange.endIndex
self.updateNextNodes(with: node, nextNodes: addedNodes[inputIndex: nextIndex], nBest: N_best)
}
}
lattice.merge(addedNodes)
terminalNodes = addedNodes
}
// (3)
// terminalNodes
let result = LatticeNode.EOSNode
for (i, nodes) in terminalNodes.nodes.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 = node.inputRange.endIndex
if count == nextIndex {
self.updateResultNode(with: node, resultNode: result)
} else {
self.updateNextNodes(with: node, nextNodes: terminalNodes[inputIndex: nextIndex], nBest: N_best)
}
}
}
return (result: result, lattice: lattice)
}
}