UOM // Porting decayCallback() back.
This commit is contained in:
parent
e053273815
commit
b3c1b2b8bc
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue