Skip to content

Commit

Permalink
[Experimental] 削除操作についてもグラフベースに変更 (#79)
Browse files Browse the repository at this point in the history
* switch to ComposingTextV2

* delete操作を実装
  • Loading branch information
ensan-hcl authored Apr 8, 2024
1 parent afabe8a commit a5e794d
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 158 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ struct ComposingTextV2: Hashable, Sendable {
self.convertTarget = Self.buildConvertTarget(input)
}

mutating func append(_ string: String, inputStyle: InputGraphInputStyle.ID) {
self.input.append(contentsOf: string.map {.init(value: $0, inputStyle: inputStyle)})
self.convertTarget = Self.buildConvertTarget(input)
}

mutating func removeLast(_ k: Int = 1) {
let rest = self.convertTarget.dropLast(k)
typealias Item = (value: String, inputStyle: InputGraphInputStyle.ID)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,16 +86,16 @@ struct CorrectGraph {
}

@discardableResult
mutating func update(with item: ComposingText.InputElement, index: Int, input: [ComposingText.InputElement]) -> IndexSet {
mutating func update(with item: ComposingTextV2.InputElement, index: Int, input: [ComposingTextV2.InputElement]) -> IndexSet {
var insertedIndexSet = IndexSet()
// 訂正のない候補を追加
do {
let nodeIndex = self.insert(
Node(
inputElementsRange: .range(index, index + 1),
inputStyle: InputGraphInputStyle(from: input[index].inputStyle).id,
inputStyle: input[index].inputStyle,
correction: .none,
value: item.character
value: item.value
),
nextTo: self.inputIndexToEndNodeIndices[index, default: IndexSet()]
)
Expand Down Expand Up @@ -123,7 +123,7 @@ struct CorrectGraph {
guard cInputStyleId.isCompatible(with: inputStyleId) else {
continue
}
if let nNode = cNode.find(key: input[cIndex].character) {
if let nNode = cNode.find(key: input[cIndex].value) {
stack.append((nNode, cIndex - 1, cRouteCount + 1, inputStyleId))
for value in nNode.value {
if value.isEmpty {
Expand Down Expand Up @@ -157,7 +157,7 @@ struct CorrectGraph {
return insertedIndexSet
}

static func build(input: [ComposingText.InputElement]) -> Self {
static func build(input: [ComposingTextV2.InputElement]) -> Self {
var correctGraph = Self()
for (index, item) in zip(input.indices, input) {
correctGraph.update(with: item, index: index, input: input)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import XCTest
final class CorrectGraphTests: XCTestCase {
func testBuildSimpleDirectInput() throws {
let graph = CorrectGraph.build(input: [
.init(character: "", inputStyle: .direct)
.init(value: "", inputStyle: .systemFlickDirect)
])
XCTAssertEqual(
graph.nodes.first(where: {$0.value == ""}),
Expand All @@ -22,7 +22,7 @@ final class CorrectGraphTests: XCTestCase {
}
func testBuildSimpleDirectInputWithTypo() throws {
let graph = CorrectGraph.build(input: [
.init(character: "", inputStyle: .direct)
.init(value: "", inputStyle: .systemFlickDirect)
])
XCTAssertEqual(
graph.nodes.first(where: {$0.value == ""}),
Expand All @@ -35,9 +35,9 @@ final class CorrectGraphTests: XCTestCase {
}
func testBuildMultipleDirectInputWithTypo() throws {
let graph = CorrectGraph.build(input: [
.init(character: "", inputStyle: .direct),
.init(character: "", inputStyle: .direct),
.init(character: "", inputStyle: .direct)
.init(value: "", inputStyle: .systemFlickDirect),
.init(value: "", inputStyle: .systemFlickDirect),
.init(value: "", inputStyle: .systemFlickDirect)
])
XCTAssertEqual(
graph.nodes.first(where: {$0.value == ""}),
Expand All @@ -59,8 +59,8 @@ final class CorrectGraphTests: XCTestCase {
}
func testBuildSimpleRomanInput() throws {
let graph = CorrectGraph.build(input: [
.init(character: "k", inputStyle: .roman2kana),
.init(character: "a", inputStyle: .roman2kana)
.init(value: "k", inputStyle: .systemRomanKana),
.init(value: "a", inputStyle: .systemRomanKana)
])
XCTAssertEqual(
graph.nodes.first(where: {$0.value == "k"}),
Expand All @@ -73,8 +73,8 @@ final class CorrectGraphTests: XCTestCase {
}
func testBuildSimpleRomanInputWithTypo() throws {
let graph = CorrectGraph.build(input: [
.init(character: "t", inputStyle: .roman2kana),
.init(character: "s", inputStyle: .roman2kana)
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "s", inputStyle: .systemRomanKana)
])
XCTAssertEqual(
graph.nodes.first(where: {$0.value == "t" && $0.inputElementsRange == .range(0, 1)}),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,28 +13,28 @@ import XCTest
final class InputGraphTests: XCTestCase {
func testBuildSimpleDirectInput() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "", inputStyle: .direct),
.init(character: "", inputStyle: .direct),
.init(character: "", inputStyle: .direct)
.init(value: "", inputStyle: .systemFlickDirect),
.init(value: "", inputStyle: .systemFlickDirect),
.init(value: "", inputStyle: .systemFlickDirect)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(inputGraph.nodes.count, 4) // Root nodes
}
func testBuildSimpleDirectInput_あかう() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "", inputStyle: .direct),
.init(character: "", inputStyle: .direct),
.init(character: "", inputStyle: .direct)
.init(value: "", inputStyle: .systemFlickDirect),
.init(value: "", inputStyle: .systemFlickDirect),
.init(value: "", inputStyle: .systemFlickDirect)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(inputGraph.nodes.count, 5) // Root nodes
}

func testBuildSimpleDirectInput_たいか() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "", inputStyle: .direct),
.init(character: "", inputStyle: .direct),
.init(character: "", inputStyle: .direct)
.init(value: "", inputStyle: .systemFlickDirect),
.init(value: "", inputStyle: .systemFlickDirect),
.init(value: "", inputStyle: .systemFlickDirect)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -49,7 +49,7 @@ final class InputGraphTests: XCTestCase {

func testBuildSimpleRoman2KanaInput_1文字だけ() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "i", inputStyle: .roman2kana)
.init(value: "i", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -59,8 +59,8 @@ final class InputGraphTests: XCTestCase {
}
func testBuildSimpleRoman2KanaInput_2文字_it() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "i", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana)
.init(value: "i", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -74,9 +74,9 @@ final class InputGraphTests: XCTestCase {
}
func testBuildSimpleRoman2KanaInput_3文字_ita() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "i", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "a", inputStyle: .roman2kana)
.init(value: "i", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "a", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -90,10 +90,10 @@ final class InputGraphTests: XCTestCase {
}
func testBuildSimpleRoman2KanaInput_4文字_sits() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "s", inputStyle: .roman2kana),
.init(character: "i", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "s", inputStyle: .roman2kana)
.init(value: "s", inputStyle: .systemRomanKana),
.init(value: "i", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "s", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -111,9 +111,9 @@ final class InputGraphTests: XCTestCase {
}
func testBuildSimpleRoman2KanaInput_3文字_its() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "i", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "s", inputStyle: .roman2kana)
.init(value: "i", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "s", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -139,10 +139,10 @@ final class InputGraphTests: XCTestCase {
}
func testBuildSimpleRoman2KanaInput_4文字_itsa() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "i", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "s", inputStyle: .roman2kana),
.init(character: "a", inputStyle: .roman2kana)
.init(value: "i", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "s", inputStyle: .systemRomanKana),
.init(value: "a", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand Down Expand Up @@ -183,13 +183,13 @@ final class InputGraphTests: XCTestCase {

func testBuildSimpleRoman2KanaInput_7文字_youshou() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "y", inputStyle: .roman2kana),
.init(character: "o", inputStyle: .roman2kana),
.init(character: "u", inputStyle: .roman2kana),
.init(character: "s", inputStyle: .roman2kana),
.init(character: "h", inputStyle: .roman2kana),
.init(character: "o", inputStyle: .roman2kana),
.init(character: "u", inputStyle: .roman2kana)
.init(value: "y", inputStyle: .systemRomanKana),
.init(value: "o", inputStyle: .systemRomanKana),
.init(value: "u", inputStyle: .systemRomanKana),
.init(value: "s", inputStyle: .systemRomanKana),
.init(value: "h", inputStyle: .systemRomanKana),
.init(value: "o", inputStyle: .systemRomanKana),
.init(value: "u", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand Down Expand Up @@ -217,8 +217,8 @@ final class InputGraphTests: XCTestCase {

func testBuildSimpleRoman2KanaInput_2文字_tt() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "t", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana)
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -240,9 +240,9 @@ final class InputGraphTests: XCTestCase {
}
func testBuildSimpleRoman2KanaInput_3文字_tta() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "t", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "a", inputStyle: .roman2kana)
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "a", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -258,9 +258,9 @@ final class InputGraphTests: XCTestCase {
}
func testBuildSimpleRoman2KanaInput_3文字_nta() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "n", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "a", inputStyle: .roman2kana)
.init(value: "n", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "a", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -274,10 +274,10 @@ final class InputGraphTests: XCTestCase {
}
func testBuildSimpleRoman2KanaInput_4文字_itta() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "i", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "a", inputStyle: .roman2kana)
.init(value: "i", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "a", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -296,11 +296,11 @@ final class InputGraphTests: XCTestCase {

func testBuildSimpleRoman2KanaInput_5文字_sitsi() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "s", inputStyle: .roman2kana),
.init(character: "i", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "s", inputStyle: .roman2kana),
.init(character: "i", inputStyle: .roman2kana)
.init(value: "s", inputStyle: .systemRomanKana),
.init(value: "i", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "s", inputStyle: .systemRomanKana),
.init(value: "i", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -320,9 +320,9 @@ final class InputGraphTests: XCTestCase {

func testBuildSimpleRoman2KanaInput_3文字_tts() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "t", inputStyle: .roman2kana),
.init(character: "t", inputStyle: .roman2kana),
.init(character: "s", inputStyle: .roman2kana)
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "s", inputStyle: .systemRomanKana)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand All @@ -346,10 +346,10 @@ final class InputGraphTests: XCTestCase {
func testBuildSimpleRoman2KanaInput_4文字_tysa() throws {
// ちゃあ/tyさ
let correctGraph = CorrectGraph.build(input: [
.init(character: "t", inputStyle: .roman2kana),
.init(character: "y", inputStyle: .roman2kana),
.init(character: "s", inputStyle: .roman2kana),
.init(character: "a", inputStyle: .roman2kana)
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "y", inputStyle: .systemRomanKana),
.init(value: "s", inputStyle: .systemRomanKana),
.init(value: "a", inputStyle: .systemRomanKana)
])
// cleanで壊れる
let inputGraph = InputGraph.build(input: correctGraph).clean()
Expand Down Expand Up @@ -381,8 +381,8 @@ final class InputGraphTests: XCTestCase {

func testBuildMixedInput_2文字_ts() throws {
let correctGraph = CorrectGraph.build(input: [
.init(character: "t", inputStyle: .roman2kana),
.init(character: "s", inputStyle: .direct)
.init(value: "t", inputStyle: .systemRomanKana),
.init(value: "s", inputStyle: .systemFlickDirect)
])
let inputGraph = InputGraph.build(input: correctGraph).clean()
XCTAssertEqual(
Expand Down
Loading

0 comments on commit a5e794d

Please sign in to comment.