mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-08-22 15:05:26 +00:00
refactor: 最適化に寄与しないspecializationを削除
This commit is contained in:
@ -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)で求めたノードにつながるようにregisterして、N_bestを求めていく。
|
||||
///
|
||||
/// (3)(1)のregisterされた結果をresultノードに追加していく。この際EOSとの連接コストを計算しておく。
|
||||
///
|
||||
/// (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)
|
||||
// nodeの持っている全てのprevnodeに対して
|
||||
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()
|
||||
}
|
||||
// removeしてからinsertした方が速い (insertはO(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)
|
||||
}
|
||||
}
|
@ -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)で求めたノードにつながるようにregisterして、N_bestを求めていく。
|
||||
///
|
||||
/// (3)(1)のregisterされた結果をresultノードに追加していく。この際EOSとの連接コストを計算しておく。
|
||||
///
|
||||
/// (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)
|
||||
// nodeの持っている全てのprevnodeに対して
|
||||
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()
|
||||
}
|
||||
// removeしてからinsertした方が速い (insertはO(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)
|
||||
// nodeの持っている全てのprevnodeに対して
|
||||
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()
|
||||
}
|
||||
// removeしてからinsertした方が速い (insertはO(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)
|
||||
}
|
||||
}
|
@ -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] {
|
||||
|
Reference in New Issue
Block a user