Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix(android): Edit line… no longer teaches the word list punctuation or words added around the heard ones

wquguru7a11c5a4023f
diff --git a/android/cli/src/main/kotlin/cicada/cli/FixTests.kt b/android/cli/src/main/kotlin/cicada/cli/FixTests.kt
index 5a10d4f..b1da624 100644
--- a/android/cli/src/main/kotlin/cicada/cli/FixTests.kt
+++ b/android/cli/src/main/kotlin/cicada/cli/FixTests.kt
@@ -7,6 +7,8 @@ import cicada.*
object FixTests {
val tests: List<Pair<String, () -> Unit>> = listOf(
"fixesReadAndWrittenLikeTheMac" to ::recordWithFixes,
+ "editsThatArentAWordForAWordTeachNothing" to ::editsThatArentAWordForAWordTeachNothing,
+ "editAroundTheHeardWordsFixesTheLineButTeachesNothing" to ::editAroundTheHeardWordsFixesTheLineButTeachesNothing,
"yesFixesTheRestOfTheMeetingHeardTheSameWayAtOnce" to ::yesFixesTheRestOfTheMeetingHeardTheSameWayAtOnce,
"recordFormatUnchangedWithoutFixes" to ::recordFormatUnchangedWithoutFixes,
"exportsShowTheFixedText" to ::exportsShowTheFixedText,
@@ -263,6 +265,25 @@ object FixTests {
check(v.knows("阿韦", "Aave"), "knows")
}
+ fun editsThatArentAWordForAWordTeachNothing() {
+ val v = Vocabulary(null)
+ v.confirm("持。", "讲。")
+ check(v.knows("持", "讲"), "trimmed of the full stop")
+ v.confirm("我来", "OK我来", "user")
+ v.confirm("然后,", "然后")
+ equal(listOf("讲"), v.terms.map { it.word }, "words added around, or only punctuation, teach nothing")
+ v.confirm("木娜", "Monad", "user")
+ v.confirm("Uni斯图 V三", "Uniswap V3")
+ check(v.knows("木娜", "Monad") && v.knows("Uni斯图 V三", "Uniswap V3"), "a word for a word is learned")
+ }
+
+ fun editAroundTheHeardWordsFixesTheLineButTeachesNothing() {
+ val t = setup("我来看一下", "我来吧")
+ equal(2, t.fx.edit("l0", t.id, "OK我来看一下", everywhere = true, keep = true), "edited")
+ equal("OK我来吧", t.line("l1").shown, "the edit still applies")
+ check(t.fx.vocabulary.terms.isEmpty(), "not taught")
+ }
+
fun confirmAndForget() {
val v = Vocabulary(null)
v.confirm("开布特", "Curve")
diff --git a/android/core/src/main/kotlin/cicada/LiveFixes.kt b/android/core/src/main/kotlin/cicada/LiveFixes.kt
index d383d4e..34d34e2 100644
--- a/android/core/src/main/kotlin/cicada/LiveFixes.kt
+++ b/android/core/src/main/kotlin/cicada/LiveFixes.kt
@@ -202,10 +202,12 @@ class LiveFixes(val store: SessionStore, val vocabulary: Vocabulary, private val
set(fixes + fix, t.id, id)
}
val word = Correction.core(fix.from, fix.to).second.trim()
- // Only a change made everywhere teaches the word list: a one-off must not.
- if (keep && everywhere) vocabulary.confirm(heard, word, "user")
- Log.d("fix edited lines=${targets.size} keep=$keep")
- say(if (keep) Strings.LF("已改 %lld 处:%@ → %@,记进词表", targets.size, heard, word) else Strings.LF("已改 %lld 处:%@ → %@", targets.size, heard, word))
+ // Only a change made everywhere teaches the word list (a one-off must not), and only a
+ // word for a word (`Vocabulary.learnable`).
+ val learned = keep && everywhere && Vocabulary.learnable(heard, word) != null
+ if (learned) vocabulary.confirm(heard, word, "user")
+ Log.d("fix edited lines=${targets.size} learned=$learned")
+ say(if (learned) Strings.LF("已改 %lld 处:%@ → %@,记进词表", targets.size, heard, word) else Strings.LF("已改 %lld 处:%@ → %@", targets.size, heard, word))
return targets.size
}
diff --git a/android/core/src/main/kotlin/cicada/Vocabulary.kt b/android/core/src/main/kotlin/cicada/Vocabulary.kt
index cdaa9cb..94e61d8 100644
--- a/android/core/src/main/kotlin/cicada/Vocabulary.kt
+++ b/android/core/src/main/kotlin/cicada/Vocabulary.kt
@@ -58,8 +58,15 @@ class Vocabulary(private val file: File? = Paths.vocabulary) {
return terms.any { key(it.word) == w && it.heard.any { x -> key(x) == h } }
}
- /// The user said yes to a fix (or added it): the word, and the form it was heard as.
- fun confirm(heard: String, word: String, source: String = "live") = change { t ->
+ /// 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.
+ fun confirm(heard: String, word: String, source: String = "live") {
+ if (heard.isEmpty()) return add("", word, source)
+ val l = learnable(heard, word) ?: return
+ add(l.first, l.second, source)
+ }
+
+ private fun add(heard: String, word: String, source: String) = change { t ->
val i = t.indexOfFirst { key(it.word) == key(word) }
if (i >= 0) {
if (heard.isNotEmpty() && t[i].heard.none { key(it) == key(heard) }) t[i] = t[i].copy(heard = t[i].heard + heard)
@@ -158,6 +165,38 @@ class Vocabulary(private val file: File? = Paths.vocabulary) {
/// More would make every sentence's request long, and more terms means more wrong swaps.
const val LIMIT = 300
+ /// What a fix can teach the list, or null: both sides trimmed of spaces and punctuation, no
+ /// punctuation inside, short, and a real swap. An edit that only adds or drops words around
+ /// the heard ones ("我来" → "OK我来") or ends on a full stop ("持。" → "讲。") teaches nothing.
+ fun learnable(heard: String, word: String): Pair<String, String>? {
+ val h = trimEdges(heard); val w = trimEdges(word)
+ if (h.isEmpty() || w.isEmpty() || h.codePointCount(0, h.length) > 20 || w.codePointCount(0, w.length) > 40 || key(h) == key(w)) return null
+ if (w.contains(h) || h.contains(w)) return null
+ if (h.codePoints().anyMatch { punctuation(it) }) return null
+ if (w.codePoints().anyMatch { punctuation(it) && it != '.'.code && it != '-'.code }) return null
+ return h to w
+ }
+
+ private fun punctuation(c: Int): Boolean = when (Character.getType(c).toByte()) {
+ Character.CONNECTOR_PUNCTUATION, Character.DASH_PUNCTUATION, Character.START_PUNCTUATION, Character.END_PUNCTUATION,
+ Character.INITIAL_QUOTE_PUNCTUATION, Character.FINAL_QUOTE_PUNCTUATION, Character.OTHER_PUNCTUATION -> true
+ else -> false
+ }
+
+ /// Spaces, punctuation and symbols: what a learned pair is trimmed of at both ends.
+ private fun edge(c: Int): Boolean = Character.isWhitespace(c) || Character.isSpaceChar(c) || punctuation(c) ||
+ when (Character.getType(c).toByte()) {
+ Character.MATH_SYMBOL, Character.CURRENCY_SYMBOL, Character.MODIFIER_SYMBOL, Character.OTHER_SYMBOL -> true
+ else -> false
+ }
+
+ private fun trimEdges(s: String): String {
+ var a = 0; var b = s.length
+ while (a < b && edge(s.codePointAt(a))) a += Character.charCount(s.codePointAt(a))
+ while (b > a && edge(s.codePointBefore(b))) b -= Character.charCount(s.codePointBefore(b))
+ return s.substring(a, b)
+ }
+
/// Same word whatever its width, case or spacing ("AAVE" is "aave").
fun key(s: String): String = Normalizer.normalize(s, Normalizer.Form.NFKC).lowercase().split(Regex("\\s+")).filter { it.isNotEmpty() }.joinToString(" ")