Spaces:
Sleeping
Sleeping
Update app.py
Browse files
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 |
+
|