Merge pull request #220 from azooKey/refactor/unify_delete_and_replace

refactor: 末尾置換と末尾削除を統合
This commit is contained in:
Miwa
2025-07-07 23:28:19 +09:00
committed by GitHub
3 changed files with 62 additions and 123 deletions

View File

@ -1,60 +0,0 @@
//
// afterLastCharacterDeleted.swift
// Keyboard
//
// Created by ensan on 2020/09/14.
// Copyright © 2020 ensan. All rights reserved.
//
import Foundation
import SwiftUtils
extension Kana2Kanji {
/// ,
/// - Parameters:
/// - deletedCount:
/// - N_best: N_best
/// - previousResult:
/// - Returns:
///
///
/// ###
/// (1)resultregister
/// N_best
///
/// (2)filter
func kana2lattice_deletedLast(deletedCount: Int, N_best: Int, previousResult: (inputData: ComposingText, nodes: Nodes)) -> (result: LatticeNode, nodes: Nodes) {
debug("削除の連続性を利用した変換、元の文字は:", previousResult.inputData.convertTarget)
let count = previousResult.inputData.input.count - deletedCount
// (1)
let result = LatticeNode.EOSNode
for nodeArray in previousResult.nodes {
for node in nodeArray {
if node.prevs.isEmpty {
continue
}
if self.dicdataStore.shouldBeRemoved(data: node.data) {
continue
}
let nextIndex = node.inputRange.endIndex
if nextIndex == count {
//
for (index, value) in node.values.enumerated() {
let newnode = node.getRegisteredNode(index, value: value)
result.prevs.append(newnode)
}
}
}
}
// (2)
let updatedNodes = previousResult.nodes.prefix(count).map {(nodeArray: [LatticeNode]) in
nodeArray.filter {$0.inputRange.endIndex <= count}
}
return (result: result, nodes: updatedNodes)
}
}

View File

@ -37,52 +37,71 @@ extension Kana2Kanji {
while nodes.last?.isEmpty ?? false {
nodes.removeLast()
}
// (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)
}
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 addedNodes.enumerated() {
for (i, nodes) in terminalNodes.enumerated() {
for node in nodes {
if node.prevs.isEmpty {
continue
@ -108,7 +127,7 @@ extension Kana2Kanji {
result.prevs.append(newnode)
}
} else {
for nextnode in addedNodes[nextIndex] {
for nextnode in terminalNodes[nextIndex] {
// node.registered.isEmpty
if self.dicdataStore.shouldBeRemoved(data: nextnode.data) {
continue
@ -135,14 +154,6 @@ extension Kana2Kanji {
}
}
}
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)
}
return (result: result, nodes: nodes)
}

View File

@ -661,22 +661,10 @@ import EfficientNGram
let diff = inputData.differenceSuffix(to: previousInputData)
//
if diff.deleted > 0 && diff.addedCount == 0 {
debug("\(#function): 最後尾削除用の関数を呼びます, 消した文字数は\(diff.deleted)")
let result = converter.kana2lattice_deletedLast(deletedCount: diff.deleted, N_best: N_best, previousResult: (inputData: previousInputData, nodes: nodes))
self.previousInputData = inputData
return result
}
//
if diff.addedCount > 0 {
debug("\(#function): 最後尾文字置換用の関数を呼びます、差分は\(diff)")
let result = converter.kana2lattice_changed(inputData, N_best: N_best, counts: (diff.deleted, diff.addedCount), previousResult: (inputData: previousInputData, nodes: nodes), needTypoCorrection: needTypoCorrection)
self.previousInputData = inputData
return result
}
fatalError("\(#function): ここに到達することは想定されていません。")
debug("\(#function): 最後尾文字置換用の関数を呼びます、差分は\(diff)")
let result = converter.kana2lattice_changed(inputData, N_best: N_best, counts: (diff.deleted, diff.addedCount), previousResult: (inputData: previousInputData, nodes: nodes), needTypoCorrection: needTypoCorrection)
self.previousInputData = inputData
return result
}
public func getAppropriateActions(_ candidate: Candidate) -> [CompleteAction] {