知了 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(mac,windows,android,linux): names and terms in the notes keep their place when the notes update
wqugurue7a9735e2b67
diff --git a/Sources/Cicada/Notes.swift b/Sources/Cicada/Notes.swiftindex ae82b7a..55f61c4 100644--- a/Sources/Cicada/Notes.swift+++ b/Sources/Cicada/Notes.swift@@ -85,6 +85,15 @@ struct NoteTerm: Codable, Equatable, Identifiable {terms.filter { !$0.heard.isEmpty } + terms.filter { $0.heard.isEmpty }}+ /// The reply's terms in the order they were shown before, new ones after: the model often+ /// returns the same names reshuffled, which would make the chips jump on every update.+ static func keepingOrder(_ old: [NoteTerm], _ new: [NoteTerm]) -> [NoteTerm] {+ let rank = Dictionary(old.enumerated().map { ($1.name, $0) }, uniquingKeysWith: { a, _ in a })+ return new.enumerated().sorted { a, b in+ (rank[a.element.name] ?? old.count + a.offset, a.offset) < (rank[b.element.name] ?? old.count + b.offset, b.offset)+ }.map(\.element)+ }+/// Roles that say a term is a person, for offering it as a speaker's name.private static let roles = ["首席", "执行官", "主席", "总裁", "董事", "经理", "创始人", "记者", "主持", "研究员","分析师", "教授", "老师", "教师", "官员", "部长", "议员", "候选人", "嘉宾", "发言人",@@ -309,6 +318,7 @@ final class NotesEngine: ObservableObject {return}if !final, !fresh { next.topics = Self.merge(base.topics, frozen: frozen, returned: next.topics) }+ if !fresh { next.terms = NoteTerm.keepingOrder((id == shownID ? notes : session.notes)?.terms ?? [], next.terms) }Self.carryTimes(from: base, into: &next)// A reply without a name keeps the last one (not after a language change).if next.title == nil, !fresh { next.title = (id == shownID ? notes : session.notes)?.title }diff --git a/Tests/CicadaTests/NotesTests.swift b/Tests/CicadaTests/NotesTests.swiftindex b2c7cd7..7327489 100644--- a/Tests/CicadaTests/NotesTests.swift+++ b/Tests/CicadaTests/NotesTests.swift@@ -165,6 +165,14 @@ import Testing#expect(NotesEngine.subject("(续)") == "(续)")}+ @Test func termsKeepTheirPlaceAcrossUpdates() {+ let t = { (n: String) in NoteTerm(name: n, note: "", heard: []) }+ let before = [t("A"), t("B"), t("C")]+ #expect(NoteTerm.keepingOrder(before, [t("C"), t("D"), t("A"), t("B")]).map(\.name) == ["A", "B", "C", "D"])+ #expect(NoteTerm.keepingOrder(before, [t("E"), t("B"), t("D")]).map(\.name) == ["B", "E", "D"])+ #expect(NoteTerm.keepingOrder([], [t("B"), t("A")]).map(\.name) == ["B", "A"])+ }+@Test func fixedWordsKeepOneOrderAcrossRedraws() {var lines: [TranscriptLine] = []for (i, w) in ["王总", "Kubernetes", "李雷", "OpenAI", "张三", "Cicada", "李雷"].enumerated() {diff --git a/android/cli/src/main/kotlin/cicada/cli/Tests.kt b/android/cli/src/main/kotlin/cicada/cli/Tests.ktindex 9bd100e..605b561 100644--- a/android/cli/src/main/kotlin/cicada/cli/Tests.kt+++ b/android/cli/src/main/kotlin/cicada/cli/Tests.kt@@ -594,6 +594,9 @@ object Tests {val abc = listOf(topic(0.0, "a"), topic(100.0, "b"), topic(200.0, "c"))equal("a,b,c", NotesEngine.merge(abc, 1, emptyList()).joinToString(",") { it.title }, "an empty reply keeps the topics")equal("a,b,c", NotesEngine.merge(abc, 1, listOf(topic(0.0, "a"))).joinToString(",") { it.title }, "only an echo keeps them too")+ val terms = { names: String -> names.split(",").map { NoteTerm(it) } }+ equal("A,B,C,D", NoteTerm.keepingOrder(terms("A,B,C"), terms("C,D,A,B")).joinToString(",") { it.name }, "terms keep their place, new ones after")+ equal("B,E,D", NoteTerm.keepingOrder(terms("A,B,C"), terms("E,B,D")).joinToString(",") { it.name }, "dropped terms leave, new ones keep the reply's order")// A runaway summary is cut to its first sentence.val runaway = topic(0.0, "t0").apply { summary = "对方展示AI眼镜后觉得是完全新的体验,提议可以出发回去。" + "最新对方又补充了一些内容,".repeat(20) }equal("对方展示AI眼镜后觉得是完全新的体验,提议可以出发回去。", NotesEngine.merge(emptyList(), 0, listOf(runaway))[0].summary, "a runaway summary")diff --git a/android/core/src/main/kotlin/cicada/Models.kt b/android/core/src/main/kotlin/cicada/Models.ktindex 345f394..7ce7908 100644--- a/android/core/src/main/kotlin/cicada/Models.kt+++ b/android/core/src/main/kotlin/cicada/Models.kt@@ -223,5 +223,13 @@ class NoteTerm(var name: String = "", var note: String = "", var heard: MutableL/// Corrected spellings first: what a reader can't work out alone.fun ordered(terms: List<NoteTerm>) = terms.filter { it.heard.isNotEmpty() } + terms.filter { it.heard.isEmpty() }++ /// The reply's terms in the order they were shown before, new ones after: the model often+ /// returns the same names reshuffled, which would make the chips jump on every update.+ fun keepingOrder(old: List<NoteTerm>, new: List<NoteTerm>): List<NoteTerm> {+ val rank = HashMap<String, Int>()+ old.forEachIndexed { i, t -> rank.putIfAbsent(t.name, i) }+ return new.withIndex().sortedWith(compareBy({ rank[it.value.name] ?: (old.size + it.index) }, { it.index })).map { it.value }+ }}}diff --git a/android/core/src/main/kotlin/cicada/Notes.kt b/android/core/src/main/kotlin/cicada/Notes.ktindex a6fffa2..e2c1b3b 100644--- a/android/core/src/main/kotlin/cicada/Notes.kt+++ b/android/core/src/main/kotlin/cicada/Notes.kt@@ -99,6 +99,7 @@ class NotesEngine(private val ling: LingClient, private val store: SessionStore)private fun update(id: String, final: Boolean) {val session: Sessionval base: MeetingNotes+ val prior: List<NoteTerm>synchronized(gate) {if (inFlight) { if (final) pendingFinal = id; return }session = store.get(id) ?: return@@ -108,6 +109,7 @@ class NotesEngine(private val ling: LingClient, private val store: SessionStore)lastAttempt = System.currentTimeMillis()status = State.Updatingbase = if (final) MeetingNotes() else if (id == shownId) notes else session.notes ?: MeetingNotes()+ prior = (if (id == shownId) notes else session.notes)?.terms ?: emptyList()}changed()try {@@ -137,6 +139,7 @@ class NotesEngine(private val ling: LingClient, private val store: SessionStore)return}if (!final) next.topics = merge(base.topics, frozen, next.topics).toMutableList()+ next.terms = NoteTerm.keepingOrder(prior, next.terms).toMutableList()carryTimes(base, next)next.title = next.title ?: base.title ?: session.notes?.titlenext.covered = lines.sizediff --git a/linux/cicada/core/models.py b/linux/cicada/core/models.pyindex 0ca6432..ee22ce8 100644--- a/linux/cicada/core/models.py+++ b/linux/cicada/core/models.py@@ -173,6 +173,15 @@ class NoteTerm:"""Corrected spellings first: what a reader can't work out alone."""return [t for t in terms if t.heard] + [t for t in terms if not t.heard]+ @staticmethod+ def keeping_order(old: list, new: list) -> list:+ """The reply's terms in the order they were shown before, new ones after: the model often+ returns the same names reshuffled, which would make the chips jump on every update."""+ rank: dict = {}+ for i, t in enumerate(old):+ rank.setdefault(t.name, i)+ return [t for _, t in sorted(enumerate(new), key=lambda p: (rank.get(p[1].name, len(old) + p[0]), p[0]))]+@dataclassclass FishboneBranch:diff --git a/linux/cicada/core/notes.py b/linux/cicada/core/notes.pyindex 57edfe7..19d94a6 100644--- a/linux/cicada/core/notes.py+++ b/linux/cicada/core/notes.py@@ -202,6 +202,9 @@ class NotesEngine:returnif not final and not fresh:nxt.topics = merge(base.topics, frozen, nxt.topics)+ if not fresh:+ prior = self.notes if sid == self.shown_id else session.notes+ nxt.terms = NoteTerm.keeping_order(prior.terms if prior else [], nxt.terms)carry_times(base, nxt)nxt.title = nxt.title or base.title or (session.notes.title if session.notes else None)nxt.covered = len(lines)diff --git a/linux/tests/test_core.py b/linux/tests/test_core.pyindex 4610755..61ac08a 100644--- a/linux/tests/test_core.py+++ b/linux/tests/test_core.py@@ -546,6 +546,11 @@ class Segmenting(Base):class Notes(Base):+ def test_terms_keep_their_place(self):+ terms = lambda names: [NoteTerm(n) for n in names.split(",")]+ self.assertEqual([t.name for t in NoteTerm.keeping_order(terms("A,B,C"), terms("C,D,A,B"))], ["A", "B", "C", "D"])+ self.assertEqual([t.name for t in NoteTerm.keeping_order(terms("A,B,C"), terms("E,B,D"))], ["B", "E", "D"])+def test_status_follows_the_key_as_on_the_mac(self):# A key the check refused is "invalid", not "no key"; a fixed key goes back to idle.from cicada.core import ling as lingmoddiff --git a/site/changelog.json b/site/changelog.jsonindex fba25ad..ef9245e 100644--- a/site/changelog.json+++ b/site/changelog.json@@ -16,6 +16,17 @@],"title": "会议记录可以分享成一个链接。","text": "记录右上角点“分享…”,选只分享纪要或连同双语全文,设有效期(7 天、30 天或 1 年),可以加密码。上传前会检查有没有写出来的密码和密钥、未公开的公司信息、不宜跨境的信息、个人身份信息和违法内容,有就先拦下并指出在哪,可以去掉这些内容再分享;检查不可用时不会分享。说话人默认显示为“说话人 N”,录音从不上传,取消分享后链接立即失效。"+ },+ {+ "kind": "fixed",+ "platforms": [+ "mac",+ "windows",+ "android",+ "linux"+ ],+ "title": "纪要里的人名和术语不再一闪一闪。",+ "text": "改正过的词按次数排好后顺序固定,不再每秒换位置;每次更新纪要,已有的人名和术语留在原位,新的排在后面。"}]},diff --git a/windows/src/Cicada.Core/Models.cs b/windows/src/Cicada.Core/Models.csindex 4c773c3..26e49a7 100644--- a/windows/src/Cicada.Core/Models.cs+++ b/windows/src/Cicada.Core/Models.cs@@ -168,4 +168,13 @@ public sealed class NoteTermvar list = terms.ToList();return [.. list.Where(t => t.Heard.Count > 0), .. list.Where(t => t.Heard.Count == 0)];}++ /// The reply's terms in the order they were shown before, new ones after: the model often+ /// returns the same names reshuffled, which would make the chips jump on every update.+ public static List<NoteTerm> KeepingOrder(IReadOnlyList<NoteTerm> old, IReadOnlyList<NoteTerm> next)+ {+ var rank = new Dictionary<string, int>();+ for (var i = 0; i < old.Count; i++) rank.TryAdd(old[i].Name, i);+ return [.. next.Select((t, i) => (t, i)).OrderBy(p => rank.TryGetValue(p.t.Name, out var r) ? r : old.Count + p.i).ThenBy(p => p.i).Select(p => p.t)];+ }}diff --git a/windows/src/Cicada.Core/Notes.cs b/windows/src/Cicada.Core/Notes.csindex b7a9e44..335eabf 100644--- a/windows/src/Cicada.Core/Notes.cs+++ b/windows/src/Cicada.Core/Notes.cs@@ -119,6 +119,7 @@ public sealed class NotesEngine{Session? session;MeetingNotes baseNotes;+ List<NoteTerm> prior;lock (gate){if (inFlight) { if (final) pendingFinal = id; return; }@@ -129,6 +130,7 @@ public sealed class NotesEnginelastAttempt = DateTime.UtcNow;Status = State.Updating;baseNotes = final ? new MeetingNotes() : (id == ShownId ? Notes : session.Notes ?? new MeetingNotes());+ prior = (id == ShownId ? Notes : session.Notes)?.Terms ?? [];}Changed?.Invoke();try@@ -163,6 +165,7 @@ public sealed class NotesEnginereturn;}if (!final && !fresh) next.Topics = Merge(baseNotes.Topics, frozen, next.Topics);+ if (!fresh) next.Terms = NoteTerm.KeepingOrder(prior, next.Terms);CarryTimes(baseNotes, next);next.Title ??= baseNotes.Title ?? session.Notes?.Title;next.Covered = lines.Count;diff --git a/windows/tests/Cicada.Tests/Program.cs b/windows/tests/Cicada.Tests/Program.csindex 01de17a..3dc4d4f 100644--- a/windows/tests/Cicada.Tests/Program.cs+++ b/windows/tests/Cicada.Tests/Program.cs@@ -913,6 +913,9 @@ static class Testsvar abc = new List<NoteTopic> { Topic(0, "a"), Topic(100, "b"), Topic(200, "c") };Equal("a,b,c", Titles(NotesEngine.Merge(abc, 1, [])), "an empty reply keeps the topics");Equal("a,b,c", Titles(NotesEngine.Merge(abc, 1, [Topic(0, "a")])), "a reply echoing only frozen topics too");+ static List<NoteTerm> Terms(string names) => [.. names.Split(',').Select(n => new NoteTerm { Name = n })];+ Equal("A,B,C,D", string.Join(",", NoteTerm.KeepingOrder(Terms("A,B,C"), Terms("C,D,A,B")).Select(t => t.Name)), "terms keep their place, new ones after");+ Equal("B,E,D", string.Join(",", NoteTerm.KeepingOrder(Terms("A,B,C"), Terms("E,B,D")).Select(t => t.Name)), "dropped terms leave, new ones keep the reply's order");}public static void FullLiveNotesJoinTheShortestSettledPair()