ApplyDecay 应用强度衰减
(ctx context.Context)
| 313 | |
| 314 | // ApplyDecay 应用强度衰减 |
| 315 | func (pm *PreferenceManager) ApplyDecay(ctx context.Context) int { |
| 316 | pm.mu.Lock() |
| 317 | defer pm.mu.Unlock() |
| 318 | |
| 319 | removed := 0 |
| 320 | |
| 321 | for userID, prefs := range pm.preferences { |
| 322 | newPrefs := []*Preference{} |
| 323 | |
| 324 | for _, pref := range prefs { |
| 325 | // 计算衰减 |
| 326 | daysSinceUpdate := time.Since(pref.UpdatedAt).Hours() / 24 |
| 327 | decay := 1.0 - (pm.config.StrengthDecay * daysSinceUpdate) |
| 328 | |
| 329 | if decay < 0 { |
| 330 | decay = 0 |
| 331 | } |
| 332 | |
| 333 | pref.Strength *= decay |
| 334 | |
| 335 | // 如果强度低于阈值,删除 |
| 336 | if pref.Strength >= pm.config.MinStrength { |
| 337 | newPrefs = append(newPrefs, pref) |
| 338 | } else { |
| 339 | removed++ |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | pm.preferences[userID] = newPrefs |
| 344 | } |
| 345 | |
| 346 | // 重建索引 |
| 347 | pm.rebuildIndex() |
| 348 | |
| 349 | return removed |
| 350 | } |
| 351 | |
| 352 | // rebuildIndex 重建类别索引 |
| 353 | func (pm *PreferenceManager) rebuildIndex() { |