Youmnaaaa commited on
Commit
626e1c7
ยท
verified ยท
1 Parent(s): 9bb19b8

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +161 -8
app.py CHANGED
@@ -842,7 +842,7 @@ def apply_keyword_override(text: str) -> Optional[str]:
842
  return "show_menu"
843
  if any(w in t for w in NEXT_WORDS):
844
  return "show_more"
845
- if any(w in t for w in ["ู‚ุงุฑู†", "ู…ู‚ุงุฑู†ุฉ", "ุงุญุณู† ูˆุงุญุฏ ููŠู‡ู…", "ุงูŠู‡ ุงู„ุงุญุณู†", "ู…ูŠู† ุงุญุณู†"]):
846
  return "compare_places"
847
  if any(w in t for w in OPEN_WORDS):
848
  cat = infer_category(text)
@@ -1197,7 +1197,7 @@ def _strip_category_words(q: str) -> str:
1197
  # must not leave "ุงุฑูˆุญ" behind as a fake item; "ุงู‚ุฑุจ ู…ุทุนู… ุงุชุงูƒู„ ููŠู‡"
1198
  # must not leave "ุงุชุงูƒู„" behind either).
1199
  "ุงุฑูˆุญ", "ู‡ุฑูˆุญ", "ุงุดูˆู", "ู‡ุดูˆู", "ุงุชุงูƒู„", "ุฃุชุงูƒู„", "ุงู‚ุฏุฑ", "ุงู„ุงู‚ูŠ", "ุงู„ุงู‚ูŠู‡",
1200
- "ุงุฏูˆุฑ", "ู‡ุฏูˆุฑ", "ุงุฏูŠู†ูŠ", "ุงุฏูŠู„ูŠ", "ู‡ุงุช", "ุฌูŠุจู„ูŠ",
1201
  # generic "something to eat" filler โ€” "ุญุชุฉ ุงูƒู„"/"ุญุชู‡ ุงูƒู„" means "a
1202
  # bite to eat" in general, not a specific dish; never a real item.
1203
  "ุญุชู‡", "ุญุชุฉ", "ุงูƒู„", "ุฃูƒู„",
@@ -1294,7 +1294,11 @@ def _query_matches_blob(query: str, blob: str) -> bool:
1294
  q = clean_text(query)
1295
  if not q or not blob:
1296
  return False
1297
- variants = [q, ar_to_latin_guess(q), _compact_key(q), _phonetic_key(q), _translated_query_terms(q)]
 
 
 
 
1298
  variants = [clean_text(v) for v in variants if clean_text(v) and len(_compact_key(v)) >= 2]
1299
  for v in variants:
1300
  if v and (v in blob or _compact_key(v) in _compact_key(blob)):
@@ -1724,7 +1728,7 @@ def is_best_near_query(text: str, intent: str = "") -> bool:
1724
 
1725
  def compare_place_names(text: str) -> list:
1726
  t = clean_text(text)
1727
- for w in ["ู‚ุงุฑู†", "ู‚ุงุฑู†ูŠ", "ู…ู‚ุงุฑู†ุฉ", "ุจูŠู†", "ู…ุง ุจูŠู†", "ู…ูŠู† ุงุญุณู†", "ุงุญุณู†"]:
1728
  t = re.sub(rf"\b{re.escape(clean_text(w))}\b", " ", t)
1729
  parts = [p.strip() for p in re.split(r"\s+ูˆ\s+|,|ุŒ|/|\\|\s+ูˆู„ุง\s+", t) if p.strip()]
1730
  cleaned = []
@@ -2119,6 +2123,119 @@ def build_response(results: List[dict], intent: str, category: str = "", item_qu
2119
  header += "\n\nููŠ ุฃู…ุงูƒู† ุชุงู†ูŠุฉ ูƒู…ุงู†ุŒ ุงูƒุชุจูŠ ยซูƒู…ุงู†ยป ู„ูˆ ุนุงูŠุฒุงู‡ู…."
2120
  return header
2121
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2122
  def handle_detail(text: str, place: Optional[dict]) -> str:
2123
  if not place:
2124
  return "ุชู‚ุตุฏ ุฃู†ู‡ูŠ ู…ูƒุงู†ุŸ ุงูƒุชุจ ุงุณู… ุงู„ู…ูƒุงู† ุฃูˆ ุงุณุฃู„ู†ูŠ ุงู„ุฃูˆู„ ุนู† ู…ุทุนู…/ุตูŠุฏู„ูŠุฉ/ูƒุงููŠู‡. ๐Ÿ˜Š"
@@ -2140,7 +2257,19 @@ def handle_detail(text: str, place: Optional[dict]) -> str:
2140
  return f"๐Ÿ• {name}: {hours or 'ุงู„ู…ูˆุงุนูŠุฏ ุบูŠุฑ ู…ุชุงุญุฉ'}{status_txt}"
2141
  if any(w in t for w in ["ุชู‚ูŠูŠู…", "ู†ุฌู…ุฉ", "rating"]):
2142
  return f"โญ {name}: {place.get('rating_norm', place.get('rating', 0)) or 'ุงู„ุชู‚ูŠูŠู… ุบูŠุฑ ู…ุชุงุญ'}"
2143
- if any(w in t for w in ["ุณุนุฑ", "ุจูƒุงู…", "ุบุงู„ูŠ", "ุฑุฎูŠุต"]):
 
 
 
 
 
 
 
 
 
 
 
 
2144
  return f"๐Ÿ’ฐ {name}: {place.get('price_range') or 'ุงู„ุณุนุฑ ุบูŠุฑ ู…ุชุงุญ'}"
2145
  return format_place_line(place)
2146
 
@@ -2322,7 +2451,7 @@ def chat(text: str, session: Session, user_lat=None, user_lon=None) -> dict:
2322
  location_from_text = entities.get("location", "")
2323
  clear_context_on_category_change(session, category, location_from_text, entities.get("place_name", ""))
2324
 
2325
- if (intent in {"best_of", "sort_results"} or has_any(text, BEST_WORDS)) and not category and not session.context_slots.get("category"):
2326
  reply = "ุฃุญุณู† ุฅูŠู‡ ุจุงู„ุธุจุทุŸ ู…ุทุนู… ูˆู„ุง ูƒุงููŠู‡ ูˆู„ุง ุตูŠุฏู„ูŠุฉ ูˆู„ุง ู…ุงุฑูƒุชุŸ ๐Ÿ˜Š"
2327
  session.add(text, reply, "missing_info", entities, None, [])
2328
  return {"reply": reply, "intent": "missing_info", "confidence": confidence, "entities": entities, "best_place": None, "all_results": []}
@@ -2390,9 +2519,23 @@ def chat(text: str, session: Session, user_lat=None, user_lon=None) -> dict:
2390
  reply = "ู…ุด ู„ุงู‚ูŠ ุงู„ุฃู…ุงูƒู† ุงู„ู„ูŠ ุนุงูŠุฒ ุชู‚ุงุฑู† ุจูŠู†ู‡ุง ุญุงู„ูŠู‹ุง ๐Ÿ˜”\nุงูƒุชุจ ุงู„ุงุณู…ูŠู† ุจุดูƒู„ ุฃูˆุถุญ."
2391
  session.add(text, reply, "no_result", entities, None, [])
2392
  return {"reply": reply, "intent": "no_result", "confidence": confidence, "entities": entities, "best_place": None, "all_results": []}
2393
- found = rank_places(pd.DataFrame(found), text, user_lat, user_lon, "best_of").to_dict("records")
 
 
 
 
 
 
 
 
 
2394
  if missing:
2395
- reply = f"ู„ู‚ูŠุช {', '.join([p.get('name','') for p in found])}ุŒ ู„ูƒู† ู…ุด ู„ุงู‚ูŠ {', '.join(missing)} ุญุงู„ูŠู‹ุง ููŠ ู‚ุงุนุฏุฉ ุงู„ุจูŠุงู†ุงุช.\nุฑุชุจุชู„ูƒ ุงู„ู…ูˆุฌูˆุฏ ุญุณุจ ุงู„ุชู‚ูŠูŠู… ๐Ÿ‘‡"
 
 
 
 
 
2396
  else:
2397
  reply = _dynamic_reply("best", category=normalize_category(found[0].get("category", "")), best_place=found[0])
2398
  session.add(text, reply, "compare_places", entities, found[0], found)
@@ -2816,6 +2959,16 @@ def clean_place(place: dict) -> dict:
2816
  "description": _first_value(place, "description", default=""),
2817
  "is_open": is_open,
2818
  "opening_hours": _first_value(place, "opening_hours", "openning_hours", default=""),
 
 
 
 
 
 
 
 
 
 
2819
  "distance_km": distance,
2820
  "image": _first_value(place, "image", "image_url", "main_image_url", "photo", "photo_url", default="") or "",
2821
  "phone": phone_val if has_phone else None,
 
842
  return "show_menu"
843
  if any(w in t for w in NEXT_WORDS):
844
  return "show_more"
845
+ if any(w in t for w in ["ู‚ุงุฑู†", "ู…ู‚ุงุฑู†ุฉ", "ุงุญุณู† ูˆุงุญุฏ ููŠู‡ู…", "ุงูŠู‡ ุงู„ุงุญุณู†", "ู…ูŠู† ุงุญุณู†", "ุงูŠู‡ ุงู„ุงู‚ุฑุจ", "ู…ูŠู† ุงู‚ุฑุจ", "ุงูŠู‡ ุฃู‚ุฑุจ"]):
846
  return "compare_places"
847
  if any(w in t for w in OPEN_WORDS):
848
  cat = infer_category(text)
 
1197
  # must not leave "ุงุฑูˆุญ" behind as a fake item; "ุงู‚ุฑุจ ู…ุทุนู… ุงุชุงูƒู„ ููŠู‡"
1198
  # must not leave "ุงุชุงูƒู„" behind either).
1199
  "ุงุฑูˆุญ", "ู‡ุฑูˆุญ", "ุงุดูˆู", "ู‡ุดูˆู", "ุงุชุงูƒู„", "ุฃุชุงูƒู„", "ุงู‚ุฏุฑ", "ุงู„ุงู‚ูŠ", "ุงู„ุงู‚ูŠู‡",
1200
+ "ุงุฏูˆุฑ", "ู‡ุฏูˆุฑ", "ุงุฏูŠู†ูŠ", "ุงุฏูŠู„ูŠ", "ู‡ุงุช", "ุฌูŠุจู„ูŠ", "ููŠู†",
1201
  # generic "something to eat" filler โ€” "ุญุชุฉ ุงูƒู„"/"ุญุชู‡ ุงูƒู„" means "a
1202
  # bite to eat" in general, not a specific dish; never a real item.
1203
  "ุญุชู‡", "ุญุชุฉ", "ุงูƒู„", "ุฃูƒู„",
 
1294
  q = clean_text(query)
1295
  if not q or not blob:
1296
  return False
1297
+ # "ุงู„" (the) is almost always prepended in speech ("ุงู„ุจุฑุฌุฑ", "ุงู„ูุฑุงุฎ")
1298
+ # but menu/place data rarely stores it ("ุจุฑุฌุฑ", "ูุฑุงุฎ") โ€” strip it from
1299
+ # every word so this harmless mismatch never blocks a real match.
1300
+ q_noal = " ".join(re.sub(r"^ุงู„(?=.{2,})", "", w) for w in q.split())
1301
+ variants = [q, q_noal, ar_to_latin_guess(q), _compact_key(q), _phonetic_key(q), _translated_query_terms(q)]
1302
  variants = [clean_text(v) for v in variants if clean_text(v) and len(_compact_key(v)) >= 2]
1303
  for v in variants:
1304
  if v and (v in blob or _compact_key(v) in _compact_key(blob)):
 
1728
 
1729
  def compare_place_names(text: str) -> list:
1730
  t = clean_text(text)
1731
+ for w in ["ู‚ุงุฑู†", "ู‚ุงุฑู†ูŠ", "ู…ู‚ุงุฑู†ุฉ", "ุจูŠู†", "ู…ุง ุจูŠู†", "ู…ูŠู† ุงุญุณู†", "ุงุญุณู†", "ุงูŠู‡ ุงู„ุงู‚ุฑุจ", "ุงูŠู‡ ุฃู‚ุฑุจ", "ู…ูŠู† ุงู‚ุฑุจ", "ุงู„ุงู‚ุฑุจ", "ุฃู‚ุฑุจ", "ุงู‚ุฑุจ", "ุงูŠู‡", "ุฅูŠู‡"]:
1732
  t = re.sub(rf"\b{re.escape(clean_text(w))}\b", " ", t)
1733
  parts = [p.strip() for p in re.split(r"\s+ูˆ\s+|,|ุŒ|/|\\|\s+ูˆู„ุง\s+", t) if p.strip()]
1734
  cleaned = []
 
2123
  header += "\n\nููŠ ุฃู…ุงูƒู† ุชุงู†ูŠุฉ ูƒู…ุงู†ุŒ ุงูƒุชุจูŠ ยซูƒู…ุงู†ยป ู„ูˆ ุนุงูŠุฒุงู‡ู…."
2124
  return header
2125
 
2126
+ PRICE_QUESTION_FILLER_WORDS = [
2127
+ "ุณุนุฑ", "ุจูƒุงู…", "ูƒุงู…", "ุชู…ู†", "ุชู…ู†ู‡", "ุชู…ู†ู‡ุง", "ู‚ุฏ ุงูŠู‡", "ู‚ุฏุงูŠ", "ุงูŠู‡", "ุฅูŠู‡", "ูŠุนู†ูŠ ูƒุงู…", "ุจ ูƒุงู…",
2128
+ ]
2129
+
2130
+
2131
+ def _extract_price_item_query(text: str) -> str:
2132
+ """Strip price-question filler words ('ุณุนุฑ'/'ุจูƒุงู…'/'ูƒุงู…'/'ุงูŠู‡'...) from a
2133
+ price question, leaving only the item name if one was mentioned.
2134
+ 'ุณุนุฑ ุงู„ุจุฑุฌุฑ ุงูŠู‡' -> 'ุงู„ุจุฑุฌุฑ', 'ุงู„ุจุฑุฌุฑ ุจูƒุงู…' -> 'ุงู„ุจุฑุฌุฑ',
2135
+ 'ูƒุงู… ุณุนุฑ ุงู„ุจูŠุชุฒุง' -> 'ุงู„ุจูŠุชุฒุง', bare 'ุจูƒุงู…ุŸ' -> '' (no item -> place-level
2136
+ price_range question, handled separately by the caller).
2137
+ """
2138
+ t = clean_text(text)
2139
+ for w in PRICE_QUESTION_FILLER_WORDS:
2140
+ cw = clean_text(w)
2141
+ if cw:
2142
+ t = re.sub(rf"\b{re.escape(cw)}\b", " ", t)
2143
+ t = re.sub(r"\s+", " ", t).strip()
2144
+ # Egyptian Arabic almost always prepends the definite article "ุงู„" when
2145
+ # asking about a specific item in speech ("ุงู„ุจุฑุฌุฑ ุจูƒุงู…ุŸ"), but the menu
2146
+ # API rarely stores items with it ("ุจุฑุฌุฑ"). Strip it so matching isn't
2147
+ # blocked by this near-universal, harmless prefix mismatch.
2148
+ t = re.sub(r"^ุงู„(?=.{2,})", "", t)
2149
+ return t
2150
+
2151
+
2152
+ _PRICE_FIELD_KEYS = ["price", "Price", "PRICE", "price_egp", "cost", "amount", "unit_price", "sale_price", "base_price", "value"]
2153
+ _NAME_FIELD_KEYS = ["name", "name_ar", "name_en", "title", "title_ar", "title_en", "item_name", "product_name", "label"]
2154
+
2155
+
2156
+ _SIZE_CONTAINER_KEYS = ["sizes", "variants", "options", "choices", "sizes_list", "variant_list"]
2157
+
2158
+
2159
+ def _collect_variant_prices(d: dict) -> list:
2160
+ """If an item has no direct price but has nested size/variant entries
2161
+ (each its own {label, price}), collect those prices instead of giving up."""
2162
+ prices = []
2163
+ for key in _SIZE_CONTAINER_KEYS:
2164
+ variants = d.get(key)
2165
+ if isinstance(variants, list):
2166
+ for v in variants:
2167
+ if isinstance(v, dict):
2168
+ p = None
2169
+ for pk in _PRICE_FIELD_KEYS:
2170
+ if v.get(pk) not in (None, ""):
2171
+ p = v.get(pk)
2172
+ break
2173
+ if p not in (None, ""):
2174
+ prices.append(p)
2175
+ return prices
2176
+
2177
+
2178
+ def find_menu_item_price(menu_obj: Any, item_query: str):
2179
+ """Walk the menu JSON (arbitrary shape โ€” categories/items/sizes/variants,
2180
+ no fixed schema assumed, fully dynamic like the rest of the item search
2181
+ in this file) looking for the item that matches `item_query`, and return
2182
+ (display_name, price) if found. If the item is found but genuinely has
2183
+ no usable price field anywhere on it, returns (display_name, None) so
2184
+ the caller can honestly say "price not available" only in that specific
2185
+ case โ€” never when the item simply wasn't found at all.
2186
+ Returns (None, None) if no matching item exists in the menu.
2187
+ """
2188
+ item = clean_text(item_query)
2189
+ if not item:
2190
+ return None, None
2191
+ best = None # (score, display_name, price)
2192
+
2193
+ def get_first(d: dict, keys):
2194
+ for k in keys:
2195
+ v = d.get(k)
2196
+ if v not in (None, ""):
2197
+ return v
2198
+ return None
2199
+
2200
+ def own_text(d: dict) -> str:
2201
+ vals = []
2202
+ for v in d.values():
2203
+ if isinstance(v, (str, int, float, bool)) and str(v).strip():
2204
+ vals.append(str(v))
2205
+ return clean_text(" ".join(vals))
2206
+
2207
+ def walk(obj):
2208
+ nonlocal best
2209
+ if isinstance(obj, dict):
2210
+ name_val = get_first(obj, _NAME_FIELD_KEYS)
2211
+ price_val = get_first(obj, _PRICE_FIELD_KEYS)
2212
+ if name_val is not None or price_val is not None:
2213
+ text = own_text(obj)
2214
+ if _query_matches_blob(item, text):
2215
+ score = 1.0 if name_val and clean_text(str(name_val)) == item else 0.8
2216
+ if price_val in (None, ""):
2217
+ variant_prices = _collect_variant_prices(obj)
2218
+ if variant_prices:
2219
+ nums = sorted(set(variant_prices))
2220
+ price_val = f"{nums[0]}" if len(nums) == 1 else f"{nums[0]}โ€“{nums[-1]}"
2221
+ if best is None or score > best[0]:
2222
+ display_name = str(name_val) if name_val else item_query
2223
+ best = (score, display_name, price_val)
2224
+ # Always recurse โ€” sizes/variants/sub-items may carry their own
2225
+ # name+price even nested inside a matched or non-matched item.
2226
+ for v in obj.values():
2227
+ if isinstance(v, (dict, list, tuple, set)):
2228
+ walk(v)
2229
+ elif isinstance(obj, (list, tuple, set)):
2230
+ for x in obj:
2231
+ walk(x)
2232
+
2233
+ walk(menu_obj)
2234
+ if best is None:
2235
+ return None, None
2236
+ return best[1], best[2]
2237
+
2238
+
2239
  def handle_detail(text: str, place: Optional[dict]) -> str:
2240
  if not place:
2241
  return "ุชู‚ุตุฏ ุฃู†ู‡ูŠ ู…ูƒุงู†ุŸ ุงูƒุชุจ ุงุณู… ุงู„ู…ูƒุงู† ุฃูˆ ุงุณุฃู„ู†ูŠ ุงู„ุฃูˆู„ ุนู† ู…ุทุนู…/ุตูŠุฏู„ูŠุฉ/ูƒุงููŠู‡. ๐Ÿ˜Š"
 
2257
  return f"๐Ÿ• {name}: {hours or 'ุงู„ู…ูˆุงุนูŠุฏ ุบูŠุฑ ู…ุชุงุญุฉ'}{status_txt}"
2258
  if any(w in t for w in ["ุชู‚ูŠูŠู…", "ู†ุฌู…ุฉ", "rating"]):
2259
  return f"โญ {name}: {place.get('rating_norm', place.get('rating', 0)) or 'ุงู„ุชู‚ูŠูŠู… ุบูŠุฑ ู…ุชุงุญ'}"
2260
+ if any(w in t for w in ["ุณุนุฑ", "ุจูƒุงู…", "ุบุงู„ูŠ", "ุฑุฎูŠุต", "ุชู…ู†", "ุชู…ู†ู‡", "ุชู…ู†ู‡ุง"]):
2261
+ item_candidate = _extract_price_item_query(text)
2262
+ if item_candidate and not is_generic_category_item(item_candidate):
2263
+ try:
2264
+ menu = fetch_menu(place)
2265
+ except Exception:
2266
+ menu = []
2267
+ item_name, price = find_menu_item_price(menu, item_candidate)
2268
+ if item_name is not None:
2269
+ if price not in (None, ""):
2270
+ return f"๐Ÿ’ฐ ุณุนุฑ {item_name} ููŠ {name} ู‡ูˆ {price} ๐Ÿ˜‹"
2271
+ return f"๐Ÿ˜” {item_name} ู…ูˆุฌูˆุฏ ููŠ ู…ู†ูŠูˆ {name} ุจุณ ุงู„ุณุนุฑ ุบูŠุฑ ู…ุชุงุญ ุญุงู„ูŠู‹ุง."
2272
+ return f"๐Ÿ˜” ู…ุด ู„ุงู‚ูŠ \"{item_candidate}\" ููŠ ู…ู†ูŠูˆ {name}. ุฌุฑุจ ุชุชุฃูƒุฏ ู…ู† ุงู„ุงุณู… ุฃูˆ ุงุณุฃู„ ุนู† ุตู†ู ุชุงู†ูŠ."
2273
  return f"๐Ÿ’ฐ {name}: {place.get('price_range') or 'ุงู„ุณุนุฑ ุบูŠุฑ ู…ุชุงุญ'}"
2274
  return format_place_line(place)
2275
 
 
2451
  location_from_text = entities.get("location", "")
2452
  clear_context_on_category_change(session, category, location_from_text, entities.get("place_name", ""))
2453
 
2454
+ if intent != "compare_places" and (intent in {"best_of", "sort_results"} or has_any(text, BEST_WORDS)) and not category and not session.context_slots.get("category"):
2455
  reply = "ุฃุญุณู† ุฅูŠู‡ ุจุงู„ุธุจุทุŸ ู…ุทุนู… ูˆู„ุง ูƒุงููŠู‡ ูˆู„ุง ุตูŠุฏู„ูŠุฉ ูˆู„ุง ู…ุงุฑูƒุชุŸ ๐Ÿ˜Š"
2456
  session.add(text, reply, "missing_info", entities, None, [])
2457
  return {"reply": reply, "intent": "missing_info", "confidence": confidence, "entities": entities, "best_place": None, "all_results": []}
 
2519
  reply = "ู…ุด ู„ุงู‚ูŠ ุงู„ุฃู…ุงูƒู† ุงู„ู„ูŠ ุนุงูŠุฒ ุชู‚ุงุฑู† ุจูŠู†ู‡ุง ุญุงู„ูŠู‹ุง ๐Ÿ˜”\nุงูƒุชุจ ุงู„ุงุณู…ูŠู† ุจุดูƒู„ ุฃูˆุถุญ."
2520
  session.add(text, reply, "no_result", entities, None, [])
2521
  return {"reply": reply, "intent": "no_result", "confidence": confidence, "entities": entities, "best_place": None, "all_results": []}
2522
+ # Rank by whatever the user actually asked for in THIS query โ€” nearest,
2523
+ # best, or both โ€” exactly like normal search. Previously this always
2524
+ # forced intent="best_of", which made is_best_query() return True
2525
+ # unconditionally and silently overrode an explicit "ุงู„ุฃู‚ุฑุจ" (nearest)
2526
+ # request with rating-based sorting (e.g. "ุงูŠู‡ ุงู„ุฃู‚ุฑุจ ุณูŠุฏูŠ ุนุจุฏ ุงู„ุฑุญู…ู†
2527
+ # ูˆู„ุง sanhok" would wrongly rank the higher-rated-but-farther place
2528
+ # first). Passing "" here means the ranking is decided purely from
2529
+ # ุงู„ู†ุต ู†ูุณู‡ (is_near_query/is_best_query), consistent with every
2530
+ # other search path.
2531
+ found = rank_places(pd.DataFrame(found), text, user_lat, user_lon, "").to_dict("records")
2532
  if missing:
2533
+ reply = f"ู„ู‚ูŠุช {', '.join([p.get('name','') for p in found])}ุŒ ู„ูƒู† ู…ุด ู„ุงู‚ูŠ {', '.join(missing)} ุญุงู„ูŠู‹ุง ููŠ ู‚ุงุนุฏุฉ ุงู„ุจูŠุงู†ุงุช.\nุฑุชุจุชู„ูƒ ุงู„ู…ูˆุฌูˆุฏ ุญุณุจ "
2534
+ reply += "ุงู„ู…ุณุงูุฉ ๐Ÿ‘‡" if is_near_query(text) and not is_best_query(text) else "ุงู„ุชู‚ูŠูŠู… ๐Ÿ‘‡"
2535
+ elif is_near_query(text) and not is_best_query(text):
2536
+ best_p = found[0]
2537
+ dist_txt = f"{best_p.get('distance_km'):.1f} ูƒู…" if best_p.get("distance_km") not in (None, "") else "ู…ุด ู…ุนุฑูˆูุฉ ุงู„ู…ุณุงูุฉ ุจุงู„ุธุจุท"
2538
+ reply = f"ุงู„ุฃู‚ุฑุจ ู‡ูˆ {best_p.get('name','')} ({dist_txt}) ๐Ÿ“"
2539
  else:
2540
  reply = _dynamic_reply("best", category=normalize_category(found[0].get("category", "")), best_place=found[0])
2541
  session.add(text, reply, "compare_places", entities, found[0], found)
 
2959
  "description": _first_value(place, "description", default=""),
2960
  "is_open": is_open,
2961
  "opening_hours": _first_value(place, "opening_hours", "openning_hours", default=""),
2962
+ # NOTE for the Flutter side: `distance_km` here is the CHATBOT
2963
+ # BACKEND's own Haversine calculation, used ONLY to rank/order these
2964
+ # results. It is not guaranteed to be pixel-identical to the distance
2965
+ # already computed elsewhere in the app (rounding, GPS timing, or a
2966
+ # slightly different formula can cause small differences). For
2967
+ # display, match this card to the app's existing place data by
2968
+ # `id`/`place_id` and show the app's own distance value instead โ€”
2969
+ # use `lat`/`lon` here only as a fallback if no app-side match/GPS
2970
+ # distance is available. This field must not be removed or renamed;
2971
+ # it's still required for the initial backend ranking.
2972
  "distance_km": distance,
2973
  "image": _first_value(place, "image", "image_url", "main_image_url", "photo", "photo_url", default="") or "",
2974
  "phone": phone_val if has_phone else None,