GetTopPreferences 获取强度最高的 N 个偏好
( ctx context.Context, userID string, limit int, )
| 284 | |
| 285 | // GetTopPreferences 获取强度最高的 N 个偏好 |
| 286 | func (pm *PreferenceManager) GetTopPreferences( |
| 287 | ctx context.Context, |
| 288 | userID string, |
| 289 | limit int, |
| 290 | ) ([]*Preference, error) { |
| 291 | pm.mu.RLock() |
| 292 | defer pm.mu.RUnlock() |
| 293 | |
| 294 | prefs := pm.preferences[userID] |
| 295 | if len(prefs) == 0 { |
| 296 | return []*Preference{}, nil |
| 297 | } |
| 298 | |
| 299 | // 复制并排序 |
| 300 | sorted := make([]*Preference, len(prefs)) |
| 301 | copy(sorted, prefs) |
| 302 | |
| 303 | sort.Slice(sorted, func(i, j int) bool { |
| 304 | return sorted[i].Strength > sorted[j].Strength |
| 305 | }) |
| 306 | |
| 307 | if limit > len(sorted) { |
| 308 | limit = len(sorted) |
| 309 | } |
| 310 | |
| 311 | return sorted[:limit], nil |
| 312 | } |
| 313 | |
| 314 | // ApplyDecay 应用强度衰减 |
| 315 | func (pm *PreferenceManager) ApplyDecay(ctx context.Context) int { |