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… no longer teaches the word list punctuation or words added around the heard ones

wquguruab28ff728653
diff --git a/linux/cicada/core/live_fixes.py b/linux/cicada/core/live_fixes.py
index cdd73c9..cfe00a1 100644
--- a/linux/cicada/core/live_fixes.py
+++ b/linux/cicada/core/live_fixes.py
@@ -10,6 +10,7 @@ import time
from . import correction
from .correction import Edit
+from .vocabulary import learnable
from .models import LineFix
from .strings import L, LF
from .support import Log
@@ -257,11 +258,13 @@ class LiveFixes:
fixes.append(f)
self._set(fixes + [fix.copy()], t.id, sid)
word = correction.core(fix.from_, fix.to)[1].strip()
- # Only a change made everywhere teaches the word list: a one-off must not.
- if remember and everywhere:
+ # Only a change made everywhere teaches the word list (a one-off must not), and only a
+ # word for a word (`learnable`).
+ learned = remember and everywhere and learnable(heard, word) is not None
+ if learned:
self.vocabulary.confirm(heard, word, source="user")
- Log.d("fix edited lines=%d keep=%s" % (len(targets), remember))
- self._say(LF("已改 %lld 处:%@ → %@,记进词表", len(targets), heard, word) if remember
+ Log.d("fix edited lines=%d learned=%s" % (len(targets), learned))
+ self._say(LF("已改 %lld 处:%@ → %@,记进词表", len(targets), heard, word) if learned
else LF("已改 %lld 处:%@ → %@", len(targets), heard, word))
return len(targets)
diff --git a/linux/cicada/core/vocabulary.py b/linux/cicada/core/vocabulary.py
index 108eba4..92d283a 100644
--- a/linux/cicada/core/vocabulary.py
+++ b/linux/cicada/core/vocabulary.py
@@ -55,6 +55,33 @@ def key(s: str) -> str:
return " ".join(unicodedata.normalize("NFKC", s).lower().split())
+def _punct(c: str) -> bool:
+ return unicodedata.category(c).startswith("P")
+
+
+def _edge(c: str) -> bool:
+ return c.isspace() or unicodedata.category(c)[0] in "PS"
+
+
+def learnable(heard: str, word: str):
+ """What a fix can teach the list, or None (Vocabulary.swift's learnable): 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."""
+ def trim(s: str) -> str:
+ a, b = 0, len(s)
+ while a < b and _edge(s[a]):
+ a += 1
+ while b > a and _edge(s[b - 1]):
+ b -= 1
+ return s[a:b]
+ h, w = trim(heard), trim(word)
+ if (not h or not w or len(h) > 20 or len(w) > 40 or key(h) == key(w) or h in w or w in h
+ or any(_punct(c) for c in h) or any(_punct(c) and c not in ".-" for c in w)):
+ return None
+ return h, w
+
+
def default_path() -> str:
return os.path.join(Paths.data(), "vocabulary.json")
@@ -115,7 +142,13 @@ class Vocabulary:
return any(key(t.word) == w and any(key(x) == h for x in t.heard) for t in self.terms)
def confirm(self, heard: str, word: str, source: str = "live") -> None:
- """The user said yes to a fix (or added it): the word, and the form it was heard as."""
+ """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."""
+ if heard:
+ pair = learnable(heard, word)
+ if pair is None:
+ return
+ heard, word = pair
with self._gate:
i = self._find(word)
if i is not None:
diff --git a/linux/tests/test_fixes.py b/linux/tests/test_fixes.py
index 006e56b..ae5a240 100644
--- a/linux/tests/test_fixes.py
+++ b/linux/tests/test_fixes.py
@@ -212,6 +212,17 @@ class Vocab(Base):
v.forget("开布特", "Curve")
self.assertFalse(v.knows("开布特", "Curve"))
+ def test_edits_that_arent_a_word_for_a_word_teach_nothing(self):
+ v = Vocabulary(None)
+ v.confirm("持。", "讲。")
+ self.assertTrue(v.knows("持", "讲"))
+ v.confirm("我来", "OK我来", source="user")
+ v.confirm("然后,", "然后")
+ self.assertEqual([t.word for t in v.terms], ["讲"])
+ v.confirm("木娜", "Monad", source="user")
+ v.confirm("Uni斯图 V三", "Uniswap V3")
+ self.assertTrue(v.knows("木娜", "Monad") and v.knows("Uni斯图 V三", "Uniswap V3"))
+
def test_prompt_puts_recent_first(self):
v = Vocabulary(None)
v.import_text("A\nB = b")
@@ -304,6 +315,13 @@ class Live(Base):
self.assertEqual(store.line(sid, "l0").shown, "我木娜找了一个")
self.assertFalse(fx.vocabulary.knows("木娜", "Monad"))
+ def test_edit_that_adds_words_around_is_applied_but_not_learned(self):
+ fx, store, sid = self.setup(["我来说一下", "别的"])
+ fx.edit("l0", sid, "OK我来说一下", everywhere=True, remember=True)
+ self.assertEqual(store.line(sid, "l0").shown, "OK我来说一下")
+ self.assertEqual(fx.vocabulary.terms, [])
+ self.assertNotIn("记进词表", fx.toast_text())
+
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)