單憑是否快過期,優先使用過期食材進行料理,乍聽之下很合理,但是,如果快要過期的東西,都是一堆「調味料」,那該怎麼辦?
為了避免拿一堆調味料當主菜使用,應該釐清用途後,再進行權值計算。
先列出一個基礎的想法:用 期限、用途、剩餘量 來算分數。
Priority Score = Expiration Score + Role Score + Quantity Score
RecommendationServiceapp/
├── services/
│ ├── ingredient_context_service.py
│ └── recommendation_service.py
class RecommendationService:
def calculate_score(self, ingredient):
score = 0
remaining_days = ingredient[
"remaining_days"
]
if remaining_days is not None:
if remaining_days <= 0:
score += 100
elif remaining_days == 1:
score += 80
elif remaining_days == 2:
score += 60
elif remaining_days == 3:
score += 40
else:
score += 10
role_scores = {
"PROTEIN": 30,
"VEGETABLE": 30,
"CARB": 20,
"DAIRY": 10,
"CONDIMENT": 0,
}
score += role_scores.get(
ingredient["role"],
0
)
return score
GET /api/recommendations/ingredients
[
{
"name": "豆腐",
"remaining_days": 1,
"role": "PROTEIN",
"score": 110
},
{
"name": "青江菜",
"remaining_days": 2,
"role": "VEGETABLE",
"score": 90
}
]