Update direction weight
(profile, direction, params)
| 253 | |
| 254 | |
| 255 | def update_weight(profile, direction, params): |
| 256 | """Update direction weight""" |
| 257 | topic = params.get('topic') |
| 258 | new_weight = params.get('weight', 0.5) |
| 259 | core_directions = profile.get('core_directions', {}) |
| 260 | |
| 261 | matched_topic = None |
| 262 | if topic: |
| 263 | for t in core_directions.keys(): |
| 264 | if topic.lower() in t.lower() or t.lower() in topic.lower(): |
| 265 | matched_topic = t |
| 266 | break |
| 267 | |
| 268 | if not matched_topic: |
| 269 | topic_weights = profile.get('topic_weights', {}) |
| 270 | for t in topic_weights.keys(): |
| 271 | if topic.lower() in t.lower() or t.lower() in topic.lower(): |
| 272 | matched_topic = t |
| 273 | core_directions = topic_weights |
| 274 | break |
| 275 | |
| 276 | if not matched_topic: |
| 277 | return (False, f"[错误] 未找到方向:{topic}", None) |
| 278 | |
| 279 | old_weight = core_directions.get(matched_topic, 0.5) |
| 280 | if direction == 'down': |
| 281 | new_weight = max(0.1, old_weight - 0.1) |
| 282 | elif direction == 'up': |
| 283 | new_weight = min(1.0, old_weight + 0.1) |
| 284 | |
| 285 | updated = profile.copy() |
| 286 | if matched_topic in profile.get('core_directions', {}): |
| 287 | updated['core_directions'] = profile['core_directions'].copy() |
| 288 | updated['core_directions'][matched_topic] = new_weight |
| 289 | if matched_topic in profile.get('topic_weights', {}): |
| 290 | updated['topic_weights'] = profile['topic_weights'].copy() |
| 291 | updated['topic_weights'][matched_topic] = new_weight |
| 292 | updated['version'] = f"0.{int(profile.get('version', '0.1').split('.')[1]) + 1}" |
| 293 | updated['updated_at'] = datetime.now().isoformat() |
| 294 | |
| 295 | msg = f"[OK] 已调整 {matched_topic} 权重:{old_weight:.2f} → {new_weight:.2f}" |
| 296 | return (True, msg, updated) |
| 297 | |
| 298 | |
| 299 | def main(): |