知了 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
import Foundation
import Testing
@testable import Cicada
@Suite struct HeardTextTests {
@Test func cleanStrayMarks() {
#expect(HeardText.clean("in..........") == "in…")
#expect(HeardText.clean(", , that") == "that")
#expect(HeardText.clean(",...... has translated at its end into infrastructure...") == "has translated at its end into infrastructure…")
#expect(HeardText.clean(". , that is causing a product twice.") == "that is causing a product twice.")
#expect(HeardText.clean("that.......... in increased production") == "that… in increased production")
#expect(HeardText.clean("would.. would be oversupplied.") == "would… would be oversupplied.")
#expect(HeardText.clean(", and......... in alternative infrastructure routes that let's.................") ==
"and… in alternative infrastructure routes that let's…")
#expect(HeardText.clean("whether it's in the UALE, , Saudi Arab, or the wider.") == "whether it's in the UALE, Saudi Arab, or the wider.")
#expect(HeardText.clean(",, and there should be more diversified.") == "and there should be more diversified.")
#expect(HeardText.clean(", you... lucrative") == "you… lucrative")
#expect(HeardText.clean("of people..") == "of people…")
#expect(HeardText.clean("to over 1.4 million a day.") == "to over 1.4 million a day.")
#expect(HeardText.clean("The U.S. said no.") == "The U.S. said no.")
#expect(HeardText.clean(",,我们周四上线。。。") == "我们周四上线…")
#expect(HeardText.clean("........") == "")
}
@Test func keepTheDraftWhenTheFinalDroppedPhrases() {
let final = "for a new escalation process for enterprise customers, and Echo 5 Finance, needs the budget. for next year by the end. of this month. in."
let draft = "to for the support team asked for a new escalation process for enterprise customers and echo 5 Finance needs the budget numbers for next year by the end of this month in foxtrot 6"
#expect(HeardText.fuller(final: final, draft: draft) == draft)
}
@Test func keepTheFinalWhenItHasAboutAsMuch() {
let final = "We need the design review finished by Tuesday, and the audit too."
#expect(HeardText.fuller(final: final, draft: "We need the design review finished by Tuesday and the the audit too") == final)
#expect(HeardText.fuller(final: final, draft: "") == final)
#expect(HeardText.fuller(final: "我们周五之前把文案定下来。", draft: "我们周五之前把落地页的文案定下来") == "我们周五之前把文案定下来。")
#expect(HeardText.fuller(final: "我们周五。", draft: "我们周五之前把落地页的文案定下来") == "我们周五之前把落地页的文案定下来")
}
@Test func englishHeardByAChineseModelEndsInLatin() {
#expect(HeardText.endsInLatin("提前准备一下会议室和午餐。reigifridadeinali"))
#expect(HeardText.endsInLatin("下会议室和午餐ouchrivesignin of Fridayndeledinalrin cop foretinge"))
#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() {
#expect(HeardText.continues("that… in increased production that you would have expected at this",
"because I think as soon as this crisis is over,", gap: 0.2, cut: true))
#expect(HeardText.continues("in, diesel and gasoline crisis in Russia that is combined with the Middle",
"that is causing a product twice.", gap: 0.1, cut: false))
#expect(HeardText.continues("So, I think the long term headwinds for global energy markets are going to be.",
"in the, and in the getting the tanker market back", gap: 0.3, cut: true))
#expect(!HeardText.continues("It's not just rhetoric.", "it costs a lot more to transport.", gap: 0.2, cut: false))
#expect(!HeardText.continues("are going to be.", "So the tanker market", gap: 0.2, cut: true))
#expect(!HeardText.continues("Is it the headlines that are moving markets?", "or is it more", gap: 0.1, cut: true))
#expect(!HeardText.continues("at this", "because I think", gap: 2.4, cut: true))
#expect(!HeardText.continues(String(repeating: "word ", count: 40), "and more", gap: 0.1, cut: true))
#expect(HeardText.continues(String(repeating: "word ", count: 39), "and five or six more words", gap: 0.1, cut: true))
#expect(HeardText.continues("我们下周", "再看这个方案", gap: 0.3, cut: true))
#expect(!HeardText.continues("我们下周再看。", "这个方案", gap: 0.3, cut: true))
}
@Test func joinedText() {
#expect(HeardText.join("are going to be.", "in the tanker market") == "are going to be in the tanker market")
#expect(HeardText.join("at this", "because I think") == "at this because I think")
#expect(HeardText.join("between…", "loading a ship.") == "between… loading a ship.")
#expect(HeardText.join("我们下周", "再看") == "我们下周再看")
}
@Test func replay() throws {
guard let arg = ProcessInfo.processInfo.environment["CICADA_REPLAY"] else { return }
let parts = arg.split(separator: ":", maxSplits: 1).map(String.init)
let decoder = JSONDecoder()
decoder.dateDecodingStrategy = .iso8601
let s = try decoder.decode(Session.self, from: Data(contentsOf: URL(fileURLWithPath: parts[0])))
var cuts: [Double] = []
if parts.count > 1, let log = try? String(contentsOfFile: parts[1], encoding: .utf8) {
let f = DateFormatter()
f.dateFormat = "yyyy-MM-dd HH:mm:ss Z"
for l in log.split(separator: "\n") where l.contains(" cut: line") {
if let d = f.date(from: String(l.prefix(25))) { cuts.append(d.timeIntervalSince(s.startedAt)) }
}
}
var out: [(t: Double, voice: Int?, src: String, from: [String])] = []
for l in s.lines where l.speaker == "them" {
let text = HeardText.clean(l.src)
guard text.contains(where: \.isLetter) else { print("DROP \(l.src)"); continue }
if let prev = out.last, let pi = s.lines.firstIndex(where: { $0.t == prev.t }) {
let cut = cuts.contains { $0 >= s.lines[pi].t && $0 < l.t + 1 }
if (prev.voice == nil || l.voice == nil || prev.voice == l.voice),
HeardText.continues(prev.src, text, gap: cut ? 0 : .infinity, cut: cut) {
out[out.count - 1].src = HeardText.join(prev.src, text)
out[out.count - 1].from.append(l.src)
continue
}
}
out.append((l.t, l.voice, text, [l.src]))
}
for o in out where o.from.count > 1 || o.from[0] != o.src {
print("BEFORE " + o.from.map { "«\($0)»" }.joined(separator: " + "))
print("AFTER [\(TranscriptExport.clock(o.t)) v\(o.voice.map(String.init) ?? "-")] \(o.src)\n")
}
print("lines: \(s.lines.count) → \(out.count)")
}
@Test func endsSentenceOnlyAtAFullSentence() {
#expect(HeardText.endsSentence("いつも動画をご覧いただきありがとうございます。"))
#expect(HeardText.endsSentence("We should ship the fix this week."))
#expect(HeardText.endsSentence("Are we shipping it this week?"))
#expect(!HeardText.endsSentence("はい。"))
#expect(!HeardText.endsSentence("Yes it is."))
#expect(!HeardText.endsSentence("いつも動画をご覧いただきありがとうござい"))
#expect(!HeardText.endsSentence("and then we went over the numbers..."))
#expect(!HeardText.endsSentence("the growth this quarter was about 3."))
}
@Test func splitAtSentenceEndKeepsTheRunOnForTheNextLine() {
let s = HeardText.splitAtSentenceEnd("にもこの新型 iPad mini Proモデルが届いたので開封をしていきたいと思います。前回こちらの動画で")
#expect(s?.head == "にもこの新型 iPad mini Proモデルが届いたので開封をしていきたいと思います。")
#expect(s?.tail == "前回こちらの動画で")
#expect(HeardText.splitAtSentenceEnd("We should ship the fix this week. And then")?.tail == "And then")
#expect(HeardText.splitAtSentenceEnd("開封をしていきたいと思います。今日はですね新型のお話です。") == nil)
#expect(HeardText.splitAtSentenceEnd("はい。それではですね") == nil)
}
@Test func chineseClausesEndAtCommasConnectivesAndParticles() {
let zh = "站着是会弯一点,但是松开它可以恢复原样那真正屁股坐上去的场景实际受力面积会比这个还要大一些所以很多人设想的"
let clauses = HeardText.boundaries(zh, language: "zh").filter { $0.kind == .clause }.map(\.at)
#expect(clauses.contains(8))
#expect(clauses.contains(zh.distance(from: zh.startIndex, to: zh.range(of: "所以")!.lowerBound)))
#expect(!HeardText.boundaries("我们去那个吧台坐一下好不好", language: "zh").contains { $0.kind == .clause })
#expect(HeardText.boundaries("我们先这样吧然后再说", language: "zh").contains { $0.at == 6 && $0.kind == .clause })
let both = HeardText.boundaries("需求会更小众和纠结一点因为如果你问有些人", language: "zh").filter { $0.kind == .clause }.map(\.at)
#expect(both == [11])
#expect(!HeardText.boundaries("还要大一些所以很多人设想的", language: "zh").contains { $0.at == 6 })
#expect(HeardText.split("要好很多很多即便施加了比较多的下压力而且", near: 6, kind: .clause, language: "zh") == nil)
#expect(HeardText.split("结论就是他会比要好很多很多即便施加了比较多的下压力", near: 13, kind: .clause, language: "zh")?.tail == "即便施加了比较多的下压力")
}
@Test func splitAtClauseNearTheCut() {
let final = "站着是会弯一点但是松开它可以恢复原样那真正屁股坐上去的场景实际受力面积会比这个还要大一些所以很多人"
let at = final.distance(from: final.startIndex, to: final.range(of: "所以")!.lowerBound)
let s = HeardText.split(final, near: at - 2, kind: .clause, language: "zh")
#expect(s?.tail == "所以很多人")
let en = "and then we went over all of the numbers for the next quarter with the team in Berlin"
let w = HeardText.split(en, near: 40, kind: .word, language: "en")
#expect(w.map { en.hasPrefix($0.head + " ") } == true)
#expect(w?.head.last?.isLetter == true && w?.tail.first?.isLetter == true)
#expect(HeardText.split("站着是会弯一点但是松开它可以恢复原样", near: 3, kind: .clause, language: "zh") == nil)
}
@Test func joinsStopAtTheCap() {
let a = "ではこちらペリペリ剥がしますはいこっちもですねそれではオープンよいしょはいはいで"
#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])
}
}