The converter's K best paths are rescored with CKIP gpt2-tiny-chinese (GPL-3.0, character-level, ~12M parameters, 22 MB as float16): score = lexicon log-prob + 0.6 x LM log-prob, with the recently committed text as context. Inference is plain Accelerate (Apple Silicon and Intel). Modes off / saver (K=3, 15 ms) / standard (K=10, 40 ms) / quality (K=30, 120 ms) in the input menu. A rerank over its time budget falls back to the lexicon; three in a row pause AI for a minute. With auto-pause on, AI pauses when the Mac is hot or the CPU is busy and drops to saver in Low Power Mode. The model loads in the background and is not loaded at all when off. Evaluation (training/lm/eval_rerank.py): chat 96.6% -> 97.5% chars, Wikipedia 92.9% -> 94.7% chars (about 25-30% fewer wrong characters). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
91 lines
3.9 KiB
Swift
91 lines
3.9 KiB
Swift
import Foundation
|
|
|
|
/// "AI 選字": reranks the converter's best paths with a language model.
|
|
///
|
|
/// score = lexicon score (log10 → ln) + λ · LM log-probability
|
|
///
|
|
/// It never blocks typing for long: each rerank has a time budget, and a rerank that runs
|
|
/// over it is dropped (the plain lexicon result is used). Three overruns in a row pause
|
|
/// AI for a minute. `systemState` lets the app pause or reduce it when the machine is
|
|
/// busy, hot or saving power.
|
|
public final class Reranker {
|
|
public enum Mode: String, CaseIterable {
|
|
case off, saver, standard, quality
|
|
|
|
/// How many paths are reranked, and the time budget for one rerank.
|
|
var k: Int { [.saver: 3, .standard: 10, .quality: 30][self] ?? 0 }
|
|
var budget: TimeInterval { [.saver: 0.015, .standard: 0.04, .quality: 0.12][self] ?? 0 }
|
|
}
|
|
|
|
/// What the system allows right now (decided by the app).
|
|
public enum SystemState { case normal, reduced, paused }
|
|
|
|
public enum Status: Equatable {
|
|
case off, loading, active, pausedBusy, pausedSlow
|
|
}
|
|
|
|
public var mode: Mode { didSet { cache.removeAll() } }
|
|
/// nil while loading (or if the model file is missing).
|
|
public var model: LanguageModel? { didSet { cache.removeAll() } }
|
|
public var lambda = 0.6
|
|
public var systemState: () -> SystemState = { .normal }
|
|
public var now: () -> Date = Date.init
|
|
public private(set) var status: Status = .off
|
|
|
|
private var overruns = 0
|
|
private var pausedUntil = Date.distantPast
|
|
private var cache: [String: [ConvertedSegment]] = [:]
|
|
static let cacheLimit = 512
|
|
static let pauseAfterOverruns = 3
|
|
static let slowPause: TimeInterval = 60
|
|
|
|
public init(mode: Mode = .standard, model: LanguageModel? = nil) {
|
|
self.mode = mode
|
|
self.model = model
|
|
}
|
|
|
|
/// The mode actually used now (after the system state), or nil when AI is not used.
|
|
var effectiveMode: Mode? {
|
|
guard mode != .off else { status = .off; return nil }
|
|
guard model != nil else { status = .loading; return nil }
|
|
if now() < pausedUntil { status = .pausedSlow; return nil }
|
|
switch systemState() {
|
|
case .paused: status = .pausedBusy; return nil
|
|
case .reduced: status = .active; return .saver
|
|
case .normal: status = .active; return mode
|
|
}
|
|
}
|
|
|
|
/// The reranked conversion, or nil to use the plain lexicon result.
|
|
func choose(_ converter: Converter, _ syllables: [Syllable], fixed: [Range<Int>: String],
|
|
context: String) -> [ConvertedSegment]? {
|
|
guard let mode = effectiveMode, let model, !syllables.isEmpty else { return nil }
|
|
let key = "\(mode.rawValue)|\(context)|\(syllables.map(\.reading).joined(separator: "-"))|"
|
|
+ fixed.sorted { $0.key.lowerBound < $1.key.lowerBound }.map { "\($0.key)=\($0.value)" }.joined(separator: ",")
|
|
if let hit = cache[key] { return hit }
|
|
|
|
let paths = converter.kBest(syllables, fixed: fixed, k: mode.k)
|
|
guard paths.count > 1 else { return paths.first?.segments }
|
|
let start = now()
|
|
var best: (score: Double, segments: [ConvertedSegment])?
|
|
for path in paths {
|
|
let text = path.segments.map(\.text).joined()
|
|
let score = path.score * log(10) + lambda * model.logProb(text, context: context)
|
|
if best == nil || score > best!.score { best = (score, path.segments) }
|
|
if now().timeIntervalSince(start) > mode.budget {
|
|
overruns += 1
|
|
if overruns >= Reranker.pauseAfterOverruns {
|
|
pausedUntil = now().addingTimeInterval(Reranker.slowPause)
|
|
overruns = 0
|
|
status = .pausedSlow
|
|
}
|
|
return nil
|
|
}
|
|
}
|
|
overruns = 0
|
|
if cache.count >= Reranker.cacheLimit { cache.removeAll() }
|
|
cache[key] = best?.segments
|
|
return best?.segments
|
|
}
|
|
}
|