Remember picked candidates (user history)

Candidates chosen with the arrow-down list are recorded on commit as
toneless reading -> phrase with a count. The converter lifts a picked phrase just
above the best lexicon entry for that reading (more for more picks), so it
comes first next time without overriding longer phrases. Up to 2000 pairs,
least recently used evicted. Stored as user-history.json on every platform; the
Mac menu's clear item clears it too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-13 14:30:55 +08:00
co-authored by Claude Opus 5.5
parent 815dcf979d
commit 2cebf0e700
19 changed files with 414 additions and 39 deletions
+13 -1
View File
@@ -71,7 +71,7 @@ public final class CompositionEngine {
public init(classifier: Classifier) {
self.classifier = classifier
self.converter = Converter(lexicon: classifier.lexicon)
self.converter = Converter(lexicon: classifier.lexicon, history: classifier.history)
}
// MARK: - Derived state
@@ -537,6 +537,7 @@ public final class CompositionEngine {
private func commitCurrent(_ given: [Segment]? = nil) -> String {
let segs = given ?? segments
let text = segs.map { render($0, final: true) }.joined()
rememberPicks(segs)
if let forced = forcedMode {
classifier.learned.record(String(keys), as: forced)
}
@@ -546,6 +547,17 @@ public final class CompositionEngine {
return text
}
/// Candidates the user picked are remembered and come first next time.
private func rememberPicks(_ segs: [Segment]) {
for seg in segs where seg.mode == .zhuyin {
guard let picks = fixed[seg.range.lowerBound], !picks.isEmpty else { continue }
let syl = convertibleSyllables(seg, final: true).syllables
for (range, text) in picks where range.upperBound <= syl.count {
classifier.history.record(Array(syl[range]), phrase: text)
}
}
}
/// Commits whatever is composed (e.g. the app lost focus). Returns "" when empty.
public func commitAll() -> String {
keys.isEmpty ? "" : commitCurrent()