Spaces:
Sleeping
Sleeping
Update recipe_recommendation/src/candidate.py
Browse files
recipe_recommendation/src/candidate.py
CHANGED
|
@@ -22,68 +22,69 @@ def extract_user_parents(user_ingredients):
|
|
| 22 |
return user_parents
|
| 23 |
|
| 24 |
|
| 25 |
-
|
| 26 |
-
# diet = user_profile.get("diet", {}).get("vegetarian_type", "").lower()
|
| 27 |
-
# if diet == "vegan" and not recipe.get("is_vegan_safe", True):
|
| 28 |
-
# return False
|
| 29 |
-
# if diet in ["vegetarian", "flexible_vegetarian"] and not recipe.get("is_vegetarian_safe", True):
|
| 30 |
-
# return False
|
| 31 |
-
# return True
|
| 32 |
-
|
| 33 |
-
def hard_filter(recipe: dict, user_profile: dict) -> bool:
|
| 34 |
-
"""
|
| 35 |
-
Apply hard filters to determine whether a recipe matches the user's dietary profile.
|
| 36 |
-
|
| 37 |
-
Args:
|
| 38 |
-
recipe (dict): Recipe data containing attributes like 'calories', 'protein', 'is_vegan_safe', etc.
|
| 39 |
-
user_profile (dict): User preferences including diet type, nutritional goals, and disliked ingredients.
|
| 40 |
-
|
| 41 |
-
Returns:
|
| 42 |
-
bool: True if the recipe passes all hard filters, False otherwise.
|
| 43 |
-
"""
|
| 44 |
-
|
| 45 |
-
# --- Dietary filter ---
|
| 46 |
diet = user_profile.get("diet", {}).get("vegetarian_type", "").lower()
|
| 47 |
if diet == "vegan" and not recipe.get("is_vegan_safe", True):
|
| 48 |
return False
|
| 49 |
if diet in ["vegetarian", "flexible_vegetarian"] and not recipe.get("is_vegetarian_safe", True):
|
| 50 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
-
|
| 53 |
-
|
|
|
|
| 54 |
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
cal_max = cal_range.get("max", 9999)
|
| 59 |
-
recipe_calories = recipe.get("calories", 0)
|
| 60 |
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
pro_min = protein_range.get("min", 0)
|
| 67 |
-
pro_max = protein_range.get("max", 999)
|
| 68 |
-
recipe_protein = recipe.get("protein", 0)
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
|
|
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
|
| 86 |
-
|
| 87 |
|
| 88 |
|
| 89 |
|
|
|
|
| 22 |
return user_parents
|
| 23 |
|
| 24 |
|
| 25 |
+
def hard_filter(recipe, user_profile):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 26 |
diet = user_profile.get("diet", {}).get("vegetarian_type", "").lower()
|
| 27 |
if diet == "vegan" and not recipe.get("is_vegan_safe", True):
|
| 28 |
return False
|
| 29 |
if diet in ["vegetarian", "flexible_vegetarian"] and not recipe.get("is_vegetarian_safe", True):
|
| 30 |
return False
|
| 31 |
+
return True
|
| 32 |
+
|
| 33 |
+
# harder filter
|
| 34 |
+
# def hard_filter(recipe: dict, user_profile: dict) -> bool:
|
| 35 |
+
# """
|
| 36 |
+
# Apply hard filters to determine whether a recipe matches the user's dietary profile.
|
| 37 |
|
| 38 |
+
# Args:
|
| 39 |
+
# recipe (dict): Recipe data containing attributes like 'calories', 'protein', 'is_vegan_safe', etc.
|
| 40 |
+
# user_profile (dict): User preferences including diet type, nutritional goals, and disliked ingredients.
|
| 41 |
|
| 42 |
+
# Returns:
|
| 43 |
+
# bool: True if the recipe passes all hard filters, False otherwise.
|
| 44 |
+
# """
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# # --- Dietary filter ---
|
| 47 |
+
# diet = user_profile.get("diet", {}).get("vegetarian_type", "").lower()
|
| 48 |
+
# if diet == "vegan" and not recipe.get("is_vegan_safe", True):
|
| 49 |
+
# return False
|
| 50 |
+
# if diet in ["vegetarian", "flexible_vegetarian"] and not recipe.get("is_vegetarian_safe", True):
|
| 51 |
+
# return False
|
| 52 |
|
| 53 |
+
# # --- Nutritional goal filter ---
|
| 54 |
+
# nutritional_goals = user_profile.get("nutritional_goals", {})
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
# # Calorie range filter
|
| 57 |
+
# cal_range = nutritional_goals.get("calories", {})
|
| 58 |
+
# cal_min = cal_range.get("min", 0)
|
| 59 |
+
# cal_max = cal_range.get("max", 9999)
|
| 60 |
+
# recipe_calories = recipe.get("calories", 0)
|
| 61 |
|
| 62 |
+
# if not (cal_min <= recipe_calories <= cal_max):
|
| 63 |
+
# return False
|
| 64 |
+
|
| 65 |
+
# # Protein range filter
|
| 66 |
+
# protein_range = nutritional_goals.get("protein", {})
|
| 67 |
+
# pro_min = protein_range.get("min", 0)
|
| 68 |
+
# pro_max = protein_range.get("max", 999)
|
| 69 |
+
# recipe_protein = recipe.get("protein", 0)
|
| 70 |
+
|
| 71 |
+
# if not (pro_min <= recipe_protein <= pro_max):
|
| 72 |
+
# return False
|
| 73 |
+
|
| 74 |
+
# # --- Disliked main ingredients filter ---
|
| 75 |
+
# disliked_main = set(user_profile.get("other_preferences", {}).get("disliked_main", []))
|
| 76 |
+
# if disliked_main:
|
| 77 |
+
# recipe_main = recipe.get("main_parent", set())
|
| 78 |
+
# if isinstance(recipe_main, list):
|
| 79 |
+
# recipe_main = set(recipe_main)
|
| 80 |
+
# elif not isinstance(recipe_main, set):
|
| 81 |
+
# recipe_main = set()
|
| 82 |
|
| 83 |
+
# # Exclude if any main ingredient is in the disliked list
|
| 84 |
+
# if recipe_main & disliked_main:
|
| 85 |
+
# return False
|
| 86 |
|
| 87 |
+
# return True
|
| 88 |
|
| 89 |
|
| 90 |
|