mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-08-22 15:05:26 +00:00
cleanupのロジックを修正
This commit is contained in:
@ -167,7 +167,7 @@ struct InputGraph {
|
||||
}
|
||||
}
|
||||
|
||||
private mutating func clean() {
|
||||
consuming func clean() -> Self {
|
||||
var newGraph = Self(nodes: [])
|
||||
var indices: [(nodeIndex: Int, fromIndex: Int?)] = [(0, nil)]
|
||||
var processedNodeIndices: [Int: Int] = [:]
|
||||
@ -191,7 +191,7 @@ struct InputGraph {
|
||||
}
|
||||
processedNodeIndices[nodeIndex] = newIndex
|
||||
}
|
||||
self = newGraph
|
||||
return newGraph
|
||||
}
|
||||
|
||||
static func build(input: CorrectGraph) -> Self {
|
||||
@ -222,9 +222,6 @@ struct InputGraph {
|
||||
}
|
||||
nodeIndices.append(contentsOf: input.allowedNextIndex[nodeIndex, default: IndexSet()])
|
||||
}
|
||||
|
||||
// invalidateしたnodeを削除する
|
||||
inputGraph.clean()
|
||||
return inputGraph
|
||||
}
|
||||
}
|
||||
|
@ -17,7 +17,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "い", inputStyle: .direct),
|
||||
.init(character: "う", inputStyle: .direct)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(inputGraph.nodes.count, 4) // Root nodes
|
||||
}
|
||||
func testBuildSimpleDirectInput_あかう() throws {
|
||||
@ -26,7 +26,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "か", inputStyle: .direct),
|
||||
.init(character: "う", inputStyle: .direct)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(inputGraph.nodes.count, 5) // Root nodes
|
||||
}
|
||||
|
||||
@ -36,14 +36,22 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "い", inputStyle: .direct),
|
||||
.init(character: "か", inputStyle: .direct)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "た"}),
|
||||
.init(character: "た", inputElementsRange: .range(0, 1), correction: .none)
|
||||
)
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "だ"}),
|
||||
.init(character: "だ", inputElementsRange: .range(0, 1), correction: .typo)
|
||||
)
|
||||
}
|
||||
|
||||
func testBuildSimpleRoman2KanaInput_1文字だけ() throws {
|
||||
let correctGraph = CorrectGraph.build(input: [
|
||||
.init(character: "i", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "い"}),
|
||||
.init(character: "い", inputElementsRange: .range(0, 1), correction: .none)
|
||||
@ -54,7 +62,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "i", inputStyle: .roman2kana),
|
||||
.init(character: "t", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "い"}),
|
||||
.init(character: "い", inputElementsRange: .range(0, 1), correction: .none)
|
||||
@ -70,7 +78,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "t", inputStyle: .roman2kana),
|
||||
.init(character: "a", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "い"}),
|
||||
.init(character: "い", inputElementsRange: .range(0, 1), correction: .none)
|
||||
@ -87,7 +95,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "t", inputStyle: .roman2kana),
|
||||
.init(character: "s", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "し"}),
|
||||
.init(character: "し", inputElementsRange: .range(0, 2), correction: .none)
|
||||
@ -107,7 +115,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "t", inputStyle: .roman2kana),
|
||||
.init(character: "s", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "い"}),
|
||||
.init(character: "い", inputElementsRange: .range(0, 1), correction: .none)
|
||||
@ -136,7 +144,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "s", inputStyle: .roman2kana),
|
||||
.init(character: "a", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "い"}),
|
||||
.init(character: "い", inputElementsRange: .range(0, 1), correction: .none)
|
||||
@ -180,7 +188,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "o", inputStyle: .roman2kana),
|
||||
.init(character: "u", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "よ"}),
|
||||
.init(character: "よ", inputElementsRange: .range(0, 2), correction: .none)
|
||||
@ -209,7 +217,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "t", inputStyle: .roman2kana),
|
||||
.init(character: "t", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "っ"}),
|
||||
.init(character: "っ", inputElementsRange: .startIndex(0), correction: .none)
|
||||
@ -228,7 +236,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "t", inputStyle: .roman2kana),
|
||||
.init(character: "a", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "っ"}),
|
||||
.init(character: "っ", inputElementsRange: .startIndex(0), correction: .none)
|
||||
@ -244,7 +252,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "t", inputStyle: .roman2kana),
|
||||
.init(character: "a", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "ん"}),
|
||||
.init(character: "ん", inputElementsRange: .startIndex(0), correction: .none)
|
||||
@ -261,7 +269,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "t", inputStyle: .roman2kana),
|
||||
.init(character: "a", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "い"}),
|
||||
.init(character: "い", inputElementsRange: .range(0, 1), correction: .none)
|
||||
@ -284,7 +292,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "s", inputStyle: .roman2kana),
|
||||
.init(character: "i", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "し"}),
|
||||
.init(character: "し", inputElementsRange: .range(0, 2), correction: .none)
|
||||
@ -306,7 +314,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "t", inputStyle: .roman2kana),
|
||||
.init(character: "s", inputStyle: .roman2kana)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "っ" && $0.correction == .none}),
|
||||
.init(character: "っ", inputElementsRange: .startIndex(0), correction: .none)
|
||||
@ -330,7 +338,7 @@ final class InputGraphTests: XCTestCase {
|
||||
.init(character: "t", inputStyle: .roman2kana),
|
||||
.init(character: "s", inputStyle: .direct)
|
||||
])
|
||||
let inputGraph = InputGraph.build(input: correctGraph)
|
||||
let inputGraph = InputGraph.build(input: correctGraph).clean()
|
||||
XCTAssertEqual(
|
||||
inputGraph.nodes.first(where: {$0.character == "t"}),
|
||||
.init(character: "t", inputElementsRange: .range(0, 1), correction: .none)
|
||||
|
@ -27,7 +27,9 @@ struct LookupGraph {
|
||||
/// 許可されたprevIndex
|
||||
var allowedPrevIndex: [Int: IndexSet] = [:]
|
||||
|
||||
static func build(input: InputGraph, character2CharId: (Character) -> UInt8) -> Self {
|
||||
static func build(input: consuming InputGraph, character2CharId: (Character) -> UInt8) -> Self {
|
||||
// 不要なノードを削除し、計算量を減らす
|
||||
let input = input.clean()
|
||||
let nodes = input.nodes.map {
|
||||
Node(character: $0.character, charId: character2CharId($0.character), inputElementsRange: $0.inputElementsRange, correction: $0.correction)
|
||||
}
|
||||
|
Reference in New Issue
Block a user