Youmnaaaa commited on
Commit
bd2f9a6
ยท
verified ยท
1 Parent(s): c1685ed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +310 -0
app.py CHANGED
@@ -3041,3 +3041,313 @@ def reset_session(session_id: str):
3041
  log.info(f"๐Ÿ—‘๏ธ Session {session_id} reset")
3042
  return {"status": "reset", "session_id": session_id}
3043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3041
  log.info(f"๐Ÿ—‘๏ธ Session {session_id} reset")
3042
  return {"status": "reset", "session_id": session_id}
3043
 
3044
+ # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
3045
+ # FINAL SAFETY PATCH โ€” robust routing/context/detail/item fixes
3046
+ # This block intentionally re-defines helpers used at runtime by FastAPI.
3047
+ # โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
3048
+
3049
+ # Strong category words with explicit precedence. This prevents old context or
3050
+ # semantic similarity from turning "ุฃู‚ุฑุจ ุณูˆุจุฑู…ุงุฑูƒุช" into pharmacy, etc.
3051
+ _CATEGORY_PATTERNS_FINAL = {
3052
+ "pharmacy": [
3053
+ "ุตูŠุฏู„ูŠู‡", "ุตูŠุฏู„ูŠุฉ", "ุตูŠุฏู„ูŠุงุช", "ุตูŠุฏู„ู‡", "ุฏูˆุง", "ุฏูˆุงุก", "ุงุฏูˆูŠู‡", "ุงุฏูˆูŠุฉ", "ุนู„ุงุฌ", "pharmacy", "drugstore"
3054
+ ],
3055
+ "supermarket": [
3056
+ "ุณูˆุจุฑู…ุงุฑูƒุช", "ุณูˆุจุฑ ู…ุงุฑูƒุช", "ุณูˆุจุฑ", "ู…ุงุฑูƒุช", "ู‡ุงูŠุจุฑ", "ุจู‚ุงู„ู‡", "ุจู‚ุงู„ุฉ", "supermarket", "market", "hypermarket", "grocery"
3057
+ ],
3058
+ "cafe": [
3059
+ "ูƒุงููŠู‡", "ูƒุงููŠุฉ", "ูƒุงููŠู‡ุงุช", "ูƒูˆููŠ", "ู‚ู‡ูˆุฉ", "ู‚ู‡ูˆู‡", "ูƒุงููŠุชูŠุฑูŠุง", "cafe", "coffee", "coffee shop"
3060
+ ],
3061
+ "housing": [
3062
+ "ุณูƒู†", "ุดู‚ู‡", "ุดู‚ุฉ", "ุดู‚ู‚", "ุงูŠุฌุงุฑ", "ุฅูŠุฌุงุฑ", "ูู†ุฏู‚", "ู‡ูˆุณุชู„", "hotel", "apartment", "housing", "rent"
3063
+ ],
3064
+ "restaurant": [
3065
+ "ู…ุทุนู…", "ู…ุทุงุนู…", "ุงูƒู„", "ุฃูƒู„", "ุทุนุงู…", "ูˆุฌุจู‡", "ูˆุฌุจุฉ", "restaurant", "food", "eat"
3066
+ ],
3067
+ }
3068
+
3069
+ _FOOD_ITEM_HINTS_FINAL = {
3070
+ "ุดุงูˆุฑู…ุง", "shawarma", "ุจุฑุฌุฑ", "burger", "ุจูŠุชุฒุง", "pizza", "ูƒุฑูŠุจ", "crepe",
3071
+ "ุจุฑูˆุณุช", "broast", "ูุฑุงุฎ", "ูุฑุฎู‡", "ูุฑุฎุฉ", "ุฏุฌุงุฌ", "chicken", "ูƒุจุงุจ", "ูƒูุชู‡", "ูƒูุชุฉ",
3072
+ "ู…ุดูˆูŠุงุช", "ู…ุดูˆูŠ", "ุณู…ูƒ", "fish", "ุฌู…ุจุฑูŠ", "ูƒุดุฑูŠ", "ู…ูƒุฑูˆู†ู‡", "ู…ูƒุฑูˆู†ุฉ", "pasta",
3073
+ "ุณุงู†ุฏูˆุชุด", "ุณู†ุฏูˆุชุด", "sandwich", "ุญูˆุงูˆุดูŠ", "ููˆู„", "ุทุนู…ูŠุฉ", "ุทุนู…ูŠู‡", "ูุชุฉ", "ูุชู‡",
3074
+ "ุฑุฒ", "ู…ู†ุฏูŠ", "ุณูˆุดูŠ", "sushi", "ุฑุงู†ุด", "ranch", "ู„ุงุฒุงู†ูŠุง"
3075
+ }
3076
+ _DRINK_ITEM_HINTS_FINAL = {
3077
+ "ุงุณู…ูˆุฒูŠ", "ุฅุณู…ูˆุฒูŠ", "ุณู…ูˆุฒูŠ", "ุณู…ูˆุฐูŠ", "smoothie", "ู…ูˆู‡ูŠุชูˆ", "mojito", "ุนุตูŠุฑ", "juice",
3078
+ "ุดุงูŠ", "tea", "ู‚ู‡ูˆุฉ", "ู‚ู‡ูˆู‡", "coffee", "ู„ุงุชูŠู‡", "latte", "ุงุณุจุฑูŠุณูˆ", "espresso", "ู†ุณูƒุงููŠู‡",
3079
+ "ู…ุดุฑูˆุจ", "ู…ุดุฑูˆุจุงุช", "milkshake", "ู…ูŠู„ูƒ", "ูƒูˆูƒุชูŠู„", "cocktail", "ูุฑุงุจุชุดูŠู†ูˆ"
3080
+ }
3081
+ _RANKING_ONLY_FINAL = {
3082
+ "ุงุญุณู†", "ุฃุญุณู†", "ุงูุถู„", "ุฃูุถู„", "ุงู„ุงุญุณู†", "ุงู„ุฃุญุณู†", "ุงู„ุงูุถู„", "ุงู„ุฃูุถู„",
3083
+ "ูƒูˆูŠุณ", "ูƒูˆูŠุณุฉ", "ุญู„ูˆ", "ุญู„ูˆู‡", "ุญู„ูˆุฉ", "ุฑุดุญู„ูŠ", "recommended", "recommend", "best",
3084
+ "ุงู‚ุฑุจ", "ุฃู‚ุฑุจ", "ู‚ุฑูŠุจ", "ู‚ุฑูŠุจุฉ", "nearest", "nearby", "near"
3085
+ }
3086
+ _DEMAND_FINAL = {"ุนุงูŠุฒ", "ุนุงูŠุฒุฉ", "ุนุงูŠุฒู‡", "ุนุงูˆุฒ", "ุนุงูˆุฒู‡", "ู…ุญุชุงุฌ", "ู…ุญุชุงุฌุฉ", "ู†ูุณูŠ", "ุจุฏูˆุฑ", "ููŠู†", "ู‡ุงุช", "ู‡ุงุชูŠ"}
3087
+ _ITEM_CONTEXT_FINAL = {
3088
+ "ุนู†ุฏู‡", "ุนู†ุฏู‡ุง", "ุนู†ุฏู‡ู…", "ููŠู‡", "ููŠู‡ุง", "ุจูŠุนู…ู„", "ุจุชุนู…ู„", "ุจูŠู‚ุฏู…", "ุจุชู‚ุฏู…",
3089
+ "serves", "serve", "has", "with"
3090
+ }
3091
+
3092
+
3093
+ def infer_category(query): # noqa: F811
3094
+ q = norm(query)
3095
+ # longer phrases first inside each category
3096
+ for cat, words in _CATEGORY_PATTERNS_FINAL.items():
3097
+ for w in sorted(words, key=len, reverse=True):
3098
+ wn = norm(w)
3099
+ if (" " in wn and wn in q) or (wn in q.split()) or (wn and wn in q):
3100
+ return cat
3101
+ return None
3102
+
3103
+
3104
+ def _final_clean_tokens(text: str) -> list[str]:
3105
+ return [clean_text(w) for w in str(text).split() if clean_text(w)]
3106
+
3107
+
3108
+ def _final_has_any(text: str, words: set | list) -> bool:
3109
+ t = norm(text)
3110
+ toks = set(t.split())
3111
+ for w in words:
3112
+ wn = norm(w)
3113
+ if not wn:
3114
+ continue
3115
+ if (" " in wn and wn in t) or wn in toks or wn in t:
3116
+ return True
3117
+ return False
3118
+
3119
+
3120
+ def _final_item_candidate(text: str) -> str:
3121
+ """Extract item-like remainder after removing demand/category/ranking/context words."""
3122
+ candidate = extract_item_query(text) or ""
3123
+ c = clean_text(candidate)
3124
+ if not c:
3125
+ # fallback from raw text
3126
+ strip_words = set()
3127
+ for arr in _CATEGORY_PATTERNS_FINAL.values():
3128
+ strip_words.update(clean_text(x) for x in arr)
3129
+ strip_words |= {clean_text(x) for x in _RANKING_ONLY_FINAL | _DEMAND_FINAL | _ITEM_CONTEXT_FINAL}
3130
+ strip_words |= {"ู…ูƒุงู†", "ุงู…ุงูƒู†", "ู…ุญู„", "ููŠู†", "ููŠ", "ู…ู†", "ุนู„ู‰", "ุนู†", "ู„ูŠ", "ู„ูŠุง"}
3131
+ toks = [w for w in _final_clean_tokens(text) if w not in strip_words and len(w) > 1]
3132
+ c = " ".join(toks)
3133
+ # remove remaining pure category/ranking words
3134
+ bad = set()
3135
+ for arr in _CATEGORY_PATTERNS_FINAL.values():
3136
+ bad.update(clean_text(x) for x in arr)
3137
+ bad |= {clean_text(x) for x in _RANKING_ONLY_FINAL | _DEMAND_FINAL | _ITEM_CONTEXT_FINAL}
3138
+ bad |= {"ู…ูƒุงู†", "ุงู…ุงูƒู†", "ู…ุญู„", "ููŠ", "ู…ู†", "ุนู„ู‰", "ุนู†", "ู„ูŠ", "ู„ูŠุง"}
3139
+ toks = [w for w in c.split() if w not in bad and len(w) > 1]
3140
+ return " ".join(toks).strip()
3141
+
3142
+
3143
+ def _looks_like_item_search(text: str) -> bool: # noqa: F811
3144
+ t = norm(text)
3145
+ cat = infer_category(text)
3146
+ cand = _final_item_candidate(text)
3147
+ cand_tokens = set(cand.split())
3148
+
3149
+ # Show-menu queries remain show_menu unless they say "place has item".
3150
+ if _final_has_any(text, _MENU_TRIGGERS) and not _final_has_any(text, _ITEM_CONTEXT_FINAL):
3151
+ return False
3152
+
3153
+ # Pure category/ranking requests are not item searches.
3154
+ if not cand:
3155
+ return False
3156
+
3157
+ # Pharmacy/supermarket/housing are not menu-item searches.
3158
+ if cat in ("pharmacy", "supermarket", "housing"):
3159
+ return False
3160
+
3161
+ # Direct known food/drink item.
3162
+ if _final_has_any(cand, _FOOD_ITEM_HINTS_FINAL | _DRINK_ITEM_HINTS_FINAL):
3163
+ return True
3164
+
3165
+ # "ู…ุทุนู…/ูƒุงููŠู‡ + item" should be item search when there is a meaningful leftover.
3166
+ # Examples: ุนุงูŠุฒุฉ ู…ุทุนู… ุดุงูˆุฑู…ุงุŒ ู…ุทุนู… ููŠู‡ ุจุฑุฌุฑุŒ ูƒุงููŠู‡ ุนู†ุฏู‡ ู…ูˆู‡ูŠุชูˆ.
3167
+ if cat in ("restaurant", "cafe") and cand and not all(w in {clean_text(x) for x in _RANKING_ONLY_FINAL} for w in cand_tokens):
3168
+ if _final_has_any(text, _DEMAND_FINAL) or _final_has_any(text, _ITEM_CONTEXT_FINAL):
3169
+ return True
3170
+
3171
+ # Unknown item with item context: "ู…ูƒุงู† ุนู†ุฏู‡ ูุชุฉ" / "ููŠู‡ ู…ูˆู‡ูŠุชูˆ".
3172
+ if _final_has_any(text, _ITEM_CONTEXT_FINAL) and cand:
3173
+ return True
3174
+
3175
+ # Unknown item without category: "ุนุงูŠุฒุฉ ูุชุฉ" / "ุนุงูŠุฒ ู…ูˆู‡ูŠุชูˆ".
3176
+ if cat is None and _final_has_any(text, _DEMAND_FINAL) and len(cand) >= 3:
3177
+ return True
3178
+
3179
+ return False
3180
+
3181
+
3182
+ def _detect_new_intent(text: str) -> str | None: # noqa: F811
3183
+ if _looks_like_item_search(text):
3184
+ return "search_by_item"
3185
+ if _final_has_any(text, _MENU_TRIGGERS):
3186
+ return "show_menu"
3187
+ return None
3188
+
3189
+
3190
+ def _detect_compare_intent(text: str) -> str | None: # noqa: F811
3191
+ """Compare only when the user refers to previous results, not when asking a new category."""
3192
+ t = norm(text)
3193
+ # If the message contains an explicit category, this is a new search, not compare previous cards.
3194
+ if infer_category(text):
3195
+ return None
3196
+ ref_words = ["ููŠู‡ู…", "ู…ู†ู‡ู…", "ุฏูˆู„", "ุงู„ู„ูŠ ููˆู‚", "ุงู„ู†ุชุงูŠุฌ", "ูˆุงุญุฏ ููŠู‡ู…", "ุงู†ู‡ูŠ ูˆุงุญุฏ", "ุงูŠู‡ ุงู„ุงุญุณู†"]
3197
+ if not any(norm(w) in t for w in ref_words):
3198
+ return None
3199
+ has_best = any(w in t for w in ["ุงุญุณู†", "ุงูุถู„", "ุงู„ุฃุญุณู†", "ุงู„ุฃูุถู„", "ุงุฎุชุงุฑ", "ุฑุดุญ"])
3200
+ has_near = any(w in t for w in ["ุงู‚ุฑุจ", "ุฃู‚ุฑุจ", "ุงู„ุงู‚ุฑุจ", "ุงู„ุฃู‚ุฑุจ"])
3201
+ if has_best and has_near:
3202
+ return "both"
3203
+ if has_best:
3204
+ return "best"
3205
+ if has_near:
3206
+ return "nearest"
3207
+ return None
3208
+
3209
+
3210
+ def detect_ref_type(text): # noqa: F811
3211
+ t = norm(text)
3212
+ tw = set(t.split())
3213
+ phone_words = ["ุฑู‚ู…", "ุฑู‚ู…ู‡", "ุฑู‚ู…ู‡ุง", "ู†ู…ุฑู‡", "ู†ู…ุฑุชู‡", "ุชู„ูŠููˆู†", "ุชู„ูŠููˆู†ู‡", "ุชู„ูŠููˆู†ู‡ุง", "ู…ูˆุจุงูŠู„", "ููˆู†", "call", "phone"]
3214
+ addr_words = ["ููŠู†", "ููŠู†ู‡", "ููŠู†ู‡ุง", "ุนู†ูˆุงู†", "ุนู†ูˆุงู†ู‡", "ุนู†ูˆุงู†ู‡ุง", "ู…ูƒุงู†ู‡", "ู…ูƒุงู†ู‡ุง", "ู„ูˆูƒูŠุดู†", "ู…ูˆู‚ุน", "map", "ุงุฑูˆุญ"]
3215
+ hour_words = ["ุจูŠูุชุญ", "ุจุชูุชุญ", "ู…ูˆุงุนูŠุฏ", "ู…ูˆุงุนูŠุฏู‡", "ู…ูˆุงุนูŠุฏู‡ุง", "ุงู…ุชู‰", "ุงู…ุชูŠ", "ุจูŠู‚ูู„", "ุจุชู‚ูู„", "ูุงุชุญ", "ุดุบุงู„"]
3216
+ rating_words = ["ุชู‚ูŠูŠู…", "ุชู‚ูŠูŠู…ู‡", "ุชู‚ูŠูŠู…ู‡ุง", "ู†ุฌู…ู‡", "ู†ุฌู…ุฉ", "rating"]
3217
+ price_words = ["ุณุนุฑ", "ุณุนุฑู‡", "ุณุนุฑู‡ุง", "ุจูƒุงู…", "ุบุงู„ูŠ", "ุฑุฎูŠุต", "price"]
3218
+ if _final_has_any(text, phone_words + addr_words + hour_words + rating_words + price_words):
3219
+ return "detail"
3220
+ if any(norm(w) in t for w in NEXT_WORDS):
3221
+ return "next"
3222
+ for w in REF_WORDS:
3223
+ wn = norm(w)
3224
+ if (" " in wn and wn in t) or (wn in tw):
3225
+ return "reference"
3226
+ return "new"
3227
+
3228
+
3229
+ def handle_detail(text, place): # noqa: F811
3230
+ """Detail handler with phone priority over address for 'ููŠู† ุฑู‚ู… ุชู„ูŠููˆู†ู‡'."""
3231
+ if not place:
3232
+ return "ุชู‚ุตุฏ ุฃู†ู‡ูŠ ู…ูƒุงู†ุŸ ุงูƒุชุจ ุงุณู… ุงู„ู…ูƒุงู† ุฃูˆ ุงุณุฃู„ู†ูŠ ุงู„ุฃูˆู„ ุนู† ู…ุทุนู…/ุตูŠุฏู„ูŠุฉ/ูƒุงููŠู‡. ๐Ÿ˜Š"
3233
+ t = norm(text)
3234
+ name = place.get("name", "ุงู„ู…ูƒุงู†")
3235
+
3236
+ # Phone FIRST because phrases like "ููŠู† ุฑู‚ู… ุชู„ูŠููˆู†ู‡" contain "ููŠู†" too.
3237
+ if _final_has_any(text, ["ุฑู‚ู…", "ุฑู‚ู…ู‡", "ุฑู‚ู…ู‡ุง", "ู†ู…ุฑู‡", "ู†ู…ุฑุชู‡", "ุชู„ูŠููˆู†", "ุชู„ูŠููˆู†ู‡", "ุชู„ูŠููˆู†ู‡ุง", "ู…ูˆุจุงูŠู„", "ููˆู†", "phone", "call"]):
3238
+ ph = place.get("phone", "")
3239
+ if isinstance(ph, (list, tuple)):
3240
+ ph = ", ".join(str(x) for x in ph if x)
3241
+ elif isinstance(ph, str) and ph.startswith("[") and ph.endswith("]"):
3242
+ ph = ph.strip("[]").replace("'", "").replace('"', "").strip()
3243
+ return f"๐Ÿ“ž ุฑู‚ู… *{name}*: {ph}" if ph and str(ph).strip() not in ("", "nan", "none", "[]") else f"ู…ุด ุนู†ุฏู†ุง ุฑู‚ู… *{name}* ุญุงู„ูŠู‹ุง."
3244
+
3245
+ if _final_has_any(text, ["ุจูŠูุชุญ", "ุจุชูุชุญ", "ู…ูˆุงุนูŠุฏ", "ู…ูˆุงุนูŠุฏู‡", "ู…ูˆุงุนูŠุฏู‡ุง", "ุงู…ุชู‰", "ุงู…ุชูŠ", "ุจูŠู‚ูู„", "ุจุชู‚ูู„", "ูุงุชุญ", "ุดุบุงู„"]):
3246
+ h = place.get("opening_hours", "")
3247
+ if not h or str(h).strip().lower() in ("", "nan", "none"):
3248
+ return f"ุงู„ู…ูˆุงุนูŠุฏ ุบูŠุฑ ู…ุชุงุญุฉ ู„ู€ *{name}* ุญุงู„ูŠู‹ุง."
3249
+ on = check_open_now(h)
3250
+ status = "๐ŸŸข ู…ูุชูˆุญ ุฏู„ูˆู‚ุชูŠ." if on == 1 else ("๐Ÿ”ด ู…ุบู„ู‚ ุฏู„ูˆู‚ุชูŠ." if on == 0 else "")
3251
+ return f"{fmt_hours(h)}{status}"
3252
+
3253
+ if _final_has_any(text, ["ุนู†ูˆุงู†", "ุนู†ูˆุงู†ู‡", "ุนู†ูˆุงู†ู‡ุง", "ููŠู†", "ููŠู†ู‡", "ููŠู†ู‡ุง", "ู…ูƒุงู†ู‡", "ู…ูƒุงู†ู‡ุง", "ู„ูˆูƒูŠุดู†", "ุงู„ู…ูˆู‚ุน", "ู…ูˆู‚ุน", "ุงุฒุงูŠ ุงุฑูˆุญ", "map", "ุงู„ุฎุฑูŠุทุฉ"]):
3254
+ addr = place.get("address", "")
3255
+ loc = place.get("location", "")
3256
+ lat = place.get("lat") or place.get("latitude")
3257
+ lon = place.get("lon") or place.get("lng") or place.get("longitude")
3258
+ parts = []
3259
+ if addr and str(addr).strip().lower() not in ("", "nan", "none"):
3260
+ parts.append(f"๐Ÿ“ {addr}")
3261
+ elif loc and str(loc).strip().lower() not in ("", "nan", "none"):
3262
+ parts.append(f"๐Ÿ“ {loc}")
3263
+ else:
3264
+ parts.append("ุงู„ุนู†ูˆุงู† ุบูŠุฑ ู…ุชุงุญ.")
3265
+ if lat and lon:
3266
+ parts.append(f"๐Ÿ—บ๏ธ https://maps.google.com/?q={lat},{lon}")
3267
+ return "\n".join(parts)
3268
+
3269
+ if _final_has_any(text, ["ุชู‚ูŠูŠู…", "ุชู‚ูŠูŠู…ู‡", "ุชู‚ูŠูŠู…ู‡ุง", "rating", "ู†ุฌูˆู…", "ู†ุฌู…ู‡", "ู†ุฌู…ุฉ"]):
3270
+ r = fmt_rating(place.get("rating_norm", place.get("rating", 0)))
3271
+ return r or "ู…ุด ุนู†ุฏู†ุง ุชู‚ูŠูŠู… ู„ู‡ุฐุง ุงู„ู…ูƒุงู†."
3272
+
3273
+ if _final_has_any(text, ["ุณุนุฑ", "ุณุนุฑู‡", "ุณุนุฑู‡ุง", "ุจูƒุงู…", "ุบุงู„ูŠ", "ุฑุฎูŠุต", "price"]):
3274
+ pr = place.get("price_range", "")
3275
+ return f"๐Ÿ’ฐ {name}: {pr}" if pr and str(pr).strip().lower() not in ("", "nan", "none") else "ู…ุด ุนู†ุฏู†ุง ู…ุนู„ูˆู…ุงุช ุนู† ุงู„ุฃุณุนุงุฑ."
3276
+
3277
+ return build_response(place, "place_details")
3278
+
3279
+
3280
+ def _should_clear_context(text: str, new_intent: str, session) -> bool: # noqa: F811
3281
+ if not session.last_intent:
3282
+ return False
3283
+ new_cat = infer_category(text) or INTENT_TO_CATEGORY.get(new_intent)
3284
+ old_cat = normalize_category(session.context_slots.get("category")) if getattr(session, "context_slots", None) else None
3285
+ old_cat = old_cat or INTENT_TO_CATEGORY.get(session.last_intent)
3286
+ if new_cat and old_cat and normalize_category(new_cat) != normalize_category(old_cat):
3287
+ return True
3288
+ t = norm(text)
3289
+ return any(p in t for p in ["ู„ุง ู‚ุตุฏูŠ", "ู…ุด ูƒุฏู‡", "ุบู„ุท", "ุจุฏู„", "ุนุงูŠุฒ ุบูŠุฑ", "ุนุงูŠุฒุฉ ุบูŠุฑ"])
3290
+
3291
+
3292
+ def clean_item_query(text: str) -> str: # noqa: F811
3293
+ """Cleaner that preserves unknown item names but removes categories/request words."""
3294
+ if not text:
3295
+ return ""
3296
+ q = clean_text(text)
3297
+ remove = set()
3298
+ for arr in _CATEGORY_PATTERNS_FINAL.values():
3299
+ remove.update(clean_text(x) for x in arr)
3300
+ remove |= {clean_text(x) for x in _DEMAND_FINAL | _ITEM_CONTEXT_FINAL | _RANKING_ONLY_FINAL}
3301
+ remove |= {"ู…ูƒุงู†", "ุงู…ุงูƒู†", "ู…ุญู„", "ููŠ", "ู…ู†", "ุนู„ู‰", "ุนู†", "ู„ูŠ", "ู„ูŠุง", "ุนุดุงู†"}
3302
+ toks = [w for w in q.split() if w not in remove and len(w) > 1]
3303
+ return " ".join(toks).strip()
3304
+
3305
+
3306
+ def search_places(query, top_k_final=5, category=None, sub_category=None,
3307
+ location=None, price_range=None, open_now_only=False,
3308
+ min_rating=None, user_lat=None, user_lon=None,
3309
+ intent=None, place_name=None): # noqa: F811
3310
+ """Strict category wrapper over semantic/name search."""
3311
+ # Use original lower-level funcs but enforce category after every path.
3312
+ strict_category = normalize_category(category or INTENT_TO_CATEGORY.get(intent))
3313
+
3314
+ if place_name:
3315
+ name_hits = name_search(place_name, top_k=10, category=strict_category)
3316
+ if name_hits.empty:
3317
+ return pd.DataFrame()
3318
+ if "name_score" in name_hits.columns:
3319
+ name_hits = name_hits.rename(columns={"name_score": "semantic_score"})
3320
+ if "search_text_clean" not in name_hits.columns:
3321
+ name_hits["search_text_clean"] = ""
3322
+ filtered = apply_filters(
3323
+ name_hits, category=strict_category, sub_category=sub_category,
3324
+ location=location, price_range=price_range, open_now_only=open_now_only,
3325
+ min_rating=min_rating, strict_location=bool(location)
3326
+ )
3327
+ if filtered.empty:
3328
+ return pd.DataFrame()
3329
+ ranked = rank(filtered, query, user_lat=user_lat, user_lon=user_lon, intent=intent)
3330
+ if strict_category and "category" in ranked.columns:
3331
+ ranked = ranked[ranked["category"].apply(lambda x: normalize_category(x) == strict_category)]
3332
+ return ranked.drop_duplicates(subset=["place_id"]).head(1)
3333
+
3334
+ sem_cands = semantic_candidates(query, top_k=40)
3335
+ all_cands = merge_places(sem_cands).drop_duplicates(subset=["place_id"])
3336
+
3337
+ filtered = apply_filters(
3338
+ all_cands, category=strict_category, sub_category=sub_category,
3339
+ location=location, price_range=price_range, open_now_only=open_now_only,
3340
+ min_rating=min_rating, strict_location=bool(location)
3341
+ )
3342
+ # HARD category enforcement: never fallback to a different category for nearest_*/explicit category.
3343
+ if strict_category and not filtered.empty and "category" in filtered.columns:
3344
+ filtered = filtered[filtered["category"].apply(lambda x: normalize_category(x) == strict_category)]
3345
+
3346
+ if filtered.empty:
3347
+ return pd.DataFrame()
3348
+
3349
+ ranked = rank(filtered, query, user_lat=user_lat, user_lon=user_lon, intent=intent)
3350
+ if _is_proximity_query(query) and user_lat is not None and user_lon is not None and "distance_km" in ranked.columns:
3351
+ ranked = ranked.sort_values(by=["distance_km", "rating_norm"], ascending=[True, False], na_position="last")
3352
+ return ranked.drop_duplicates(subset=["place_id"]).head(top_k_final)
3353
+