Spaces:
Paused
Paused
| import re | |
| def extract_parameters(variables_list, user_input): | |
| extracted_params = [] | |
| for pattern in variables_list: | |
| regex = re.sub(r"(\w+):\{(.+?)\}", r"(?P<\1>.+?)", pattern) | |
| match = re.match(regex, user_input) | |
| if match: | |
| extracted_params = [{"key": k, "value": v} for k, v in match.groupdict().items()] | |
| break | |
| if not extracted_params: | |
| city_pattern = r"(\bAnkara\b|\bİstanbul\b|\bİzmir\b)" | |
| cities = re.findall(city_pattern, user_input) | |
| if len(cities) >= 2: | |
| extracted_params = [ | |
| {"key": "from_location", "value": cities[0]}, | |
| {"key": "to_location", "value": cities[1]} | |
| ] | |
| return extracted_params | |
| def resolve_placeholders(text: str, session: dict, variables: dict) -> str: | |
| def replacer(match): | |
| full = match.group(1) | |
| try: | |
| if full.startswith("variables."): | |
| key = full.split(".", 1)[1] | |
| return str(variables.get(key, f"{{{full}}}")) | |
| elif full.startswith("session."): | |
| key = full.split(".", 1)[1] | |
| return str(session.get("variables", {}).get(key, f"{{{full}}}")) | |
| elif full.startswith("auth_tokens."): | |
| parts = full.split(".") | |
| if len(parts) == 3: | |
| intent, token_type = parts[1], parts[2] | |
| return str(session.get("auth_tokens", {}).get(intent, {}).get(token_type, f"{{{full}}}")) | |
| else: | |
| return f"{{{full}}}" | |
| else: | |
| return f"{{{full}}}" | |
| except Exception: | |
| return f"{{{full}}}" | |
| return re.sub(r"\{([^{}]+)\}", replacer, text) | |
| def validate_variable_formats(variables, variable_format_map, data_formats): | |
| errors = {} | |
| for var_name, format_name in variable_format_map.items(): | |
| value = variables.get(var_name) | |
| if value is None: | |
| continue | |
| format_def = data_formats.get(format_name) | |
| if not format_def: | |
| continue | |
| if "valid_options" in format_def: | |
| if value not in format_def["valid_options"]: | |
| errors[var_name] = format_def.get("error_message", f"{var_name} değeri geçersiz.") | |
| elif "pattern" in format_def: | |
| if not re.fullmatch(format_def["pattern"], value): | |
| errors[var_name] = format_def.get("error_message", f"{var_name} formatı geçersiz.") | |
| return len(errors) == 0, errors | |