Files
AzooKeyKanaKanjiConverter/Sources/KanaKanjiConverterModule/ConversionAlgorithms/Core/SuffixReplacementProcessing.swift
2025-07-06 23:10:32 +09:00

161 lines
7.3 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, nodes: Nodes), needTypoCorrection: Bool) -> (result: LatticeNode, nodes: Nodes) {
// (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 nodes = previousResult.nodes.prefix(commonCount).map {(nodes: [LatticeNode]) in
nodes.filter {$0.inputRange.endIndex <= commonCount}
}
while nodes.last?.isEmpty ?? false {
nodes.removeLast()
}
let terminalNodes: Nodes
if counts.added == 0 {
terminalNodes = nodes.map {
$0.filter {
$0.inputRange.endIndex == count
}
}
} else {
// (2)
let addedNodes: [[LatticeNode]] = (0..<count).map {(i: Int) in
self.dicdataStore.getLOUDSDataInRange(inputData: inputData, from: i, toIndexRange: max(commonCount, i) ..< count, needTypoCorrection: needTypoCorrection)
}
// (3)
for nodeArray in nodes {
for node in nodeArray {
if node.prevs.isEmpty {
continue
}
if self.dicdataStore.shouldBeRemoved(data: node.data) {
continue
}
//
let nextIndex = node.inputRange.endIndex
for nextnode in addedNodes[nextIndex] {
if self.dicdataStore.shouldBeRemoved(data: nextnode.data) {
continue
}
//
let ccValue: PValue = self.dicdataStore.getCCValue(node.data.rcid, nextnode.data.lcid)
// nodeprevnode
for (index, value) in node.values.enumerated() {
let newValue: PValue = ccValue + value
// index
let lastindex: Int = (nextnode.prevs.lastIndex(where: {$0.totalValue >= newValue}) ?? -1) + 1
if lastindex == N_best {
continue
}
let newnode: RegisteredNode = node.getRegisteredNode(index, value: newValue)
//
if nextnode.prevs.count >= N_best {
nextnode.prevs.removeLast()
}
// removeinsert (insertO(N))
nextnode.prevs.insert(newnode, at: lastindex)
}
}
}
}
for (index, nodeArray) in addedNodes.enumerated() where index < nodes.endIndex {
nodes[index].append(contentsOf: nodeArray)
}
for nodeArray in addedNodes.suffix(counts.added) {
nodes.append(nodeArray)
}
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 = node.inputRange.endIndex
if count == nextIndex {
//
for index in node.prevs.indices {
let newnode = node.getRegisteredNode(index, value: node.values[index])
result.prevs.append(newnode)
}
} else {
for nextnode in terminalNodes[nextIndex] {
// node.registered.isEmpty
if self.dicdataStore.shouldBeRemoved(data: nextnode.data) {
continue
}
//
let ccValue = self.dicdataStore.getCCValue(node.data.rcid, nextnode.data.lcid)
// nodeprevnode
for (index, value) in node.values.enumerated() {
let newValue = ccValue + value
// index
let lastindex: Int = (nextnode.prevs.lastIndex(where: {$0.totalValue >= newValue}) ?? -1) + 1
if lastindex == N_best {
continue
}
let newnode: RegisteredNode = node.getRegisteredNode(index, value: newValue)
//
if nextnode.prevs.count >= N_best {
nextnode.prevs.removeLast()
}
// removeinsert (insertO(N))
nextnode.prevs.insert(newnode, at: lastindex)
}
}
}
}
}
return (result: result, nodes: nodes)
}
}