AI candidate selection: rerank with a Traditional Chinese language model (macOS)
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>
This commit is contained in:
@@ -63,6 +63,11 @@ public final class CompositionEngine {
|
||||
public private(set) var pendingBacktick = false
|
||||
static let holdMinScore = -3.5
|
||||
|
||||
/// "AI 選字" (language-model reranking); nil = lexicon only.
|
||||
public var reranker: Reranker?
|
||||
/// The last characters committed, used as context for the reranker.
|
||||
private var recentText = ""
|
||||
|
||||
private var previousMode: InputMode?
|
||||
private var previousCommitTime = Date.distantPast
|
||||
/// How long the previous segment's mode keeps influencing the next one.
|
||||
@@ -186,9 +191,16 @@ public final class CompositionEngine {
|
||||
private func conversion(of seg: Segment, final: Bool) -> (segments: [ConvertedSegment], pending: String) {
|
||||
let (syl, pending) = convertibleSyllables(seg, final: final)
|
||||
let fixedHere = (fixed[seg.range.lowerBound] ?? [:]).filter { $0.key.upperBound <= syl.count }
|
||||
if let reranker, let chosen = reranker.choose(converter, syl, fixed: fixedHere, context: rerankContext) {
|
||||
return (chosen, pending)
|
||||
}
|
||||
return (converter.convert(syl, fixed: fixedHere), pending)
|
||||
}
|
||||
|
||||
private var rerankContext: String {
|
||||
now().timeIntervalSince(previousCommitTime) < contextWindow ? recentText : ""
|
||||
}
|
||||
|
||||
// MARK: - Cursor
|
||||
|
||||
/// Absolute key index after each syllable of a Zhuyin segment.
|
||||
@@ -542,6 +554,7 @@ public final class CompositionEngine {
|
||||
let segs = given ?? segments
|
||||
let text = segs.map { render($0, final: true) }.joined()
|
||||
rememberPicks(segs)
|
||||
recentText = String((rerankContext + text).suffix(20))
|
||||
if let forced = forcedMode {
|
||||
classifier.learned.record(String(keys), as: forced)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user