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(windows): Edit line… no longer teaches the word list punctuation or words added around the heard ones

wquguru331e094a6ded
diff --git a/windows/src/Cicada.Core/LiveFixes.cs b/windows/src/Cicada.Core/LiveFixes.cs
index a2176b0..8106156 100644
--- a/windows/src/Cicada.Core/LiveFixes.cs
+++ b/windows/src/Cicada.Core/LiveFixes.cs
@@ -226,6 +226,7 @@ public sealed class LiveFixes
{
int count;
string heard, to;
+ bool learned;
lock (gate)
{
if (store.Get(id) is not { } s || s.Lines.FirstOrDefault(l => l.Id == lineId) is not { } l
@@ -242,11 +243,13 @@ public sealed class LiveFixes
count = targets.Count;
heard = e.Heard;
to = Correction.Core(e.Fix.From, e.Fix.To).To.Trim();
- // Only a change made everywhere teaches the word list: a one-off must not.
- if (remember && everywhere) Vocabulary.Confirm(heard, to, "user");
+ // Only a change made everywhere teaches the word list (a one-off must not), and only a
+ // word for a word (`Vocabulary.Learnable`).
+ learned = remember && everywhere && Vocabulary.Learnable(heard, to) is not null;
+ if (learned) Vocabulary.Confirm(heard, to, "user");
}
- Log.Debug($"fix edited lines={count} keep={remember}");
- Say(remember ? Strings.LF("已改 %lld 处:%@ → %@,记进词表", count, heard, to) : Strings.LF("已改 %lld 处:%@ → %@", count, heard, to));
+ Log.Debug($"fix edited lines={count} learned={learned}");
+ Say(learned ? Strings.LF("已改 %lld 处:%@ → %@,记进词表", count, heard, to) : Strings.LF("已改 %lld 处:%@ → %@", count, heard, to));
return count;
}
diff --git a/windows/src/Cicada.Core/Vocabulary.cs b/windows/src/Cicada.Core/Vocabulary.cs
index 6fc5825..3804ca1 100644
--- a/windows/src/Cicada.Core/Vocabulary.cs
+++ b/windows/src/Cicada.Core/Vocabulary.cs
@@ -1,3 +1,4 @@
+using System.Globalization;
using System.Text;
using System.Text.Json.Serialization;
@@ -57,9 +58,37 @@ public sealed class Vocabulary
lock (gate) return Find(word) is { } t && t.Heard.Any(x => Key(x) == h);
}
- /// The user said yes to a fix (or added it): the word, and the form it was heard as.
+ /// What a fix can teach the list, or null: 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.
+ public static (string Heard, string Word)? Learnable(string heard, string word)
+ {
+ static bool Edge(Rune r) => Rune.IsWhiteSpace(r) || Rune.IsPunctuation(r) || Rune.IsSymbol(r);
+ static string Trim(string s)
+ {
+ var r = s.EnumerateRunes().ToList();
+ int a = 0, b = r.Count;
+ while (a < b && Edge(r[a])) a++;
+ while (b > a && Edge(r[b - 1])) b--;
+ return string.Concat(r.Skip(a).Take(b - a).Select(x => x.ToString()));
+ }
+ var h = Trim(heard); var w = Trim(word);
+ if (h.Length == 0 || w.Length == 0 || new StringInfo(h).LengthInTextElements > 20 || new StringInfo(w).LengthInTextElements > 40
+ || Key(h) == Key(w) || w.Contains(h, StringComparison.Ordinal) || h.Contains(w, StringComparison.Ordinal)
+ || h.EnumerateRunes().Any(Rune.IsPunctuation)
+ || w.EnumerateRunes().Any(r => Rune.IsPunctuation(r) && r.Value != '.' && r.Value != '-')) return null;
+ return (h, w);
+ }
+
+ /// 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.
public void Confirm(string heard, string word, string source = "live")
{
+ if (heard.Length > 0)
+ {
+ if (Learnable(heard, word) is not { } l) return;
+ (heard, word) = l;
+ }
lock (gate)
{
if (Find(word) is { } t)
diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.cs
index 42afa06..8a0a5eb 100644
--- a/windows/tests/Cicada.Tests/Program.cs
+++ b/windows/tests/Cicada.Tests/Program.cs
@@ -2912,6 +2912,28 @@ static class Tests
Check(v.Knows("莫拉", "Morpho") && v.Knows("莫法", "Morpho"), "both heard forms");
}
+ public static void VocabEditsThatArentAWordForAWordTeachNothing()
+ {
+ var v = new Vocabulary(null);
+ v.Confirm("持。", "讲。");
+ Check(v.Knows("持", "讲"), "trimmed of the full stop");
+ v.Confirm("我来", "OK我来", "user");
+ v.Confirm("然后,", "然后");
+ Equal("讲", string.Join(",", v.Terms.Select(t => t.Word)), "words around the heard ones teach nothing");
+ v.Confirm("木娜", "Monad", "user");
+ v.Confirm("Uni斯图 V三", "Uniswap V3");
+ Check(v.Knows("木娜", "Monad") && v.Knows("Uni斯图 V三", "Uniswap V3"), "a word for a word is learned");
+ }
+
+ public static void FixesEditThatIsntAWordForAWordStillAppliesButTeachesNothing()
+ {
+ var (fx, store, id) = FixSetup("我来", "我来吧");
+ fx.Edit("l0", id, "OK我来", everywhere: true, remember: true);
+ Equal("OK我来", FixLine(store, id, "l0").Shown, "applied");
+ Equal(0, fx.Vocabulary.Terms.Count, "nothing taught");
+ Check(fx.Toast?.Text.Contains("记进词表") == false, "the toast doesn't say it was saved: " + fx.Toast?.Text);
+ }
+
public static void VocabOwnersAndPreview()
{
var v = new Vocabulary(null);