Update intents.py
Browse files- intents.py +125 -51
intents.py
CHANGED
|
@@ -1,10 +1,10 @@
|
|
| 1 |
# intents.py
|
| 2 |
import re
|
| 3 |
from collections import Counter
|
| 4 |
-
from typing import List
|
| 5 |
|
| 6 |
-
#
|
| 7 |
-
|
| 8 |
# EN
|
| 9 |
"the","a","an","and","or","of","to","in","on","for","with","by","from",
|
| 10 |
"is","are","was","were","be","been","being","at","as","that","this","it",
|
|
@@ -16,70 +16,144 @@ STOPWORDS = {
|
|
| 16 |
"và","hoặc","của","cho","từ","là","ở","trong","trên","dưới","đến","đi",
|
| 17 |
"cái","những","các","một","một số","này","kia","đó","thì","được","bị",
|
| 18 |
"khi","nếu","để","ra","vào","theo","với","như","hay","đã","đang","sẽ",
|
| 19 |
-
"tôi","bạn","chúng","chúng tôi","chúng ta","họ","anh","chị"
|
| 20 |
}
|
|
|
|
| 21 |
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
def _normalize(text: str) -> List[str]:
|
| 25 |
-
# tách từ: chữ + có dấu tiếng Việt + số; chuyển lower
|
| 26 |
-
tokens = [t.lower() for t in _WORD_RE.findall(text or "")]
|
| 27 |
-
return tokens
|
| 28 |
|
| 29 |
def extract_keywords(text: str, top_k: int = 8, min_len: int = 2) -> List[str]:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
"""
|
| 34 |
-
tokens = _normalize(text)
|
| 35 |
-
# bỏ stopwords và token quá ngắn / toàn số
|
| 36 |
-
filtered = [
|
| 37 |
-
t for t in tokens
|
| 38 |
-
if len(t) >= min_len
|
| 39 |
-
and t not in STOPWORDS
|
| 40 |
-
and not t.isdigit()
|
| 41 |
-
]
|
| 42 |
-
if not filtered:
|
| 43 |
return []
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
|
|
|
|
|
|
| 48 |
return [w for w, _ in ranked[:top_k]]
|
| 49 |
|
| 50 |
def is_table_query(text: str) -> bool:
|
| 51 |
-
"""
|
| 52 |
-
Phát hiện nhanh yêu cầu 'hiển thị dạng bảng / liệt kê dữ liệu'.
|
| 53 |
-
Dựa trên từ khóa tiếng Việt & Anh, phù hợp dashboard DataTables/Excel/CSV.
|
| 54 |
-
"""
|
| 55 |
if not text:
|
| 56 |
return False
|
| 57 |
s = text.lower()
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
"bảng", "danh sách", "liệt kê", "bảng biểu", "datatable", "data table",
|
| 62 |
-
"bảng dữ liệu", "hiển thị bảng", "bảng thống kê", "bảng tổng hợp",
|
| 63 |
-
"excel", "csv", "table", "grid view", "spreadsheet"
|
| 64 |
]
|
| 65 |
-
if any(k in s for k in
|
| 66 |
return True
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
column_row_cues = ["cột", "hàng", "column", "row", "lọc", "sắp xếp", "filter", "sort", "paginate"]
|
| 70 |
-
verbs = ["liệt kê", "hiển thị", "xem", "list", "show", "display"]
|
| 71 |
-
if any(v in s for v in verbs) and any(c in s for c in column_row_cues):
|
| 72 |
return True
|
| 73 |
-
|
| 74 |
-
# Mẫu câu “list all …”, “show all …”
|
| 75 |
if re.search(r"\b(list|show|display)\s+all\b", s):
|
| 76 |
return True
|
| 77 |
-
|
| 78 |
-
# Heuristic theo keywords trích xuất (nếu người dùng nói về 'phiếu', 'hóa chất', 'doanh thu' v.v.)
|
| 79 |
kws = set(extract_keywords(s, top_k=10))
|
| 80 |
-
domain_like = {"
|
| 81 |
-
"thiết",
|
| 82 |
-
if kws & domain_like and ({"list",
|
| 83 |
return True
|
| 84 |
-
|
| 85 |
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
# intents.py
|
| 2 |
import re
|
| 3 |
from collections import Counter
|
| 4 |
+
from typing import Dict, List, Tuple, Optional
|
| 5 |
|
| 6 |
+
# ========= Basics =========
|
| 7 |
+
_STOPWORDS = {
|
| 8 |
# EN
|
| 9 |
"the","a","an","and","or","of","to","in","on","for","with","by","from",
|
| 10 |
"is","are","was","were","be","been","being","at","as","that","this","it",
|
|
|
|
| 16 |
"và","hoặc","của","cho","từ","là","ở","trong","trên","dưới","đến","đi",
|
| 17 |
"cái","những","các","một","một số","này","kia","đó","thì","được","bị",
|
| 18 |
"khi","nếu","để","ra","vào","theo","với","như","hay","đã","đang","sẽ",
|
| 19 |
+
"tôi","bạn","chúng","chúng tôi","chúng ta","họ","anh","chị","xin","vui","lòng"
|
| 20 |
}
|
| 21 |
+
_WORD_RE = re.compile(r"[A-Za-zÀ-ỹ0-9_.]+")
|
| 22 |
|
| 23 |
+
def _norm(text: str) -> List[str]:
|
| 24 |
+
return [t.lower() for t in _WORD_RE.findall(text or "")]
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
|
| 26 |
def extract_keywords(text: str, top_k: int = 8, min_len: int = 2) -> List[str]:
|
| 27 |
+
tokens = _norm(text)
|
| 28 |
+
toks = [t for t in tokens if len(t) >= min_len and t not in _STOPWORDS and not t.isdigit()]
|
| 29 |
+
if not toks:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
return []
|
| 31 |
+
first_pos = {}
|
| 32 |
+
for i, t in enumerate(toks):
|
| 33 |
+
if t not in first_pos:
|
| 34 |
+
first_pos[t] = i
|
| 35 |
+
cnt = Counter(toks)
|
| 36 |
+
ranked = sorted(cnt.items(), key=lambda kv: (-kv[1], first_pos[kv[0]]))
|
| 37 |
return [w for w, _ in ranked[:top_k]]
|
| 38 |
|
| 39 |
def is_table_query(text: str) -> bool:
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
if not text:
|
| 41 |
return False
|
| 42 |
s = text.lower()
|
| 43 |
+
strong = [
|
| 44 |
+
"bảng", "danh sách", "liệt kê", "datatable", "data table", "bảng dữ liệu",
|
| 45 |
+
"bảng thống kê", "excel", "csv", "table", "grid view", "spreadsheet"
|
|
|
|
|
|
|
|
|
|
| 46 |
]
|
| 47 |
+
if any(k in s for k in strong):
|
| 48 |
return True
|
| 49 |
+
if any(v in s for v in ["liệt kê", "hiển thị", "xem", "list", "show", "display"]) and \
|
| 50 |
+
any(c in s for c in ["cột","hàng","column","row","lọc","sắp xếp","filter","sort","paginate"]):
|
|
|
|
|
|
|
|
|
|
| 51 |
return True
|
|
|
|
|
|
|
| 52 |
if re.search(r"\b(list|show|display)\s+all\b", s):
|
| 53 |
return True
|
|
|
|
|
|
|
| 54 |
kws = set(extract_keywords(s, top_k=10))
|
| 55 |
+
domain_like = {"pyc","phiếu","hóa","hóa chất","doanh","doanh thu","khách","khách hàng",
|
| 56 |
+
"thiết","thiết bị","tài","tài liệu","scan","gcn","báo","báo giá","hợp","hợp đồng"}
|
| 57 |
+
if kws & domain_like and ({"list","danh","liệt","table"} & kws):
|
| 58 |
return True
|
|
|
|
| 59 |
return False
|
| 60 |
+
|
| 61 |
+
# ========= Intent detection =========
|
| 62 |
+
# Chuẩn hoá một số mẫu số hiệu
|
| 63 |
+
PYC_RE = re.compile(r"\b(?:(?:pyc|pvc|pyd)\s*[-:]?\s*)?(\d{2,5})\b", re.I)
|
| 64 |
+
DUOI_BG = re.compile(r"\b(?:25[.\-_/])?(\d{2,4})\b") # ví dụ 25.074, 25-074, 074
|
| 65 |
+
MA_BG_RE = re.compile(r"\b25[.\-_/](\d{2,4})\b")
|
| 66 |
+
|
| 67 |
+
def _find_duoi_bg(text: str) -> Optional[str]:
|
| 68 |
+
m = MA_BG_RE.search(text)
|
| 69 |
+
if m:
|
| 70 |
+
return m.group(1).zfill(3)
|
| 71 |
+
# nếu chỉ có số ngắn như "074" thì cũng nhận
|
| 72 |
+
m2 = DUOI_BG.search(text)
|
| 73 |
+
if m2:
|
| 74 |
+
return m2.group(1).zfill(3)
|
| 75 |
+
return None
|
| 76 |
+
|
| 77 |
+
def _find_pyc(text: str) -> Optional[str]:
|
| 78 |
+
m = PYC_RE.search(text)
|
| 79 |
+
if m:
|
| 80 |
+
return m.group(1)
|
| 81 |
+
return None
|
| 82 |
+
|
| 83 |
+
def detect_intent(text: str) -> Tuple[str, Dict]:
|
| 84 |
+
"""
|
| 85 |
+
Trả về (intent, entities)
|
| 86 |
+
intent gồm:
|
| 87 |
+
- create_quote_from_price_list
|
| 88 |
+
- create_quote_from_pyc
|
| 89 |
+
- list_price_tables
|
| 90 |
+
- pyc_pending / pyc_done / pyc_all
|
| 91 |
+
- doanh_thu, hoa_chat, hskh, tai_lieu
|
| 92 |
+
- help, general_fallback
|
| 93 |
+
entities có thể gồm: {'pyc': '140', 'duoi_bg': '074', 'nhom_banggia': 'hoa_ly' ...}
|
| 94 |
+
"""
|
| 95 |
+
s = (text or "").lower()
|
| 96 |
+
entities: Dict = {}
|
| 97 |
+
duoi = _find_duoi_bg(s)
|
| 98 |
+
pyc = _find_pyc(s)
|
| 99 |
+
if duoi: entities["duoi_bg"] = duoi
|
| 100 |
+
if pyc: entities["pyc"] = pyc
|
| 101 |
+
|
| 102 |
+
# ==== Nhận diện "BÁO GIÁ" ====
|
| 103 |
+
if any(k in s for k in ["báo giá","bao gia","baogia","bg "]):
|
| 104 |
+
# nguồn tạo báo giá?
|
| 105 |
+
if any(k in s for k in ["từ pyc","tu pyc","phiếu yêu cầu","phieu yeu cau","từ phiếu"]):
|
| 106 |
+
return "create_quote_from_pyc", entities
|
| 107 |
+
if any(k in s for k in ["từ bảng giá","tu bang gia","bảng giá","bang gia","price list"]):
|
| 108 |
+
return "create_quote_from_price_list", entities
|
| 109 |
+
# nếu chỉ nói "báo giá" → hỏi hướng đi
|
| 110 |
+
return "create_quote_ask_source", entities
|
| 111 |
+
|
| 112 |
+
# ==== BẢNG GIÁ ====
|
| 113 |
+
if any(k in s for k in ["bảng giá tham khảo","bang gia tham khao","bảng giá","bang gia","price list"]):
|
| 114 |
+
# nhóm bảng giá: hóa lý / điện / cơ / nhiệt / dung tích / khối lượng
|
| 115 |
+
mapping = {
|
| 116 |
+
"hóa lý":"hoa_ly", "hoa ly":"hoa_ly", "điện":"dien", "co do dai":"co_ap_suat",
|
| 117 |
+
"cơ độ dài":"co_ap_suat","áp suất":"co_ap_suat", "nhiệt":"nhiet_yte",
|
| 118 |
+
"y tế":"nhiet_yte", "dung tích":"dung_tich", "lưu lượng":"dung_tich",
|
| 119 |
+
"khối lượng":"khoi_luong", "khoi luong":"khoi_luong"
|
| 120 |
+
}
|
| 121 |
+
for k, v in mapping.items():
|
| 122 |
+
if k in s:
|
| 123 |
+
entities["nhom_banggia"] = v
|
| 124 |
+
break
|
| 125 |
+
return "list_price_tables", entities
|
| 126 |
+
|
| 127 |
+
# ==== PYC ====
|
| 128 |
+
if "pyc" in s or "phiếu yêu cầu" in s or "phieu yeu cau" in s:
|
| 129 |
+
if any(k in s for k in ["chờ xử lý","cho xu ly","pending","chưa làm","chua lam"]):
|
| 130 |
+
return "pyc_pending", entities
|
| 131 |
+
if any(k in s for k in ["đã hoàn thành","da hoan thanh","done","completed"]):
|
| 132 |
+
return "pyc_done", entities
|
| 133 |
+
return "pyc_all", entities
|
| 134 |
+
|
| 135 |
+
# ==== DOANH THU / THỐNG KÊ ====
|
| 136 |
+
if any(k in s for k in ["doanh thu","thống kê","thong ke","revenue"]):
|
| 137 |
+
return "doanh_thu", entities
|
| 138 |
+
|
| 139 |
+
# ==== HÓA CHẤT ====
|
| 140 |
+
if any(k in s for k in ["hóa chất","hoa chat","qlhc","quản lý vật tư","quan ly vat tu"]):
|
| 141 |
+
return "hoa_chat", entities
|
| 142 |
+
|
| 143 |
+
# ==== HỒ SƠ LƯU ====
|
| 144 |
+
if any(k in s for k in ["hồ sơ lưu","ho so luu","hskh","quản lý hồ sơ","quan ly ho so"]):
|
| 145 |
+
return "hskh", entities
|
| 146 |
+
|
| 147 |
+
# ==== TÀI LIỆU ====
|
| 148 |
+
if any(k in s for k in ["tài liệu","tai lieu","pdf","quy trình","quy trinh","document"]):
|
| 149 |
+
return "tai_lieu", entities
|
| 150 |
+
|
| 151 |
+
# ==== TABLE QUERY (hiển thị dạng bảng) ====
|
| 152 |
+
if is_table_query(s):
|
| 153 |
+
return "show_table", entities
|
| 154 |
+
|
| 155 |
+
# ==== HELP / FALLBACK ====
|
| 156 |
+
if any(k in s for k in ["trợ giúp","tro giup","help","hướng dẫn","huong dan"]):
|
| 157 |
+
return "help", entities
|
| 158 |
+
|
| 159 |
+
return "general_fallback", entities
|