The recipe optimization system ensures accurate measurements, proper cooking techniques, and provides intelligent substitutions and dietary modifications:
class RecipeOptimizer:
def _structure_recipe(self, raw_recipe: str, analysis: Dict) -> Dict:
"""
Convert AI-generated recipe text into structured, usable format.
Adds measurement precision, timing details, and quality controls.
"""
sections = self._parse_recipe_sections(raw_recipe)
optimized_ingredients = self._optimize_ingredient_measurements(
sections.get('ingredients', []),
analysis.get('serving_size', 4)
)
timed_instructions = self._add_timing_to_steps(
sections.get('instructions', []),
analysis.get('cooking_methods', [])
)
nutrition_info = self._estimate_nutrition(optimized_ingredients)
return {
'title': analysis.get('dish_name', 'Delicious Recipe'),
'cuisine_type': analysis.get('cuisine_type', 'International'),
'difficulty': analysis.get('complexity', 'Intermediate'),
'total_time': analysis.get('estimated_total_time', 45),
'serving_size': analysis.get('serving_size', 4),
'ingredients': optimized_ingredients,
'instructions': timed_instructions,
'equipment': sections.get('equipment', []),
'tips': sections.get('tips', []),
'variations': sections.get('variations', []),
'nutrition': nutrition_info
}
def _optimize_ingredient_measurements(self, ingredients: List[str], serving_size: int) -> List[Dict]:
"""
Convert ingredient descriptions into precise measurements.
Scales quantities appropriately and suggests substitutions.
"""
optimized = []
for ingredient in ingredients:
parsed = self._parse_ingredient(ingredient)
scaled_quantity = self._scale_quantity(parsed['quantity'], serving_size)
substitutions = self._suggest_substitutions(parsed['item'])
optimized.append({
'item': parsed['item'],
'quantity': scaled_quantity,
'unit': parsed['unit'],
'preparation': parsed.get('preparation', ''),
'substitutions': substitutions,
'optional': 'optional' in ingredient.lower()
})
return optimized
def _add_timing_to_steps(self, instructions: List[str], cooking_methods: List[str]) -> List[Dict]:
"""
Add precise timing information to each cooking step.
Considers cooking method complexity and ingredient requirements.
"""
timed_steps = []
cumulative_time = 0
for i, step in enumerate(instructions):
step_analysis = self._analyze_cooking_step(step, cooking_methods)
step_time = step_analysis['estimated_time']
cumulative_time += step_time
timed_steps.append({
'step_number': i + 1,
'instruction': step,
'estimated_time': step_time,
'cumulative_time': cumulative_time,
'technique': step_analysis['primary_technique'],
'temperature': step_analysis.get('temperature'),
'visual_cues': step_analysis.get('visual_cues', []),
'pro_tips': step_analysis.get('pro_tips', [])
})
return timed_steps
def _estimate_nutrition(self, ingredients: List[Dict]) -> Dict:
"""
Provide basic nutritional estimates based on ingredients.
Uses standard nutritional databases for common ingredients.
"""
nutrition_db = {
'chicken': {'calories_per_100g': 165, 'protein': 31, 'fat': 3.6},
'rice': {'calories_per_100g': 130, 'carbs': 28, 'protein': 2.7},
'olive oil': {'calories_per_100g': 884, 'fat': 100},
'vegetables': {'calories_per_100g': 25, 'carbs': 5, 'fiber': 3}
}
total_calories = 0
total_protein = 0
total_carbs = 0
total_fat = 0
for ingredient in ingredients:
item_name = ingredient['item'].lower()
nutrition_data = self._find_nutrition_match(item_name, nutrition_db)
if nutrition_data:
estimated_grams = self._estimate_ingredient_weight(ingredient)
factor = estimated_grams / 100
total_calories += nutrition_data.get('calories_per_100g', 0) * factor
total_protein += nutrition_data.get('protein', 0) * factor
total_carbs += nutrition_data.get('carbs', 0) * factor
total_fat += nutrition_data.get('fat', 0) * factor
return {
'calories_per_serving': round(total_calories / 4),
'protein_g': round(total_protein / 4, 1),
'carbs_g': round(total_carbs / 4, 1),
'fat_g': round(total_fat / 4, 1),
'disclaimer': 'Nutritional values are estimates based on standard ingredients'
}