知了 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): 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
wquguru53846adf3d0f
diff --git a/windows/src/Cicada.Core/Correction.cs b/windows/src/Cicada.Core/Correction.csindex 56ad961..d5ad5ac 100644--- a/windows/src/Cicada.Core/Correction.cs+++ b/windows/src/Cicada.Core/Correction.cs@@ -197,18 +197,66 @@ public static class Correctionreturn [.. kept, .. added];}- /// 改这句…: 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").- public static (LineFix Fix, string Heard)? UserEdit(string shown, string edited, string src, IReadOnlyList<LineFix> fixes)+ /// 改这句…: 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").+ public static (LineFix Fix, string Heard, bool Swap)? UserEdit(string shown, string edited, string src, IReadOnlyList<LineFix> fixes){var now = edited.Trim();if (now.Length == 0 || now == shown) return null;- var c = Core(shown, now);- if (c.From.Length == 0 || c.To.Length == 0) return null;- var heard = c.From;- if (!src.Contains(c.From, StringComparison.Ordinal) && fixes.FirstOrDefault(f => f.Applied && c.From.Contains(f.To, StringComparison.Ordinal)) is { } earlier)+ // Characters as the Mac counts them (grapheme clusters), so the spans match Swift's.+ static string[] Chars(string x)+ {+ var e = StringInfo.GetTextElementEnumerator(x);+ var o = new List<string>();+ while (e.MoveNext()) o.Add(e.GetTextElement());+ return [.. o];+ }+ string[] a = Chars(shown), b = Chars(now);+ int p = 0;+ while (p < Math.Min(a.Length, b.Length) && a[p] == b[p]) p++;+ int q = 0;+ while (q < Math.Min(a.Length, b.Length) - p && a[a.Length - 1 - q] == b[b.Length - 1 - q]) q++;+ bool swap = a.Length - p - q > 0 && b.Length - p - q > 0;+ // Tokens of the shown line: a run of ASCII letters and digits, else one character.+ static bool Latin(string c) => c.Length == 1 && c[0] < 128 && char.IsLetterOrDigit(c[0]);+ var tokens = new List<(int Lo, int Hi)>();+ for (int k = 0; k < a.Length;)+ {+ int e = k + 1;+ if (Latin(a[k])) while (e < a.Length && Latin(a[e])) e++;+ tokens.Add((k, e));+ k = e;+ }+ int start = p, end = a.Length - q;+ foreach (var t in tokens) if (t.Lo <= start && start < t.Hi) { start = t.Lo; break; }+ if (end > start) foreach (var t in tokens) if (t.Lo <= end - 1 && end - 1 < t.Hi) { end = t.Hi; break; }+ string Span() => string.Concat(a[start..end]);+ int Count(string x)+ {+ if (x.Length == 0) return 0;+ int n = 0;+ for (int at = shown.IndexOf(x, StringComparison.Ordinal); at >= 0; at = shown.IndexOf(x, at + x.Length, StringComparison.Ordinal)) n++;+ return n;+ }+ while (!Span().Any(char.IsLetterOrDigit) || Count(Span()) != 1)+ {+ int before = tokens.FindLastIndex(t => t.Hi <= start), after = tokens.FindIndex(t => t.Lo >= end);+ if (before >= 0) start = tokens[before].Lo;+ else if (after >= 0) end = tokens[after].Hi;+ else break;+ }+ var from = Span();+ var to = string.Concat(b[start..(b.Length - (a.Length - end))]);+ if (from.Length == 0 || from == to) return null;+ var heard = from;+ if (!src.Contains(from, StringComparison.Ordinal) && fixes.FirstOrDefault(f => f.Applied && from.Contains(f.To, StringComparison.Ordinal)) is { } earlier)heard = Core(earlier.From, earlier.To).From;- return (new LineFix(c.From, c.To, user: true), heard);+ return (new LineFix(from, to, user: true), heard, swap);}}diff --git a/windows/src/Cicada.Core/LiveFixes.cs b/windows/src/Cicada.Core/LiveFixes.csindex 8106156..3ac2c0f 100644--- a/windows/src/Cicada.Core/LiveFixes.cs+++ b/windows/src/Cicada.Core/LiveFixes.cs@@ -212,12 +212,13 @@ public sealed class LiveFixes// MARK: 改这句…/// How many lines of the meeting the same edit would change ("整场听成 木娜 的 2 处都改").- public (string Heard, int Count)? SameHeard(string lineId, string id, string edited)+ /// An edit that only adds or drops words stays on its line (count 1, nothing to learn).+ public (string Heard, int Count, bool Swap)? SameHeard(string lineId, string id, string edited){if (store.Get(id) is not { } s || s.Lines.FirstOrDefault(l => l.Id == lineId) is not { } l|| Correction.UserEdit(l.Shown, edited, l.Src, l.Fixes ?? []) is not { } e) return null;- var n = s.Lines.Count(x => x.Shown.Contains(e.Fix.From, StringComparison.Ordinal));- return (e.Heard, Math.Max(1, n));+ var n = e.Swap ? s.Lines.Count(x => x.Shown.Contains(e.Fix.From, StringComparison.Ordinal)) : 1;+ return (e.Heard, Math.Max(1, n), e.Swap);}/// The user rewrote a line (改这句…): what changed becomes their own fix, on this line or on@@ -231,6 +232,7 @@ public sealed class LiveFixes{if (store.Get(id) is not { } s || s.Lines.FirstOrDefault(l => l.Id == lineId) is not { } l|| Correction.UserEdit(l.Shown, edited, l.Src, l.Fixes ?? []) is not { } e) return 0;+ everywhere = everywhere && e.Swap;var targets = everywhere ? s.Lines.Where(x => x.Shown.Contains(e.Fix.From, StringComparison.Ordinal)).ToList() : [l];Remember(id, targets.Select(t => t.Id));foreach (var t in targets)@@ -247,6 +249,9 @@ public sealed class LiveFixes// word for a word (`Vocabulary.Learnable`).learned = remember && everywhere && Vocabulary.Learnable(heard, to) is not null;if (learned) Vocabulary.Confirm(heard, to, "user");+ // A swap reads as the words swapped; an added or dropped word as the span it changed+ // ("in → in now", not "in → now").+ if (!e.Swap) (heard, to) = (e.Fix.From, e.Fix.To);}Log.Debug($"fix edited lines={count} learned={learned}");Say(learned ? Strings.LF("已改 %lld 处:%@ → %@,记进词表", count, heard, to) : Strings.LF("已改 %lld 处:%@ → %@", count, heard, to));diff --git a/windows/src/Cicada/FixViews.cs b/windows/src/Cicada/FixViews.csindex 69d60a3..065a1b7 100644--- a/windows/src/Cicada/FixViews.cs+++ b/windows/src/Cicada/FixViews.cs@@ -238,10 +238,14 @@ static class FixViewsone.Checked += (_, _) => { everywhere = false; Options(); };stack.Children.Add(all); stack.Children.Add(one);}- var remember = new CheckBox { Content = L("记进词表,以后自动改"), IsChecked = keep, FontSize = 12, Margin = new Thickness(0, 6, 0, 0), IsEnabled = same.Count <= 1 || everywhere };- remember.Checked += (_, _) => keep = true;- remember.Unchecked += (_, _) => keep = false;- stack.Children.Add(remember);+ // Only a word for a word can teach the list: an added or dropped word can't.+ if (same.Swap)+ {+ var remember = new CheckBox { Content = L("记进词表,以后自动改"), IsChecked = keep, FontSize = 12, Margin = new Thickness(0, 6, 0, 0), IsEnabled = same.Count <= 1 || everywhere };+ remember.Checked += (_, _) => keep = true;+ remember.Unchecked += (_, _) => keep = false;+ stack.Children.Add(remember);+ }options.Content = stack;}diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.csindex 8a0a5eb..6e04874 100644--- a/windows/tests/Cicada.Tests/Program.cs+++ b/windows/tests/Cicada.Tests/Program.cs@@ -2836,6 +2836,41 @@ static class TestsCheck(Correction.UserEdit("a", " a ", "a", []) is null, "unchanged");}+ public static void FixAddedWordStaysOnItsLineAndNeverMatchesPunctuation()+ {+ // "now" added before the full stop: the fix is the last word, unique in the line, not ".".+ const string line = "Great, let's lock that in.";+ var e = Correction.UserEdit(line, "Great, let's lock that in now.", line, []);+ Equal("in", e?.Fix.From, "from");+ Equal("in now", e?.Fix.To, "to");+ Equal(false, e?.Swap, "added, not swapped");+ Equal("Great, let's lock that in now.", Correction.Apply([e!.Value.Fix], line).Text, "applied");+ // A word added at the start.+ var s = Correction.UserEdit(line, "OK Great, let's lock that in.", line, []);+ Equal("Great", s?.Fix.From, "start from");+ Equal("OK Great", s?.Fix.To, "start to");+ Equal(false, s?.Swap, "start swap");+ // A word that appears twice: grown until the span is unique.+ var t = Correction.UserEdit("in the room in the hall", "in the room inside the hall", "in the room in the hall", []);+ Equal("in the room inside the hall", Correction.Apply([t!.Value.Fix], "in the room in the hall").Text, "unique span");+ // Chinese: an inserted word takes its neighbour.+ var c = Correction.UserEdit("好的我来看一下", "好的OK我来看一下", "好的我来看一下", []);+ Equal("好的OK我来看一下", c is { } cc ? Correction.Apply([cc.Fix], "好的我来看一下").Text : null, "Chinese insert");+ Equal(false, c?.Swap, "Chinese swap");+ // A real swap is still a swap.+ Equal(true, Correction.UserEdit("我木娜找了", "我Monad找了", "我木娜找了", [])?.Swap, "swap");+ }++ public static void FixesAddedWordEditStaysOnItsLine()+ {+ var (fx, store, id) = FixSetup("Great, let's lock that in.", "Done.");+ Equal(1, fx.SameHeard("l0", id, "Great, let's lock that in now.")?.Count, "one line");+ Equal(1, fx.Edit("l0", id, "Great, let's lock that in now.", everywhere: true, remember: true), "changed");+ Equal("Done.", FixLine(store, id, "l1").Shown, "the full stop elsewhere untouched");+ Equal(0, fx.Vocabulary.Terms.Count, "nothing taught");+ Check(fx.Toast?.Text.Contains("in → in now") == true, "the toast shows the span it changed: " + fx.Toast?.Text);+ }+public static void NumberedIntegerPlaceholders(){var files = new Dictionary<string, string> { ["en.lproj/Localizable.strings"] = "\"整场听成 %@ 的 %lld 处都改\" = \"All %2$lld places heard as %1$@ in this meeting\";" };