Update app.py
Browse files
app.py
CHANGED
|
@@ -254,12 +254,13 @@ async def log_order_tracking(order_id: str, status: str, message: str = None):
|
|
| 254 |
|
| 255 |
from fuzzywuzzy import fuzz
|
| 256 |
|
| 257 |
-
|
|
|
|
| 258 |
best_match = None
|
| 259 |
best_score = 0
|
| 260 |
for item in menu_items:
|
| 261 |
dish_name = item["name"]
|
| 262 |
-
score = fuzz.
|
| 263 |
if score > best_score:
|
| 264 |
best_score = score
|
| 265 |
best_match = dish_name
|
|
@@ -397,21 +398,21 @@ async def process_order_flow(user_id: str, message: str) -> str:
|
|
| 397 |
return "Sure! What dish would you like to order?"
|
| 398 |
|
| 399 |
# If no state exists or not in order flow, try to detect a dish using fuzzy matching.
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
| 409 |
-
|
| 410 |
-
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
phone_pattern = r'(\+?\d{10,15})'
|
| 416 |
phone_match = re.search(phone_pattern, message)
|
| 417 |
address = None
|
|
|
|
| 254 |
|
| 255 |
from fuzzywuzzy import fuzz
|
| 256 |
|
| 257 |
+
|
| 258 |
+
def match_dish(user_input: str, threshold: int = 70) -> str:
|
| 259 |
best_match = None
|
| 260 |
best_score = 0
|
| 261 |
for item in menu_items:
|
| 262 |
dish_name = item["name"]
|
| 263 |
+
score = fuzz.token_sort_ratio(user_input.lower(), dish_name.lower())
|
| 264 |
if score > best_score:
|
| 265 |
best_score = score
|
| 266 |
best_match = dish_name
|
|
|
|
| 398 |
return "Sure! What dish would you like to order?"
|
| 399 |
|
| 400 |
# If no state exists or not in order flow, try to detect a dish using fuzzy matching.
|
| 401 |
+
if not state or state.flow != "order":
|
| 402 |
+
found_dish = match_dish(message)
|
| 403 |
+
if found_dish:
|
| 404 |
+
state = ConversationState()
|
| 405 |
+
state.flow = "order"
|
| 406 |
+
state.data["dish"] = found_dish
|
| 407 |
+
state.update_last_active()
|
| 408 |
+
user_state[user_id] = state
|
| 409 |
+
numbers = re.findall(r'\d+', message)
|
| 410 |
+
if numbers:
|
| 411 |
+
quantity = int(numbers[0])
|
| 412 |
+
if quantity <= 0:
|
| 413 |
+
return "Please enter a valid quantity (e.g., 1, 2, 3)."
|
| 414 |
+
state.data["quantity"] = quantity
|
| 415 |
+
state.step = 3
|
| 416 |
phone_pattern = r'(\+?\d{10,15})'
|
| 417 |
phone_match = re.search(phone_pattern, message)
|
| 418 |
address = None
|