From 62c870247854be5c6ce499d83a1d481e08d01ee4 Mon Sep 17 00:00:00 2001 From: ThearchyHelios Date: Wed, 4 Nov 2026 09:56:42 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B7=A8=E8=BD=AE=E9=87=8D=E5=A4=8D=E7=9A=84?= =?UTF-8?q?=E6=9C=AA=E5=86=B3=E9=97=AE=E9=A2=98=E5=8F=AA=E7=95=99=E4=B8=80?= =?UTF-8?q?=E6=9D=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 十轮起草每轮都会重新问一遍同一件事,68 条问题里有一半是重复。按第一个分句去重:问的是 同一件事就只留先出现的那一句,措辞不同的仍然留下,交给人判断。 --- backend/app/api/ledger.py | 35 ++++++++++++++++++++++- backend/tests/test_ledger_docdraft_api.py | 9 ++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/backend/app/api/ledger.py b/backend/app/api/ledger.py index cb2e9a0a..96debf77 100644 --- a/backend/app/api/ledger.py +++ b/backend/app/api/ledger.py @@ -666,6 +666,39 @@ def _entry_notes(blocks: list[dict[str, Any]]) -> list[str]: return draftplan.merge_entry_fields(blocks)[1] +#: 问题去重时取第一句多长:同一件事每轮换个说法,前面那句「问的是什么」通常一样,后面才是补充 +_QUESTION_KEY = 30 +_CLAUSE = ",,。;;::!!??" +_PUNCT = set(" \t\r\n,。;:、!?,.!?;:'\"()()[]【】《》<>-_/\\|`~") + + +def _question_key(text: str) -> str: + """一句话的「问的是什么」:取第一个分句(问的是同一件事,后面常跟着各轮的补充说明)。""" + head = text + for mark in _CLAUSE: + at = head.find(mark) + if at > 0: + head = head[:at] + return "".join(ch for ch in head if ch not in _PUNCT)[:_QUESTION_KEY] + + +def _merge_questions(kept: list[str], incoming: list[str]) -> list[str]: + """跨轮合并未决问题:同一件事每轮都会重新问一遍(68 条里有一半是重复),按归一化后的前 24 个字去重, + 保留先出现的那一句。措辞不同但意思一样的仍会留下——机器分不出来,交给人决定要不要回。""" + seen = {_question_key(q) for q in kept} + out = list(kept) + for item in incoming: + text = item.strip() if isinstance(item, str) else "" + if not text: + continue + key = _question_key(text) + if key in seen: + continue + seen.add(key) + out.append(text) + return out + + def _fresh_metrics(metrics: list[dict[str, Any]], covered: list[str]) -> list[dict[str, Any]]: """这一轮里还没起草过的指标,按名字认;同一轮重复交同名指标的只留第一条。 @@ -799,7 +832,7 @@ async def _extract_in_background(draft_id: str) -> None: blocks.append({"sql": plan["sql"], "entry_fields": plan["entry_fields"], "metrics": fresh}) covered += [m["name"] for m in fresh if m.get("name")] problems += round_problems - questions += plan["open_questions"] + questions = _merge_questions(questions, plan["open_questions"]) uncovered = list(result.get("uncovered") or []) diagnostics = result.get("diagnostics") or diagnostics doc_truncated = doc_truncated or bool(result.get("doc_truncated")) diff --git a/backend/tests/test_ledger_docdraft_api.py b/backend/tests/test_ledger_docdraft_api.py index 95f570eb..d2b715d0 100644 --- a/backend/tests/test_ledger_docdraft_api.py +++ b/backend/tests/test_ledger_docdraft_api.py @@ -682,3 +682,12 @@ def test_the_schema_text_expands_the_tables_the_document_names_first(): picked = api_ledger._focus_tables(tables, "指标取自 kpi_info,属性在 attr_info 里") assert picked[:2] == ["kpi_info", "attr_info"] assert set(picked) >= {"kpi_info", "attr_info"} + + +def test_questions_asked_again_next_round_are_deduped(): + """同一件事每轮换个说法再问一遍:只留先出现的那一句,别在页面上堆几十条重复问题。""" + first = ["文档说明比率类指标可被「配置 → 计算口径」覆盖,要不要读配置?"] + again = ["文档说明比率类指标可被「配置 → 计算口径」覆盖,本模板按内置口径起草,是否要读?", + "报废模架数需要跨表,关联键未确认"] + merged = api_ledger._merge_questions(first, again) + assert merged == [first[0], "报废模架数需要跨表,关联键未确认"]