知了 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: catch Ling's notes, Chinese remarks and leaked reasoning in translations, and keep quotes intact
wquguru89e1727c74a6
diff --git a/Sources/LiveSubs/Translator.swift b/Sources/LiveSubs/Translator.swiftindex d32dd6f..e485d13 100644--- a/Sources/LiveSubs/Translator.swift+++ b/Sources/LiveSubs/Translator.swift@@ -193,22 +193,36 @@ final class TranslationService {// A line is a remark only when it opens or ends like one and talks about the job itself,// so a subtitle that happens to start with "The text…" is kept.let opener = ["here is", "here's", "the subtitle", "this subtitle", "the text", "this text", "the original",- "note", "(note", "i'm sorry", "i am sorry", "sorry", "it seems", "it appears", "this appears"]+ "the user", "note", "(note", "i'm sorry", "i am sorry", "sorry", "it seems", "it appears", "this appears"]let about = ["translat", "subtitle", "garbled", "transcri", "asr", "speech recognition", "best-effort"]+ // The same in Chinese: "请提供需要翻译的字幕内容", "您似乎忘记提供……".+ let zhAbout = ["字幕", "翻译"], zhRemark = ["请提供", "似乎", "看起来", "无法", "乱码", "没有附上", "您只发送"]+ // Reasoning that leaked into the reply ("…</think>") goes; the answer follows it.+ let answer = reply.components(separatedBy: "</think>").last ?? replyvar kept: [String] = []- for raw in reply.components(separatedBy: .newlines) {+ for raw in answer.components(separatedBy: .newlines) {var line = raw.trimmingCharacters(in: .whitespaces)if line.isEmpty { continue }let lower = line.lowercased()+ let remark = (opener.contains(where: { lower.hasPrefix($0) }) || line.hasSuffix(":") || line.hasSuffix(":"))+ && about.contains(where: { lower.contains($0) })+ || zhAbout.contains(where: { line.contains($0) }) && zhRemark.contains(where: { line.contains($0) })if lower.hasPrefix("translation:") {line = String(line.dropFirst("translation:".count)).trimmingCharacters(in: .whitespaces)- } else if opener.contains(where: { lower.hasPrefix($0) }) || line.hasSuffix(":") || line.hasSuffix(":"),- about.contains(where: { lower.contains($0) }) {+ } else if remark {+ // A note after the translation ("(Note: …" and its list of other wordings) ends it.+ if !kept.isEmpty { break }continue}if !line.isEmpty { kept.append(line) }}- let out = kept.joined(separator: " ").trimmingCharacters(in: CharacterSet(charactersIn: "\"“”「」 "))+ var out = kept.joined(separator: " ")+ // Quotes come off only when they wrap the whole reply; a closing ” inside it belongs to the text.+ for (open, close) in [("\"", "\""), ("“", "”"), ("「", "」")]+ where out.count >= 2 && out.hasPrefix(open) && out.hasSuffix(close) && out.dropFirst().dropLast().contains(open) == false {+ out = String(out.dropFirst().dropLast())+ }+ out = out.trimmingCharacters(in: .whitespaces)guard !out.isEmpty else { return nil }let letters = out.unicodeScalars.filter { $0.properties.isAlphabetic }let cjk = letters.filter { (0x3040...0x9FFF).contains($0.value) || (0xAC00...0xD7AF).contains($0.value) }.countdiff --git a/Tests/LiveSubsTests/TranslatorTests.swift b/Tests/LiveSubsTests/TranslatorTests.swiftindex 3d7851f..d0f5862 100644--- a/Tests/LiveSubsTests/TranslatorTests.swift+++ b/Tests/LiveSubsTests/TranslatorTests.swift@@ -24,6 +24,25 @@ import Testing#expect(TranslationService.cleanTranslation("我们用 API 和 SDK。", target: "zh-CN") == "我们用 API 和 SDK。")}+ /// Replies found in stored meetings.+ @Test func remarksFromRecordedMeetings() {+ let note = "好的。\n\n(Note: The subtitle \"Right.\" is a very short affirmation. In a live meeting context, it can be translated in a few ways:\n- 对。\n- 没错。)"+ #expect(TranslationService.cleanTranslation(note, target: "zh-CN") == "好的。")+ #expect(TranslationService.cleanTranslation("The user wants me to translate the subtitle \"Yes.\" from English into Simplified Chinese. The translation is \"是的。\"", target: "zh-CN") == nil)+ #expect(TranslationService.cleanTranslation("请提供需要翻译的字幕内容。", target: "zh-CN") == nil)+ #expect(TranslationService.cleanTranslation("The user wants a translation of \"Yes.\"\nI must output only the translation.</think>\n是的。", target: "zh-CN") == "是的。")+ #expect(TranslationService.cleanTranslation("您似乎忘记提供需要翻译的字幕内容了。请提供具体的英文字幕文本。", target: "zh-CN") == nil)+ let mixed = "The subtitle appears to be a mix of Chinese and garbled English. The Chinese portion translates to:\n\"Only 500 messages today.\""+ #expect(TranslationService.cleanTranslation(mixed, target: "en-US") == "Only 500 messages today.")+ }++ @Test func quotesInsideTheTextStay() {+ let s = "我想到了一个人,他说:“在这个派对上,永远都是晚上十点。”"+ #expect(TranslationService.cleanTranslation(s, target: "zh-CN") == s)+ #expect(TranslationService.cleanTranslation("He said \"no\" and \"never\"", target: "en-US") == "He said \"no\" and \"never\"")+ #expect(TranslationService.cleanTranslation("我们会把字幕翻译成中文。", target: "zh-CN") == "我们会把字幕翻译成中文。")+ }+@Test func translationInTheWrongScriptIsDropped() {#expect(TranslationService.cleanTranslation("We ship on Friday.", target: "zh-CN") == nil)#expect(TranslationService.cleanTranslation("我们周五发布。", target: "en-US") == nil)