Your phone, on your command. Gotcha is world's first fully on-device AI copilot for Android that turns natural language into real device actions — safely.
fix: improve SpeechTextSanitizer punctuation handling and regex robustness for CJK and Unicode support
Rishabh Bajpai4e3fbf1f029d
diff --git a/app/src/main/java/com/gotcha/audio/SpeechTextSanitizer.kt b/app/src/main/java/com/gotcha/audio/SpeechTextSanitizer.ktindex 0baf016..20aa694 100644--- a/app/src/main/java/com/gotcha/audio/SpeechTextSanitizer.kt+++ b/app/src/main/java/com/gotcha/audio/SpeechTextSanitizer.kt@@ -12,6 +12,8 @@ object SpeechTextSanitizer {private const val MAX_ORDINAL = 12+ private const val SENTENCE_TERMINATORS = ".?!"+private val ORDINALS = arrayOf("First", "Second", "Third", "Fourth", "Fifth", "Sixth","Seventh", "Eighth", "Ninth", "Tenth", "Eleventh", "Twelfth"@@ -77,10 +79,18 @@ object SpeechTextSanitizer {fun flushBlock() {if (block.isEmpty()) return+ val lastIdx = block.lastIndexval numbered = block.mapIndexed { idx, line ->- ordinalFor(idx + 1) + BULLET_PREFIX.replace(line, "")+ val item = ordinalFor(idx + 1) + BULLET_PREFIX.replace(line, "")+ // Strip trailing sentence punctuation from non-final items so the+ // ". " join separator doesn't double up; preserve the last item's+ // own terminator (? / !) so a question / exclamation survives.+ if (idx == lastIdx) item else item.trimEnd { it in SENTENCE_TERMINATORS }}- out.append(numbered.joinToString(". ")).append(".\n")+ val joined = numbered.joinToString(". ")+ val lastChar = joined.lastOrNull()+ val suffix = if (lastChar != null && lastChar in SENTENCE_TERMINATORS) "" else "."+ out.append(joined).append(suffix).append("\n")block = mutableListOf()}@@ -99,15 +109,17 @@ object SpeechTextSanitizer {/*** Replace a bare URL match with the literal word "link", but keep any* sentence-ending punctuation that immediately follows it (`link.` rather- * than eating the period). Trailing `[.,;:!?)\]']` chars are treated as- * outside the URL.+ * than eating the period). Trailing ASCII + CJK punctuation chars are+ * treated as outside the URL.*/private fun replaceBareUrl(url: String): String {val trailing = url.takeLastWhile { it in TRAILING_PUNCT_CHARS }return "link$trailing"}- private const val TRAILING_PUNCT_CHARS = ".,;:!?)]'"+ private const val TRAILING_PUNCT_CHARS =+ ".,;:!?)]'\"" ++ "。,!?、;:)"private val EMOJI_REGEX = Regex("[\\x{1F000}-\\x{1FFFF}\\x{2600}-\\x{27BF}\\x{2B00}-\\x{2BFF}\\x{FE0F}\\x{FE0E}\\x{200D}]+"@@ -117,8 +129,12 @@ object SpeechTextSanitizer {private val INLINE_CODE = Regex("`([^`]+)`")private val BOLD_ASTERISK = Regex("\\*\\*(.+?)\\*\\*")private val BOLD_UNDERSCORE = Regex("__(.+?)__")- private val ITALIC_ASTERISK = Regex("(?<![\\w*])\\*(.+?)\\*(?![\\w*])")- private val ITALIC_UNDERSCORE = Regex("(?<![\\w])_(.+?)_(?![\\w])")+ private val ITALIC_ASTERISK = Regex(+ "(?U)(?<![\\w*])\\*(.+?)\\*(?![\\w*])"+ )+ private val ITALIC_UNDERSCORE = Regex(+ "(?U)(?<![\\w])_(.+?)_(?![\\w])"+ )private val STRIKE = Regex("~~(.+?)~~")private val HEADING_PREFIX = Regex("^[ \\t]{0,3}#{1,6}[ \\t]+", RegexOption.MULTILINE)private val BLOCKQUOTE_PREFIX = Regex("^[ \\t]{0,3}>[ \\t]+", RegexOption.MULTILINE)@@ -129,5 +145,5 @@ object SpeechTextSanitizer {private val MULTI_EXCLAIM = Regex("!{2,}")private val MULTI_QUESTION = Regex("\\?{2,}")private val ELLIPSIS_UNICODE = Regex("…")- private val WHITESPACE_RUN = Regex("[ \\t\\n\\r]+")+ private val WHITESPACE_RUN = Regex("(?U)\\s+")}diff --git a/app/src/test/java/com/gotcha/audio/SpeechTextSanitizerTest.kt b/app/src/test/java/com/gotcha/audio/SpeechTextSanitizerTest.ktindex ec63f55..bf07234 100644--- a/app/src/test/java/com/gotcha/audio/SpeechTextSanitizerTest.kt+++ b/app/src/test/java/com/gotcha/audio/SpeechTextSanitizerTest.kt@@ -226,4 +226,65 @@ class SpeechTextSanitizerTest {// Non-bullet text already has its own punctuation — don't add extra.assertEquals("Hello there.", SpeechTextSanitizer.sanitize("Hello there."))}++ @Test+ fun `sanitize does not double up punctuation when bullet items already end with sentence punctuation`() {+ assertEquals(+ "First, Done. Second, Start.",+ SpeechTextSanitizer.sanitize("- Done.\n- Start.")+ )+ assertEquals(+ "First, Done. Second, really?",+ SpeechTextSanitizer.sanitize("- Done.\n- really?")+ )+ assertEquals(+ "First, wow!",+ SpeechTextSanitizer.sanitize("- wow!")+ )+ }++ @Test+ fun `sanitize keeps CJK sentence punctuation after a URL`() {+ // Japanese full stop (。) and Chinese fullwidth comma (,) are common+ // sentence terminators after URLs in CJK text.+ assertEquals(+ "詳細は link を参照。",+ SpeechTextSanitizer.sanitize("詳細は https://x.com を参照。")+ )+ assertEquals(+ "见 link,",+ SpeechTextSanitizer.sanitize("见 https://x.com,")+ )+ }++ @Test+ fun `sanitize keeps a trailing double quote after a URL`() {+ assertEquals(+ "see \"link\" today",+ SpeechTextSanitizer.sanitize("see \"https://x.com\" today")+ )+ }++ @Test+ fun `sanitize collapses CJK ideographic spaces`() {+ // U+3000 (ideographic space) is whitespace under UNICODE_CHARACTER_CLASS.+ assertEquals(+ "今天 天气 不错",+ SpeechTextSanitizer.sanitize("今天\u3000天气\u3000不错")+ )+ }++ @Test+ fun `sanitize preserves asterisks inside Hindi words`() {+ // Without UNICODE_CHARACTER_CLASS, \w doesn't match Devanagari chars,+ // so `*ब*` between Hindi words would be wrongly stripped as italic.+ assertEquals(+ "अ*ब*स",+ SpeechTextSanitizer.sanitize("अ*ब*स")+ )+ assertEquals(+ "यह italic है",+ SpeechTextSanitizer.sanitize("यह *italic* है")+ )+ }}