refactor: 最適化に寄与しないspecializationを削除

This commit is contained in:
Miwa / Ensan
2025-07-03 01:14:28 +09:00
parent 328af40c7e
commit 2632e39aa0
3 changed files with 10 additions and 291 deletions

View File

@ -1,111 +0,0 @@
//
// 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)
}
}

View File

@ -1,155 +0,0 @@
//
// afterCharacterAdded.swift
// Keyboard
//
// Created by ensan on 2020/09/14.
// Copyright © 2020 ensan. All rights reserved.
//
import Foundation
import SwiftUtils
extension Kana2Kanji {
/// ,
/// - Parameters:
/// - inputData: InputData
/// - N_best: N_best
/// - addedCount:
/// - previousResult:
/// - Returns:
/// -
/// ###
/// (0)
///
/// (1)
///
/// (2)(1)registerN_best
///
/// (3)(1)registerresultEOS
///
/// (4)
func kana2lattice_added(_ inputData: ComposingText, N_best: Int, addedCount: Int, previousResult: (inputData: ComposingText, nodes: Nodes), needTypoCorrection: Bool) -> (result: LatticeNode, nodes: Nodes) {
debug("\(addedCount)文字追加。追加されたのは「\(inputData.input.suffix(addedCount))")
if addedCount == 1 {
return kana2lattice_addedLast(inputData, N_best: N_best, previousResult: previousResult, needTypoCorrection: needTypoCorrection)
}
// (0)
var nodes = previousResult.nodes
let count = inputData.input.count
// (1)
let addedNodes: [[LatticeNode]] = (.zero ..< count).map {(i: Int) in
self.dicdataStore.getLOUDSDataInRange(
inputData: inputData,
from: i,
toIndexRange: (max(previousResult.inputData.input.count, i) ..< max(previousResult.inputData.input.count, min(count, i + self.dicdataStore.maxlength + 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
assert(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
}
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 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)
}
}
}
}
}
for (index, nodeArray) in addedNodes.enumerated() {
if index < nodes.endIndex {
nodes[index].append(contentsOf: nodeArray)
} else {
nodes.append(nodeArray)
}
}
return (result: result, nodes: nodes)
}
}

View File

@ -626,17 +626,17 @@ import EfficientNGram
#if os(iOS)
let needTypoCorrection = true
#else
let needTypoCorrection = false
let needTypoCorrection = true
#endif
guard let previousInputData else {
debug("convertToLattice: 新規計算用の関数を呼びますA")
debug("\(#function): 新規計算用の関数を呼びますA")
let result = converter.kana2lattice_all(inputData, N_best: N_best, needTypoCorrection: needTypoCorrection)
self.previousInputData = inputData
return result
}
debug("convertToLattice: before \(previousInputData) after \(inputData)")
debug("\(#function): before \(previousInputData) after \(inputData)")
//
if previousInputData == inputData {
@ -647,7 +647,7 @@ import EfficientNGram
//
if let completedData, previousInputData.inputHasSuffix(inputOf: inputData) {
debug("convertToLattice: 文節確定用の関数を呼びます、確定された文節は\(completedData)")
debug("\(#function): 文節確定用の関数を呼びます、確定された文節は\(completedData)")
let result = converter.kana2lattice_afterComplete(inputData, completedData: completedData, N_best: N_best, previousResult: (inputData: previousInputData, nodes: nodes), needTypoCorrection: needTypoCorrection)
self.previousInputData = inputData
self.completedData = nil
@ -659,37 +659,22 @@ import EfficientNGram
let diff = inputData.differenceSuffix(to: previousInputData)
//
//
if diff.deleted > 0 && diff.addedCount == 0 {
debug("convertToLattice: 最後尾削除用の関数を呼びます, 消した文字数は\(diff.deleted)")
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.deleted > 0 {
debug("convertToLattice: 最後尾文字置換用の関数を呼びます、差分は\(diff)")
//
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
}
// 1
if diff.deleted == 0 && diff.addedCount != 0 {
debug("convertToLattice: 最後尾追加用の関数を呼びます、追加文字数は\(diff.addedCount)")
let result = converter.kana2lattice_added(inputData, N_best: N_best, addedCount: diff.addedCount, previousResult: (inputData: previousInputData, nodes: nodes), needTypoCorrection: needTypoCorrection)
self.previousInputData = inputData
return result
}
//
if true {
debug("convertToLattice: 新規計算用の関数を呼びますB")
let result = converter.kana2lattice_all(inputData, N_best: N_best, needTypoCorrection: needTypoCorrection)
self.previousInputData = inputData
return result
}
fatalError("\(#function): ここに到達することは想定されていません。")
}
public func getAppropriateActions(_ candidate: Candidate) -> [CompleteAction] {