summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSho Sakuma <me@m1sk9.dev>2026-01-24 03:04:54 +0900
committerSho Sakuma <me@m1sk9.dev>2026-01-24 03:04:54 +0900
commit3b935ce99257587aaae85b8b09f0396111bc7e4c (patch)
treecc72c28651fe31d549f3e1b88fd93f82d0559c05
parentc650c58ab534208cde93f2d7038ceaf2291859a1 (diff)
downloadLunaticChat-3b935ce99257587aaae85b8b09f0396111bc7e4c.tar.gz
LunaticChat-3b935ce99257587aaae85b8b09f0396111bc7e4c.tar.bz2
LunaticChat-3b935ce99257587aaae85b8b09f0396111bc7e4c.zip
feat: Improve the cache strategy for kana conversion
-rw-r--r--platform-paper/src/main/kotlin/dev/m1sk9/lunaticChat/paper/converter/RomanjiConverter.kt55
1 files changed, 37 insertions, 18 deletions
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 8dc0d99..9e54f24 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
@@ -12,7 +12,8 @@ class RomanjiConverter(
) {
/**
* Converts the given romaji input to Japanese using the API client.
- * Utilizes a cache to store and retrieve previous conversion results.
+ * Utilizes a word-level cache to store and retrieve previous conversion results.
+ * Each word is cached separately to improve cache hit rate.
*
* Step 1: Romanji -> Hiragana (using KanaConverter)
* Step 2: Hiragana -> Kanji/Kana (using Google IME API)
@@ -29,27 +30,45 @@ class RomanjiConverter(
return null
}
- cache.get(input)?.let {
- return it
- }
+ val words = input.split(" ")
+ val results = mutableListOf<String>()
- // Step 1: Romanji -> Hiragana
- val hiragana = KanaConverter.toHiragana(input)
- if (debugMode) {
- logger.info("Romanji -> Hiragana: $input -> $hiragana")
- }
+ for (word in words) {
+ if (word.isEmpty()) {
+ continue
+ }
- // Step 2: Hiragana -> Kanji/Kana
- val result =
- try {
- apiClient.convert(hiragana)
- } catch (e: Exception) {
- logger.warning("Failed to convert $hiragana: ${e.message}")
- return hiragana // Return hiragana if API fails
+ // Check cache first
+ val cached = cache.get(word)
+ if (cached != null) {
+ if (debugMode) {
+ logger.info("Cache hit for word: $word -> $cached")
+ }
+ results.add(cached)
+ continue
}
- cache.put(input, result)
- return result
+ // Step 1: Romanji -> Hiragana
+ val hiragana = KanaConverter.toHiragana(word)
+ if (debugMode) {
+ logger.info("Romanji -> Hiragana: $word -> $hiragana")
+ }
+
+ // Step 2: Hiragana -> Kanji/Kana
+ val converted =
+ try {
+ apiClient.convert(hiragana)
+ } catch (e: Exception) {
+ logger.warning("Failed to convert $hiragana: ${e.message}")
+ hiragana // Use hiragana if API fails
+ }
+
+ // Cache the word-level conversion
+ cache.put(word, converted)
+ results.add(converted)
+ }
+
+ return results.joinToString(" ")
}
private fun isRomajiOnly(input: String): Boolean =