知了 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: keep the words fast speech loses when a long line is ended
wquguru2893a023177d
diff --git a/Sources/LiveSubs/HeardText.swift b/Sources/LiveSubs/HeardText.swiftindex 7860e23..4a07fc5 100644--- a/Sources/LiveSubs/HeardText.swift+++ b/Sources/LiveSubs/HeardText.swift@@ -22,6 +22,22 @@ enum HeardText {return s.trimmingCharacters(in: .whitespacesAndNewlines)}+ /// The final text, unless it lost a good part of what the last draft of the line had. In fast+ /// speech without a break the recognizer's final pass drops whole phrases ("needs the budget.+ /// for next year by the end. of this month. in.") that its drafts had heard (issue #1), whether+ /// the line ended on its own or was cut. The draft may misspell a word; that beats losing five.+ static func fuller(final: String, draft: String) -> String {+ // Words, or characters in Chinese and Japanese (about two to a word).+ let cjk = (final + draft).unicodeScalars.contains { $0.properties.isIdeographic }+ func size(_ s: String) -> Int {+ cjk ? s.filter { $0.isLetter || $0.isNumber }.count+ : s.split(whereSeparator: { $0.isWhitespace }).filter { $0.contains(where: { $0.isLetter || $0.isNumber }) }.count+ }+ let f = size(final), d = size(draft)+ guard d >= f + (cjk ? 6 : 3), Double(d) >= Double(f) * 1.15 else { return final }+ return clean(draft)+ }+/// The longest pause, in seconds, that still joins two pieces of one sentence.static let joinGap = 1.5/// A line this long takes nothing more: past it a translation is slow and the line hard to read.diff --git a/Sources/LiveSubs/ListenEngine.swift b/Sources/LiveSubs/ListenEngine.swiftindex d50f92f..e517c00 100644--- a/Sources/LiveSubs/ListenEngine.swift+++ b/Sources/LiveSubs/ListenEngine.swift@@ -266,6 +266,8 @@ final class ListenEngine {lastVolatileSent = ""volatileEpoch += 1cutAsked = false+ // The new transcriber's audio clock starts again at zero.+ lastCutThrough = 0onStateChange?()let transcriber = SpeechTranscriber(locale: Locale(identifier: lang.locale),@@ -325,6 +327,12 @@ final class ListenEngine {var text = String(result.text.characters).trimmingCharacters(in: .whitespacesAndNewlines)if result.isFinal {text = HeardText.clean(text)+ let fuller = HeardText.fuller(final: text, draft: volatileText)+ if fuller != text {+ debugLog("final dropped words the draft had: kept the draft")+ contentLog("FINAL was: \(text)")+ text = fuller+ }volatileText = ""volatileEpoch += 1// A "." or "…" from a breath or a click is no sentence: Ling would answer it with a note that@@ -588,7 +596,8 @@ final class ListenEngine {Task { [weak self] indo {try await analyzer.finalize(through: CMTime(seconds: through, preferredTimescale: 1000))- debugLog(String(format: "cut: line of %d chars ended after %.1f s (%@)", chars, held, why))+ debugLog(String(format: "cut: line of %d chars ended after %.1f s (%@) at %.1f s of %.1f s",+ chars, held, why, through, c.fed))} catch {debugLog("cut: finalize failed \(error)")self?.cutAsked = falsediff --git a/Tests/LiveSubsTests/HeardTextTests.swift b/Tests/LiveSubsTests/HeardTextTests.swiftindex 7fb17d6..ed9e46c 100644--- a/Tests/LiveSubsTests/HeardTextTests.swift+++ b/Tests/LiveSubsTests/HeardTextTests.swift@@ -24,6 +24,21 @@ import Testing#expect(HeardText.clean("........") == "")}+ @Test func keepTheDraftWhenTheFinalDroppedPhrases() {+ // Issue #1, from a 200 wpm run: the final pass lost "the support team asked", "numbers", "foxtrot 6".+ let final = "for a new escalation process for enterprise customers, and Echo 5 Finance, needs the budget. for next year by the end. of this month. in."+ let draft = "to for the support team asked for a new escalation process for enterprise customers and echo 5 Finance needs the budget numbers for next year by the end of this month in foxtrot 6"+ #expect(HeardText.fuller(final: final, draft: draft) == draft)+ }++ @Test func keepTheFinalWhenItHasAboutAsMuch() {+ let final = "We need the design review finished by Tuesday, and the audit too."+ #expect(HeardText.fuller(final: final, draft: "We need the design review finished by Tuesday and the the audit too") == final)+ #expect(HeardText.fuller(final: final, draft: "") == final)+ #expect(HeardText.fuller(final: "我们周五之前把文案定下来。", draft: "我们周五之前把落地页的文案定下来") == "我们周五之前把文案定下来。")+ #expect(HeardText.fuller(final: "我们周五。", draft: "我们周五之前把落地页的文案定下来") == "我们周五之前把落地页的文案定下来")+ }+@Test func joinOnlyWhatCarriesOn() {// Cut mid-sentence by a pause, carried on at once.#expect(HeardText.continues("that… in increased production that you would have expected at this",