知了 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: Edit line… that adds a word fixes only that line, never every line with a full stop, and adding a word at the start works
wqugurubc6cc3c5b8e3
diff --git a/Sources/Cicada/Correction.swift b/Sources/Cicada/Correction.swiftindex c5b6a15..097237c 100644--- a/Sources/Cicada/Correction.swift+++ b/Sources/Cicada/Correction.swift@@ -186,19 +186,55 @@ enum Correction {return kept + fresh}- /// 改这句…: the user rewrote the line as shown. What changed, as a fix on top of the others,- /// and the heard form it replaces (for the word list): the words in `src` when they are- /// there, else the heard words of the fix the user corrected again ("莫纳" → "Morpho" → "Monad").- static func userEdit(shown: String, edited: String, src: String, fixes: [LineFix]) -> (fix: LineFix, heard: String)? {+ /// 改这句…: the user rewrote the line as shown. What changed, as a fix on top of the others: the+ /// changed span grown to whole tokens (a Latin word, or one CJK character), and further, a token+ /// at a time, until it holds a letter and is the only such span in the line. An added word+ /// ("lock that in." → "lock that in now.") is then "in" → "in now" on this line, never "." on+ /// every line. `swap` is whether words were replaced (not only added or dropped): only a swap+ /// may go to the rest of the meeting and teach the word list. `heard` is the words in `src`+ /// when they are there, else the heard words of the fix the user corrected again.+ static func userEdit(shown: String, edited: String, src: String, fixes: [LineFix]) -> (fix: LineFix, heard: String, swap: Bool)? {let new = edited.trimmingCharacters(in: .whitespacesAndNewlines)guard !new.isEmpty, new != shown else { return nil }- let c = core(shown, new)- guard !c.from.isEmpty, !c.to.isEmpty else { return nil }- var heard = c.from- if !src.contains(c.from), let earlier = fixes.first(where: { $0.applied && c.from.contains($0.to) }) {+ let a = Array(shown), b = Array(new)+ var p = 0+ while p < min(a.count, b.count), a[p] == b[p] { p += 1 }+ var q = 0+ while q < min(a.count, b.count) - p, a[a.count - 1 - q] == b[b.count - 1 - q] { q += 1 }+ let swap = a.count - p - q > 0 && b.count - p - q > 0+ // Tokens of the shown line: a run of ASCII letters and digits, else one character.+ let latin: (Character) -> Bool = { $0.isASCII && ($0.isLetter || $0.isNumber) }+ var tokens: [Range<Int>] = []+ var k = 0+ while k < a.count {+ var e = k + 1+ if latin(a[k]) { while e < a.count, latin(a[e]) { e += 1 } }+ tokens.append(k..<e)+ k = e+ }+ var start = p, end = a.count - q+ if let t = tokens.first(where: { $0.contains(start) }) { start = t.lowerBound }+ if end > start, let t = tokens.first(where: { $0.contains(end - 1) }) { end = t.upperBound }+ func span() -> String { String(a[start..<end]) }+ func count(_ x: String) -> Int {+ guard !x.isEmpty else { return 0 }+ var n = 0, r = shown.startIndex..<shown.endIndex+ while let f = shown.range(of: x, range: r) { n += 1; r = f.upperBound..<shown.endIndex }+ return n+ }+ while !span().contains(where: { $0.isLetter || $0.isNumber }) || count(span()) != 1 {+ if let t = tokens.last(where: { $0.upperBound <= start }) { start = t.lowerBound }+ else if let t = tokens.first(where: { $0.lowerBound >= end }) { end = t.upperBound }+ else { break }+ }+ let from = span()+ let to = String(b[start..<(b.count - (a.count - end))])+ guard !from.isEmpty, from != to else { return nil }+ var heard = from+ if !src.contains(from), let earlier = fixes.first(where: { $0.applied && from.contains($0.to) }) {heard = core(earlier.from, earlier.to).from}- return (LineFix(from: c.from, to: c.to, user: true), heard)+ return (LineFix(from: from, to: to, user: true), heard, swap)}}diff --git a/Sources/Cicada/FixViews.swift b/Sources/Cicada/FixViews.swiftindex 85aef55..b487827 100644--- a/Sources/Cicada/FixViews.swift+++ b/Sources/Cicada/FixViews.swift@@ -37,9 +37,12 @@ struct LineEditor: View {}.pickerStyle(.radioGroup).labelsHidden().font(.system(size: 12))}- Toggle("记进词表,以后自动改", isOn: $keep)- .toggleStyle(.checkbox).font(.system(size: 12))- .disabled(same.count > 1 && !everywhere)+ // Only a word for a word can teach the list: an added or dropped word can't.+ if same.swap {+ Toggle("记进词表,以后自动改", isOn: $keep)+ .toggleStyle(.checkbox).font(.system(size: 12))+ .disabled(same.count > 1 && !everywhere)+ }}HStack(spacing: 8) {Text("回车 改成 · esc 取消").font(.system(size: 11)).foregroundStyle(Palette.tertiary)diff --git a/Sources/Cicada/LiveFixes.swift b/Sources/Cicada/LiveFixes.swiftindex ff343a2..c6b0596 100644--- a/Sources/Cicada/LiveFixes.swift+++ b/Sources/Cicada/LiveFixes.swift@@ -207,11 +207,12 @@ final class LiveFixes: ObservableObject {// MARK: 改这句…/// How many lines of the meeting the same edit would change ("整场同样听成 木娜 的 2 处都改").- func sameHeard(lineID: String, in id: String, edited: String) -> (heard: String, count: Int)? {+ /// An edit that only adds or drops words stays on its line (count 1, nothing to learn).+ func sameHeard(lineID: String, in id: String, edited: String) -> (heard: String, count: Int, swap: Bool)? {guard let s = store.session(id), let l = s.lines.first(where: { $0.id == lineID }),let e = Correction.userEdit(shown: l.shown, edited: edited, src: l.src, fixes: l.fixes ?? []) else { return nil }- let n = s.lines.filter { $0.shown.contains(e.fix.from) }.count- return (e.heard, max(1, n))+ let n = e.swap ? s.lines.filter { $0.shown.contains(e.fix.from) }.count : 1+ return (e.heard, max(1, n), e.swap)}/// The user rewrote a line (改这句…): what changed becomes their own fix, on this line or on@@ -220,6 +221,7 @@ final class LiveFixes: ObservableObject {func edit(lineID: String, in id: String, to edited: String, everywhere: Bool, remember keep: Bool) -> Int {guard let s = store.session(id), let l = s.lines.first(where: { $0.id == lineID }),let e = Correction.userEdit(shown: l.shown, edited: edited, src: l.src, fixes: l.fixes ?? []) else { return 0 }+ let everywhere = everywhere && e.swaplet targets = everywhere ? s.lines.filter { $0.shown.contains(e.fix.from) } : [l]remember(id, lines: targets.map(\.id))for t in targets {diff --git a/Tests/CicadaTests/CorrectionTests.swift b/Tests/CicadaTests/CorrectionTests.swiftindex 1a5f376..ce308d7 100644--- a/Tests/CicadaTests/CorrectionTests.swift+++ b/Tests/CicadaTests/CorrectionTests.swift@@ -135,4 +135,27 @@ import Testing#expect(back.shown == "b")#expect(back.src == "a")}++ @Test func addedWordStaysOnItsLineAndNeverMatchesPunctuation() {+ // "now" added before the full stop: the fix is the last word, unique in the line, not ".".+ let e = Correction.userEdit(shown: "Great, let's lock that in.", edited: "Great, let's lock that in now.", src: "Great, let's lock that in.", fixes: [])+ #expect(e?.fix.from == "in")+ #expect(e?.fix.to == "in now")+ #expect(e?.swap == false)+ #expect(Correction.apply([e!.fix], to: "Great, let's lock that in.").text == "Great, let's lock that in now.")+ // A word added at the start.+ let s = Correction.userEdit(shown: "Great, let's lock that in.", edited: "OK Great, let's lock that in.", src: "Great, let's lock that in.", fixes: [])+ #expect(s?.fix.from == "Great")+ #expect(s?.fix.to == "OK Great")+ #expect(s?.swap == false)+ // A word that appears twice: grown until the span is unique.+ let t = Correction.userEdit(shown: "in the room in the hall", edited: "in the room inside the hall", src: "in the room in the hall", fixes: [])+ #expect(Correction.apply([t!.fix], to: "in the room in the hall").text == "in the room inside the hall")+ // Chinese: an inserted word takes its neighbour.+ let c = Correction.userEdit(shown: "好的我来看一下", edited: "好的OK我来看一下", src: "好的我来看一下", fixes: [])+ #expect(c.map { Correction.apply([$0.fix], to: "好的我来看一下").text } == "好的OK我来看一下")+ #expect(c?.swap == false)+ // A real swap is still a swap.+ #expect(Correction.userEdit(shown: "我木娜找了", edited: "我Monad找了", src: "我木娜找了", fixes: [])?.swap == true)+ }}