UOM // Only update observation if half-decayed.

This commit is contained in:
ShikiSuen 2022-07-23 23:48:46 +08:00
parent 1cbcbb4b6f
commit d2ed575d36
1 changed files with 24 additions and 6 deletions

View File

@ -70,19 +70,26 @@ extension vChewing {
saveCallback() saveCallback()
return return
} }
//
if var theNeta = mutLRUMap[key] { if var theNeta = mutLRUMap[key] {
_ = suggest(
walkedAnchors: walkedAnchors, cursorIndex: cursorIndex, timestamp: timestamp,
decayCallback: {
theNeta.observation.update(candidate: candidate, timestamp: timestamp) theNeta.observation.update(candidate: candidate, timestamp: timestamp)
mutLRUList.insert(theNeta, at: 0) self.mutLRUList.insert(theNeta, at: 0)
mutLRUMap[key] = theNeta self.mutLRUMap[key] = theNeta
IME.prtDebugIntel("UOM: Observation finished with existing observation: \(key)") IME.prtDebugIntel("UOM: Observation finished with existing observation: \(key)")
saveCallback() saveCallback()
} }
)
}
} }
public func suggest( public func suggest(
walkedAnchors: [Megrez.NodeAnchor], walkedAnchors: [Megrez.NodeAnchor],
cursorIndex: Int, cursorIndex: Int,
timestamp: Double timestamp: Double,
decayCallback: @escaping () -> Void = {}
) -> [Megrez.Unigram] { ) -> [Megrez.Unigram] {
let key = convertKeyFrom(walkedAnchors: walkedAnchors, cursorIndex: cursorIndex) let key = convertKeyFrom(walkedAnchors: walkedAnchors, cursorIndex: cursorIndex)
let currentReadingKey = convertKeyFrom(walkedAnchors: walkedAnchors, cursorIndex: cursorIndex, readingOnly: true) let currentReadingKey = convertKeyFrom(walkedAnchors: walkedAnchors, cursorIndex: cursorIndex, readingOnly: true)
@ -97,6 +104,7 @@ extension vChewing {
var currentHighScore = 0.0 var currentHighScore = 0.0
for overrideNeta in Array(observation.overrides) { for overrideNeta in Array(observation.overrides) {
let override: Override = overrideNeta.value let override: Override = overrideNeta.value
let overrideScore: Double = getScore( let overrideScore: Double = getScore(
eventCount: override.count, eventCount: override.count,
totalCount: observation.count, totalCount: observation.count,
@ -105,6 +113,16 @@ extension vChewing {
lambda: mutDecayExponent lambda: mutDecayExponent
) )
if (0...currentHighScore).contains(overrideScore) { continue } if (0...currentHighScore).contains(overrideScore) { continue }
let overrideDetectionScore: Double = getScore(
eventCount: override.count,
totalCount: observation.count,
eventTimestamp: override.timestamp,
timestamp: timestamp,
lambda: mutDecayExponent * 2
)
if (0...currentHighScore).contains(overrideDetectionScore) { decayCallback() }
let newUnigram = Megrez.Unigram( let newUnigram = Megrez.Unigram(
keyValue: .init(key: currentReadingKey, value: overrideNeta.key), score: overrideScore keyValue: .init(key: currentReadingKey, value: overrideNeta.key), score: overrideScore
) )