cleanupのロジックを修正

This commit is contained in:
Miwa / Ensan
2024-03-17 12:20:55 +09:00
parent 44cecf2242
commit e9142810cf
3 changed files with 30 additions and 23 deletions

View File

@ -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()])
}
// invalidatenode
inputGraph.clean()
return inputGraph
}
}

View File

@ -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)

View File

@ -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)
}