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… that adds a word fixes only that line, never every line with a full stop, and adding a word at the start works

wquguru2f51cd877171
diff --git a/android/app/src/main/kotlin/com/wquguru/cicada/FixViews.kt b/android/app/src/main/kotlin/com/wquguru/cicada/FixViews.kt
index 5c93e1a..eeb62fe 100644
--- a/android/app/src/main/kotlin/com/wquguru/cicada/FixViews.kt
+++ b/android/app/src/main/kotlin/com/wquguru/cicada/FixViews.kt
@@ -186,14 +186,15 @@ object FixViews {
fun update() {
val same = app.fixes.sameHeard(line.id, sessionId, field.text.toString())
heard.visibility = if (same == null) View.GONE else View.VISIBLE
- keep.visibility = heard.visibility
- scope.visibility = if (same != null && same.second > 1) View.VISIBLE else View.GONE
+ // Only a word for a word can teach the list: an added or dropped word can't.
+ keep.visibility = if (same != null && same.swap) View.VISIBLE else View.GONE
+ scope.visibility = if (same != null && same.count > 1) View.VISIBLE else View.GONE
if (same != null) {
- heard.text = LF("听成 %@", same.first)
- everywhere.text = LF("整场听成 %@ 的 %lld 处都改", same.first, same.second)
+ heard.text = LF("听成 %@", same.heard)
+ everywhere.text = LF("整场听成 %@ 的 %lld 处都改", same.heard, same.count)
}
// Only a change made everywhere teaches the word list.
- keep.isEnabled = same == null || same.second <= 1 || everywhere.isChecked
+ keep.isEnabled = same == null || same.count <= 1 || everywhere.isChecked
}
field.addTextChangedListener(object : android.text.TextWatcher {
override fun beforeTextChanged(s: CharSequence?, a: Int, b: Int, c: Int) {}
@@ -209,8 +210,8 @@ object FixViews {
addView(ui.button(L("改成"), "primary") {
d.dismiss()
val same = app.fixes.sameHeard(line.id, sessionId, field.text.toString()) ?: return@button
- val all = same.second <= 1 || everywhere.isChecked
- app.background.execute { app.fixes.edit(line.id, sessionId, field.text.toString(), everywhere = all, keep = keep.isChecked && all) }
+ val all = same.count <= 1 || everywhere.isChecked
+ app.background.execute { app.fixes.edit(line.id, sessionId, field.text.toString(), everywhere = all, keep = keep.isChecked && all && same.swap) }
}, ui.weight().apply { leftMargin = ui.dp(10) })
}, ui.lp(top = 8))
d = ui.sheet(activity, L("改这句"), body)
diff --git a/android/cli/src/main/kotlin/cicada/cli/FixTests.kt b/android/cli/src/main/kotlin/cicada/cli/FixTests.kt
index b1da624..f560714 100644
--- a/android/cli/src/main/kotlin/cicada/cli/FixTests.kt
+++ b/android/cli/src/main/kotlin/cicada/cli/FixTests.kt
@@ -27,6 +27,7 @@ object FixTests {
"mergeKeepsRefusals" to ::mergeKeepsRefusals,
"recheckTakesBackWhatItNoLongerProposesButNotTheUsers" to ::recheckTakesBack,
"userEditOnTopOfAFix" to ::userEditOnTopOfAFix,
+ "addedWordStaysOnItsLineAndNeverMatchesPunctuation" to ::addedWordStaysOnItsLineAndNeverMatchesPunctuation,
"vocabImportLinesWithHeardForms" to ::importLinesWithHeardForms,
"vocabSameWordWhateverWidthOrCase" to ::sameWordWhateverWidthOrCase,
"vocabConfirmAndForget" to ::confirmAndForget,
@@ -239,10 +240,10 @@ object FixTests {
val src = "扩展到这个莫纳的 Link"
val shown = Correction.apply(fixes, src).text
val e = Correction.userEdit(shown, "扩展到这个Monad的 Link", src, fixes)
- equal(LineFix("Morpho", "Monad", user = true), e?.first, "fix on the fix")
- equal("莫纳", e?.second, "heard form")
- equal("扩展到这个Monad的 Link", Correction.apply(fixes + e!!.first, src).text, "shown")
- equal("木娜", Correction.userEdit("我木娜找了一个", "我在 Monad 找了一个", "我木娜找了一个", emptyList())?.second, "plain heard word")
+ equal(LineFix("Morpho", "Monad", user = true), e?.fix, "fix on the fix")
+ equal("莫纳", e?.heard, "heard form")
+ equal("扩展到这个Monad的 Link", Correction.apply(fixes + e!!.fix, src).text, "shown")
+ equal("木娜", Correction.userEdit("我木娜找了一个", "我在 Monad 找了一个", "我木娜找了一个", emptyList())?.heard, "plain heard word")
equal(null, Correction.userEdit("a", " a ", "a", emptyList()), "unchanged")
}
@@ -279,11 +280,35 @@ object FixTests {
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")
+ equal(1, t.fx.sameHeard("l0", t.id, "OK我来看一下")?.count, "an added word offers this line only")
+ equal(1, t.fx.edit("l0", t.id, "OK我来看一下", everywhere = true, keep = true), "edited")
+ equal("OK我来看一下", t.line("l0").shown, "the edit applies")
+ equal("我来吧", t.line("l1").shown, "and stays on its line")
check(t.fx.vocabulary.terms.isEmpty(), "not taught")
}
+ fun addedWordStaysOnItsLineAndNeverMatchesPunctuation() {
+ val line = "Great, let's lock that in."
+ // "now" added before the full stop: the fix is the last word, unique in the line, not ".".
+ val e = Correction.userEdit(line, "Great, let's lock that in now.", line, emptyList())!!
+ equal("in", e.fix.from, "from"); equal("in now", e.fix.to, "to"); equal(false, e.swap, "not a swap")
+ equal("Great, let's lock that in now.", Correction.apply(listOf(e.fix), line).text, "applied")
+ // A word added at the start.
+ val s = Correction.userEdit(line, "OK Great, let's lock that in.", line, emptyList())!!
+ equal("Great", s.fix.from, "start from"); equal("OK Great", s.fix.to, "start to"); equal(false, s.swap, "start not a swap")
+ equal("OK Great, let's lock that in.", Correction.apply(listOf(s.fix), line).text, "start applied")
+ // A word that appears twice: grown until the span is unique.
+ val r = "in the room in the hall"
+ val t = Correction.userEdit(r, "in the room inside the hall", r, emptyList())!!
+ equal("in the room inside the hall", Correction.apply(listOf(t.fix), r).text, "unique span")
+ // Chinese: an inserted word takes its neighbour.
+ val c = Correction.userEdit("好的我来看一下", "好的OK我来看一下", "好的我来看一下", emptyList())!!
+ equal("好的OK我来看一下", Correction.apply(listOf(c.fix), "好的我来看一下").text, "Chinese applied")
+ equal(false, c.swap, "Chinese not a swap")
+ // A real swap is still a swap.
+ equal(true, Correction.userEdit("我木娜找了", "我Monad找了", "我木娜找了", emptyList())?.swap, "swap")
+ }
+
fun confirmAndForget() {
val v = Vocabulary(null)
v.confirm("开布特", "Curve")
@@ -363,7 +388,7 @@ object FixTests {
fun editEverywhereTeachesTheListAndUndoPutsItBack() {
val t = setup("我木娜找了一个", "木娜那边也有", "别的")
- equal(2, t.fx.sameHeard("l0", t.id, "我Monad找了一个")?.second, "same heard")
+ equal(2, t.fx.sameHeard("l0", t.id, "我Monad找了一个")?.count, "same heard")
equal(2, t.fx.edit("l0", t.id, "我Monad找了一个", everywhere = true, keep = true), "edited")
equal("Monad那边也有", t.line("l1").shown, "everywhere")
check(t.fx.vocabulary.knows("木娜", "Monad"), "taught")
diff --git a/android/core/src/main/kotlin/cicada/Correction.kt b/android/core/src/main/kotlin/cicada/Correction.kt
index 8ff923f..b1a0be5 100644
--- a/android/core/src/main/kotlin/cicada/Correction.kt
+++ b/android/core/src/main/kotlin/cicada/Correction.kt
@@ -151,17 +151,57 @@ object Correction {
return kept + fresh
}
- /// 改这句…: the user rewrote the line as shown. What changed, as a fix on top of the others,
- /// and the heard form it replaces (for the word list): the words in `src` when they are
- /// there, else the heard words of the fix the user corrected again ("莫纳" → "Morpho" → "Monad").
- fun userEdit(shown: String, edited: String, src: String, fixes: List<LineFix>): Pair<LineFix, String>? {
+ data class UserEdit(val fix: LineFix, val heard: String, val swap: Boolean)
+
+ /// 改这句…: the user rewrote the line as shown. What changed, as a fix on top of the others: the
+ /// changed span grown to whole tokens (a Latin word, or one CJK character), and further, a token
+ /// at a time, until it holds a letter and is the only such span in the line. An added word
+ /// ("lock that in." → "lock that in now.") is then "in" → "in now" on this line, never "." on
+ /// every line. `swap` is whether words were replaced (not only added or dropped): only a swap
+ /// may go to the rest of the meeting and teach the word list. `heard` is the words in `src`
+ /// when they are there, else the heard words of the fix the user corrected again.
+ fun userEdit(shown: String, edited: String, src: String, fixes: List<LineFix>): UserEdit? {
val new = edited.trim()
if (new.isEmpty() || new == shown) return null
- val (cf, ct) = core(shown, new)
- if (cf.isEmpty() || ct.isEmpty()) return null
- var heard = cf
- if (!src.contains(cf)) fixes.firstOrNull { it.applied && cf.contains(it.to) }?.let { heard = core(it.from, it.to).first }
- return LineFix(cf, ct, user = true) to heard
+ // Code points, as the Mac's Characters: a span never splits a surrogate pair.
+ val a = shown.codePoints().toArray(); val b = new.codePoints().toArray()
+ fun str(x: IntArray, from: Int, to: Int) = String(x, from, to - from)
+ var p = 0
+ while (p < minOf(a.size, b.size) && a[p] == b[p]) p++
+ var q = 0
+ while (q < minOf(a.size, b.size) - p && a[a.size - 1 - q] == b[b.size - 1 - q]) q++
+ val swap = a.size - p - q > 0 && b.size - p - q > 0
+ // Tokens of the shown line: a run of ASCII letters and digits, else one character.
+ fun latin(c: Int) = c < 128 && Character.isLetterOrDigit(c)
+ val tokens = mutableListOf<IntRange>() // start until end (exclusive)
+ var k = 0
+ while (k < a.size) {
+ var e = k + 1
+ if (latin(a[k])) while (e < a.size && latin(a[e])) e++
+ tokens.add(k until e)
+ k = e
+ }
+ var start = p; var end = a.size - q
+ tokens.firstOrNull { start in it }?.let { start = it.first }
+ if (end > start) tokens.firstOrNull { end - 1 in it }?.let { end = it.last + 1 }
+ fun span() = str(a, start, end)
+ fun count(x: String): Int {
+ if (x.isEmpty()) return 0
+ var n = 0; var at = shown.indexOf(x)
+ while (at >= 0) { n++; at = shown.indexOf(x, at + x.length) }
+ return n
+ }
+ while (span().codePoints().noneMatch { Character.isLetterOrDigit(it) } || count(span()) != 1) {
+ val before = tokens.lastOrNull { it.last + 1 <= start }
+ val after = tokens.firstOrNull { it.first >= end }
+ if (before != null) start = before.first else if (after != null) end = after.last + 1 else break
+ }
+ val from = span()
+ val to = str(b, start, b.size - (a.size - end))
+ if (from.isEmpty() || from == to) return null
+ var heard = from
+ if (!src.contains(from)) fixes.firstOrNull { it.applied && from.contains(it.to) }?.let { heard = core(it.from, it.to).first }
+ return UserEdit(LineFix(from, to, user = true), heard, swap)
}
/// The line as shown: each applied fix replaces its words where they still are. `marks` are
diff --git a/android/core/src/main/kotlin/cicada/LiveFixes.kt b/android/core/src/main/kotlin/cicada/LiveFixes.kt
index 34d34e2..86d7750 100644
--- a/android/core/src/main/kotlin/cicada/LiveFixes.kt
+++ b/android/core/src/main/kotlin/cicada/LiveFixes.kt
@@ -178,12 +178,16 @@ class LiveFixes(val store: SessionStore, val vocabulary: Vocabulary, private val
// MARK: 改这句…
+ class Same(val heard: String, val count: Int, val swap: Boolean)
+
/// How many lines of the meeting the same edit would change ("整场听成 木娜 的 2 处都改").
- fun sameHeard(lineId: String, id: String, edited: String): Pair<String, Int>? {
+ /// An edit that only adds or drops words stays on its line (count 1, nothing to learn).
+ fun sameHeard(lineId: String, id: String, edited: String): Same? {
val lines = store.lines(id)
val l = lines.firstOrNull { it.id == lineId } ?: return null
- val (fix, heard) = Correction.userEdit(l.shown, edited, l.src, l.fixes ?: emptyList()) ?: return null
- return heard to maxOf(1, lines.count { it.shown.contains(fix.from) })
+ val e = Correction.userEdit(l.shown, edited, l.src, l.fixes ?: emptyList()) ?: return null
+ val n = if (e.swap) lines.count { it.shown.contains(e.fix.from) } else 1
+ return Same(e.heard, maxOf(1, n), e.swap)
}
/// The user rewrote a line (改这句…): what changed becomes their own fix, on this line or on
@@ -191,7 +195,9 @@ class LiveFixes(val store: SessionStore, val vocabulary: Vocabulary, private val
@Synchronized fun edit(lineId: String, id: String, edited: String, everywhere: Boolean, keep: Boolean): Int {
val lines = store.lines(id)
val l = lines.firstOrNull { it.id == lineId } ?: return 0
- val (fix, heard) = Correction.userEdit(l.shown, edited, l.src, l.fixes ?: emptyList()) ?: return 0
+ val e = Correction.userEdit(l.shown, edited, l.src, l.fixes ?: emptyList()) ?: return 0
+ val (fix, heard) = e
+ val everywhere = everywhere && e.swap
val targets = if (everywhere) lines.filter { it.shown.contains(fix.from) } else listOf(l)
remember(id, targets.map { it.id })
for (t in targets) {