summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/src/main/kotlin/dev/m1sk9/lunaticChat/engine/converter/KanaConverter.kt69
-rw-r--r--platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/RomanjiConverter.kt13
2 files changed, 79 insertions, 3 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
diff --git a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/RomanjiConverter.kt b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/RomanjiConverter.kt
index 9e54f24..c961af8 100644
--- a/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/RomanjiConverter.kt
+++ b/platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/RomanjiConverter.kt
@@ -48,11 +48,18 @@ class RomanjiConverter(
continue
}
+ // Pre-validate: Check if the word is valid romaji before attempting conversion
+ // This prevents partial conversion of English words (e.g., "This" -> "てぃs")
+ if (!KanaConverter.isValidRomaji(word)) {
+ if (debugMode) {
+ logger.info("Word is not valid romaji, keeping original: $word")
+ }
+ results.add(word)
+ continue
+ }
+
// Step 1: Romanji -> Hiragana
val hiragana = KanaConverter.toHiragana(word)
- if (debugMode) {
- logger.info("Romanji -> Hiragana: $word -> $hiragana")
- }
// Step 2: Hiragana -> Kanji/Kana
val converted =