# intents.py import re, unicodedata from collections import Counter from typing import Dict, List, Tuple, Optional # ---------------- Utils ---------------- def strip_accents(s: str) -> str: if not s: return "" return "".join(ch for ch in unicodedata.normalize("NFD", s) if unicodedata.category(ch) != "Mn") _STOPWORDS = { "the","a","an","and","or","of","to","in","on","for","with","by","from", "is","are","was","were","be","been","being","at","as","that","this","it", "into","about","over","under","per","via","than","then","so","but","if", "not","no","yes","do","does","did","done","can","could","should","would", "will","may","might","must","your","my","our","their","his","her","you", "we","they","i", "va","hoac","cua","cho","tu","la","o","trong","tren","duoi","den","di", "cai","nhung","cac","mot","mot so","nay","kia","do","thi","duoc","bi", "khi","neu","de","ra","vao","theo","voi","nhu","hay","da","dang","se", "toi","ban","chung","chung toi","chung ta","ho","anh","chi","xin","vui","long" } _WORD_RE = re.compile(r"[A-Za-zÀ-ỹ0-9_.]+") def _norm_tokens(text: str) -> List[str]: return [t.lower() for t in _WORD_RE.findall(text or "")] def extract_keywords(text: str, top_k: int = 8, min_len: int = 2) -> List[str]: tokens = _norm_tokens(text) toks = [t for t in tokens if len(t) >= min_len and t not in _STOPWORDS and not t.isdigit()] if not toks: return [] first_pos = {} for i, t in enumerate(toks): if t not in first_pos: first_pos[t] = i cnt = Counter(toks) ranked = sorted(cnt.items(), key=lambda kv: (-kv[1], first_pos[kv[0]])) return [w for w, _ in ranked[:top_k]] def is_table_query(text: str) -> bool: if not text: return False s = text.lower() sn = strip_accents(s) strong = [ "bảng","danh sách","liệt kê","datatable","data table","bảng dữ liệu", "bảng thống kê","excel","csv","table","grid view","spreadsheet" ] strong_no = [strip_accents(x) for x in strong] if any(k in s for k in strong) or any(k in sn for k in strong_no): return True if (any(v in s for v in ["liệt kê","hiển thị","xem","list","show","display"]) or any(v in sn for v in ["liet ke","hien thi","xem","list","show","display"])) and \ (any(c in s for c in ["cột","hàng","column","row","lọc","sắp xếp","filter","sort","paginate"]) or any(c in sn for c in ["cot","hang","column","row","loc","sap xep","filter","sort","paginate"])): return True if re.search(r"\b(list|show|display)\s+all\b", sn): return True kws = set(extract_keywords(sn, top_k=10)) domain_like = {"pyc","phieu","hoa","hoa chat","doanh","doanh thu","khach","khach hang", "thiet","thiet bi","tai","tai lieu","scan","gcn","bao","bao gia","hop","hop dong"} if kws & domain_like and ({"list","danh","liet","table"} & kws): return True return False # ---------------- Intent detection ---------------- PYC_RE = re.compile(r"\b(?:(?:pyc|pvc|pyd)\s*[-:]?\s*)?(\d{2,5})\b", re.I) DUOI_BG = re.compile(r"\b(?:25[.\-_/])?(\d{2,4})\b") MA_BG_RE = re.compile(r"\b25[.\-_/](\d{2,4})\b") def _find_duoi_bg(text: str) -> Optional[str]: m = MA_BG_RE.search(text) if m: return m.group(1).zfill(3) m2 = DUOI_BG.search(text) if m2: return m2.group(1).zfill(3) return None def _find_pyc(text: str) -> Optional[str]: m = PYC_RE.search(text) if m: return m.group(1) return None def detect_intent(text: str) -> Tuple[str, Dict]: s = (text or "").lower() sn = strip_accents(s) entities: Dict = {} duoi = _find_duoi_bg(sn) pyc = _find_pyc(sn) if duoi: entities["duoi_bg"] = duoi if pyc: entities["pyc"] = pyc # --- Báo giá / Giá --- if any(k in s for k in ["báo giá"]) or \ any(k in sn for k in ["bao gia","baogia","bg "]) or \ sn.strip() in {"gia","gia?","gia bao nhieu"}: if any(k in s for k in ["từ pyc","phiếu yêu cầu"]) or any(k in sn for k in ["tu pyc","phieu yeu cau","tu phieu"]): return "create_quote_from_pyc", entities if any(k in s for k in ["từ bảng giá","bảng giá"]) or any(k in sn for k in ["tu bang gia","bang gia","price list"]): return "create_quote_from_price_list", entities return "create_quote_ask_source", entities # --- Bảng giá tham khảo --- if any(k in s for k in ["bảng giá tham khảo","bảng giá"]) or any(k in sn for k in ["bang gia tham khao","bang gia","price list"]): mapping = { "hóa lý":"hoa_ly", "hoa ly":"hoa_ly", "điện":"dien", "dien":"dien", "cơ độ dài":"co_ap_suat","co do dai":"co_ap_suat","áp suất":"co_ap_suat","ap suat":"co_ap_suat", "nhiệt":"nhiet_yte","nhiet":"nhiet_yte","y tế":"nhiet_yte","y te":"nhiet_yte", "dung tích":"dung_tich","dung tich":"dung_tich","lưu lượng":"dung_tich","luu luong":"dung_tich", "khối lượng":"khoi_luong","khoi luong":"khoi_luong" } for k, v in mapping.items(): if k in s or strip_accents(k) in sn: entities["nhom_banggia"] = v break return "list_price_tables", entities # --- PYC --- if ("pyc" in sn) or ("phieu yeu cau" in sn): if any(k in sn for k in ["cho xu ly","pending","chua lam"]): return "pyc_pending", entities if any(k in sn for k in ["da hoan thanh","done","completed"]): return "pyc_done", entities return "pyc_all", entities # --- Doanh thu --- if any(k in sn for k in ["doanh thu","thong ke","revenue"]): return "doanh_thu", entities # --- Hóa chất --- if any(k in sn for k in ["hoa chat","qlhc","quan ly vat tu"]): return "hoa_chat", entities # --- Hồ sơ lưu --- if any(k in sn for k in ["ho so luu","hskh","quan ly ho so"]): return "hskh", entities # --- Tài liệu --- if any(k in sn for k in ["tai lieu","pdf","quy trinh","document"]): return "tai_lieu", entities # --- Table query --- if is_table_query(s): return "show_table", entities if any(k in sn for k in ["tro giup","help","huong dan"]): return "help", entities return "general_fallback", entities