知了 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: fixing misheard words no longer turns a Chinese word into its English translation, or changes letters inside an English word
wquguru2ef62fc1ce4c
diff --git a/Sources/Cicada/Correction.swift b/Sources/Cicada/Correction.swiftindex bf70c9b..029fd5f 100644--- a/Sources/Cicada/Correction.swift+++ b/Sources/Cicada/Correction.swift@@ -78,11 +78,24 @@ enum Correction {c.from.lowercased() != c.to.lowercased(), distance(c.from.lowercased(), c.to.lowercased()) <= 2 {return .apply}- // A new name (Chinese heard as a Latin name, or a respelled one): sound can't check it.- if latinTo, e.confidence >= askAt { return .ask }+ // A new name (Chinese heard as a Latin name, or a respelled one): sound can't check it. A+ // name has a capital or a digit; "X平台 → X platform" is a translation, and one 对 to it+ // taught the word list 平台 → platform, which then rewrote every 平台 in the meeting.+ if latinTo, e.confidence >= askAt, nameShaped(c.to) { return .ask }return .drop}+ /// Latin text that could be a name: a capital or a digit in it (Morpho, GPT4o), not "platform".+ static func nameShaped(_ s: String) -> Bool {+ s.contains { $0.isASCII && ($0.isUppercase || $0.isNumber) }+ }++ /// A Chinese word "fixed" into a plain English one (平台 → platform): a translation, not a+ /// misheard name.+ static func translation(heard: String, word: String) -> Bool {+ han(heard) && word.contains(where: \.isASCIILetter) && word.allSatisfy(\.isASCII) && !nameShaped(word)+ }+/// Toneless pinyin, one syllable per character, from the system's Mandarin transform.static func pinyin(_ s: String) -> [String] {s.map { ch in@@ -138,13 +151,28 @@ enum Correction {/// The line as shown: each applied fix replaces its words where they still are. The ranges are/// the replaced words, for the dotted underline.+ /// Where `s` stands as words of its own in `text`: a Latin edge of it never runs on into a+ /// Latin word around it. `core` pads a fix to two characters, so the word list learned+ /// "S平 → X平", which matched inside "iOS平台" and made it "iOX平台".+ static func word(_ s: String, in text: String) -> Range<String.Index>? {+ let latin: (Character?) -> Bool = { $0.map { $0.isASCII && ($0.isLetter || $0.isNumber) } ?? false }+ var from = text.startIndex+ while from < text.endIndex, let r = text.range(of: s, range: from..<text.endIndex) {+ let before = r.lowerBound > text.startIndex ? text[text.index(before: r.lowerBound)] : nil+ let after = r.upperBound < text.endIndex ? text[r.upperBound] : nil+ if !(latin(s.first) && latin(before)), !(latin(s.last) && latin(after)) { return r }+ from = text.index(after: r.lowerBound)+ }+ return nil+ }+static func apply(_ fixes: [LineFix], to src: String) -> (text: String, marks: [Range<String.Index>]) {var text = srcvar spans: [(String, Int)] = [] // replacement and its character offset, kept valid as text changes// The user's own last: written against the line as it was shown, other fixes in.let ordered = fixes.filter { $0.user != true } + fixes.filter { $0.user == true }for f in ordered where f.applied && !f.from.isEmpty {- guard let r = text.range(of: f.from) else { continue }+ guard let r = word(f.from, in: text) ?? text.range(of: f.from) else { continue }let start = text.distance(from: text.startIndex, to: r.lowerBound)let delta = f.to.count - f.from.countspans = spans.map { $0.1 > start ? ($0.0, $0.1 + delta) : $0 }diff --git a/Sources/Cicada/LiveFixes.swift b/Sources/Cicada/LiveFixes.swiftindex d9d4831..29e54d5 100644--- a/Sources/Cicada/LiveFixes.swift+++ b/Sources/Cicada/LiveFixes.swift@@ -106,8 +106,10 @@ final class LiveFixes: ObservableObject {private func known(in src: String, _ fixes: [LineFix]) -> [LineFix] {var out = fixesfor t in vocabulary.terms {- // A heard form two words claim (两个都留) is never fixed silently.- for h in t.heard where h.count >= 2 && src.contains(h) && !refused.contains(Self.pair(h, t.word))+ // A heard form two words claim (两个都留) is never fixed silently. A translation a yes taught+ // before that was refused (平台 → platform) isn't either; one the user typed is.+ for h in t.heard where h.count >= 2 && Correction.word(h, in: src) != nil && !refused.contains(Self.pair(h, t.word))+ && (t.source == "user" || !Correction.translation(heard: h, word: t.word))&& vocabulary.owners(ofHeard: h, except: t.word).isEmpty {if out.contains(where: { $0.from == h || ($0.applied && $0.from.contains(h)) || ($0.off == true && $0.from == h) }) { continue }out.append(LineFix(from: h, to: t.word))diff --git a/Sources/Cicada/Vocabulary.swift b/Sources/Cicada/Vocabulary.swiftindex f638cf4..f782bf7 100644--- a/Sources/Cicada/Vocabulary.swift+++ b/Sources/Cicada/Vocabulary.swift@@ -62,10 +62,12 @@ final class Vocabulary: ObservableObject {}/// The user said yes to a fix (or added it): the word, and the form it was heard as. A pair- /// that isn't a word for a word (`learnable`) adds the word only, or nothing.+ /// that isn't a word for a word (`learnable`) adds the word only, or nothing. A yes in a meeting+ /// never teaches a translation (平台 → platform): it would rewrite that word everywhere after.func confirm(heard: String, word: String, source: String = "live") {if !heard.isEmpty {- guard let l = Self.learnable(heard: heard, word: word) else { return }+ guard let l = Self.learnable(heard: heard, word: word),+ source == "user" || !Correction.translation(heard: l.heard, word: l.word) else { return }return add(heard: l.heard, word: l.word, source: source)}add(heard: "", word: word, source: source)diff --git a/Tests/CicadaTests/CorrectionTests.swift b/Tests/CicadaTests/CorrectionTests.swiftindex 364683e..4d51f71 100644--- a/Tests/CicadaTests/CorrectionTests.swift+++ b/Tests/CicadaTests/CorrectionTests.swift@@ -45,6 +45,26 @@ import Testing#expect(verdict("莫纳", "Morpho", 0.7) == .drop)}+ /// A learned "S平 → X平" never reaches into "iOS平台"; on its own it still applies.+ @Test func learnedWordsStayWhole() {+ #expect(Correction.word("S平", in: "iOS平台很好") == nil)+ #expect(Correction.word("A工", in: "DATA工具") == nil)+ #expect(Correction.word("S平", in: "在S平台上") != nil)+ #expect(Correction.word("S平", in: "iOS平台和S平台") != nil)+ #expect(Correction.word("莫拉", in: "解释这个莫拉的") != nil)+ #expect(Correction.apply([LineFix(from: "S平", to: "X平")], to: "iOS平台和S平台").text == "iOS平台和X平台")+ }++ /// A translation isn't a misheard name: one 对 to "X平台 → X platform" taught 平台 → platform.+ @Test func translationsAreNotAsked() {+ #expect(verdict("X平台", "X platform", 0.9) == .drop)+ #expect(verdict("平台", "platform", 0.95) == .drop)+ #expect(verdict("respons", "responsible", 0.85) == .drop)+ #expect(verdict("GPT四", "GPT4", 0.85) == .ask)+ #expect(Correction.translation(heard: "平台", word: "platform"))+ #expect(!Correction.translation(heard: "莫拉", word: "Morpho"))+ }+@Test func confirmedHeardFormIsApplied() {#expect(verdict("莫拉", "Morpho", 0.5, known: [("莫拉", "Morpho")]) == .apply)#expect(verdict("解释这个莫拉的", "解释这个Morpho的", 0.5, known: [("莫拉", "Morpho")]) == .apply)diff --git a/Tests/CicadaTests/VocabularyTests.swift b/Tests/CicadaTests/VocabularyTests.swiftindex c260b27..ed7194a 100644--- a/Tests/CicadaTests/VocabularyTests.swift+++ b/Tests/CicadaTests/VocabularyTests.swift@@ -20,6 +20,14 @@ import Testing#expect(v.knows(heard: "阿韦", word: "Aave"))}+ @Test func aYesNeverTeachesATranslation() {+ let v = Vocabulary(url: nil)+ v.confirm(heard: "平台", word: "platform")+ #expect(v.terms.isEmpty)+ v.confirm(heard: "平台", word: "platform", source: "user")+ #expect(v.knows(heard: "平台", word: "platform"))+ }+@Test func confirmAndForget() {let v = Vocabulary(url: nil)v.confirm(heard: "开布特", word: "Curve")