|
| 1 | +import nltk |
| 2 | +import random |
| 3 | +from nltk.corpus import stopwords |
| 4 | +from nltk.tokenize import word_tokenize |
| 5 | + |
| 6 | +# Sample database of recipes |
| 7 | +recipes = { |
| 8 | + "Spaghetti Carbonara": { |
| 9 | + "ingredients": "pasta, eggs, bacon, parmesan cheese, black pepper", |
| 10 | + "source_link": "https://www.example.com/spaghetti-carbonara", |
| 11 | + "preferences": ["non-vegetarian"], |
| 12 | + "instructions": "1. Cook pasta according to package instructions.\n2. In a separate bowl, whisk together eggs, grated parmesan cheese, and black pepper.\n3. In a skillet, cook bacon until crispy, then remove from heat.\n4. Toss cooked pasta with the bacon and bacon fat.\n5. Add the egg mixture to the pasta, tossing quickly to avoid scrambling the eggs. The heat from the pasta will cook the eggs and create a creamy sauce. Serve immediately.", |
| 13 | + }, |
| 14 | + "Chicken Stir-Fry": { |
| 15 | + "ingredients": "chicken, broccoli, carrots, soy sauce, garlic, ginger", |
| 16 | + "source_link": "https://www.example.com/chicken-stir-fry", |
| 17 | + "preferences": ["non-vegetarian"], |
| 18 | + "instructions": "1. Heat oil in a wok or large skillet over high heat.\n2. Add chopped chicken and cook until browned and cooked through.\n3. Add minced garlic and grated ginger, stir for a minute.\n4. Add chopped vegetables (broccoli, carrots) and stir-fry until tender-crisp.\n5. Pour in soy sauce and toss everything together. Serve hot.", |
| 19 | + }, |
| 20 | + "Caprese Salad": { |
| 21 | + "ingredients": "tomatoes, mozzarella cheese, basil, olive oil, balsamic vinegar", |
| 22 | + "source_link": "https://www.example.com/caprese-salad", |
| 23 | + "preferences": ["vegetarian"], |
| 24 | + "instructions": "1. Slice tomatoes and mozzarella cheese.\n2. Arrange tomato and mozzarella slices on a plate, alternating them.\n3. Drizzle with olive oil and balsamic vinegar.\n4. Sprinkle fresh basil leaves over the top. Serve as a refreshing salad.", |
| 25 | + }, |
| 26 | + "Chocolate Brownies": { |
| 27 | + "ingredients": "chocolate, butter, sugar, eggs, flour, vanilla extract", |
| 28 | + "source_link": "https://www.example.com/chocolate-brownies", |
| 29 | + "preferences": ["vegetarian"], |
| 30 | + "instructions": "1. Preheat oven to 350°F (175°C). Grease a baking pan.\n2. In a microwave-safe bowl, melt chocolate and butter together.\n3. Stir in sugar, eggs, flour, and vanilla extract until well combined.\n4. Pour the batter into the prepared pan.\n5. Bake for about 25 minutes or until a toothpick inserted in the center comes out with moist crumbs. Let it cool before cutting into squares.", |
| 31 | + }, |
| 32 | + "Mushroom Risotto": { |
| 33 | + "ingredients": "arborio rice, mushrooms, vegetable broth, onion, garlic, parmesan cheese", |
| 34 | + "source_link": "https://www.example.com/mushroom-risotto", |
| 35 | + "preferences": ["vegetarian"], |
| 36 | + "instructions": "1. In a large skillet, sauté chopped onion and minced garlic in olive oil until translucent.\n2. Add arborio rice and stir until coated with oil.\n3. Gradually add vegetable broth, one ladleful at a time, stirring until the liquid is absorbed.\n4. Stir in sliced mushrooms and continue adding broth until the rice is creamy and cooked.\n5. Stir in grated parmesan cheese and serve hot.", |
| 37 | + }, |
| 38 | + "Grilled Salmon": { |
| 39 | + "ingredients": "salmon fillet, lemon, olive oil, garlic, dill, salt, pepper", |
| 40 | + "source_link": "https://www.example.com/grilled-salmon", |
| 41 | + "preferences": ["non-vegetarian"], |
| 42 | + "instructions": "1. Preheat grill to medium-high heat.\n2. Rub salmon fillet with olive oil and season with salt, pepper, and minced garlic.\n3. Place the salmon on the grill and cook for a few minutes on each side until it flakes easily with a fork.\n4. Squeeze fresh lemon juice over the salmon and sprinkle with chopped dill before serving.", |
| 43 | + }, |
| 44 | + "Vegetable Curry": { |
| 45 | + "ingredients": "mixed vegetables, coconut milk, curry paste, onion, garlic, ginger", |
| 46 | + "source_link": "https://www.example.com/vegetable-curry", |
| 47 | + "preferences": ["vegetarian", "vegan"], |
| 48 | + "instructions": "1. In a large pot, sauté chopped onion, minced garlic, and grated ginger in oil until fragrant.\n2. Add mixed vegetables and cook for a few minutes.\n3. Stir in curry paste and cook for another minute.\n4. Pour in coconut milk and bring to a simmer until the vegetables are tender.\n5. Serve the vegetable curry over cooked rice.", |
| 49 | + }, |
| 50 | + "Apple Pie": { |
| 51 | + "ingredients": "apples, sugar, flour, butter, cinnamon, pie crust", |
| 52 | + "source_link": "https://www.example.com/apple-pie", |
| 53 | + "preferences": ["vegetarian"], |
| 54 | + "instructions": "1. Preheat oven to 375°F (190°C).\n2. Peel and slice apples, then mix with sugar, cinnamon, and flour.\n3. Roll out pie crust and place it in a pie dish.\n4. Fill the pie crust with the apple mixture.\n5. Add small pieces of butter on top of the apples.\n6. Roll out another pie crust for the top, seal the edges, and cut slits for venting.\n7. Bake for about 45 minutes or until the crust is golden brown and the apples are tender.", |
| 55 | + }, |
| 56 | +} |
| 57 | +# Instructions for each recipe |
| 58 | +recipe_instructions = { |
| 59 | + "Spaghetti Carbonara": "...", # Instructions for Spaghetti Carbonara |
| 60 | + "Chicken Stir-Fry": "...", # Instructions for Chicken Stir-Fry |
| 61 | + "Caprese Salad": "...", # Instructions for Caprese Salad |
| 62 | + "Chocolate Brownies": "...", # Instructions for Chocolate Brownies |
| 63 | + "Mushroom Risotto": "...", # Instructions for Mushroom Risotto |
| 64 | + "Grilled Salmon": "...", # Instructions for Grilled Salmon |
| 65 | + "Vegetable Curry": "...", # Instructions for Vegetable Curry |
| 66 | + "Apple Pie": "...", # Instructions for Apple Pie |
| 67 | +} |
| 68 | + |
| 69 | +# Function to preprocess the recipes |
| 70 | +def preprocess_recipe(recipe): |
| 71 | + stop_words = set(stopwords.words("english")) |
| 72 | + tokens = word_tokenize(recipe.lower()) |
| 73 | + return [word for word in tokens if word.isalpha() and word not in stop_words] |
| 74 | + |
| 75 | +# Function to generate a unique recipe |
| 76 | +def generate_recipe(ingredients, preferences=None): |
| 77 | + processed_ingredients = preprocess_recipe(ingredients) |
| 78 | + |
| 79 | + matching_recipes = [] |
| 80 | + for name, recipe_info in recipes.items(): |
| 81 | + if all(ingredient in recipe_info["ingredients"] for ingredient in processed_ingredients): |
| 82 | + if not preferences or any(pref in preferences for pref in recipe_info["preferences"]): |
| 83 | + matching_recipes.append(name) |
| 84 | + |
| 85 | + if not matching_recipes: |
| 86 | + return "Sorry, no recipe found with those ingredients and preferences. Try something else." |
| 87 | + |
| 88 | + generated_recipe_name = random.choice(matching_recipes) |
| 89 | + return generated_recipe_name, recipes[generated_recipe_name]["source_link"] |
| 90 | + |
| 91 | +# Function to get user ingredient substitutions |
| 92 | + |
| 93 | +# Function to suggest similar recipes |
| 94 | +def suggest_similar_recipes(generated_recipe_name): |
| 95 | + similar_recipes = random.sample([name for name in recipes.keys() if name != generated_recipe_name], 2) |
| 96 | + return similar_recipes |
| 97 | + |
| 98 | +# Main function |
| 99 | +def main(): |
| 100 | + print("AI-Powered Recipe Generator") |
| 101 | + print("Available Recipes:") |
| 102 | + for idx, recipe_name in enumerate(recipes.keys(), start=1): |
| 103 | + print(f"{idx}. {recipe_name}") |
| 104 | + |
| 105 | + user_ingredients = input("Enter the list of ingredients you have (comma-separated): ") |
| 106 | + user_preferences = input("Enter your dietary preferences (comma-separated, or press Enter to skip): ").split(",") |
| 107 | + |
| 108 | + generated_recipe_name, source_link = generate_recipe(user_ingredients, user_preferences) |
| 109 | + |
| 110 | + print("\nGenerated Recipe:") |
| 111 | + print(f"Recipe: {generated_recipe_name}") |
| 112 | + print("Ingredients:", recipes[generated_recipe_name]["ingredients"]) |
| 113 | + print("Instructions:") |
| 114 | + print(recipe_instructions[generated_recipe_name]) |
| 115 | + print("Source Link:", source_link) |
| 116 | + |
| 117 | + |
| 118 | + similar_recipes = suggest_similar_recipes(generated_recipe_name) |
| 119 | + print("\nYou may also like these recipes:") |
| 120 | + for idx, recipe_name in enumerate(similar_recipes, start=1): |
| 121 | + print(f"{idx}. {recipe_name}") |
| 122 | + |
| 123 | +if __name__ == "__main__": |
| 124 | + nltk.download("punkt") |
| 125 | + nltk.download("stopwords") |
| 126 | + main() |
0 commit comments