Fred808 commited on
Commit
8e56df0
·
verified ·
1 Parent(s): f006713

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -15
app.py CHANGED
@@ -398,27 +398,32 @@ def match_dish(user_input: str, threshold: int = 70) -> str:
398
 
399
  def match_dishes(user_input: str, threshold: int = 70) -> list:
400
  """
401
- First, try to find an exact (whole word) match for dish names.
402
- If found, return those matches; otherwise, fall back to fuzzy matching.
 
 
403
  """
 
 
404
  matched_dishes = []
405
- user_input_lower = user_input.lower().strip()
406
-
407
- # Check for exact whole-word matches
408
  for item in menu_items:
409
  dish_name = item["name"]
410
- # Use word boundaries so that 'fried rice' only matches 'Fried Rice'
 
411
  pattern = r'\b' + re.escape(dish_name.lower()) + r'\b'
412
- if re.search(pattern, user_input_lower):
413
  matched_dishes.append(dish_name)
414
 
 
415
  if matched_dishes:
416
  return list(set(matched_dishes))
417
 
418
- # If no exact match, use fuzzy matching as fallback.
419
  for item in menu_items:
420
  dish_name = item["name"]
421
- score = fuzz.token_sort_ratio(user_input_lower, dish_name.lower())
422
  if score >= threshold:
423
  matched_dishes.append(dish_name)
424
 
@@ -763,10 +768,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
763
  asyncio.create_task(save_order())
764
  asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
765
 
766
- # Email notification to admin (using your SMTP integration)
767
- admin_emails = os.getenv("ADMIN_EMAILS", "admin@example.com").split(",")
768
- email_subject = f"New Order Received: {order_id}"
769
- email_body = (
770
  f"New Order Received:\n"
771
  f"Order ID: {order_id}\n"
772
  f"Dish(es): {dish_summary}\n"
@@ -777,7 +779,7 @@ async def process_order_flow(user_id: str, message: str) -> str:
777
  f"Extras: {state.data.get('extras', 'None')}\n"
778
  f"Status: Pending Payment"
779
  )
780
- email_sent = send_email_notification(admin_emails, email_subject, email_body)
781
  if email_sent:
782
  print("Email notification sent successfully.")
783
  else:
@@ -811,7 +813,6 @@ async def process_order_flow(user_id: str, message: str) -> str:
811
  # Instead of immediately canceling, re-prompt for confirmation.
812
  return "Please confirm your order by typing 'yes' to confirm or 'no' to cancel."
813
 
814
- # Final fallback
815
  return ""
816
 
817
 
 
398
 
399
  def match_dishes(user_input: str, threshold: int = 70) -> list:
400
  """
401
+ Returns a list of dish names that appear as whole words in the input.
402
+ First, normalize the input to collapse multiple spaces.
403
+ Then, try an exact whole-word match using a regex with word boundaries.
404
+ If no exact match is found, fall back to fuzzy matching.
405
  """
406
+ # Normalize input: lowercase, strip, and collapse spaces
407
+ normalized_input = re.sub(r'\s+', ' ', user_input.lower().strip())
408
  matched_dishes = []
409
+
410
+ # Try exact matching with word boundaries.
 
411
  for item in menu_items:
412
  dish_name = item["name"]
413
+ # Create a pattern with word boundaries; e.g., for "fried rice":
414
+ # pattern becomes: r'\bfried rice\b'
415
  pattern = r'\b' + re.escape(dish_name.lower()) + r'\b'
416
+ if re.search(pattern, normalized_input):
417
  matched_dishes.append(dish_name)
418
 
419
+ # If we found any exact matches, return them.
420
  if matched_dishes:
421
  return list(set(matched_dishes))
422
 
423
+ # If no exact match, fall back to fuzzy matching.
424
  for item in menu_items:
425
  dish_name = item["name"]
426
+ score = fuzz.token_sort_ratio(normalized_input, dish_name.lower())
427
  if score >= threshold:
428
  matched_dishes.append(dish_name)
429
 
 
768
  asyncio.create_task(save_order())
769
  asyncio.create_task(log_order_tracking(order_id, "Order Placed", "Order placed and awaiting payment."))
770
 
771
+ order_details = (
 
 
 
772
  f"New Order Received:\n"
773
  f"Order ID: {order_id}\n"
774
  f"Dish(es): {dish_summary}\n"
 
779
  f"Extras: {state.data.get('extras', 'None')}\n"
780
  f"Status: Pending Payment"
781
  )
782
+ email_sent = send_email_notification(order_details)
783
  if email_sent:
784
  print("Email notification sent successfully.")
785
  else:
 
813
  # Instead of immediately canceling, re-prompt for confirmation.
814
  return "Please confirm your order by typing 'yes' to confirm or 'no' to cancel."
815
 
 
816
  return ""
817
 
818