Youmnaaaa commited on
Commit
1b41ad5
ยท
verified ยท
1 Parent(s): 0cda9cc

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -4
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
- t = re.sub(rf"\b{re.escape(clean_text(w))}\b", " ", t)
 
 
 
 
 
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
- t = re.sub(rf"\bูˆ?{re.escape(cw)}[ู‡ู‡ุงูˆูŠูˆุง]{{0,3}}\b", " ", t)
 
 
 
 
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
- place_name = entities.get("place_name") or extract_menu_place_name(text)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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: