Skip to content

知了 Cicada · 任何会议和直播的实时字幕、同声传译、AI 会议纪要,Mac、Windows、Linux、Android 免费开源。Live subtitles, interpreting and meeting notes for any call or stream, on Mac, Windows, Linux and Android.

fix: Edit line… no longer teaches the word list punctuation or words added around the heard ones

wquguru28ab28e0f292
diff --git a/Sources/Cicada/LiveFixes.swift b/Sources/Cicada/LiveFixes.swift
index b8c9d91..ff343a2 100644
--- a/Sources/Cicada/LiveFixes.swift
+++ b/Sources/Cicada/LiveFixes.swift
@@ -232,11 +232,14 @@ final class LiveFixes: ObservableObject {
set(fixes + [e.fix], line: t.id, in: id)
}
let c = Correction.core(e.fix.from, e.fix.to)
- // Only a change made everywhere teaches the word list: a one-off must not.
- if keep, everywhere { vocabulary.confirm(heard: e.heard, word: c.to.trimmingCharacters(in: .whitespaces), source: "user") }
- debugLog("fix edited lines=\(targets.count) keep=\(keep)")
- say(keep ? L("已改 \(targets.count) 处:\(e.heard) → \(c.to.trimmingCharacters(in: .whitespaces)),记进词表")
- : L("已改 \(targets.count) 处:\(e.heard) → \(c.to.trimmingCharacters(in: .whitespaces))"))
+ // Only a change made everywhere teaches the word list (a one-off must not), and only a
+ // word for a word (`Vocabulary.learnable`).
+ let word = c.to.trimmingCharacters(in: .whitespaces)
+ let learned = keep && everywhere && Vocabulary.learnable(heard: e.heard, word: word) != nil
+ if learned { vocabulary.confirm(heard: e.heard, word: word, source: "user") }
+ debugLog("fix edited lines=\(targets.count) learned=\(learned)")
+ say(learned ? L("已改 \(targets.count) 处:\(e.heard) → \(word),记进词表")
+ : L("已改 \(targets.count) 处:\(e.heard) → \(word)"))
return targets.count
}
diff --git a/Sources/Cicada/Vocabulary.swift b/Sources/Cicada/Vocabulary.swift
index 2244a3c..f638cf4 100644
--- a/Sources/Cicada/Vocabulary.swift
+++ b/Sources/Cicada/Vocabulary.swift
@@ -42,13 +42,36 @@ final class Vocabulary: ObservableObject {
.split(whereSeparator: \.isWhitespace).joined(separator: " ")
}
+ /// What a fix can teach the list, or nil: both sides trimmed of spaces and punctuation, no
+ /// punctuation inside, short, and a real swap. An edit that only adds or drops words around the
+ /// heard ones ("我来" → "OK我来") or ends on a full stop ("持。" → "讲。") teaches nothing.
+ nonisolated static func learnable(heard: String, word: String) -> (heard: String, word: String)? {
+ let edge = CharacterSet.whitespacesAndNewlines.union(.punctuationCharacters).union(.symbols)
+ let h = heard.trimmingCharacters(in: edge), w = word.trimmingCharacters(in: edge)
+ guard !h.isEmpty, !w.isEmpty, h.count <= 20, w.count <= 40, key(h) != key(w),
+ !w.contains(h), !h.contains(w),
+ h.unicodeScalars.allSatisfy({ !CharacterSet.punctuationCharacters.contains($0) }),
+ w.unicodeScalars.allSatisfy({ !CharacterSet.punctuationCharacters.contains($0) || $0 == "." || $0 == "-" })
+ else { return nil }
+ return (h, w)
+ }
+
func knows(heard: String, word: String) -> Bool {
let h = Self.key(heard), w = Self.key(word)
return terms.contains { Self.key($0.word) == w && $0.heard.contains { Self.key($0) == h } }
}
- /// The user said yes to a fix (or added it): the word, and the form it was heard as.
+ /// 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.
func confirm(heard: String, word: String, source: String = "live") {
+ if !heard.isEmpty {
+ guard let l = Self.learnable(heard: heard, word: word) else { return }
+ return add(heard: l.heard, word: l.word, source: source)
+ }
+ add(heard: "", word: word, source: source)
+ }
+
+ private func add(heard: String, word: String, source: String) {
let w = Self.key(word)
if let i = terms.firstIndex(where: { Self.key($0.word) == w }) {
if !heard.isEmpty, !terms[i].heard.contains(where: { Self.key($0) == Self.key(heard) }) { terms[i].heard.append(heard) }
diff --git a/Tests/CicadaTests/VocabularyTests.swift b/Tests/CicadaTests/VocabularyTests.swift
index e3ca8be..c260b27 100644
--- a/Tests/CicadaTests/VocabularyTests.swift
+++ b/Tests/CicadaTests/VocabularyTests.swift
@@ -59,4 +59,16 @@ import Testing
#expect(v.preview("Morpho = 莫法\nPendle\n" + String(repeating: "长", count: 41)) == .init(added: 1, updated: 1, skipped: [3]))
#expect(v.terms.count == 1)
}
+
+ @Test func editsThatArentAWordForAWordTeachNothing() {
+ let v = Vocabulary(url: nil)
+ v.confirm(heard: "持。", word: "讲。")
+ #expect(v.knows(heard: "持", word: "讲"))
+ v.confirm(heard: "我来", word: "OK我来", source: "user")
+ v.confirm(heard: "然后,", word: "然后")
+ #expect(v.terms.map(\.word) == ["讲"])
+ v.confirm(heard: "木娜", word: "Monad", source: "user")
+ v.confirm(heard: "Uni斯图 V三", word: "Uniswap V3")
+ #expect(v.knows(heard: "木娜", word: "Monad") && v.knows(heard: "Uni斯图 V三", word: "Uniswap V3"))
+ }
}