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(linux): 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

wqugurubd62bda660ff
diff --git a/linux/cicada/app/records_window.py b/linux/cicada/app/records_window.py
index f8784c4..334affb 100644
--- a/linux/cicada/app/records_window.py
+++ b/linux/cicada/app/records_window.py
@@ -1396,7 +1396,7 @@ class RecordsWindow(Gtk.Window):
ui.clear(options)
same = fx.same_heard(l.id, sid, entry.get_text())
if same is not None:
- heard, n = same
+ heard, n, swap = same
options.pack_start(ui.hbox(6, ui.text(L("听成"), 12, "tertiary", wrap=False, ellipsize=False),
ui.text(heard, 12, "ink", wrap=False)), False, False, 0)
keep = Gtk.CheckButton(label=L("记进词表,以后自动改"))
@@ -1414,7 +1414,9 @@ class RecordsWindow(Gtk.Window):
keep.set_sensitive(state["everywhere"])
options.pack_start(all_, False, False, 0)
options.pack_start(one, False, False, 0)
- options.pack_start(keep, False, False, 0)
+ # Only a word for a word can teach the list: an added or dropped word can't.
+ if swap:
+ options.pack_start(keep, False, False, 0)
options.show_all()
entry.connect("changed", update)
entry.connect("activate", lambda *_: apply())
diff --git a/linux/cicada/core/correction.py b/linux/cicada/core/correction.py
index 763d3ae..cb5573a 100644
--- a/linux/cicada/core/correction.py
+++ b/linux/cicada/core/correction.py
@@ -226,18 +226,65 @@ def merge(old: list, new: list) -> list:
def user_edit(shown: str, edited: str, src: str, fixes: list):
- """改这句…: 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"). None: no change."""
+ """改这句…: 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 ("莫纳" → "Morpho" → "Monad").
+ (fix, heard, swap), or None: no change (Correction.swift's userEdit)."""
new = edited.strip()
if not new or new == shown:
return None
- cf, ct = core(shown, new)
- if not cf or not ct:
+ a, b = shown, new
+ p = 0
+ while p < min(len(a), len(b)) and a[p] == b[p]:
+ p += 1
+ q = 0
+ while q < min(len(a), len(b)) - p and a[len(a) - 1 - q] == b[len(b) - 1 - q]:
+ q += 1
+ swap = len(a) - p - q > 0 and len(b) - p - q > 0
+
+ def latin(c: str) -> bool:
+ return c.isascii() and c.isalnum()
+ # Tokens of the shown line, as (start, end): a run of ASCII letters and digits, else one character.
+ tokens = []
+ k = 0
+ while k < len(a):
+ e = k + 1
+ if latin(a[k]):
+ while e < len(a) and latin(a[e]):
+ e += 1
+ tokens.append((k, e))
+ k = e
+ start, end = p, len(a) - q
+ t = next((t for t in tokens if t[0] <= start < t[1]), None)
+ if t is not None:
+ start = t[0]
+ if end > start:
+ t = next((t for t in tokens if t[0] <= end - 1 < t[1]), None)
+ if t is not None:
+ end = t[1]
+ while True:
+ span = a[start:end]
+ if any(c.isalnum() for c in span) and span and a.count(span) == 1:
+ break
+ before = [t for t in tokens if t[1] <= start]
+ after = [t for t in tokens if t[0] >= end]
+ if before:
+ start = before[-1][0]
+ elif after:
+ end = after[0][1]
+ else:
+ break
+ frm = a[start:end]
+ to = b[start:len(b) - (len(a) - end)]
+ if not frm or frm == to:
return None
- heard = cf
- if cf not in src:
- earlier = next((f for f in fixes if f.applied and f.to in cf), None)
+ heard = frm
+ if frm not in src:
+ earlier = next((f for f in fixes if f.applied and f.to in frm), None)
if earlier is not None:
heard = core(earlier.from_, earlier.to)[0]
- return LineFix(cf, ct, user=True), heard
+ return LineFix(frm, to, user=True), heard, swap
diff --git a/linux/cicada/core/live_fixes.py b/linux/cicada/core/live_fixes.py
index cfe00a1..a30dc4b 100644
--- a/linux/cicada/core/live_fixes.py
+++ b/linux/cicada/core/live_fixes.py
@@ -227,15 +227,16 @@ class LiveFixes:
def same_heard(self, line_id: str, sid: str, edited: str):
"""How many lines of the meeting the same edit would change ("整场同样听成 木娜 的 2 处都改"):
- (heard, count), or None when the edit changes nothing."""
+ (heard, count, swap), or None when the edit changes nothing. An edit that only adds or drops
+ words stays on its line (count 1, nothing to learn)."""
l = self.store.line(sid, line_id)
if l is None:
return None
e = correction.user_edit(l.shown, edited, l.src, l.fixes or [])
if e is None:
return None
- n = sum(1 for x in self.store.lines(sid) if e[0].from_ in x.shown)
- return e[1], max(1, n)
+ n = sum(1 for x in self.store.lines(sid) if e[0].from_ in x.shown) if e[2] else 1
+ return e[1], max(1, n), e[2]
def edit(self, line_id: str, sid: str, edited: str, everywhere: bool, remember: bool) -> int:
"""The user rewrote a line (改这句…): what changed becomes their own fix, on this line or on every
@@ -245,7 +246,8 @@ class LiveFixes:
e = correction.user_edit(l.shown, edited, l.src, l.fixes or []) if l else None
if e is None:
return 0
- fix, heard = e
+ fix, heard, swap = e
+ everywhere = everywhere and swap
targets = [x for x in self.store.lines(sid) if fix.from_ in x.shown] if everywhere else [l]
self._remember(sid, [t.id for t in targets])
for t in targets:
diff --git a/linux/tests/test_fixes.py b/linux/tests/test_fixes.py
index ae5a240..40abde6 100644
--- a/linux/tests/test_fixes.py
+++ b/linux/tests/test_fixes.py
@@ -179,13 +179,35 @@ class Correction(unittest.TestCase):
fixes = [LineFix("莫纳", "Morpho")]
src = "扩展到这个莫纳的 Link"
shown = apply(fixes, src)[0]
- fix, heard = correction.user_edit(shown, "扩展到这个Monad的 Link", src, fixes)
+ fix, heard, swap = correction.user_edit(shown, "扩展到这个Monad的 Link", src, fixes)
+ self.assertTrue(swap)
self.assertEqual(fix, LineFix("Morpho", "Monad", user=True))
self.assertEqual(heard, "莫纳")
self.assertEqual(apply(fixes + [fix], src)[0], "扩展到这个Monad的 Link")
self.assertEqual(correction.user_edit("我木娜找了一个", "我在 Monad 找了一个", "我木娜找了一个", [])[1], "木娜")
self.assertIsNone(correction.user_edit("a", " a ", "a", []))
+ def test_added_word_stays_on_its_line_and_never_matches_punctuation(self):
+ # "now" added before the full stop: the fix is the last word, unique in the line, not ".".
+ line = "Great, let's lock that in."
+ fix, _, swap = correction.user_edit(line, "Great, let's lock that in now.", line, [])
+ self.assertEqual((fix.from_, fix.to, swap), ("in", "in now", False))
+ self.assertEqual(apply([fix], line)[0], "Great, let's lock that in now.")
+ # A word added at the start.
+ fix, _, swap = correction.user_edit(line, "OK Great, let's lock that in.", line, [])
+ self.assertEqual((fix.from_, fix.to, swap), ("Great", "OK Great", False))
+ # A word that appears twice: grown until the span is unique.
+ line = "in the room in the hall"
+ fix = correction.user_edit(line, "in the room inside the hall", line, [])[0]
+ self.assertEqual(apply([fix], line)[0], "in the room inside the hall")
+ # Chinese: an inserted word takes its neighbour.
+ line = "好的我来看一下"
+ fix, _, swap = correction.user_edit(line, "好的OK我来看一下", line, [])
+ self.assertEqual(apply([fix], line)[0], "好的OK我来看一下")
+ self.assertFalse(swap)
+ # A real swap is still a swap.
+ self.assertTrue(correction.user_edit("我木娜找了", "我Monad找了", "我木娜找了", [])[2])
+
class Vocab(Base):
"""The Mac's VocabularyTests."""
@@ -322,6 +344,15 @@ class Live(Base):
self.assertEqual(fx.vocabulary.terms, [])
self.assertNotIn("记进词表", fx.toast_text())
+ def test_added_word_is_never_offered_for_the_whole_meeting(self):
+ fx, store, sid = self.setup(["Great, let's lock that in.", "We are in.", "Fine."])
+ self.assertEqual(fx.same_heard("l0", sid, "Great, let's lock that in now.")[1:], (1, False))
+ self.assertEqual(fx.edit("l0", sid, "Great, let's lock that in now.", everywhere=True, remember=True), 1)
+ self.assertEqual(store.line(sid, "l0").shown, "Great, let's lock that in now.")
+ self.assertEqual(store.line(sid, "l1").shown, "We are in.")
+ self.assertEqual(store.line(sid, "l2").shown, "Fine.")
+ self.assertEqual(fx.vocabulary.terms, [])
+
def test_one_off_edit_does_not_teach_the_list(self):
fx, store, sid = self.setup(["我木娜找了一个", "木娜那边也有"])
fx.edit("l0", sid, "我Monad找了一个", everywhere=False, remember=True)