Files
AzooKeyKanaKanjiConverter/Sources/KanaKanjiConverterModule/Kana2Kanji/added_last_1_character.swift

112 lines
4.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.

//
// afterCharacterAdded.swift
// Keyboard
//
// Created by ensan on 2020/09/14.
// Copyright © 2020 ensan. All rights reserved.
//
import Foundation
import SwiftUtils
extension Kana2Kanji {
/// ,
/// - Parameters:
/// - addedCharacter:
/// - N_best: N_best
/// - previousResult:
/// - Returns:
/// -
/// ###
/// (0)
///
/// (1)
///
/// (2)(1)registerN_best
///
/// (3)(1)registerresultEOS
///
/// (4)
func kana2lattice_addedLast(_ inputData: ComposingText, N_best: Int, previousResult: (inputData: ComposingText, nodes: Nodes), needTypoCorrection: Bool) -> (result: LatticeNode, nodes: Nodes) {
debug("一文字追加。内部文字列は\(inputData.input).\(previousResult.nodes.map {($0.first?.data.ruby, $0.first?.inputRange)})")
// (0)
var nodes = previousResult.nodes
let count = previousResult.inputData.input.count
// (1)
let addedNodes: [[LatticeNode]] = (0...count).map {(i: Int) in
self.dicdataStore.getLOUDSDataInRange(inputData: inputData, from: i, toIndexRange: count ..< count+1, needTypoCorrection: needTypoCorrection)
}
//
// (2)
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] {
// node.registered.isEmpty
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)
}
}
}
}
// (3)
let result = LatticeNode.EOSNode
for (i, nodeArray) in addedNodes.enumerated() {
for node in nodeArray {
if node.prevs.isEmpty {
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}
}
//
for index in node.prevs.indices {
let newnode = node.getRegisteredNode(index, value: node.values[index])
result.prevs.append(newnode)
}
}
}
// (4)
for (index, nodeArray) in addedNodes.enumerated() where index < nodes.endIndex {
nodes[index].append(contentsOf: nodeArray)
}
nodes.append(addedNodes.last ?? [])
return (result: result, nodes: nodes)
}
}