mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-12-03 11:08:33 +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,6 +37,15 @@ extension Kana2Kanji {
|
|||||||
while nodes.last?.isEmpty ?? false {
|
while nodes.last?.isEmpty ?? false {
|
||||||
nodes.removeLast()
|
nodes.removeLast()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
let terminalNodes: Nodes
|
||||||
|
if counts.added == 0 {
|
||||||
|
terminalNodes = nodes.map {
|
||||||
|
$0.filter {
|
||||||
|
$0.inputRange.endIndex == count
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
// (2)
|
// (2)
|
||||||
let addedNodes: [[LatticeNode]] = (0..<count).map {(i: Int) in
|
let addedNodes: [[LatticeNode]] = (0..<count).map {(i: Int) in
|
||||||
self.dicdataStore.getLOUDSDataInRange(inputData: inputData, from: i, toIndexRange: max(commonCount, i) ..< count, needTypoCorrection: needTypoCorrection)
|
self.dicdataStore.getLOUDSDataInRange(inputData: inputData, from: i, toIndexRange: max(commonCount, i) ..< count, needTypoCorrection: needTypoCorrection)
|
||||||
@@ -79,10 +88,20 @@ 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)
|
||||||
|
}
|
||||||
|
terminalNodes = addedNodes
|
||||||
|
}
|
||||||
|
|
||||||
// (3)
|
// (3)
|
||||||
|
// terminalNodesの各要素を結果ノードに接続する
|
||||||
let result = LatticeNode.EOSNode
|
let result = LatticeNode.EOSNode
|
||||||
for (i, nodes) in addedNodes.enumerated() {
|
|
||||||
|
for (i, nodes) in terminalNodes.enumerated() {
|
||||||
for node in nodes {
|
for node in nodes {
|
||||||
if node.prevs.isEmpty {
|
if node.prevs.isEmpty {
|
||||||
continue
|
continue
|
||||||
@@ -108,7 +127,7 @@ extension Kana2Kanji {
|
|||||||
result.prevs.append(newnode)
|
result.prevs.append(newnode)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
for nextnode in addedNodes[nextIndex] {
|
for nextnode in terminalNodes[nextIndex] {
|
||||||
// この関数はこの時点で呼び出して、後のnode.registered.isEmptyで最終的に弾くのが良い。
|
// この関数はこの時点で呼び出して、後のnode.registered.isEmptyで最終的に弾くのが良い。
|
||||||
if self.dicdataStore.shouldBeRemoved(data: nextnode.data) {
|
if self.dicdataStore.shouldBeRemoved(data: nextnode.data) {
|
||||||
continue
|
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)
|
return (result: result, nodes: nodes)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -661,23 +661,11 @@ import EfficientNGram
|
|||||||
|
|
||||||
let diff = inputData.differenceSuffix(to: previousInputData)
|
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)")
|
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)
|
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
|
self.previousInputData = inputData
|
||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
fatalError("\(#function): ここに到達することは想定されていません。")
|
|
||||||
}
|
|
||||||
|
|
||||||
public func getAppropriateActions(_ candidate: Candidate) -> [CompleteAction] {
|
public func getAppropriateActions(_ candidate: Candidate) -> [CompleteAction] {
|
||||||
if ["[]", "()", "{}", "〈〉", "〔〕", "()", "「」", "『』", "【】", "{}", "<>", "《》", "\"\"", "\'\'", "””"].contains(candidate.text) {
|
if ["[]", "()", "{}", "〈〉", "〔〕", "()", "「」", "『』", "【】", "{}", "<>", "《》", "\"\"", "\'\'", "””"].contains(candidate.text) {
|
||||||
|
|||||||
Reference in New Issue
Block a user