Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1044,6 +1044,12 @@ def _strip_category_words(q: str) -> str:
|
|
| 1044 |
"ู
ุงุฑูุช", "ุณูุจุฑ", "ุณูุจุฑู
ุงุฑูุช", "ุณูุจุฑ ู
ุงุฑูุช", "ูุงูุจุฑ", "supermarket", "market", "grocery",
|
| 1045 |
"ุณูู", "ุดูู", "ุดูุฉ", "ููุฏู", "housing", "hotel", "apartment",
|
| 1046 |
"ูุฑูุจ", "ูุฑูุจู", "ูุฑูุจุฉ", "ุญูุงููุง", "ุญููู", "ุฌูุจู", "near", "nearby", "nearest", "me",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1047 |
]
|
| 1048 |
for w in generic_words:
|
| 1049 |
cw = clean_text(w)
|
|
@@ -1163,9 +1169,19 @@ def is_generic_category_item(value: str) -> bool:
|
|
| 1163 |
return q in {clean_text(x) for x in generic}
|
| 1164 |
|
| 1165 |
def extract_menu_place_name(text: str) -> str:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1166 |
t = clean_text(text)
|
| 1167 |
-
for w in ["ูุงุช", "ุงุจุนุช", "ูุฑููู", "ุงุนุฑุถ", "ู
ููู", "ู
ูููู", "menu",
|
| 1168 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1169 |
return re.sub(r"\s+", " ", t).strip()
|
| 1170 |
|
| 1171 |
|
|
@@ -1262,6 +1278,10 @@ def extract_place_name_from_text(text: str) -> str:
|
|
| 1262 |
# verb phrases that come before item/food words โ strip these so
|
| 1263 |
# 'ููู ูุงููู ุจุชุนู
ู ูุงูู' doesn't leave 'ุจุชุนู
ู ูุงูู' as a fake place name
|
| 1264 |
"ุจุชุนู
ู", "ุจูุนู
ู", "ุจุชูุฏู
", "ุจููุฏู
", "ุนูุฏู", "ุนูุฏูุง", "ููู", "ูููุง",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1265 |
]
|
| 1266 |
# Arabic words frequently carry attached possessive/verb suffixes
|
| 1267 |
# (e.g. "ู
ูุงุนูุฏู" = "ู
ูุงุนูุฏ" + "ู", "ุจููุชุญูุง" = "ุจููุชุญ" + "ูุง") AND a
|
|
@@ -1273,7 +1293,11 @@ def extract_place_name_from_text(text: str) -> str:
|
|
| 1273 |
# name like "ูุนููุงูู" behind.
|
| 1274 |
for w in stop_phrases:
|
| 1275 |
cw = clean_text(w)
|
| 1276 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1277 |
t = re.sub(r"\s+", " ", t).strip()
|
| 1278 |
if not t or len(t) < 2:
|
| 1279 |
return ""
|
|
@@ -2035,7 +2059,24 @@ def chat(text: str, session: Session, user_lat=None, user_lon=None) -> dict:
|
|
| 2035 |
session.add(text, reply, "show_menu", entities, None, [])
|
| 2036 |
return {"reply": reply, "intent": "show_menu", "confidence": confidence, "entities": entities, "best_place": None, "all_results": [], "menu_items": []}
|
| 2037 |
|
| 2038 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2039 |
generic_menu_only = clean_text(place_name) in {"", "ุงูู
ููู", "ู
ููู", "ู
ูููู", "menu", "ูุงุฆู
ู", "ูุงุฆู
ุฉ"}
|
| 2040 |
|
| 2041 |
if generic_menu_only and session.last_place:
|
|
|
|
| 1044 |
"ู
ุงุฑูุช", "ุณูุจุฑ", "ุณูุจุฑู
ุงุฑูุช", "ุณูุจุฑ ู
ุงุฑูุช", "ูุงูุจุฑ", "supermarket", "market", "grocery",
|
| 1045 |
"ุณูู", "ุดูู", "ุดูุฉ", "ููุฏู", "housing", "hotel", "apartment",
|
| 1046 |
"ูุฑูุจ", "ูุฑูุจู", "ูุฑูุจุฉ", "ุญูุงููุง", "ุญููู", "ุฌูุจู", "near", "nearby", "nearest", "me",
|
| 1047 |
+
# Search/looking-for verb phrases: these carry no product meaning on
|
| 1048 |
+
# their own and must not survive as a leftover "item" candidate.
|
| 1049 |
+
# e.g. "ุนุงูุฒู ุฏูุฑ ุนูู ู
ุทุนู
ูุฑูุจ" must reduce to "" (category search),
|
| 1050 |
+
# not leave "ุฏูุฑ ุนูู" behind as a fake item_query.
|
| 1051 |
+
"ุจุฏูุฑ ุนูู", "ุจุฏูุฑ", "ูุฏูุฑ ุนูู", "ูุฏูุฑ", "ุฏูุฑ ุนูู", "ุฏูุฑูู", "ุฏูุฑู ุนูู",
|
| 1052 |
+
"ุงุจุญุซ ุนู", "ุงุจุญุซูู ุนู", "ูุงูููู", "ูุงูู ูู", "ูุฑููู", "ูุฑููู",
|
| 1053 |
]
|
| 1054 |
for w in generic_words:
|
| 1055 |
cw = clean_text(w)
|
|
|
|
| 1169 |
return q in {clean_text(x) for x in generic}
|
| 1170 |
|
| 1171 |
def extract_menu_place_name(text: str) -> str:
|
| 1172 |
+
"""Strip menu-request filler words from raw text, leaving only what could
|
| 1173 |
+
be a real place name. Handles attached Arabic prefixes/suffixes so
|
| 1174 |
+
'ุงูู
ููู', 'ู
ููู ุจุชุงุนู', 'ู
ููู ุจุชุงุนูุง', 'ูุฑููู ุงูู
ููู' all reduce to ''
|
| 1175 |
+
instead of leaving junk like 'ุจุชุงุนู' or 'ุงูู
ููู' behind.
|
| 1176 |
+
"""
|
| 1177 |
t = clean_text(text)
|
| 1178 |
+
for w in ["ูุงุช", "ุงุจุนุช", "ุงุจุนุชูู", "ูุฑููู", "ูุฑููู", "ุงุนุฑุถ", "ู
ููู", "ู
ูููู", "menu",
|
| 1179 |
+
"ุจุชุงุน", "ุจุชุงุนู", "ุจุชุงุนูุง", "ุจุชุงุนู", "ุชุจุนู", "ุชุจุนูุง", "ุชุจุนู", "ูุงุฆู
ุฉ", "ูุงุฆู
ู",
|
| 1180 |
+
# request-verb prefixes: "ุนุงูุฒู ู
ููู" must reduce to "", not "ุนุงูุฒู".
|
| 1181 |
+
"ุนุงูุฒ", "ุนุงูุฒู", "ุนุงูุฒุฉ", "ุนุงูุฒ", "ุนุงูุฒู", "ุนุงูุฒุฉ", "ู
ุญุชุงุฌ", "ู
ุญุชุงุฌุฉ", "ู
ุญุชุงุฌู",
|
| 1182 |
+
"ู
ู
ูู", "ูู ุณู
ุญุช", "ู
ู ูุถูู", "ุงุจุบู", "ุงุจุบุง", "ุงุจู", "ููุณู"]:
|
| 1183 |
+
cw = clean_text(w)
|
| 1184 |
+
t = re.sub(rf"\bู?(?:ุงู)?{re.escape(cw)}[ููุงูููุง]{{0,3}}\b", " ", t)
|
| 1185 |
return re.sub(r"\s+", " ", t).strip()
|
| 1186 |
|
| 1187 |
|
|
|
|
| 1278 |
# verb phrases that come before item/food words โ strip these so
|
| 1279 |
# 'ููู ูุงููู ุจุชุนู
ู ูุงูู' doesn't leave 'ุจุชุนู
ู ูุงูู' as a fake place name
|
| 1280 |
"ุจุชุนู
ู", "ุจูุนู
ู", "ุจุชูุฏู
", "ุจููุฏู
", "ุนูุฏู", "ุนูุฏูุง", "ููู", "ูููุง",
|
| 1281 |
+
# menu-follow-up filler words โ 'ู
ููู ุจุชุงุนู' / 'ูุฑููู ุงูู
ููู' / 'ูุงุช ุงูู
ููู'
|
| 1282 |
+
# must never leave 'ุจุชุงุนู' or 'ูุฑููู' behind as a fake place name.
|
| 1283 |
+
"ุจุชุงุน", "ุจุชุงุนู", "ุจุชุงุนูุง", "ุจุชุงุนู", "ุชุจุนู", "ุชุจุนูุง", "ุชุจุนู",
|
| 1284 |
+
"ูุฑููู", "ูุฑููู", "ุงุนุฑุถ", "ุงุจุนุช", "ุงุจุนุชูู", "ูุงุช",
|
| 1285 |
]
|
| 1286 |
# Arabic words frequently carry attached possessive/verb suffixes
|
| 1287 |
# (e.g. "ู
ูุงุนูุฏู" = "ู
ูุงุนูุฏ" + "ู", "ุจููุชุญูุง" = "ุจููุชุญ" + "ูุง") AND a
|
|
|
|
| 1293 |
# name like "ูุนููุงูู" behind.
|
| 1294 |
for w in stop_phrases:
|
| 1295 |
cw = clean_text(w)
|
| 1296 |
+
# Allow an optional leading "ู" (and) and/or "ุงู" (the) โ Arabic
|
| 1297 |
+
# attaches these directly with no space ("ุงูู
ููู", "ูุนููุงูู"), so a
|
| 1298 |
+
# plain \bword\b never matches inside them. Without this, "ุงูู
ููู"
|
| 1299 |
+
# survives stripping and gets mistaken for a place name.
|
| 1300 |
+
t = re.sub(rf"\bู?(?:ุงู)?{re.escape(cw)}[ููุงูููุง]{{0,3}}\b", " ", t)
|
| 1301 |
t = re.sub(r"\s+", " ", t).strip()
|
| 1302 |
if not t or len(t) < 2:
|
| 1303 |
return ""
|
|
|
|
| 2059 |
session.add(text, reply, "show_menu", entities, None, [])
|
| 2060 |
return {"reply": reply, "intent": "show_menu", "confidence": confidence, "entities": entities, "best_place": None, "all_results": [], "menu_items": []}
|
| 2061 |
|
| 2062 |
+
# IMPORTANT: compute the menu-filler-stripped candidate FIRST and
|
| 2063 |
+
# prefer it. entities.get("place_name") can be wrong here โ upstream
|
| 2064 |
+
# NER/regex extraction sometimes mistakes leftover filler words like
|
| 2065 |
+
# "ุจุชุงุนู" or "ุงูู
ููู" (with its attached "ุงู") for a place name (see
|
| 2066 |
+
# extract_place_name_from_text). If we let that value short-circuit
|
| 2067 |
+
# via `or`, extract_menu_place_name(text) never runs and follow-ups
|
| 2068 |
+
# like "ู
ููู ุจุชุงุนู" or "ูุฑููู ุงูู
ููู" wrongly try to search for a
|
| 2069 |
+
# place called "ุจุชุงุนู" instead of using session.last_place.
|
| 2070 |
+
stripped_place_name = extract_menu_place_name(text)
|
| 2071 |
+
entity_place_name = entities.get("place_name") or ""
|
| 2072 |
+
if stripped_place_name:
|
| 2073 |
+
place_name = stripped_place_name
|
| 2074 |
+
elif clean_text(entity_place_name) in {"ุจุชุงุนู", "ุจุชุงุนูุง", "ุจุชุงุนู", "ุชุจุนู", "ุชุจุนูุง", "ุชุจุนู", "ูุฑููู", "ูุฑููู"}:
|
| 2075 |
+
# Defensive: even if extract_place_name_from_text still returned
|
| 2076 |
+
# one of these filler words, never treat it as a real place name.
|
| 2077 |
+
place_name = ""
|
| 2078 |
+
else:
|
| 2079 |
+
place_name = entity_place_name
|
| 2080 |
generic_menu_only = clean_text(place_name) in {"", "ุงูู
ููู", "ู
ููู", "ู
ูููู", "menu", "ูุงุฆู
ู", "ูุงุฆู
ุฉ"}
|
| 2081 |
|
| 2082 |
if generic_menu_only and session.last_place:
|