mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-08-22 15:05:26 +00:00
Merge pull request #220 from azooKey/refactor/unify_delete_and_replace
refactor: 末尾置換と末尾削除を統合
This commit is contained in:
@ -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)まず、計算済みノードを捜査して、新しい文末につながるものをresultにregisterしていく。
|
||||
/// 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)
|
||||
}
|
||||
|
||||
}
|
@ -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)
|
||||
// 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)
|
||||
}
|
||||
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)
|
||||
// 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() 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)
|
||||
}
|
||||
|
||||
|
@ -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] {
|
||||
|
Reference in New Issue
Block a user