知了 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: switch to English within seconds when it follows Chinese with no pause
wqugurud9c44626f67b
diff --git a/Sources/LiveSubs/HeardText.swift b/Sources/LiveSubs/HeardText.swiftindex 4a07fc5..f11b8b9 100644--- a/Sources/LiveSubs/HeardText.swift+++ b/Sources/LiveSubs/HeardText.swift@@ -38,6 +38,16 @@ enum HeardText {return clean(draft)}+ /// The line ends in a run of Latin letters, the way a Chinese, Japanese or Korean model writes+ /// English it hears: 15 letters or more after its last CJK character.+ static func endsInLatin(_ text: String) -> Bool {+ let isCJK: (Character) -> Bool = { c in+ c.unicodeScalars.contains { (0x3040...0x9FFF).contains($0.value) || (0xAC00...0xD7AF).contains($0.value) }+ }+ let tail = text.reversed().prefix(while: { !isCJK($0) })+ return tail.filter { $0.isASCII && $0.isLetter }.count >= 15+ }+/// 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 39226e0..5168e41 100644--- a/Sources/LiveSubs/ListenEngine.swift+++ b/Sources/LiveSubs/ListenEngine.swift@@ -48,6 +48,7 @@ final class ListenEngine {private var pendingSwitch: Lang?private var needsDetection = trueprivate var nextPeriodicDetection = 30.0+ private var lastForeignCheck = Date.distantPastprivate var lastDetectionAt = Date.distantPastprivate var ambiguousDetections = 0/// Sentences finished before detection settled the language: recorded once it confirms the@@ -386,10 +387,12 @@ final class ListenEngine {if settings.sourceMode == .auto, conf < 0.6, text.count >= 4 {needsDetection = true}+ checkForeignLetters(text)} else {if volatileText.isEmpty { volatileStartedAt = Date(); volatileStartClock = router.fedSeconds() }if text != volatileText { volatileChangedAt = Date() }volatileText = text+ checkForeignLetters(text)}renderSubtitles()}@@ -411,6 +414,20 @@ final class ListenEngine {// MARK: Language detection+ /// A Chinese, Japanese or Korean model hearing English writes it out in Latin letters+ /// ("OKo where wen the lanch", "reigifridadeinali") with a confidence that looks fine; without+ /// this the switch waits for the next periodic check, 20 s of speech later. Only a line that+ /// ends in a run of Latin letters counts: English words mixed into Chinese ("还在做 onboarding+ /// 的 UI") would ask for checks that each risk a wrong switch.+ private func checkForeignLetters(_ text: String) {+ guard settings.sourceMode == .auto, !needsDetection, let code = activeLang?.code,+ ["zh", "ja", "ko"].contains(where: { code.hasPrefix($0) }),+ Date().timeIntervalSince(lastForeignCheck) >= 8, HeardText.endsInLatin(text) else { return }+ lastForeignCheck = Date()+ debugLog("detect: Latin letters from the \(code) model, checking the language")+ needsDetection = true+ }+private func maybeDetectLanguage() {guard isRunning, settings.sourceMode == .auto, !detecting, candidates.count >= 2 else { return }var stats = router.stats(window: 6)diff --git a/Tests/LiveSubsTests/HeardTextTests.swift b/Tests/LiveSubsTests/HeardTextTests.swiftindex ed9e46c..d74c950 100644--- a/Tests/LiveSubsTests/HeardTextTests.swift+++ b/Tests/LiveSubsTests/HeardTextTests.swift@@ -39,6 +39,16 @@ import Testing#expect(HeardText.fuller(final: "我们周五。", draft: "我们周五之前把落地页的文案定下来") == "我们周五之前把落地页的文案定下来")}+ @Test func englishHeardByAChineseModelEndsInLatin() {+ // From test runs: English after Chinese, still on the zh-CN model.+ #expect(HeardText.endsInLatin("提前准备一下会议室和午餐。reigifridadeinali"))+ #expect(HeardText.endsInLatin("下会议室和午餐ouchrivesignin of Fridayndeledinalrin cop foretinge"))+ // English terms in Chinese speech.+ #expect(!HeardText.endsInLatin("Want还在做 onbording的 UIAtaging环境不太稳定。"))+ #expect(!HeardText.endsInLatin("我们这个 featur的 rroatemape下周要定下来 datline是月底 backent那边的 API已经 bready了"))+ #expect(!HeardText.endsInLatin("必要 dev帮忙看一下 Quberned的配置,另外 productmanager希望我们在 sprintreview之前把 Demo准备好"))+ }+@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",