Kndeh commited on
Commit ·
cf33f5a
1
Parent(s): b99ba63
fix: filtering
Browse files- src/models/ai_engine.py +19 -2
src/models/ai_engine.py
CHANGED
|
@@ -295,7 +295,10 @@ def is_valid_item(nm, price_str, cnt_str=None):
|
|
| 295 |
return False
|
| 296 |
|
| 297 |
price_raw_clean = str(price_str).strip()
|
| 298 |
-
|
|
|
|
|
|
|
|
|
|
| 299 |
|
| 300 |
# Reject only obvious transaction codes / serial IDs:
|
| 301 |
# must be all-caps, contain digits, AND contain a separator like / or - or be >= 8 digits
|
|
@@ -640,8 +643,15 @@ def run_donut_ocr(img_array, processor, model, device, model_loaded,
|
|
| 640 |
raw_json = processor.token2json(sequence_stripped)
|
| 641 |
if isinstance(raw_json, list):
|
| 642 |
raw_json = {"menu": raw_json}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 643 |
|
| 644 |
# Always also run regex fallback and merge any items it finds that token2json missed
|
|
|
|
| 645 |
regex_json = fallback_regex_parse(sequence)
|
| 646 |
token_names = {clean_item_name(e.get("nm", "")).lower()
|
| 647 |
for e in (raw_json.get("menu", []) if isinstance(raw_json, dict) else [])
|
|
@@ -649,7 +659,14 @@ def run_donut_ocr(img_array, processor, model, device, model_loaded,
|
|
| 649 |
for extra in regex_json.get("menu", []):
|
| 650 |
if not isinstance(extra, dict): continue
|
| 651 |
extra_nm = clean_item_name(extra.get("nm", ""))
|
| 652 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 653 |
if not isinstance(raw_json, dict): raw_json = {}
|
| 654 |
raw_json.setdefault("menu", []).append(extra)
|
| 655 |
token_names.add(extra_nm.lower())
|
|
|
|
| 295 |
return False
|
| 296 |
|
| 297 |
price_raw_clean = str(price_str).strip()
|
| 298 |
+
# Strip known currency prefix (Rp, $, etc.) then reject if ANY letter remains in price
|
| 299 |
+
price_no_currency = re.sub(r'^[Rr][Pp]\.?\s*|^[$€£¥]\s*', '', price_raw_clean)
|
| 300 |
+
if re.search(r'[a-zA-Z]', price_no_currency):
|
| 301 |
+
return False
|
| 302 |
|
| 303 |
# Reject only obvious transaction codes / serial IDs:
|
| 304 |
# must be all-caps, contain digits, AND contain a separator like / or - or be >= 8 digits
|
|
|
|
| 643 |
raw_json = processor.token2json(sequence_stripped)
|
| 644 |
if isinstance(raw_json, list):
|
| 645 |
raw_json = {"menu": raw_json}
|
| 646 |
+
|
| 647 |
+
# Normalize menu to list if it's a dict
|
| 648 |
+
if isinstance(raw_json, dict):
|
| 649 |
+
menu_node = raw_json.get("menu")
|
| 650 |
+
if isinstance(menu_node, dict):
|
| 651 |
+
raw_json["menu"] = [menu_node]
|
| 652 |
|
| 653 |
# Always also run regex fallback and merge any items it finds that token2json missed
|
| 654 |
+
# Only merge items that have BOTH a valid name AND a numeric-looking price (prevent junk)
|
| 655 |
regex_json = fallback_regex_parse(sequence)
|
| 656 |
token_names = {clean_item_name(e.get("nm", "")).lower()
|
| 657 |
for e in (raw_json.get("menu", []) if isinstance(raw_json, dict) else [])
|
|
|
|
| 659 |
for extra in regex_json.get("menu", []):
|
| 660 |
if not isinstance(extra, dict): continue
|
| 661 |
extra_nm = clean_item_name(extra.get("nm", ""))
|
| 662 |
+
extra_price = extra.get("price", "")
|
| 663 |
+
# Guard: only merge if price string is actually numeric (has digits, no stray letters)
|
| 664 |
+
if not extra_nm or not is_valid_price_string(extra_price):
|
| 665 |
+
continue
|
| 666 |
+
price_no_curr = re.sub(r'^[Rr][Pp]\.?\s*|^[$€£¥]\s*', '', str(extra_price).strip())
|
| 667 |
+
if re.search(r'[a-zA-Z]', price_no_curr):
|
| 668 |
+
continue
|
| 669 |
+
if extra_nm.lower() not in token_names:
|
| 670 |
if not isinstance(raw_json, dict): raw_json = {}
|
| 671 |
raw_json.setdefault("menu", []).append(extra)
|
| 672 |
token_names.add(extra_nm.lower())
|