知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.
Swift androidlinuxlive-captionsmacosmeeting-notesmenubar-appsimultaneous-interpretationspeech-recognitionsubtitlesswiftswiftuitranslationwindows
fix(android): fixing misheard words no longer turns a Chinese word into its English translation, or changes letters inside an English word
wquguru6d13b5c75007
diff --git a/android/cli/src/main/kotlin/cicada/cli/FixTests.kt b/android/cli/src/main/kotlin/cicada/cli/FixTests.ktindex 81dc03a..40740ce 100644--- a/android/cli/src/main/kotlin/cicada/cli/FixTests.kt+++ b/android/cli/src/main/kotlin/cicada/cli/FixTests.kt@@ -20,6 +20,8 @@ object FixTests {"lowConfidenceIsDropped" to ::lowConfidenceIsDropped,"spellingFixNeedsHighConfidence" to ::spellingFixNeedsHighConfidence,"newNamesAreAsked" to ::newNamesAreAsked,+ "learnedWordsStayWhole" to ::learnedWordsStayWhole,+ "translationsAreNotAsked" to ::translationsAreNotAsked,"confirmedHeardFormIsApplied" to ::confirmedHeardFormIsApplied,"rejectedIsDropped" to ::rejectedIsDropped,"coreDropsSharedEnds" to ::coreDropsSharedEnds,@@ -33,6 +35,7 @@ object FixTests {"addedWordStaysOnItsLineAndNeverMatchesPunctuation" to ::addedWordStaysOnItsLineAndNeverMatchesPunctuation,"vocabImportLinesWithHeardForms" to ::importLinesWithHeardForms,"vocabSameWordWhateverWidthOrCase" to ::sameWordWhateverWidthOrCase,+ "vocabAYesNeverTeachesATranslation" to ::aYesNeverTeachesATranslation,"vocabConfirmAndForget" to ::confirmAndForget,"vocabPromptPutsRecentFirst" to ::promptPutsRecentFirst,"vocabSavesAndLoads" to ::savesAndLoads,@@ -196,6 +199,26 @@ object FixTests {equal(D, verdict("莫纳", "Morpho", 0.7), "莫纳")}+ /// A learned "S平 → X平" never reaches into "iOS平台"; on its own it still applies.+ fun learnedWordsStayWhole() {+ equal(null, Correction.word("S平", "iOS平台很好"), "inside iOS")+ equal(null, Correction.word("A工", "DATA工具"), "inside DATA")+ check(Correction.word("S平", "在S平台上") != null, "on its own")+ check(Correction.word("S平", "iOS平台和S平台") != null, "the second one")+ check(Correction.word("莫拉", "解释这个莫拉的") != null, "Chinese")+ equal("iOS平台和X平台", Correction.apply(listOf(LineFix("S平", "X平")), "iOS平台和S平台").text, "apply")+ }++ /// A translation isn't a misheard name: one 对 to "X平台 → X platform" taught 平台 → platform.+ fun translationsAreNotAsked() {+ equal(D, verdict("X平台", "X platform", 0.9), "X平台")+ equal(D, verdict("平台", "platform", 0.95), "平台")+ equal(D, verdict("respons", "responsible", 0.85), "respons")+ equal(Q, verdict("GPT四", "GPT4", 0.85), "GPT四")+ check(Correction.translation("平台", "platform"), "a translation")+ check(!Correction.translation("莫拉", "Morpho"), "a name")+ }+fun confirmedHeardFormIsApplied() {equal(A, verdict("莫拉", "Morpho", 0.5, known = listOf("莫拉" to "Morpho")), "known")equal(A, verdict("解释这个莫拉的", "解释这个Morpho的", 0.5, known = listOf("莫拉" to "Morpho")), "known in a phrase")@@ -351,6 +374,14 @@ object FixTests {equal(true, Correction.userEdit("我木娜找了", "我Monad找了", "我木娜找了", emptyList())?.swap, "swap")}+ fun aYesNeverTeachesATranslation() {+ val v = Vocabulary(null)+ v.confirm("平台", "platform")+ check(v.terms.isEmpty(), "a yes teaches no translation")+ v.confirm("平台", "platform", source = "user")+ check(v.knows("平台", "platform"), "the user's own does")+ }+fun confirmAndForget() {val v = Vocabulary(null)v.confirm("开布特", "Curve")diff --git a/android/core/src/main/kotlin/cicada/Correction.kt b/android/core/src/main/kotlin/cicada/Correction.ktindex 3205857..a2880ce 100644--- a/android/core/src/main/kotlin/cicada/Correction.kt+++ b/android/core/src/main/kotlin/cicada/Correction.kt@@ -82,11 +82,21 @@ object Correction {if (!latinFrom && !latinTo && han(cf) && han(ct) && soundsAlike(cf, ct)) return Verdict.Applyif (latinFrom && latinTo && cf.all { it.code < 128 } && ct.all { it.code < 128 } && e.confidence >= SPELLING_AT &&distance(cf.lowercase(), ct.lowercase()) <= 2) return Verdict.Apply- // A new name (Chinese heard as a Latin name, or a respelled one): sound can't check it.- if (latinTo && e.confidence >= ASK_AT) return Verdict.Ask+ // A new name (Chinese heard as a Latin name, or a respelled one): sound can't check it. A+ // name has a capital or a digit; "X平台 → X platform" is a translation, and one 对 to it+ // taught the word list 平台 → platform, which then rewrote every 平台 in the meeting.+ if (latinTo && e.confidence >= ASK_AT && nameShaped(ct)) return Verdict.Askreturn Verdict.Drop}+ /// Latin text that could be a name: a capital or a digit in it (Morpho, GPT4o), not "platform".+ fun nameShaped(s: String): Boolean = s.any { it.code < 128 && (it.isUpperCase() || it.isDigit()) }++ /// A Chinese word "fixed" into a plain English one (平台 → platform): a translation, not a+ /// misheard name.+ fun translation(heard: String, word: String): Boolean =+ han(heard) && word.any { it.code < 128 && it.isLetter() } && word.all { it.code < 128 } && !nameShaped(word)+fun pinyin(s: String): List<String> = s.map { pinyinOf(it).trim().lowercase() }private val initials = listOf("zh", "ch", "sh", "b", "p", "m", "f", "d", "t", "n", "l", "g", "k", "h", "j", "q",@@ -217,6 +227,24 @@ object Correction {/// the replaced words (start, end in UTF-16 units), for the dotted underline.class Applied(val text: String, val marks: List<IntRange>)+ /// Where `s` stands as words of its own in `text` (its start, or null): a Latin edge of it never+ /// runs on into a Latin word around it. `core` pads a fix to two characters, so the word list+ /// learned "S平 → X平", which matched inside "iOS平台" and made it "iOX平台".+ fun word(s: String, text: String): Int? {+ if (s.isEmpty()) return null+ var from = 0+ while (from < text.length) {+ val at = text.indexOf(s, from)+ if (at < 0) return null+ val end = at + s.length+ val before = at > 0 && word(text[at - 1])+ val after = end < text.length && word(text[end])+ if (!(word(s.first()) && before) && !(word(s.last()) && after)) return at+ from = at + 1+ }+ return null+ }+fun apply(fixes: List<LineFix>, src: String): Applied {var text = srcval spans = mutableListOf<Pair<String, Int>>() // replacement and its offset, kept valid as text changes@@ -224,7 +252,7 @@ object Correction {val ordered = fixes.filter { it.user != true } + fixes.filter { it.user == true }for (f in ordered) {if (!f.applied || f.from.isEmpty()) continue- val start = text.indexOf(f.from)+ val start = word(f.from, text) ?: text.indexOf(f.from)if (start < 0) continueval delta = f.to.length - f.from.lengthfor (k in spans.indices) if (spans[k].second > start) spans[k] = spans[k].first to spans[k].second + deltadiff --git a/android/core/src/main/kotlin/cicada/LiveFixes.kt b/android/core/src/main/kotlin/cicada/LiveFixes.ktindex 933bce9..c6a8994 100644--- a/android/core/src/main/kotlin/cicada/LiveFixes.kt+++ b/android/core/src/main/kotlin/cicada/LiveFixes.kt@@ -89,8 +89,10 @@ class LiveFixes(val store: SessionStore, val vocabulary: Vocabulary, private valval out = fixes.toMutableList()for (t in vocabulary.terms) {for (h in t.heard) {- // A heard form two words claim (两个都留) is never fixed silently.- if (h.length < 2 || !src.contains(h) || pair(h, t.word) in refused || vocabulary.owners(h, t.word).isNotEmpty()) continue+ // A heard form two words claim (两个都留) is never fixed silently. A translation a yes taught+ // before that was refused (平台 → platform) isn't either; one the user typed is.+ if (t.source != "user" && Correction.translation(h, t.word)) continue+ if (h.length < 2 || Correction.word(h, src) == null || pair(h, t.word) in refused || vocabulary.owners(h, t.word).isNotEmpty()) continueif (out.any { it.from == h || (it.applied && it.from.contains(h)) || (it.off == true && it.from == h) }) continueout.add(LineFix(h, t.word))}diff --git a/android/core/src/main/kotlin/cicada/Vocabulary.kt b/android/core/src/main/kotlin/cicada/Vocabulary.ktindex 94e61d8..9a1eb6f 100644--- a/android/core/src/main/kotlin/cicada/Vocabulary.kt+++ b/android/core/src/main/kotlin/cicada/Vocabulary.kt@@ -59,10 +59,12 @@ class Vocabulary(private val file: File? = Paths.vocabulary) {}/// The user said yes to a fix (or added it): the word, and the form it was heard as. A pair- /// that isn't a word for a word (`learnable`) adds nothing.+ /// that isn't a word for a word (`learnable`) adds nothing. A yes in a meeting never teaches a+ /// translation (平台 → platform): it would rewrite that word everywhere after.fun confirm(heard: String, word: String, source: String = "live") {if (heard.isEmpty()) return add("", word, source)val l = learnable(heard, word) ?: return+ if (source != "user" && Correction.translation(l.first, l.second)) returnadd(l.first, l.second, source)}