知了 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: the meeting record no longer splits a Chinese word like 所以 across two lines
wquguruac4d8e0929f1
diff --git a/Sources/Cicada/HeardText.swift b/Sources/Cicada/HeardText.swiftindex 60e1eeb..d11dcbb 100644--- a/Sources/Cicada/HeardText.swift+++ b/Sources/Cicada/HeardText.swift@@ -94,6 +94,20 @@ enum HeardText {"如果", "虽然", "或者", "另外", "接下来", "总之", "因此", "于是", "就是说"]static let particles: Set<String> = ["吧", "呢", "吗", "啊", "嘛", "呀", "啦"]+ /// Offsets inside a Chinese connective, each with where it starts: no line ends there, even+ /// where the tokenizer splits the word ("所" "以").+ static func insideConnectives(_ chars: [Character]) -> [Int: Int] {+ var out: [Int: Int] = [:]+ for c in connectives {+ let w = Array(c)+ guard chars.count >= w.count else { continue }+ for p in 0...(chars.count - w.count) where Array(chars[p..<(p + w.count)]) == w {+ for i in (p + 1)..<(p + w.count) { out[i] = p }+ }+ }+ return out+ }+static func isCJK(_ text: String) -> Bool {text.unicodeScalars.contains { $0.properties.isIdeographic || (0x3040...0x30FF).contains($0.value) }}diff --git a/Sources/Cicada/RecordRefiner.swift b/Sources/Cicada/RecordRefiner.swiftindex fd7e420..c5d44dd 100644--- a/Sources/Cicada/RecordRefiner.swift+++ b/Sources/Cicada/RecordRefiner.swift@@ -118,6 +118,7 @@ struct RecordRefiner {// nearest punctuation when there is some a few characters away.// Otherwise it ends between words: Chinese comes one character per run, and "审 / 计" is no line end.let words = Self.wordStarts(record)+ let inside = HeardText.insideConnectives(record)for k in ends.indices {var e = Self.snap(ends[k], in: record)if e == ends[k], e > 0, e < m, !words.contains(e) {@@ -125,6 +126,8 @@ struct RecordRefiner {let before = (0...e).last { words.contains($0) } ?? 0e = after - e <= e - before ? after : before}+ // "所 / 以": the tokenizer splits some connectives; the line ends before the whole word.+ if let start = inside[e] { e = start }ends[k] = max(e, k > 0 ? ends[k - 1] : 0)}return endsdiff --git a/Tests/CicadaTests/HeardTextTests.swift b/Tests/CicadaTests/HeardTextTests.swiftindex 75e0b56..7dfa879 100644--- a/Tests/CicadaTests/HeardTextTests.swift+++ b/Tests/CicadaTests/HeardTextTests.swift@@ -175,4 +175,9 @@ import Testing#expect(HeardText.continues(a, "いつもですね", gap: 0.2, cut: true))#expect(!HeardText.continues(a, "いつもですねこれですねこちらのフィルムも丁寧に剥がしておお", gap: 0.2, cut: true))}++ @Test func noLineEndsInsideAConnective() {+ let t = Array("还要大一些所以很多人")+ #expect(HeardText.insideConnectives(t) == [6: 5])+ }}diff --git a/Tests/CicadaTests/RecordRefinerTests.swift b/Tests/CicadaTests/RecordRefinerTests.swiftindex 7e673a4..526a7d6 100644--- a/Tests/CicadaTests/RecordRefinerTests.swift+++ b/Tests/CicadaTests/RecordRefinerTests.swift@@ -36,6 +36,16 @@ import Testing#expect(out.map(\.text).joined() == "每天跑一次回收完发通知。")}+ /// A line ends before a connective, not inside one the tokenizer splits ("所 / 以", issue #20).+ @Test func connectiveStaysWhole() {+ var r = RecordRefiner()+ _ = r.emitted(id: 1, start: 0, end: 4, text: "实际面积会比这个还要大一些所")+ _ = r.emitted(id: 2, start: 4, end: 7, text: "以很多人设想的这个概率不是很大")+ _ = r.emitted(id: 3, start: 7.5, end: 9, text: "好的。")+ let out = r.heard([run("实际面积会比这个还要大一些所以很多人设想的这个概率不是很大", 0.1, 6.9)], through: 7.2)+ #expect(out.map(\.text) == ["实际面积会比这个还要大一些", "所以很多人设想的这个概率不是很大"])+ }+/// Words the live recognizer lost at a cut still reach the record.@Test func lostWordsAreKept() {var r = RecordRefiner()