Youmnaaaa commited on
Commit
9bb19b8
·
verified ·
1 Parent(s): 36b2eed

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +35 -14
app.py CHANGED
@@ -183,7 +183,7 @@ CAT_SINGLE = {
183
  }
184
 
185
  KEYWORD_INTENTS = {
186
- "greeting": ["السلام عليكم", "سلام عليكم", "وعليكم السلام", "اهلا", "أهلا", "هلا", "مرحبا", "صباح الخير", "مساء الخير", "سلام", "hi", "hello"],
187
  "thanks": ["شكرا", "شكراً", "تسلم", "تسلمي", "يسلمو", "مشكور", "thanks", "thank you"],
188
  "goodbye": ["مع السلامة", "مع السلامه", "باي", "وداعا", "bye", "goodbye"],
189
  "confirm": ["تمام", "اوك", "أوك", "ماشي", "حاضر", "اه", "اها", "ايوه", "نعم"],
@@ -1240,14 +1240,15 @@ def extract_item_by_pattern(text: str) -> str:
1240
  if candidate and len(candidate) >= 2 and not is_generic_category_item(candidate):
1241
  return candidate
1242
 
1243
- # Pure English natural-language pattern: keep the product terms, drop intent/category words.
1244
- if re.search(r"[a-zA-Z]", raw):
1245
- words = [w for w in re.split(r"\s+", q) if w]
1246
- kept = [w for w in words if w not in EN_STOPWORDS and clean_text(w) not in NON_ITEM_WORDS]
1247
- candidate = _strip_category_words(" ".join(kept))
1248
- if candidate and len(candidate) >= 2:
1249
- return candidate
1250
-
 
1251
  return ""
1252
 
1253
  def _translated_query_terms(q: str) -> str:
@@ -1788,9 +1789,20 @@ def _name_similarity(query: str, names: str) -> float:
1788
  if not q or not n: return 0.0
1789
  q_ns=q.replace(" ",""); n_ns=n.replace(" ","")
1790
  q_words=[w for w in q.split() if len(w)>1]
1791
- if q==n or q in n or n in q: return 1.0
1792
- if q_ns and (q_ns in n_ns or n_ns in q_ns): return 0.98
1793
- if q_words and all(w in n for w in q_words): return 0.92
 
 
 
 
 
 
 
 
 
 
 
1794
  common=sum(1 for w in q_words if w in n)
1795
  token=0.45+0.35*common/max(len(q_words),1) if common else 0.0
1796
  fuzzy=difflib.SequenceMatcher(None,q_ns,n_ns).ratio()*0.88
@@ -2325,8 +2337,17 @@ def chat(text: str, session: Session, user_lat=None, user_lon=None) -> dict:
2325
  # keyword, so it often mis-routes these to fallback/out_of_scope.
2326
  # Typing just a place name is one of the most common real-world
2327
  # search patterns, so we try a direct name_search before bailing out.
2328
- candidate_name = entities.get("place_name") or extract_place_name_from_text(text)
2329
- if intent != "out_of_scope" and candidate_name and len(candidate_name) >= 2:
 
 
 
 
 
 
 
 
 
2330
  named_df = name_search(candidate_name, top_k=5)
2331
  if not named_df.empty:
2332
  named_results = [row_to_dict(r) for _, r in named_df.iterrows()]
 
183
  }
184
 
185
  KEYWORD_INTENTS = {
186
+ "greeting": ["السلام عليكم", "سلام عليكم", "وعليكم السلام", "اهلا", "أهلا", "هلا", "هاي", "مرحبا", "صباح الخير", "مساء الخير", "سلام", "hi", "hello", "hey"],
187
  "thanks": ["شكرا", "شكراً", "تسلم", "تسلمي", "يسلمو", "مشكور", "thanks", "thank you"],
188
  "goodbye": ["مع السلامة", "مع السلامه", "باي", "وداعا", "bye", "goodbye"],
189
  "confirm": ["تمام", "اوك", "أوك", "ماشي", "حاضر", "اه", "اها", "ايوه", "نعم"],
 
1240
  if candidate and len(candidate) >= 2 and not is_generic_category_item(candidate):
1241
  return candidate
1242
 
1243
+ # NOTE: there used to be a second, unconditional "any English word ->
1244
+ # treat as item" fallback here. It fired even with ZERO pattern signal
1245
+ # (no عنده/محتاج/want/need/has/have/...), so a bare "hi" or "hello"
1246
+ # became item_query="hi"/"hello" and hijacked a greeting. The pattern
1247
+ # check above already covers genuine English requests like "i want a
1248
+ # shawarma restaurant near me" (via "want" in WEAK_ITEM_PATTERN_WORDS) or
1249
+ # "cafe with iced coffee" (via "with" in STRONG_ITEM_PATTERN_WORDS), so
1250
+ # no separate no-signal-required fallback is needed — every extraction
1251
+ # here must be justified by an actual pattern word.
1252
  return ""
1253
 
1254
  def _translated_query_terms(q: str) -> str:
 
1789
  if not q or not n: return 0.0
1790
  q_ns=q.replace(" ",""); n_ns=n.replace(" ","")
1791
  q_words=[w for w in q.split() if len(w)>1]
1792
+ if q == n:
1793
+ return 1.0
1794
+ # Containment shortcuts are only trustworthy when the shorter string
1795
+ # occurs at a real WORD boundary in the longer one, AND is long enough
1796
+ # to be meaningful. Without this, short strings falsely score a perfect
1797
+ # match against any longer name that happens to start with them — e.g.
1798
+ # "هاي" (greeting) is a raw substring of "هايبر بني سويف" (a real
1799
+ # supermarket) even though they're completely different words.
1800
+ if len(q) >= 4 and (re.search(rf"\b{re.escape(q)}\b", n) or (len(n) >= 4 and re.search(rf"\b{re.escape(n)}\b", q))):
1801
+ return 1.0
1802
+ if len(q_ns) >= 4 and (q_ns in n_ns or n_ns in q_ns):
1803
+ return 0.98
1804
+ if len(q_words) >= 1 and all(re.search(rf"\b{re.escape(w)}\b", n) for w in q_words) and (len(q_words) >= 2 or len(q_words[0]) >= 4):
1805
+ return 0.92
1806
  common=sum(1 for w in q_words if w in n)
1807
  token=0.45+0.35*common/max(len(q_words),1) if common else 0.0
1808
  fuzzy=difflib.SequenceMatcher(None,q_ns,n_ns).ratio()*0.88
 
2337
  # keyword, so it often mis-routes these to fallback/out_of_scope.
2338
  # Typing just a place name is one of the most common real-world
2339
  # search patterns, so we try a direct name_search before bailing out.
2340
+ # IMPORTANT: only for genuinely ambiguous intents. "greeting"/
2341
+ # "thanks"/"goodbye"/"confirm"/"deny" already matched an explicit,
2342
+ # unambiguous keyword (e.g. "هاي") — never second-guess those by
2343
+ # trying to reinterpret them as a place name, even if some place's
2344
+ # name happens to fuzzy-overlap with the greeting word (e.g. "هاي"
2345
+ # vs "هايبر ..."). A confirmed greeting must always short-circuit to
2346
+ # a greeting reply with no cards, per spec.
2347
+ candidate_name = ""
2348
+ if intent == "fallback":
2349
+ candidate_name = entities.get("place_name") or extract_place_name_from_text(text)
2350
+ if candidate_name and len(candidate_name) >= 2:
2351
  named_df = name_search(candidate_name, top_k=5)
2352
  if not named_df.empty:
2353
  named_results = [row_to_dict(r) for _, r in named_df.iterrows()]