From 50440e84fbb194a2b32b024f4ca83288147639ad Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=AD=A3=E8=88=AA?= <1915581435@qq.com> Date: Tue, 21 Apr 2026 22:55:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E9=A3=8E=E9=99=A9=E5=85=B3=E9=94=AE?= =?UTF-8?q?=E8=AF=8D=E7=BA=A2=E8=89=B2=E6=A0=87=E8=AE=B0=20+=20=E7=82=B9?= =?UTF-8?q?=E5=87=BB=E6=98=BE=E7=A4=BA=E6=9D=83=E9=87=8D=E8=B4=A1=E7=8C=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 后端: _extract_reason_tokens 返回 [{token, weight}] 格式 - 前端: detect/batch 页面风险关键词使用红色标签样式 - 点击关键词弹窗显示权重值及判定倾向 Co-Authored-By: Claude Opus 4.7 --- backend/app/ml/naive_bayes_classifier.py | 10 +++++----- miniprogram/app.wxss | 6 ++++++ miniprogram/pages/batch/index.js | 13 +++++++++++++ miniprogram/pages/batch/index.wxml | 6 ++++++ miniprogram/pages/detect/index.js | 13 +++++++++++++ miniprogram/pages/detect/index.wxml | 2 +- 6 files changed, 44 insertions(+), 6 deletions(-) diff --git a/backend/app/ml/naive_bayes_classifier.py b/backend/app/ml/naive_bayes_classifier.py index 3316a0b..bc1299a 100644 --- a/backend/app/ml/naive_bayes_classifier.py +++ b/backend/app/ml/naive_bayes_classifier.py @@ -119,7 +119,7 @@ class NaiveBayesSpamClassifier: "trained_at": self.metadata.get("trained_at"), } - def _extract_reason_tokens(self, text: str, classes: list[str], x_row) -> list[str]: + def _extract_reason_tokens(self, text: str, classes: list[str], x_row) -> list[dict]: try: vocab = self.vectorizer.vocabulary_ feature_names = self.vectorizer.get_feature_names_out() @@ -138,12 +138,12 @@ class NaiveBayesSpamClassifier: if idx is None: continue delta = class_log_prob[spam_idx][idx] - class_log_prob[ham_idx][idx] - scored.append((token, delta)) + scored.append({"token": token, "weight": round(delta, 4)}) - scored.sort(key=lambda row: abs(row[1]), reverse=True) - return [token for token, _ in scored[:5]] + scored.sort(key=lambda row: abs(row["weight"]), reverse=True) + return scored[:5] except Exception: - return list(text[:5]) + return [{"token": ch, "weight": 0.0} for ch in list(text[:5])] def model_info(self) -> dict: return { diff --git a/miniprogram/app.wxss b/miniprogram/app.wxss index 08933e5..b36da0a 100644 --- a/miniprogram/app.wxss +++ b/miniprogram/app.wxss @@ -393,6 +393,12 @@ button.btn::after { color: #ffd487; } +.tag-danger { + background: rgba(255, 91, 111, 0.18); + border: 1rpx solid rgba(255, 108, 128, 0.42); + color: #ffdce1; +} + .chip-group { display: flex; flex-wrap: wrap; diff --git a/miniprogram/pages/batch/index.js b/miniprogram/pages/batch/index.js index 2ba20d0..79bba95 100644 --- a/miniprogram/pages/batch/index.js +++ b/miniprogram/pages/batch/index.js @@ -66,5 +66,18 @@ Page({ '测试报告我已经同步到项目群。' ].join('\n') }) + }, + + showTokenWeight(e) { + const token = e.currentTarget.dataset.token + const weight = e.currentTarget.dataset.weight + const weightNum = Number(weight || 0) + const direction = weightNum >= 0 ? '倾向垃圾判定' : '倾向正常判定' + wx.showModal({ + title: '关键词权重', + content: `关键词"${token}"\n权重贡献:${weightNum >= 0 ? '+' : ''}${weightNum.toFixed(4)}\n(${direction})`, + showCancel: false, + confirmText: '关闭' + }) } }) diff --git a/miniprogram/pages/batch/index.wxml b/miniprogram/pages/batch/index.wxml index c6e1a72..9475202 100644 --- a/miniprogram/pages/batch/index.wxml +++ b/miniprogram/pages/batch/index.wxml @@ -69,6 +69,12 @@ + + 风险关键词 + + {{tokenItem.token}} + + diff --git a/miniprogram/pages/detect/index.js b/miniprogram/pages/detect/index.js index 947d7ed..b9ef950 100644 --- a/miniprogram/pages/detect/index.js +++ b/miniprogram/pages/detect/index.js @@ -98,5 +98,18 @@ Page({ gotoHistory() { wx.navigateTo({ url: '/pages/history/index' }) + }, + + showTokenWeight(e) { + const token = e.currentTarget.dataset.token + const weight = e.currentTarget.dataset.weight + const weightNum = Number(weight || 0) + const direction = weightNum >= 0 ? '倾向垃圾判定' : '倾向正常判定' + wx.showModal({ + title: '关键词权重', + content: `关键词"${token}"\n权重贡献:${weightNum >= 0 ? '+' : ''}${weightNum.toFixed(4)}\n(${direction})`, + showCancel: false, + confirmText: '关闭' + }) } }) diff --git a/miniprogram/pages/detect/index.wxml b/miniprogram/pages/detect/index.wxml index 037462c..0f38c45 100644 --- a/miniprogram/pages/detect/index.wxml +++ b/miniprogram/pages/detect/index.wxml @@ -75,7 +75,7 @@ 风险关键词 - {{item}} + {{item.token}}