UOM // Porting decayCallback() back.

This commit is contained in:
ShikiSuen 2022-08-08 17:39:33 +08:00
parent e053273815
commit b3c1b2b8bc
1 changed files with 25 additions and 14 deletions

View File

@ -280,33 +280,44 @@ extension vChewing.LMUserOverride {
} }
// TODO: // TODO:
if var theNeta = mutLRUMap[key] { if var theNeta = mutLRUMap[key] {
theNeta.observation.update( _ = getSuggestion(
candidate: candidate, timestamp: timestamp, forceHighScoreOverride: forceHighScoreOverride key: key, timestamp: timestamp, headReading: "",
decayCallback: {
theNeta.observation.update(
candidate: candidate, timestamp: timestamp, forceHighScoreOverride: forceHighScoreOverride
)
self.mutLRUList.insert(theNeta, at: 0)
self.mutLRUMap[key] = theNeta
IME.prtDebugIntel("UOM: Observation finished with existing observation: \(key)")
saveCallback()
}
) )
mutLRUList.insert(theNeta, at: 0)
mutLRUMap[key] = theNeta
IME.prtDebugIntel("UOM: Observation finished with existing observation: \(key)")
saveCallback()
} }
} }
private func getSuggestion(key: String, timestamp: Double, headReading: String) -> Suggestion { private func getSuggestion(
key: String, timestamp: Double, headReading: String, decayCallback: @escaping () -> Void = {}
) -> Suggestion {
guard !key.isEmpty, let kvPair = mutLRUMap[key] else { return .init() } guard !key.isEmpty, let kvPair = mutLRUMap[key] else { return .init() }
let observation: Observation = kvPair.observation let observation: Observation = kvPair.observation
var candidates: [(String, Megrez.Unigram)] = .init() var candidates: [(String, Megrez.Unigram)] = .init()
var forceHighScoreOverride = false var forceHighScoreOverride = false
var score: Double = 0 var currentHighScore: Double = 0
for (i, theObservation) in observation.overrides { for (i, theObservation) in observation.overrides {
let overrideScore = getScore( let overrideScore = getScore(
eventCount: theObservation.count, totalCount: observation.count, eventCount: theObservation.count, totalCount: observation.count,
eventTimestamp: theObservation.timestamp, timestamp: timestamp, lambda: mutDecayExponent eventTimestamp: theObservation.timestamp, timestamp: timestamp, lambda: mutDecayExponent
) )
if overrideScore == 0.0 { continue } if (0...currentHighScore).contains(overrideScore) { continue }
if overrideScore > score { let overrideDetectionScore: Double = getScore(
candidates.append((headReading, .init(value: i, score: overrideScore))) eventCount: theObservation.count, totalCount: observation.count,
forceHighScoreOverride = theObservation.forceHighScoreOverride eventTimestamp: theObservation.timestamp, timestamp: timestamp, lambda: mutDecayExponent * 2
score = overrideScore )
} if (0...currentHighScore).contains(overrideDetectionScore) { decayCallback() }
candidates.append((headReading, .init(value: i, score: overrideScore)))
forceHighScoreOverride = theObservation.forceHighScoreOverride
currentHighScore = overrideScore
} }
return .init(candidates: candidates, forceHighScoreOverride: forceHighScoreOverride) return .init(candidates: candidates, forceHighScoreOverride: forceHighScoreOverride)
} }