translate: detect kanji-heavy Japanese as Japanese, not Chinese
- Commit
5c88121bb40c0a5afbe5b1d2c93b6cbf349c3e2f- Author
- Vayun Mathur <[email protected]>
- When
- 2026-12-01 19:17 UTC (4 days ago)
- Parents
a50cb07
Auto source-language detection picked the single most frequent script in a text and treated that script's language as the answer. Japanese writes much of its vocabulary in kanji, which the detector counts as Han, so a Japanese sentence whose kanji outnumber (or merely tie) its kana was classified as Chinese: detectLanguageCode returned zh-Hant for Japanese text and zh-Hans for the tie case. NllbTranslator then fed the Chinese source token to NLLB for Japanese input under the Auto source option, mistranslating it. Kana never occurs in Chinese text, so its presence is decisive evidence for Japanese. scriptOf now returns ja whenever kana is present, counting the surrounding Han characters toward the Japanese total so the existing guard against a script quoted inside a mostly Latin text still applies unchanged. Pure Han text with no kana still resolves to Simplified/Traditional Chinese exactly as before. Regression test in LanguageDetectionTest covers a kanji-majority sample and a kanji/kana tie; both failed before the fix (expected ja, was zh-Hant) and pass after it. The full batched :translate:testDevUnitTest task passes (93 tests). The companion testDevUnitTestNative task keeps one pre-existing failure in DbBackedPagesTest (navigation language picker finds two English nodes), which fails identically without this change, and the :library:ml cargo tasks were excluded because the base Cargo.lock is stale (--locked refuses to update it); neither is touched here.
Diff
translate/src/main/java/com/vayunmathur/translate/domain/LanguageDetection.kt
diff --git a/translate/src/main/java/com/vayunmathur/translate/domain/LanguageDetection.kt b/translate/src/main/java/com/vayunmathur/translate/domain/LanguageDetection.ktindex 7f75552510..22efb93d81 100644--- a/translate/src/main/java/com/vayunmathur/translate/domain/LanguageDetection.kt+++ b/translate/src/main/java/com/vayunmathur/translate/domain/LanguageDetection.kt@@ -107,6 +107,17 @@ object LanguageDetection { else -> if (ch.isLetter()) latin++ } }+ // Kana never occurs in Chinese, so any kana settles Japanese even+ // when the kanji around it (counted as Han) outnumber the kana —+ // picking only the single strongest script read kanji-heavy+ // Japanese as Chinese. The Han characters count toward the+ // Japanese total here: they are part of the same text, and the+ // Latin guard below still applies to that combined total.+ val kana = counts["ja"] ?: 0+ if (kana > 0) {+ if (kana + (counts["han"] ?: 0) < latin) return null+ return "ja"+ } val strongest = counts.maxByOrNull { it.value } ?: return null // A script quoted inside a mostly Latin text does not make the text // that language; the script must at least match the Latin letters.
translate/src/test/java/com/vayunmathur/translate/domain/LanguageDetectionTest.kt
diff --git a/translate/src/test/java/com/vayunmathur/translate/domain/LanguageDetectionTest.kt b/translate/src/test/java/com/vayunmathur/translate/domain/LanguageDetectionTest.ktindex 1fd03e6df7..a5b421dc83 100644--- a/translate/src/test/java/com/vayunmathur/translate/domain/LanguageDetectionTest.kt+++ b/translate/src/test/java/com/vayunmathur/translate/domain/LanguageDetectionTest.kt@@ -54,6 +54,16 @@ class LanguageDetectionTest { assertEquals("hi", detectLanguageCode("नमस्ते दुनिया, आज मौसम अच्छा है।")) } + @Test+ fun `japanese with more kanji than kana is not read as chinese`() {+ // Kana never occurs in Chinese, so its presence settles Japanese+ // even when kanji (counted as Han) outnumber or merely tie kana:+ // both samples were returned as zh-Hant / zh-Hans before the fix,+ // so Auto source used the Chinese token for Japanese text.+ assertEquals("ja", detectLanguageCode("日本語を勉強します"))+ assertEquals("ja", detectLanguageCode("私は学生です"))+ }+ @Test fun `chinese script variants are distinguished`() { assertEquals("zh-Hans", detectLanguageCode("我们在这里学习中文。"))