
    q(i                     b    d dl Z d dlZd dlmZmZ d dlmZ  e       Z G d d      ZdadefdZ	y)    N)ListDict)
get_configc                       e Zd ZdZd Zdee   fdZddee   de	dee   fdZ
dd	ede	dee   fd
Zdde	de	dee   fdZddede	dee   fdZ	 	 	 ddee   d	ede	dede	dee   fdZde	defdZy)RecipeSearchServicez$Search service for RecipeNLG datasetc                 .    | j                         | _        y)z-Initialize search service with sample recipesN)_load_sample_recipesrecipesselfs    I/Users/apple/Desktop/TU/AI/AI-Recipe-Generator/services/search_service.py__init__zRecipeSearchService.__init__   s    002    returnc                 ~    ddg ddddddd	d
dg ddddd
dd	ddg ddddddd	ddg ddddd
dd	ddg ddddddd	gS )z
        Load sample recipes (in production, load from RecipeNLG dataset)
        For now, using hardcoded sample data
           zClassic Tomato Pasta)pastatomatogarlic	olive oilbasiluY   Boil pasta. Sauté garlic in olive oil. Add tomatoes. Combine with pasta. Top with basil.lunch      easy)idtitleingredientsinstructions	meal_typecooking_timeservings
difficulty   zGrilled Chicken Salad)chickenlettucer   cucumberr   lemonzDGrill chicken. Chop vegetables. Mix with olive oil and lemon. Serve.      zMushroom Risotto)ricemushroomonionr   brothparmesanuW   Sauté onion and garlic. Add rice. Gradually add broth. Stir in mushrooms and parmesan.dinner(   mediumzVegetable Stir Fry)broccolicarrotzbell pepperz	soy saucer   gingerzCHeat oil. Stir fry vegetables. Add soy sauce and ginger. Serve hot.      Pancakes)floureggmilkzbaking powderbutterzmaple syrupzLMix dry ingredients. Add wet ingredients. Cook on griddle. Serve with syrup.	breakfast r   s    r   r	   z(RecipeSearchService._load_sample_recipes   s     /R {$ "$	 0a f$ "$	 +[ y% "&	 -e e% "$	 #a n( "$	S3
 3	
r   Nr   limitc                   	 |t         j                  }|D cg c]  }|j                          }}g }| j                  D ]Q  }|d   D cg c]  }|j                          c}	t	        	fd|D              }|dkD  s>|j                  ||d       S |j                  d d       |d| D cg c]  }|d	   	 c}S c c}w c c}w c c}w )
a  
        Search recipes by ingredients
        
        Args:
            ingredients (list): List of ingredients to search
            limit (int): Maximum number of results
        
        Returns:
            list: Matching recipes sorted by relevance
        Nr   c              3   N   K   | ]  t        fd D              sd  yw)c              3   &   K   | ]  }|v  
 y wNr>   .0r_ingings     r   	<genexpr>zFRecipeSearchService.search_by_ingredients.<locals>.<genexpr>.<genexpr>^   s     @nRW@ns   r   Nany)rE   rG   recipe_ingredientss    @r   rH   z<RecipeSearchService.search_by_ingredients.<locals>.<genexpr>^   s     oS@n[m@n=n!os   %%r   )recipescorec                     | d   S )NrM   r>   )xs    r   <lambda>z;RecipeSearchService.search_by_ingredients.<locals>.<lambda>g   s
    !G* r   T)keyreverserL   )configMAX_SEARCH_RESULTSlowerr
   sumappendsort)
r   r   r?   rG   search_ingredientsscored_recipesrL   matchesitemrK   s
            @r   search_by_ingredientsz)RecipeSearchService.search_by_ingredientsI   s     =--E 6AAcciikAA ll 	F9?9N!O##))+!Oo'9ooG{%%$$' 	 	 4dC+9&5+AB4XBB! B
 "P Cs   B=	C.Cr    c                     |t         j                  }| j                  D cg c])  }|d   j                         |j                         k(  s(|+ }}|d| S c c}w )a  
        Search recipes by meal type
        
        Args:
            meal_type (str): Type of meal (breakfast, lunch, dinner)
            limit (int): Maximum number of results
        
        Returns:
            list: Recipes matching the meal type
        Nr    rS   rT   r
   rU   )r   r    r?   rresultss        r   search_by_meal_typez'RecipeSearchService.search_by_meal_typej   sX     =--E"llZan.B.B.D	HY.Y1ZZv [   )AAmax_timec                 ~    |t         j                  }| j                  D cg c]  }|d   |k  s| }}|d| S c c}w )a  
        Search recipes by maximum cooking time
        
        Args:
            max_time (int): Maximum cooking time in minutes
            limit (int): Maximum number of results
        
        Returns:
            list: Recipes within time limit
        Nr!   )rS   rT   r
   )r   rd   r?   r`   ra   s        r   search_by_cooking_timez*RecipeSearchService.search_by_cooking_time{   sI     =--E"llLa.?8.K1LLv Ms   ::r#   c                     |t         j                  }| j                  D cg c])  }|d   j                         |j                         k(  s(|+ }}|d| S c c}w )a
  
        Search recipes by difficulty level
        
        Args:
            difficulty (str): Difficulty level (easy, medium, hard)
            limit (int): Maximum number of results
        
        Returns:
            list: Recipes matching difficulty
        Nr#   r_   )r   r#   r?   r`   ra   s        r   search_by_difficultyz(RecipeSearchService.search_by_difficulty   sZ     =--E"ll\ao.C.C.EIYIYI[.[1\\v ]rc   max_cooking_timec                 ,  	 |t         j                  }| j                  j                         }|rB|D cg c]  }|j	                          }}|D 		cg c]  	t        	fd|D              s	 }}	|r4|D 	cg c])  }	|	d   j	                         |j	                         k(  s(|	+ }}	|r|D 	cg c]  }	|	d   |k  s|	 }}	|r4|D 	cg c])  }	|	d   j	                         |j	                         k(  s(|	+ }}	|d| S c c}w c c}	w c c}	w c c}	w c c}	w )a{  
        Advanced search with multiple filters
        
        Args:
            ingredients (list): Required ingredients
            meal_type (str): Type of meal
            max_cooking_time (int): Maximum cooking time
            difficulty (str): Difficulty level
            limit (int): Maximum results
        
        Returns:
            list: Filtered recipes
        Nc              3   N   K   | ]  t        fd d   D                yw)c              3   B   K   | ]  }|j                         v   y wrC   )rU   rD   s     r   rH   z@RecipeSearchService.advanced_search.<locals>.<genexpr>.<genexpr>   s     GUC5;;=(Gs   r   NrI   )rE   rG   r`   s    @r   rH   z6RecipeSearchService.advanced_search.<locals>.<genexpr>   s)      1 Ga6FGG1s   !%r    r!   r#   )rS   rT   r
   copyrU   rJ   )
r   r   r    ri   r#   r?   ra   rG   rY   r`   s
            `r   advanced_searchz#RecipeSearchService.advanced_search   s)     =--E,,##% 9D!E##))+!E!E") QS 1-1 .q G  ")YQQ{^-A-A-CyGX-XqYGY ")SQQ~->BR-RqSGS ")[QQ|_-B-B-D
HXHXHZ-Zq[G[v% "F Z T \s5   C=D,D8)D"D.D<D)D2D	recipe_idc                 >    | j                   D ]  }|d   |k(  s|c S  y)zGet a specific recipe by IDr   N)r
   )r   ro   rL   s      r   get_recipe_by_idz$RecipeSearchService.get_recipe_by_id   s+    ll 	Fd|y(	 r   rC   )NNNNN)__name__
__module____qualname____doc__r   r   r   r	   strintr]   rb   rf   rh   rn   rq   r>   r   r   r   r      s    .38
d4j 8
tCc C3 CRVW[R\ CBS  T
 "s 3 $t* "s 3 $t* " OSGK$()49 ) )),)AD)!)-1$Z)V# $ r   r   r   c                  .    t         
t               a t         S )z%Get or create search service instance)search_servicer   r>   r   r   get_search_servicerz      s     ,.r   )
jsonostypingr   r   rS   r   r   ry   rz   r>   r   r   <module>r~      s9     	  	E ER / r   