知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.
Swift androidlinuxlive-captionsmacosmeeting-notesmenubar-appsimultaneous-interpretationspeech-recognitionsubtitlesswiftswiftuitranslationwindows
fix(windows): fixing misheard words no longer turns a Chinese word into its English translation, or changes letters inside an English word
wquguru9ac1feb789fe
diff --git a/windows/src/Cicada.Core/Correction.cs b/windows/src/Cicada.Core/Correction.csindex 0e954f3..db7fed1 100644--- a/windows/src/Cicada.Core/Correction.cs+++ b/windows/src/Cicada.Core/Correction.cs@@ -76,11 +76,21 @@ public static class Correctionif (!latinFrom && !latinTo && Han(c.From) && Han(c.To) && SoundsAlike(c.From, c.To)) return Verdict.Apply;if (latinFrom && latinTo && c.From.All(char.IsAscii) && c.To.All(char.IsAscii) && e.Confidence >= SpellingAt&& Distance(c.From.ToLowerInvariant(), c.To.ToLowerInvariant()) <= 2) return Verdict.Apply;- // A new name (Chinese heard as a Latin name, or a respelled one): sound can't check it.- if (latinTo && e.Confidence >= AskAt) return Verdict.Ask;+ // 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 (latinTo && e.Confidence >= AskAt && NameShaped(c.To)) return Verdict.Ask;return Verdict.Drop;}+ /// Latin text that could be a name: a capital or a digit in it (Morpho, GPT4o), not "platform".+ public static bool NameShaped(string s) => s.Any(c => char.IsAsciiLetterUpper(c) || char.IsAsciiDigit(c));++ /// A Chinese word "fixed" into a plain English one (平台 → platform): a translation, not a+ /// misheard name.+ public static bool Translation(string heard, string word) =>+ Han(heard) && word.Any(char.IsAsciiLetter) && word.All(char.IsAscii) && !NameShaped(word);+// Toneless pinyin for every CJK Unified Ideograph (U+4E00–U+9FFF) that has a reading: Windows// has no Mandarin transform of its own, so this table was generated once from Apple's ICU/CLDR// Han-Latin transform (the one the Mac uses, kCFStringTransformMandarinLatin with diacritics@@ -146,6 +156,24 @@ public static class Correctionreturn row[b.Length];}+ /// 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平台".+ public static int WordIn(string s, string text)+ {+ if (s.Length == 0) return -1;+ static bool Latin(char c) => char.IsAsciiLetterOrDigit(c);+ for (int from = 0; from < text.Length;)+ {+ int at = text.IndexOf(s, from, StringComparison.Ordinal);+ if (at < 0) return -1;+ int end = at + s.Length;+ if (!(Latin(s[0]) && at > 0 && Latin(text[at - 1])) && !(Latin(s[^1]) && end < text.Length && Latin(text[end]))) return at;+ from = at + 1;+ }+ return -1;+ }+/// The line as shown: each applied fix replaces its words where they still are. The marks are/// the replaced words (start, length in the shown text), for the dotted underline.public static (string Text, List<(int Start, int Length)> Marks) Apply(IReadOnlyList<LineFix> fixes, string src)@@ -156,7 +184,8 @@ public static class Correctionforeach (var f in fixes.Where(f => f.User != true).Concat(fixes.Where(f => f.User == true))){if (!f.Applied || f.From.Length == 0) continue;- var at = text.IndexOf(f.From, StringComparison.Ordinal);+ var at = WordIn(f.From, text);+ if (at < 0) at = text.IndexOf(f.From, StringComparison.Ordinal);if (at < 0) continue;var delta = f.To.Length - f.From.Length;for (int k = 0; k < spans.Count; k++) if (spans[k].At > at) spans[k] = (spans[k].Word, spans[k].At + delta);diff --git a/windows/src/Cicada.Core/LiveFixes.cs b/windows/src/Cicada.Core/LiveFixes.csindex e8d69b5..777ca4a 100644--- a/windows/src/Cicada.Core/LiveFixes.cs+++ b/windows/src/Cicada.Core/LiveFixes.cs@@ -108,8 +108,10 @@ public sealed class LiveFixesforeach (var t in Vocabulary.Terms)foreach (var h in t.Heard){- // A heard form two words claim (两个都留) is never fixed silently.- if (Correction.Length(h) < 2 || !src.Contains(h, StringComparison.Ordinal) || refused.Contains(Pair(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 (Correction.Length(h) < 2 || Correction.WordIn(h, src) < 0 || refused.Contains(Pair(h, t.Word))+ || (t.Source != "user" && Correction.Translation(h, t.Word))|| Vocabulary.Owners(h, t.Word).Count > 0) continue;if (out_.Any(f => f.From == h || (f.Applied && f.From.Contains(h, StringComparison.Ordinal)) || (f.Off == true && f.From == h))) continue;out_.Add(new LineFix(h, t.Word));diff --git a/windows/src/Cicada.Core/Vocabulary.cs b/windows/src/Cicada.Core/Vocabulary.csindex 3804ca1..1857597 100644--- a/windows/src/Cicada.Core/Vocabulary.cs+++ b/windows/src/Cicada.Core/Vocabulary.cs@@ -81,12 +81,13 @@ public sealed class Vocabulary}/// 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 the word only, or nothing.+ /// that isn't a word for a word (`Learnable`) adds the word only, or nothing. A yes in a meeting+ /// never teaches a translation (平台 → platform): it would rewrite that word everywhere after.public void Confirm(string heard, string word, string source = "live"){if (heard.Length > 0){- if (Learnable(heard, word) is not { } l) return;+ if (Learnable(heard, word) is not { } l || (source != "user" && Correction.Translation(l.Heard, l.Word))) return;(heard, word) = l;}lock (gate)diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.csindex 5a12790..0d9c0bb 100644--- a/windows/tests/Cicada.Tests/Program.cs+++ b/windows/tests/Cicada.Tests/Program.cs@@ -2814,6 +2814,28 @@ static class TestsEqual(Correction.Verdict.Drop, Verdict("莫纳", "Morpho", 0.7), "莫纳");}+ /// A learned "S平 → X平" never reaches into "iOS平台"; on its own it still applies.+ public static void FixLearnedWordsStayWhole()+ {+ Check(Correction.WordIn("S平", "iOS平台很好") < 0, "inside iOS");+ Check(Correction.WordIn("A工", "DATA工具") < 0, "inside DATA");+ Check(Correction.WordIn("S平", "在S平台上") >= 0, "on its own");+ Check(Correction.WordIn("S平", "iOS平台和S平台") >= 0, "the second one");+ Check(Correction.WordIn("莫拉", "解释这个莫拉的") >= 0, "Chinese edges");+ Equal("iOS平台和X平台", Correction.Apply([new("S平", "X平")], "iOS平台和S平台").Text, "applied to the whole one");+ }++ /// A translation isn't a misheard name: one 对 to "X平台 → X platform" taught 平台 → platform.+ public static void FixTranslationsAreNotAsked()+ {+ Equal(Correction.Verdict.Drop, Verdict("X平台", "X platform", 0.9), "X平台");+ Equal(Correction.Verdict.Drop, Verdict("平台", "platform", 0.95), "平台");+ Equal(Correction.Verdict.Drop, Verdict("respons", "responsible", 0.85), "respons");+ Equal(Correction.Verdict.Ask, Verdict("GPT四", "GPT4", 0.85), "GPT四");+ Check(Correction.Translation("平台", "platform"), "a translation");+ Check(!Correction.Translation("莫拉", "Morpho"), "a name");+ }+public static void FixConfirmedHeardFormIsApplied(){Equal(Correction.Verdict.Apply, Verdict("莫拉", "Morpho", 0.5, known: [("莫拉", "Morpho")]), "known");@@ -2979,6 +3001,15 @@ static class Testsfinally { Strings.Load(Path.Combine(AppContext.BaseDirectory, "Resources"), "zh-Hans"); }}+ public static void VocabAYesNeverTeachesATranslation()+ {+ var v = new Vocabulary(null);+ v.Confirm("平台", "platform");+ Equal(0, v.Terms.Count, "a yes in a meeting");+ v.Confirm("平台", "platform", source: "user");+ Check(v.Knows("平台", "platform"), "the user's own");+ }+public static void VocabImportLinesWithHeardForms(){var v = new Vocabulary(null);