空提交
This commit is contained in:
@@ -93,6 +93,9 @@ class RecommendationEngine(BaseModule):
|
||||
context = input_data.get('context', {})
|
||||
|
||||
try:
|
||||
# 检查数据充足性
|
||||
data_check = self._check_data_sufficiency(user_data)
|
||||
|
||||
# 生成完整餐食搭配推荐
|
||||
meal_combinations = self._generate_meal_combinations(user_data, meal_type, preferences, context)
|
||||
|
||||
@@ -101,6 +104,7 @@ class RecommendationEngine(BaseModule):
|
||||
'recommendations': meal_combinations,
|
||||
'reasoning': self._generate_meal_reasoning(meal_combinations, user_data, meal_type),
|
||||
'confidence': self._calculate_recommendation_confidence(user_data),
|
||||
'data_sufficiency': data_check,
|
||||
'metadata': {
|
||||
'meal_type': meal_type,
|
||||
'combination_count': len(meal_combinations)
|
||||
@@ -117,27 +121,34 @@ class RecommendationEngine(BaseModule):
|
||||
combinations = []
|
||||
|
||||
try:
|
||||
# 1. 基于用户历史数据生成搭配
|
||||
historical_combinations = self._generate_historical_combinations(user_data, meal_type)
|
||||
combinations.extend(historical_combinations)
|
||||
# 检查数据充足性
|
||||
data_check = self._check_data_sufficiency(user_data)
|
||||
|
||||
# 2. 基于用户偏好生成个性化搭配
|
||||
personalized_combinations = self._generate_personalized_combinations(user_data, meal_type, preferences)
|
||||
combinations.extend(personalized_combinations)
|
||||
# 数据充足:使用个性化推荐
|
||||
if data_check['sufficient']:
|
||||
# 1. 基于用户历史数据生成搭配
|
||||
historical_combinations = self._generate_historical_combinations(user_data, meal_type)
|
||||
combinations.extend(historical_combinations)
|
||||
|
||||
# 2. 基于用户偏好生成个性化搭配
|
||||
personalized_combinations = self._generate_personalized_combinations(user_data, meal_type, preferences)
|
||||
combinations.extend(personalized_combinations)
|
||||
|
||||
# 3. 如果个性化推荐不足,补充模板
|
||||
if len(combinations) < 3:
|
||||
template_combinations = self._generate_template_combinations(user_data, meal_type)
|
||||
combinations.extend(template_combinations)
|
||||
|
||||
# 3. 基于相似用户生成搭配
|
||||
similar_user_combinations = self._generate_similar_user_combinations(user_data, meal_type)
|
||||
combinations.extend(similar_user_combinations)
|
||||
|
||||
# 4. 如果没有足够数据,使用模板生成
|
||||
if len(combinations) < 3:
|
||||
# 数据不足:使用通用模板推荐
|
||||
else:
|
||||
self.logger.info(f"用户数据不足({data_check['days']}天),使用通用推荐")
|
||||
template_combinations = self._generate_template_combinations(user_data, meal_type)
|
||||
combinations.extend(template_combinations)
|
||||
|
||||
# 5. 去重和排序
|
||||
# 去重和排序
|
||||
combinations = self._deduplicate_and_rank_combinations(combinations, user_data)
|
||||
|
||||
# 6. 确保至少有一些推荐
|
||||
# 确保至少有推荐
|
||||
if not combinations:
|
||||
combinations = self._generate_fallback_combinations(meal_type)
|
||||
|
||||
@@ -145,7 +156,6 @@ class RecommendationEngine(BaseModule):
|
||||
|
||||
except Exception as e:
|
||||
self.logger.error(f"生成餐食搭配失败: {e}")
|
||||
# 返回基础推荐
|
||||
return self._generate_fallback_combinations(meal_type)
|
||||
|
||||
def _generate_historical_combinations(self, user_data: UserData, meal_type: str) -> List[Dict[str, Any]]:
|
||||
@@ -666,18 +676,44 @@ class RecommendationEngine(BaseModule):
|
||||
else:
|
||||
return f"推荐{top_rec['food']},基于您的个人偏好"
|
||||
|
||||
def _check_data_sufficiency(self, user_data: UserData) -> Dict[str, Any]:
|
||||
"""检查数据充足性(按日期去重计算天数)"""
|
||||
if not user_data.meals:
|
||||
return {
|
||||
'sufficient': False,
|
||||
'days': 0,
|
||||
'meals': 0,
|
||||
'message': '暂无餐食记录,将提供通用推荐'
|
||||
}
|
||||
|
||||
# 按日期去重计算天数
|
||||
unique_dates = set(meal.get('date') for meal in user_data.meals if meal.get('date'))
|
||||
meal_count = len(user_data.meals)
|
||||
day_count = len(unique_dates)
|
||||
|
||||
return {
|
||||
'sufficient': day_count >= 3,
|
||||
'days': day_count,
|
||||
'meals': meal_count,
|
||||
'message': f'已收集{day_count}天{meal_count}餐数据' + (',数据充足' if day_count >= 3 else ',建议继续记录')
|
||||
}
|
||||
|
||||
def _calculate_recommendation_confidence(self, user_data: UserData) -> float:
|
||||
"""计算推荐置信度"""
|
||||
meal_count = len(user_data.meals)
|
||||
data_check = self._check_data_sufficiency(user_data)
|
||||
meal_count = data_check['meals']
|
||||
day_count = data_check['days']
|
||||
feedback_count = len(user_data.feedback)
|
||||
|
||||
# 基于数据量计算置信度
|
||||
if meal_count >= 15 and feedback_count >= 5:
|
||||
if day_count >= 5 and feedback_count >= 5:
|
||||
return 0.9
|
||||
elif meal_count >= 10 and feedback_count >= 3:
|
||||
elif day_count >= 3 and meal_count >= 7:
|
||||
return 0.7
|
||||
elif meal_count >= 5:
|
||||
return 0.5
|
||||
elif day_count >= 3:
|
||||
return 0.6
|
||||
elif meal_count >= 3:
|
||||
return 0.4
|
||||
else:
|
||||
return 0.3
|
||||
|
||||
@@ -991,7 +1027,7 @@ if __name__ == "__main__":
|
||||
# 初始化应用
|
||||
config = BaseConfig()
|
||||
if initialize_app(config):
|
||||
print("✅ 应用初始化成功")
|
||||
print(" 应用初始化成功")
|
||||
|
||||
# 测试餐食推荐
|
||||
test_user_id = "test_user_001"
|
||||
@@ -999,7 +1035,7 @@ if __name__ == "__main__":
|
||||
result = generate_meal_recommendations(test_user_id, "lunch", {"taste": "sweet"})
|
||||
if result and result.get('success'):
|
||||
recommendations = result.get('recommendations', [])
|
||||
print(f"✅ 餐食推荐成功,推荐了{len(recommendations)}种食物")
|
||||
print(f" 餐食推荐成功,推荐了{len(recommendations)}种食物")
|
||||
for rec in recommendations[:3]:
|
||||
print(f" - {rec['food']}: {rec.get('score', 0):.2f}")
|
||||
|
||||
@@ -1007,10 +1043,10 @@ if __name__ == "__main__":
|
||||
result = find_similar_foods(test_user_id, "米饭")
|
||||
if result and result.get('success'):
|
||||
similar_foods = result.get('similar_foods', [])
|
||||
print(f"✅ 相似食物查找成功,找到{len(similar_foods)}种相似食物")
|
||||
print(f" 相似食物查找成功,找到{len(similar_foods)}种相似食物")
|
||||
|
||||
# 清理应用
|
||||
cleanup_app()
|
||||
print("✅ 应用清理完成")
|
||||
print(" 应用清理完成")
|
||||
else:
|
||||
print("❌ 应用初始化失败")
|
||||
print(" 应用初始化失败")
|
||||
|
||||
Reference in New Issue
Block a user