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): fixing misheard words no longer turns a Chinese word into its English translation, or changes letters inside an English word

wquguruee60b8663d4c
diff --git a/linux/cicada/core/correction.py b/linux/cicada/core/correction.py
index b8121a3..afe0eb2 100644
--- a/linux/cicada/core/correction.py
+++ b/linux/cicada/core/correction.py
@@ -53,6 +53,22 @@ def label(f) -> tuple:
cf, ct = core(f.from_, f.to)
return (f.from_, f.to) if not cf or not ct else (cf, ct)
+def word(s: str, text: str) -> int:
+ """Where `s` stands as words of its own in `text` (its offset, or -1): 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平台"."""
+ if not s:
+ return -1
+ i = text.find(s)
+ while i >= 0:
+ before = text[i - 1] if i > 0 else ""
+ after = text[i + len(s)] if i + len(s) < len(text) else ""
+ if not (_word(s[0]) and before and _word(before)) and not (_word(s[-1]) and after and _word(after)):
+ return i
+ i = text.find(s, i + 1)
+ return -1
+
+
def apply(fixes: list, src: str) -> tuple:
"""The line as shown: each applied fix replaces its words where they still are. The marks are the
replaced words as (start, end) character offsets into the text, for the underline."""
@@ -63,7 +79,9 @@ def apply(fixes: list, src: str) -> tuple:
for f in ordered:
if not f.applied or not f.from_:
continue
- start = text.find(f.from_)
+ start = word(f.from_, text)
+ if start < 0:
+ start = text.find(f.from_)
if start < 0:
continue
delta = len(f.to) - len(f.from_)
@@ -110,12 +128,25 @@ def decide(e: Edit, known, rejected) -> str:
if latin_from and latin_to and cf.isascii() and ct.isascii() and e.confidence >= SPELLING_AT \
and cf.lower() != ct.lower() and distance(cf.lower(), ct.lower()) <= 2:
return APPLY
- # A new name (Chinese heard as a Latin name, or a respelled one): sound can't check it.
- if latin_to and e.confidence >= ASK_AT:
+ # 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 latin_to and e.confidence >= ASK_AT and name_shaped(ct):
return ASK
return DROP
+def name_shaped(s: str) -> bool:
+ """Latin text that could be a name: a capital or a digit in it (Morpho, GPT4o), not "platform"."""
+ return any(c.isascii() and (c.isupper() or c.isdigit()) for c in s)
+
+
+def translation(heard: str, word: str) -> bool:
+ """A Chinese word "fixed" into a plain English one (平台 → platform): a translation, not a
+ misheard name."""
+ return han(heard) and any(_latin_letter(c) for c in word) and word.isascii() and not name_shaped(word)
+
+
def _latin_letter(c: str) -> bool:
return c.isascii() and c.isalpha()
diff --git a/linux/cicada/core/live_fixes.py b/linux/cicada/core/live_fixes.py
index 8680e6c..3f3fe1f 100644
--- a/linux/cicada/core/live_fixes.py
+++ b/linux/cicada/core/live_fixes.py
@@ -127,8 +127,10 @@ class LiveFixes:
out = list(fixes)
for t in self.vocabulary.snapshot():
for h in t.heard:
- # A heard form two words claim (两个都留) is never fixed silently.
- if len(h) < 2 or h not in src or _pair(h, t.word) in self._refused or self.vocabulary.owners(h, t.word):
+ # 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 len(h) < 2 or correction.word(h, src) < 0 or _pair(h, t.word) in self._refused \
+ or (t.source != "user" and correction.translation(h, t.word)) or self.vocabulary.owners(h, t.word):
continue
if any(f.from_ == h or (f.applied and h in f.from_) or (f.off is True and f.from_ == h) for f in out):
continue
diff --git a/linux/cicada/core/vocabulary.py b/linux/cicada/core/vocabulary.py
index 92d283a..eb2c2ea 100644
--- a/linux/cicada/core/vocabulary.py
+++ b/linux/cicada/core/vocabulary.py
@@ -10,7 +10,7 @@ import unicodedata
from dataclasses import dataclass, field
from datetime import datetime
-from . import jsonfmt
+from . import correction, jsonfmt
from .models import iso_read, iso_write, now
from .support import Log, Paths
@@ -143,10 +143,11 @@ class Vocabulary:
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. 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."""
if heard:
pair = learnable(heard, word)
- if pair is None:
+ if pair is None or (source != "user" and correction.translation(*pair)):
return
heard, word = pair
with self._gate:
diff --git a/linux/tests/test_fixes.py b/linux/tests/test_fixes.py
index 8dd4549..08e5bc6 100644
--- a/linux/tests/test_fixes.py
+++ b/linux/tests/test_fixes.py
@@ -135,6 +135,24 @@ class Correction(unittest.TestCase):
self.assertEqual(verdict("Ero", "Euler", 0.85), ASK)
self.assertEqual(verdict("莫纳", "Morpho", 0.7), DROP)
+ def test_learned_words_stay_whole(self):
+ """A learned "S平 → X平" never reaches into "iOS平台"; on its own it still applies."""
+ self.assertEqual(correction.word("S平", "iOS平台很好"), -1)
+ self.assertEqual(correction.word("A工", "DATA工具"), -1)
+ self.assertNotEqual(correction.word("S平", "在S平台上"), -1)
+ self.assertNotEqual(correction.word("S平", "iOS平台和S平台"), -1)
+ self.assertNotEqual(correction.word("莫拉", "解释这个莫拉的"), -1)
+ self.assertEqual(apply([LineFix("S平", "X平")], "iOS平台和S平台")[0], "iOS平台和X平台")
+
+ def test_translations_are_not_asked(self):
+ """A translation isn't a misheard name: one 对 to "X平台 → X platform" taught 平台 → platform."""
+ self.assertEqual(verdict("X平台", "X platform", 0.9), DROP)
+ self.assertEqual(verdict("平台", "platform", 0.95), DROP)
+ self.assertEqual(verdict("respons", "responsible", 0.85), DROP)
+ self.assertEqual(verdict("GPT四", "GPT4", 0.85), ASK)
+ self.assertTrue(correction.translation("平台", "platform"))
+ self.assertFalse(correction.translation("莫拉", "Morpho"))
+
def test_confirmed_heard_form_is_applied(self):
self.assertEqual(verdict("莫拉", "Morpho", 0.5, known=[("莫拉", "Morpho")]), APPLY)
self.assertEqual(verdict("解释这个莫拉的", "解释这个Morpho的", 0.5, known=[("莫拉", "Morpho")]), APPLY)
@@ -239,6 +257,13 @@ class Vocab(Base):
self.assertEqual(len(v.terms), 1)
self.assertTrue(v.knows("阿韦", "Aave"))
+ def test_a_yes_never_teaches_a_translation(self):
+ v = Vocabulary(None)
+ v.confirm("平台", "platform")
+ self.assertEqual(v.terms, [])
+ v.confirm("平台", "platform", source="user")
+ self.assertTrue(v.knows("平台", "platform"))
+
def test_confirm_and_forget(self):
v = Vocabulary(None)
v.confirm("开布特", "Curve")