mirror of
https://github.com/mii443/AzooKeyKanaKanjiConverter.git
synced 2025-08-22 23:15:25 +00:00
refactor: Roman2Kana関連の特定テーブル前提の処理を削除 (#225)
* refactor: possibleNextsを手動で列挙せず、自動で列挙できるようにした * refactor: remove table-specific implementation
This commit is contained in:
@ -1028,58 +1028,14 @@ public final class DicdataStore {
|
|||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
static let possibleNexts: [String: [String]] = [
|
static let possibleNexts: [String: [String]] = {
|
||||||
"x": ["ァ", "ィ", "ゥ", "ェ", "ォ", "ッ", "ャ", "ュ", "ョ", "ヮ"],
|
var results: [String: [String]] = [:]
|
||||||
"l": ["ァ", "ィ", "ゥ", "ェ", "ォ", "ッ", "ャ", "ュ", "ョ", "ヮ"],
|
for (key, value) in Roman2Kana.katakanaChanges {
|
||||||
"xt": ["ッ"],
|
for prefixCount in 0 ..< key.count where 0 < prefixCount {
|
||||||
"lt": ["ッ"],
|
let prefix = String(key.prefix(prefixCount))
|
||||||
"xts": ["ッ"],
|
results[prefix, default: []].append(value)
|
||||||
"lts": ["ッ"],
|
}
|
||||||
"xy": ["ャ", "ュ", "ョ"],
|
}
|
||||||
"ly": ["ャ", "ュ", "ョ"],
|
return results
|
||||||
"xw": ["ヮ"],
|
}()
|
||||||
"lw": ["ヮ"],
|
|
||||||
"v": ["ヴ"],
|
|
||||||
"k": ["カ", "キ", "ク", "ケ", "コ"],
|
|
||||||
"q": ["クァ", "クィ", "クゥ", "クェ", "クォ"],
|
|
||||||
"qy": ["クャ", "クィ", "クュ", "クェ", "クョ"],
|
|
||||||
"qw": ["クヮ", "クィ", "クゥ", "クェ", "クォ"],
|
|
||||||
"ky": ["キャ", "キィ", "キュ", "キェ", "キョ"],
|
|
||||||
"g": ["ガ", "ギ", "グ", "ゲ", "ゴ"],
|
|
||||||
"gy": ["ギャ", "ギィ", "ギュ", "ギェ", "ギョ"],
|
|
||||||
"s": ["サ", "シ", "ス", "セ", "ソ"],
|
|
||||||
"sy": ["シャ", "シィ", "シュ", "シェ", "ショ"],
|
|
||||||
"sh": ["シャ", "シィ", "シュ", "シェ", "ショ"],
|
|
||||||
"z": ["ザ", "ジ", "ズ", "ゼ", "ゾ"],
|
|
||||||
"zy": ["ジャ", "ジィ", "ジュ", "ジェ", "ジョ"],
|
|
||||||
"j": ["ジ"],
|
|
||||||
"t": ["タ", "チ", "ツ", "テ", "ト"],
|
|
||||||
"ty": ["チャ", "チィ", "チュ", "チェ", "チョ"],
|
|
||||||
"ts": ["ツ"],
|
|
||||||
"th": ["テャ", "ティ", "テュ", "テェ", "テョ"],
|
|
||||||
"tw": ["トァ", "トィ", "トゥ", "トェ", "トォ"],
|
|
||||||
"cy": ["チャ", "チィ", "チュ", "チェ", "チョ"],
|
|
||||||
"ch": ["チ"],
|
|
||||||
"d": ["ダ", "ヂ", "ヅ", "デ", "ド"],
|
|
||||||
"dy": ["ヂャ", "ヂィ", "ヂュ", "ヂェ", "ヂョ"],
|
|
||||||
"dh": ["デャ", "ディ", "デュ", "デェ", "デョ"],
|
|
||||||
"dw": ["ドァ", "ドィ", "ドゥ", "ドェ", "ドォ"],
|
|
||||||
"n": ["ナ", "ニ", "ヌ", "ネ", "ノ", "ン"],
|
|
||||||
"ny": ["ニャ", "ニィ", "ニュ", "ニェ", "ニョ"],
|
|
||||||
"h": ["ハ", "ヒ", "フ", "ヘ", "ホ"],
|
|
||||||
"hy": ["ヒャ", "ヒィ", "ヒュ", "ヒェ", "ヒョ"],
|
|
||||||
"hw": ["ファ", "フィ", "フェ", "フォ"],
|
|
||||||
"f": ["フ"],
|
|
||||||
"b": ["バ", "ビ", "ブ", "ベ", "ボ"],
|
|
||||||
"by": ["ビャ", "ビィ", "ビュ", "ビェ", "ビョ"],
|
|
||||||
"p": ["パ", "ピ", "プ", "ペ", "ポ"],
|
|
||||||
"py": ["ピャ", "ピィ", "ピュ", "ピェ", "ピョ"],
|
|
||||||
"m": ["マ", "ミ", "ム", "メ", "モ"],
|
|
||||||
"my": ["ミャ", "ミィ", "ミュ", "ミェ", "ミョ"],
|
|
||||||
"y": ["ヤ", "ユ", "イェ", "ヨ"],
|
|
||||||
"r": ["ラ", "リ", "ル", "レ", "ロ"],
|
|
||||||
"ry": ["リャ", "リィ", "リュ", "リェ", "リョ"],
|
|
||||||
"w": ["ワ", "ウィ", "ウェ", "ヲ"],
|
|
||||||
"wy": ["ヰ", "ヱ"]
|
|
||||||
]
|
|
||||||
}
|
}
|
||||||
|
@ -283,6 +283,45 @@ enum Roman2Kana {
|
|||||||
"whu": "う",
|
"whu": "う",
|
||||||
"whe": "うぇ",
|
"whe": "うぇ",
|
||||||
"who": "うぉ",
|
"who": "うぉ",
|
||||||
|
"bb": "っb",
|
||||||
|
"cc": "っc",
|
||||||
|
"dd": "っd",
|
||||||
|
"ff": "っf",
|
||||||
|
"gg": "っg",
|
||||||
|
"hh": "っh",
|
||||||
|
"jj": "っj",
|
||||||
|
"kk": "っk",
|
||||||
|
"ll": "っl",
|
||||||
|
"mm": "っm",
|
||||||
|
"pp": "っp",
|
||||||
|
"qq": "っq",
|
||||||
|
"rr": "っr",
|
||||||
|
"ss": "っs",
|
||||||
|
"tt": "っt",
|
||||||
|
"vv": "っv",
|
||||||
|
"ww": "っw",
|
||||||
|
"xx": "っx",
|
||||||
|
"yy": "っy",
|
||||||
|
"zz": "っz",
|
||||||
|
"nb": "んb",
|
||||||
|
"nc": "んc",
|
||||||
|
"nd": "んd",
|
||||||
|
"nf": "んf",
|
||||||
|
"ng": "んg",
|
||||||
|
"nh": "んh",
|
||||||
|
"nj": "んj",
|
||||||
|
"nk": "んk",
|
||||||
|
"nl": "んl",
|
||||||
|
"nm": "んm",
|
||||||
|
"np": "んp",
|
||||||
|
"nq": "んq",
|
||||||
|
"nr": "んr",
|
||||||
|
"ns": "んs",
|
||||||
|
"nt": "んt",
|
||||||
|
"nv": "んv",
|
||||||
|
"nw": "んw",
|
||||||
|
"nx": "んx",
|
||||||
|
"nz": "んz",
|
||||||
"xn": "ん",
|
"xn": "ん",
|
||||||
"zh": "←",
|
"zh": "←",
|
||||||
"zj": "↓",
|
"zj": "↓",
|
||||||
@ -290,28 +329,20 @@ enum Roman2Kana {
|
|||||||
"zl": "→"
|
"zl": "→"
|
||||||
].map {(Array($0.key), Array($0.value))})
|
].map {(Array($0.key), Array($0.value))})
|
||||||
|
|
||||||
static func toHiragana(currentText: [Character], added: Character) -> [Character] {
|
static let maxKeyCount = hiraganaChanges.lazy.map { $0.key.count }.max() ?? 0
|
||||||
let last_3 = currentText.suffix(3)
|
|
||||||
if let kana = Roman2Kana.hiraganaChanges[last_3 + [added]] {
|
|
||||||
return currentText.prefix(currentText.count - last_3.count) + kana
|
|
||||||
}
|
|
||||||
let last_2 = currentText.suffix(2)
|
|
||||||
if let kana = Roman2Kana.hiraganaChanges[last_2 + [added]] {
|
|
||||||
return currentText.prefix(currentText.count - last_2.count) + kana
|
|
||||||
}
|
|
||||||
let last_1 = currentText.suffix(1)
|
|
||||||
if let kana = Roman2Kana.hiraganaChanges[last_1 + [added]] {
|
|
||||||
return currentText.prefix(currentText.count - last_1.count) + kana
|
|
||||||
}
|
|
||||||
if last_1 == [added] && String(added).onlyRomanAlphabet {
|
|
||||||
return currentText.prefix(currentText.count - last_1.count) + ["っ", added]
|
|
||||||
}
|
|
||||||
if last_1 == ["n"] && added != "y"{
|
|
||||||
return currentText.prefix(currentText.count - last_1.count) + ["ん", added]
|
|
||||||
}
|
|
||||||
|
|
||||||
if let kana = Roman2Kana.hiraganaChanges[[added]] {
|
static func toHiragana(currentText: [Character], added: Character) -> [Character] {
|
||||||
return currentText + kana
|
for n in (0 ..< maxKeyCount).reversed() {
|
||||||
|
if n == 0 {
|
||||||
|
if let kana = Roman2Kana.hiraganaChanges[[added]] {
|
||||||
|
return currentText + kana
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
let last = currentText.suffix(n)
|
||||||
|
if let kana = Roman2Kana.hiraganaChanges[last + [added]] {
|
||||||
|
return currentText.prefix(currentText.count - last.count) + kana
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return currentText + [added]
|
return currentText + [added]
|
||||||
}
|
}
|
||||||
|
26
Tests/KanaKanjiConverterModuleTests/Roman2KanaTests.swift
Normal file
26
Tests/KanaKanjiConverterModuleTests/Roman2KanaTests.swift
Normal file
@ -0,0 +1,26 @@
|
|||||||
|
@testable import KanaKanjiConverterModule
|
||||||
|
import XCTest
|
||||||
|
|
||||||
|
final class Roman2KanaTests: XCTestCase {
|
||||||
|
func testToHiragana() throws {
|
||||||
|
// xtsu -> っ
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array(""), added: "x"), Array("x"))
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array("x"), added: "t"), Array("xt"))
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array("xt"), added: "s"), Array("xts"))
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array("xts"), added: "u"), Array("っ"))
|
||||||
|
|
||||||
|
// kanto -> かんと
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array(""), added: "k"), Array("k"))
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array("k"), added: "a"), Array("か"))
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array("か"), added: "n"), Array("かn"))
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array("かn"), added: "t"), Array("かんt"))
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array("かんt"), added: "o"), Array("かんと"))
|
||||||
|
|
||||||
|
// zl -> →
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array(""), added: "z"), Array("z"))
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array("z"), added: "l"), Array("→"))
|
||||||
|
|
||||||
|
// TT -> TT
|
||||||
|
XCTAssertEqual(Roman2Kana.toHiragana(currentText: Array("T"), added: "T"), Array("TT"))
|
||||||
|
}
|
||||||
|
}
|
@ -329,4 +329,11 @@ final class DicdataStoreTests: XCTestCase {
|
|||||||
XCTAssertEqual(dynamicUserDictResult?.data.metadata, .isFromUserDictionary)
|
XCTAssertEqual(dynamicUserDictResult?.data.metadata, .isFromUserDictionary)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func testPossibleNexts() throws {
|
||||||
|
let possibleNexts = DicdataStore.possibleNexts
|
||||||
|
XCTAssertEqual(Set(possibleNexts["f", default: []]).symmetricDifference(["ファ", "フィ", "フ", "フェ", "フォ", "フャ", "フュ", "フョ", "フゥ", "ッf"]), [])
|
||||||
|
XCTAssertEqual(Set(possibleNexts["xy", default: []]).symmetricDifference(["ャ", "ョ", "ュ"]), [])
|
||||||
|
XCTAssertEqual(possibleNexts["", default: []], [])
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user