Spaces:
Runtime error
Runtime error
| """ | |
| Paso 4: Parsing y estructuración del texto del menú. | |
| """ | |
| import re | |
| from typing import Dict, List | |
| SECTION_PATTERNS = [ | |
| r"(?i)(entrantes|starters|aperitivos)", | |
| r"(?i)(plato principal|principales|main courses?)", | |
| r"(?i)(postres|desserts)", | |
| r"(?i)(bebidas|drinks|beverages)", | |
| ] | |
| PRICE_PATTERN = r"€\s*(\d+(?:[.,]\d{1,2})?)" | |
| def parse_menu_structure(text: str) -> List[Dict]: | |
| """ | |
| Parsea el texto del menú y extrae platos con precios. | |
| Returns: | |
| Lista de dicts con {name, price, section}. | |
| """ | |
| dishes = [] | |
| current_section = "General" | |
| for line in text.split("\n"): | |
| line = line.strip() | |
| if not line: | |
| continue | |
| # Detectar secciones | |
| for pattern in SECTION_PATTERNS: | |
| if re.search(pattern, line): | |
| current_section = line | |
| break | |
| # Detectar platos con precio | |
| price_match = re.search(PRICE_PATTERN, line) | |
| if price_match: | |
| dish_name = re.sub(PRICE_PATTERN, "", line).strip() | |
| if dish_name: | |
| dishes.append({ | |
| "name": dish_name, | |
| "price": price_match.group(0), | |
| "section": current_section, | |
| }) | |
| return dishes | |