知了 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: the meeting record no longer moves a word from one speaker's sentence into the next speaker's line
wquguru4cb8512b0d2b
diff --git a/Sources/Cicada/RecordRefiner.swift b/Sources/Cicada/RecordRefiner.swiftindex c5d44dd..38e57f3 100644--- a/Sources/Cicada/RecordRefiner.swift+++ b/Sources/Cicada/RecordRefiner.swift@@ -99,9 +99,12 @@ struct RecordRefiner {var j = mif freeTail { j = (0...m).min { d[n][$0] < d[n][$1] || d[n][$0] == d[n][$1] && $0 > $1 }! }// Walk back, noting the record position at each live position (the furthest one).+ // `low`: the nearest one, before the record words inserted there.var at = Array(repeating: 0, count: n + 1)+ var low = atvar i = nat[n] = j+ low[n] = jwhile i > 0 || j > 0 {if i > 0, j > 0, d[i][j] == d[i - 1][j - 1] + (Self.same(live[i - 1], record[j - 1]) ? 0 : 1) {i -= 1; j -= 1@@ -109,9 +112,11 @@ struct RecordRefiner {i -= 1} else {j -= 1+ low[i] = jcontinue}if at[i] == 0 { at[i] = j }+ low[i] = j}var ends = bounds.map { b in at[b] }// The live lines were often cut mid-sentence for the subtitles: a line ends at the record's@@ -119,13 +124,23 @@ struct RecordRefiner {// Otherwise it ends between words: Chinese comes one character per run, and "审 / 计" is no line end.let words = Self.wordStarts(record)let inside = HeardText.insideConnectives(record)+ // A line the speaker ended a sentence on was not cut: its end is where the next voice may+ // start, and the record often drops the "." ("lock that in" + " Okay,"): snapping there+ // took the next speaker's word. It only takes the mark that ends its last word. Words the+ // live recognizer lost between the two ("Friday." + " Great." + "Let's…") cost the same on+ // either side: its "." is the record's first one there, and they start the next line.+ let mark: (Character) -> Bool = { "。!?.!?".contains($0) }for k in ends.indices {- var e = Self.snap(ends[k], in: record)+ let closed = lines[k].last.map(mark) ?? false+ var e = ends[k]+ if !closed { e = Self.snap(e, in: record) }+ else if bounds[k] > 0, case let p = low[bounds[k] - 1], p < e - 1, mark(record[p]) { e = p + 1 }if e == ends[k], e > 0, e < m, !words.contains(e) {let after = (e...m).first { words.contains($0) || $0 == m } ?? mlet before = (0...e).last { words.contains($0) } ?? 0e = after - e <= e - before ? after : before}+ if closed, e > 0, e < m, mark(record[e]) { e += 1 }// "所 / 以": the tokenizer splits some connectives; the line ends before the whole word.if let start = inside[e] { e = start }ends[k] = max(e, k > 0 ? ends[k - 1] : 0)diff --git a/Tests/CicadaTests/RecordRefinerTests.swift b/Tests/CicadaTests/RecordRefinerTests.swiftindex 526a7d6..b738953 100644--- a/Tests/CicadaTests/RecordRefinerTests.swift+++ b/Tests/CicadaTests/RecordRefinerTests.swift@@ -96,6 +96,61 @@ import Testing#expect(out.map(\.text).joined() == "他这个管理员可能就是因为他权限比较大了审计日志一定要记下来")}+ /// Two voices 0.2 s apart (the sample meeting): no word crosses the end of a line that closed a+ /// sentence, though the record heard "Great." where the live line had "Great,".+ @Test func wordsStayWithTheirSpeaker() {+ var r = RecordRefiner()+ _ = r.emitted(id: 1, start: 0, end: 4, text: "Uh, I think we can ship March 3rd if design signs off by Friday.")+ _ = r.emitted(id: 2, start: 4.2, end: 5.5, text: "Great, let's lock that in.")+ var out = r.heard([run(" Uh, I think we can ship March 3rd if design signs off by Friday.", 0.1, 3.9)], through: 4.1)+ out += r.heard([run(" Great.", 4.2, 4.6)])+ out += r.heard([run(" let's lock that in", 4.7, 5.4)])+ _ = r.emitted(id: 3, start: 9.5, end: 11, text: "Okay, so where are we on the launch?")+ out += r.heard([run(" Okay, so where are we on the launch?", 9.5, 11)], through: 11.2)+ out += r.finish()+ #expect(out.map(\.text).map { $0.trimmingCharacters(in: .whitespaces) } == [+ "Uh, I think we can ship March 3rd if design signs off by Friday.",+ "Great. let's lock that in",+ "Okay, so where are we on the launch?",+ ])+ // The record heard the two sentences as one.+ var one = RecordRefiner()+ _ = one.emitted(id: 1, start: 0, end: 4, text: "Uh, I think we can ship March 3rd if design signs off by Friday.")+ _ = one.emitted(id: 2, start: 4.2, end: 5.5, text: "Great, let's lock that in.")+ _ = one.emitted(id: 3, start: 9.5, end: 11, text: "Okay, so where are we on the launch?")+ let both = one.heard([run(" Uh, I think we can ship March 3rd if design signs off by Friday. Great. let's lock that in", 0.1, 5.4)], through: 5.6)+ #expect(both.map { $0.text.trimmingCharacters(in: .whitespaces) } == [+ "Uh, I think we can ship March 3rd if design signs off by Friday.", "Great. let's lock that in",+ ])+ }++ /// The next line comes late (the live recognizer dropped "Great."): the record's words past a+ /// line that closed a sentence wait for it.+ @Test func closedLineLeavesTheRestForTheNext() {+ var r = RecordRefiner()+ _ = r.emitted(id: 1, start: 0, end: 4, text: "Uh, I think we can ship March 3rd if design signs off by Friday.")+ var out = r.heard([run(" Uh, I think we can ship March 3rd if design signs off by Friday.", 0.1, 3.9)], through: 4.1)+ out += r.heard([run(" Great.", 4.2, 4.6)])+ out += r.heard([run(" let's lock that in", 4.7, 5.4)], through: 5.6)+ // The line it was said in comes now: the first one settles with it as context.+ out += r.emitted(id: 2, start: 4.7, end: 5.4, text: "Let's lock that in")+ out += r.finish()+ #expect(out.map { $0.text.trimmingCharacters(in: .whitespaces) } == [+ "Uh, I think we can ship March 3rd if design signs off by Friday.", "Great. let's lock that in",+ ])+ }++ /// A closed line whose end the alignment puts inside its last word still keeps its ".".+ @Test func closedLineKeepsItsMark() {+ var r = RecordRefiner()+ _ = r.emitted(id: 1, start: 0, end: 0.5, text: "Great.")+ _ = r.emitted(id: 2, start: 0.7, end: 1.5, text: "lock that in.")+ _ = r.emitted(id: 3, start: 6, end: 7, text: "Okay, so where are we on the launch?")+ var out = r.heard([run(" Great.", 0, 0.5)])+ out += r.heard([run(" Let's lock that in", 0.6, 1.5)], through: 1.7)+ #expect(out.first?.text.trimmingCharacters(in: .whitespaces) == "Great.")+ }+@Test func finishSettlesTheRest() {var r = RecordRefiner()_ = r.emitted(id: 1, start: 0, end: 2, text: "好的。")