diff options
| author | Sho Sakuma <me@m1sk9.dev> | 2026-02-07 18:49:07 +0900 |
|---|---|---|
| committer | Sho Sakuma <me@m1sk9.dev> | 2026-02-07 18:49:07 +0900 |
| commit | d77f7a5fe06113cd472e907643f1172a148d052a (patch) | |
| tree | 856ec712378ad0308a789f9ae0b3df14db5924c1 /engine/src/main | |
| parent | d70a6bd33824e92731d6369debb5eba613e1e7ed (diff) | |
| download | LunaticChat-d77f7a5fe06113cd472e907643f1172a148d052a.tar.gz LunaticChat-d77f7a5fe06113cd472e907643f1172a148d052a.tar.bz2 LunaticChat-d77f7a5fe06113cd472e907643f1172a148d052a.zip | |
fix: Fix kana converter valid romaji
Diffstat (limited to 'engine/src/main')
| -rw-r--r-- | engine/src/main/kotlin/dev/m1sk9/lunaticChat/engine/converter/KanaConverter.kt | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/engine/src/main/kotlin/dev/m1sk9/lunaticChat/engine/converter/KanaConverter.kt b/engine/src/main/kotlin/dev/m1sk9/lunaticChat/engine/converter/KanaConverter.kt index 1b3c67d..24e3560 100644 --- a/engine/src/main/kotlin/dev/m1sk9/lunaticChat/engine/converter/KanaConverter.kt +++ b/engine/src/main/kotlin/dev/m1sk9/lunaticChat/engine/converter/KanaConverter.kt @@ -254,6 +254,75 @@ object KanaConverter { } /** + * Checks if the input can be fully converted to hiragana without any remaining alphabetic characters. + * This validates that the input is valid romaji before attempting conversion. + * + * @param input The text to validate + * @return true if the input is valid romaji, false otherwise + */ + fun isValidRomaji(input: String): Boolean { + val lowerInput = input.lowercase() + var i = 0 + + while (i < lowerInput.length) { + val char = lowerInput[i] + + // Non-alphabetic characters are allowed (spaces, numbers, symbols) + if (char !in 'a'..'z') { + i++ + continue + } + + // Check for double consonant (valid in romaji for っ) + if (i + 1 < lowerInput.length) { + val next = lowerInput[i + 1] + if (char == next && char in "bcdfghjklmpqrstvwxyz") { + i++ + continue + } + } + + // Try to find the longest match in the trie + var node: TrieNode = romanjiTrie + var matchLength = 0 + var j = i + + while (j < lowerInput.length && lowerInput[j] in 'a'..'z') { + node = + when (node) { + is TrieNode.Branch -> { + if (node.value != null) { + matchLength = j - i + } + node.children[lowerInput[j]] ?: break + } + is TrieNode.Leaf -> { + matchLength = j - i + break + } + } + j++ + } + + // Check for terminal match + if (node is TrieNode.Leaf) { + matchLength = j - i + } else if (node is TrieNode.Branch && node.value != null) { + matchLength = j - i + } + + // If no match found, this character cannot be converted - not valid romaji + if (matchLength == 0) { + return false + } + + i += matchLength + } + + return true + } + + /** * Converts romanji text to hiragana. * * @param input The romanji text to convert |
