dknowc-dsh 2.3.1 → 2.4.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +11 -3
- package/cordis.patch.yml +1 -1
- package/package.json +7 -4
- package/skills/dk-trace-report/SKILL.md +288 -0
- package/skills/dk-trace-report/config/examples/doc.json +56 -0
- package/skills/dk-trace-report/config/examples/legal.json +51 -0
- package/skills/dk-trace-report/config/examples/ppt.json +47 -0
- package/skills/dk-trace-report/config/examples/tax.json +46 -0
- package/skills/dk-trace-report/config/examples/trusted.json +122 -0
- package/skills/dk-trace-report/reference/sample_input.json +60 -0
- package/skills/dk-trace-report/resources/ECHARTS-NOTICE.txt +10 -0
- package/skills/dk-trace-report/resources/echarts.custom.min.js +48 -0
- package/skills/dk-trace-report/scripts/build_report_input.py +333 -0
- package/skills/{dknowc-trusted-search → dk-trace-report}/scripts/chart_embed.py +4 -1
- package/skills/{dknowc-trusted-search → dk-trace-report}/scripts/render_trace_html.py +1071 -536
- package/skills/dk-trace-report/scripts/run_paths.py +95 -0
- package/skills/dk-trace-report/scripts/trust_digest.py +77 -0
- package/skills/dknowc-legal-assistant/README.md +95 -0
- package/skills/dknowc-legal-assistant/SKILL.md +122 -0
- package/skills/dknowc-legal-assistant/attribution.json +5 -0
- package/skills/dknowc-legal-assistant/reference/answer-and-report.md +221 -0
- package/skills/dknowc-legal-assistant/reference/capability-layer.md +143 -0
- package/skills/dknowc-legal-assistant/reference/legal_claims.example.json +96 -0
- package/skills/dknowc-legal-assistant/reference/onboarding_scripts.md +198 -0
- package/skills/dknowc-legal-assistant/reference/policy_tags.json +2002 -0
- package/skills/dknowc-legal-assistant/reference/retrieval-statute.md +81 -0
- package/skills/dknowc-legal-assistant/reference/retrieval-v2.md +133 -0
- package/skills/dknowc-legal-assistant/reference/sample_search_result.md +33 -0
- package/skills/dknowc-legal-assistant/reference/sample_trace_report.html +940 -0
- package/skills/dknowc-legal-assistant/reference/tags.md +53 -0
- package/skills/dknowc-legal-assistant/reference/troubleshooting.md +50 -0
- package/skills/dknowc-legal-assistant/reference/verification-pass.md +59 -0
- package/skills/dknowc-legal-assistant/resources/echarts.custom.min.js +48 -0
- package/skills/dknowc-legal-assistant/scripts/api_key.py +68 -0
- package/skills/dknowc-legal-assistant/scripts/attribution.py +61 -0
- package/skills/dknowc-legal-assistant/scripts/calc_legal.py +375 -0
- package/skills/dknowc-legal-assistant/scripts/calc_period.py +155 -0
- package/skills/dknowc-legal-assistant/scripts/check_answer.py +664 -0
- package/skills/dknowc-legal-assistant/scripts/initialize.py +188 -0
- package/skills/dknowc-legal-assistant/scripts/legal_client.py +208 -0
- package/skills/dknowc-legal-assistant/scripts/legal_search.py +1059 -0
- package/skills/dknowc-legal-assistant/scripts/merge_search_results.py +630 -0
- package/skills/dknowc-legal-assistant/scripts/path_guard.py +92 -0
- package/skills/dknowc-legal-assistant/scripts/question_profile.py +215 -0
- package/skills/dknowc-legal-assistant/scripts/register_key.mjs +1152 -0
- package/skills/dknowc-legal-assistant/scripts/run_eval.py +109 -0
- package/skills/dknowc-legal-assistant/scripts/source_cards.py +321 -0
- package/skills/dknowc-legal-assistant/scripts/tag_catalog.py +162 -0
- package/skills/dknowc-legal-assistant/scripts/trace_report.config.json +66 -0
- package/skills/dknowc-legal-assistant/scripts/verify_claims.py +448 -0
- package/skills/dknowc-legal-assistant/scripts/websearch_convert.py +396 -0
- package/skills/dknowc-official-doc-writer/README.md +32 -21
- package/skills/dknowc-official-doc-writer/SKILL.md +28 -15
- package/skills/dknowc-official-doc-writer/config/trace_report.config.json +56 -0
- package/skills/dknowc-official-doc-writer/reference/anti_ai_patterns.md +14 -2
- package/skills/dknowc-official-doc-writer/reference/fact_discipline.md +4 -0
- package/skills/dknowc-official-doc-writer/reference/genre_writing_notes.md +4 -0
- package/skills/dknowc-official-doc-writer/reference/initialization.md +13 -8
- package/skills/dknowc-official-doc-writer/reference/local_memory_guide.md +7 -7
- package/skills/dknowc-official-doc-writer/reference/material_usage_guidance.md +21 -0
- package/skills/dknowc-official-doc-writer/reference/materials_pipeline.md +64 -7
- package/skills/dknowc-official-doc-writer/reference/onboarding_scripts.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/outline_rules.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/output_guide.md +13 -13
- package/skills/dknowc-official-doc-writer/reference/research_notes.md +5 -3
- package/skills/dknowc-official-doc-writer/reference/review_checklist.md +6 -2
- package/skills/dknowc-official-doc-writer/reference/search_guide.md +44 -32
- package/skills/dknowc-official-doc-writer/reference/search_policy.md +63 -36
- package/skills/dknowc-official-doc-writer/reference/standards/01_report.md +3 -3
- package/skills/dknowc-official-doc-writer/reference/standards/02_qingshi.md +6 -3
- package/skills/dknowc-official-doc-writer/reference/standards/03_pifu.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/04_tongzhi.md +12 -3
- package/skills/dknowc-official-doc-writer/reference/standards/05_yijian.md +8 -2
- package/skills/dknowc-official-doc-writer/reference/standards/06_han.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/08_tongbao.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/09_tonggao.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/10_gonggao.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/11_fuhan_approve.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/12_fuhan_objection.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/13_reminder.md +3 -3
- package/skills/dknowc-official-doc-writer/reference/standards/14_generic.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/16_decision.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/18_order.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/19_gazette.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/20_motion.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/22_application.md +2 -2
- package/skills/dknowc-official-doc-writer/reference/standards/23_publicity.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/standards/25_plan.md +2 -1
- package/skills/dknowc-official-doc-writer/reference/standards/26_summary.md +7 -1
- package/skills/dknowc-official-doc-writer/reference/standards/27_speech.md +4 -1
- package/skills/dknowc-official-doc-writer/reference/standards/99_expressions.md +1 -1
- package/skills/dknowc-official-doc-writer/reference/task_router.md +4 -4
- package/skills/dknowc-official-doc-writer/reference/working_principles.md +4 -4
- package/skills/dknowc-official-doc-writer/reference/writing_method.md +166 -0
- package/skills/dknowc-official-doc-writer/reference/writing_rules.md +10 -4
- package/skills/dknowc-official-doc-writer/scripts/api_key.py +24 -25
- package/skills/dknowc-official-doc-writer/scripts/attribution.py +1 -1
- package/skills/dknowc-official-doc-writer/scripts/build_trace_json.py +205 -13
- package/skills/dknowc-official-doc-writer/scripts/check_materials.py +367 -22
- package/skills/dknowc-official-doc-writer/scripts/deep_convert.py +5 -0
- package/skills/dknowc-official-doc-writer/scripts/deep_query.py +2 -1
- package/skills/dknowc-official-doc-writer/scripts/dkag_search.py +20 -7
- package/skills/dknowc-official-doc-writer/scripts/extract_reference.py +1 -0
- package/skills/dknowc-official-doc-writer/scripts/format_document.py +88 -30
- package/skills/dknowc-official-doc-writer/scripts/initialize.py +232 -6
- package/skills/dknowc-official-doc-writer/scripts/local_memory.py +2 -6
- package/skills/dknowc-official-doc-writer/scripts/merge_search_results.py +100 -7
- package/skills/dknowc-official-doc-writer/scripts/outline_reference.py +2 -1
- package/skills/dknowc-official-doc-writer/scripts/register.mjs +265 -32
- package/skills/dknowc-official-doc-writer/scripts/review_document.py +1 -0
- package/skills/dknowc-official-doc-writer/scripts/source_note_html.py +117 -10
- package/skills/dknowc-official-doc-writer/scripts/template_generator.py +1 -0
- package/skills/dknowc-official-doc-writer/scripts/update_skill.py +339 -0
- package/skills/dknowc-official-doc-writer/scripts/websearch_convert.py +326 -0
- package/skills/dknowc-ppt-assistant/README.md +7 -9
- package/skills/dknowc-ppt-assistant/SKILL.md +28 -19
- package/skills/dknowc-ppt-assistant/config/trace_report.config.json +65 -0
- package/skills/dknowc-ppt-assistant/references/initialization.md +20 -8
- package/skills/dknowc-ppt-assistant/references/material_usage.md +46 -9
- package/skills/dknowc-ppt-assistant/references/onboarding_scripts.md +7 -5
- package/skills/dknowc-ppt-assistant/references/sample_trace_report.html +372 -920
- package/skills/dknowc-ppt-assistant/references/search_intro.md +2 -2
- package/skills/dknowc-ppt-assistant/references/svg-authoring.md +2 -2
- package/skills/dknowc-ppt-assistant/scripts/api_key.py +5 -28
- package/skills/dknowc-ppt-assistant/scripts/build_trace_input.py +188 -0
- package/skills/dknowc-ppt-assistant/scripts/initialize.py +238 -3
- package/skills/dknowc-ppt-assistant/scripts/mcp_convert.py +1 -10
- package/skills/dknowc-ppt-assistant/scripts/merge_search_results.py +604 -0
- package/skills/dknowc-ppt-assistant/scripts/preview_slide_html.py +4 -11
- package/skills/dknowc-ppt-assistant/scripts/register_key.mjs +224 -63
- package/skills/dknowc-ppt-assistant/scripts/trusted_search.py +29 -12
- package/skills/dknowc-ppt-assistant/scripts/update_skill.py +314 -0
- package/skills/dknowc-ppt-assistant/scripts/websearch_convert.py +326 -0
- package/skills/dknowc-ppt-assistant/workflows/generate-pptx.md +92 -29
- package/skills/dknowc-ppt-assistant/workflows/routing.md +1 -1
- package/skills/dknowc-tax-assistant/README.md +54 -0
- package/skills/dknowc-tax-assistant/SKILL.md +99 -0
- package/skills/dknowc-tax-assistant/attribution.json +5 -0
- package/skills/dknowc-tax-assistant/reference/answer-and-report.md +100 -0
- package/skills/dknowc-tax-assistant/reference/capability-layer.md +205 -0
- package/skills/dknowc-tax-assistant/reference/onboarding_scripts.md +68 -0
- package/skills/dknowc-tax-assistant/reference/policy_tags.json +2002 -0
- package/skills/dknowc-tax-assistant/reference/retrieval-policy.md +43 -0
- package/skills/dknowc-tax-assistant/reference/retrieval-v2.md +58 -0
- package/skills/dknowc-tax-assistant/reference/tags.md +53 -0
- package/skills/dknowc-tax-assistant/reference/tax_claims.example.json +76 -0
- package/skills/dknowc-tax-assistant/reference/trace_report.config.json +46 -0
- package/skills/dknowc-tax-assistant/reference/troubleshooting.md +70 -0
- package/skills/dknowc-tax-assistant/reference/websearch-fallback.md +59 -0
- package/skills/dknowc-tax-assistant/resources/THIRD_PARTY_NOTICES.md +9 -0
- package/skills/dknowc-tax-assistant/resources/echarts.custom.min.js +48 -0
- package/skills/dknowc-tax-assistant/scripts/api_key.py +60 -0
- package/skills/dknowc-tax-assistant/scripts/attribution.py +61 -0
- package/skills/dknowc-tax-assistant/scripts/calc_period.py +156 -0
- package/skills/dknowc-tax-assistant/scripts/calc_tax.py +179 -0
- package/skills/dknowc-tax-assistant/scripts/check_answer.py +210 -0
- package/skills/dknowc-tax-assistant/scripts/initialize.py +208 -0
- package/skills/dknowc-tax-assistant/scripts/merge_search_results.py +631 -0
- package/skills/dknowc-tax-assistant/scripts/path_guard.py +23 -0
- package/skills/dknowc-tax-assistant/scripts/question_profile.py +444 -0
- package/skills/dknowc-tax-assistant/scripts/register_key.mjs +1173 -0
- package/skills/dknowc-tax-assistant/scripts/run_dir.py +149 -0
- package/skills/dknowc-tax-assistant/scripts/source_cards.py +372 -0
- package/skills/dknowc-tax-assistant/scripts/tag_catalog.py +164 -0
- package/skills/dknowc-tax-assistant/scripts/tax_client.py +218 -0
- package/skills/dknowc-tax-assistant/scripts/tax_search.py +403 -0
- package/skills/dknowc-tax-assistant/scripts/verify_claims.py +587 -0
- package/skills/dknowc-tax-assistant/scripts/websearch_convert.py +352 -0
- package/skills/dknowc-trusted-search/README.md +25 -19
- package/skills/dknowc-trusted-search/SKILL.md +205 -73
- package/skills/dknowc-trusted-search/config/trace_report.config.json +119 -0
- package/skills/dknowc-trusted-search/reference/onboarding_scripts.md +3 -3
- package/skills/dknowc-trusted-search/reference/sample_trace_report.html +286 -191
- package/skills/dknowc-trusted-search/resources/ECHARTS-NOTICE.txt +10 -0
- package/skills/dknowc-trusted-search/scripts/api_key.py +0 -1
- package/skills/dknowc-trusted-search/scripts/attribution.py +1 -1
- package/skills/dknowc-trusted-search/scripts/build_trace_input.py +173 -0
- package/skills/dknowc-trusted-search/scripts/deep_convert.py +79 -30
- package/skills/dknowc-trusted-search/scripts/deep_query.py +22 -33
- package/skills/dknowc-trusted-search/scripts/initialize.py +343 -2
- package/skills/dknowc-trusted-search/scripts/merge_search_results.py +222 -35
- package/skills/dknowc-trusted-search/scripts/register_key.mjs +241 -29
- package/skills/dknowc-trusted-search/scripts/source_display.py +59 -0
- package/skills/dknowc-trusted-search/scripts/trusted_search.py +67 -34
- package/skills/dknowc-trusted-search/scripts/update_skill.py +300 -0
- package/skills/dknowc-trusted-search/scripts/websearch_convert.py +328 -0
- package/src/index.js +18 -2
- package/skills/dknowc-official-doc-writer/scripts/render_trace_html.py +0 -2574
- package/skills/dknowc-ppt-assistant/official-docs/output/.gitkeep +0 -0
- package/skills/dknowc-ppt-assistant/official-docs/search-results/.gitkeep +0 -0
- package/skills/dknowc-ppt-assistant/scripts/render_trace_html.py +0 -2642
- package/skills/dknowc-trusted-search/official-docs/output/.gitkeep +0 -0
- package/skills/dknowc-trusted-search/official-docs/search-results/.gitkeep +0 -0
|
@@ -0,0 +1,604 @@
|
|
|
1
|
+
#!/usr/bin/env python3
|
|
2
|
+
"""多路检索结果合并(库内多路/联网路/宿主 WebSearch 转换产物 → 渲染器统一形态)。
|
|
3
|
+
|
|
4
|
+
背景(1.4.0):复杂问题按复杂度分级可能产生多路产物——深度搜索(deep_query.py,
|
|
5
|
+
data.searches[] 形态)多路 + 可信搜索补搜(trusted_search.py,content.data.检索文章
|
|
6
|
+
形态)多路。渲染脚本 render_trace_html.py 只接受单个 input_json,此前需 Agent 手工
|
|
7
|
+
合并(实测易错)。本脚本把任意多路、跨通道产物程序化合并为渲染器可直接消费的单 JSON:
|
|
8
|
+
|
|
9
|
+
- **合并式去重**:按"规范化标题 + 源网址"双键判同一材料——
|
|
10
|
+
标题去空白/全角/开头"关于印发/关于"/结尾公文后缀词;同一材料**合并段落**(段落级
|
|
11
|
+
去重,保留各检索召回的不同段落)与**字段取并集**(保留非空字段、合并搜索条件标记);
|
|
12
|
+
不再按标题整体丢弃后篇。
|
|
13
|
+
- **深度产物转换**:deep_query 的 data.searches[].result + data.common_articles 摊平为
|
|
14
|
+
检索文章形态(子查询 query 作为默认搜索条件分组),与可信产物统一。
|
|
15
|
+
- **policyFiles 收集**:各可信产物携带的 policyFiles 合并去重(writtenText+title 双键),
|
|
16
|
+
写入合并产物 content.data.policyFiles(文号匹配数据源)。
|
|
17
|
+
- **分组标签**:每路通过 --search-key 指定该路的搜索条件(核验报告"检索分组 tabs"
|
|
18
|
+
与过程回顾按此分组);未指定时可信产物默认"本次检索"、深度产物用子查询。
|
|
19
|
+
- **显式材料编号**(1.4.0 追加):合并产物的每篇材料带 `编号` = 它在合并结果中的
|
|
20
|
+
1-based 序号;答案角标 `[n]` 的 n 就用这个编号(渲染器优先读显式编号,不再依赖
|
|
21
|
+
数组位置)。**写答案时读 `编号` 字段,不要按"某一路材料清单里的第几条"编号。**
|
|
22
|
+
|
|
23
|
+
用法:
|
|
24
|
+
python3 scripts/merge_search_results.py \
|
|
25
|
+
--input official-docs/search-results/route1.json --search-key "政策依据路" \
|
|
26
|
+
--input official-docs/search-results/route2.json --search-key "数据口径路" \
|
|
27
|
+
--output official-docs/search-results/merged_all.json
|
|
28
|
+
"""
|
|
29
|
+
|
|
30
|
+
from __future__ import annotations
|
|
31
|
+
|
|
32
|
+
import argparse
|
|
33
|
+
import json
|
|
34
|
+
import re
|
|
35
|
+
import sys
|
|
36
|
+
import unicodedata
|
|
37
|
+
from pathlib import Path
|
|
38
|
+
from typing import Any, Dict, List, Optional
|
|
39
|
+
from urllib.parse import parse_qsl, urlencode, urlparse, urlunparse
|
|
40
|
+
|
|
41
|
+
SKILL_ROOT = Path(__file__).resolve().parent.parent
|
|
42
|
+
SEARCH_RESULTS_DIR = SKILL_ROOT / "official-docs" / "search-results"
|
|
43
|
+
|
|
44
|
+
# 公文标题前后缀归一(合并去重用)
|
|
45
|
+
TITLE_PREFIX = ("关于印发", "关于", "印发", "发布", "印发《")
|
|
46
|
+
TITLE_SUFFIX = ("办法的通知", "办法", "实施细则的通知", "实施细则", "意见的通知",
|
|
47
|
+
"意见", "规定的通知", "规定", "公告", "的通知")
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def _norm_title(value: Any) -> str:
|
|
51
|
+
"""标题归一:全半角/大小写、去空白与书名号,去公文前后缀(变体判同一主体)。"""
|
|
52
|
+
text = unicodedata.normalize("NFKC", str(value or "")).lower()
|
|
53
|
+
text = re.sub(r"[《》〈〉「」『』\[\]【】()()\"'“”‘’]", "", text)
|
|
54
|
+
text = re.sub(r"\s+", "", text)
|
|
55
|
+
for prefix in TITLE_PREFIX:
|
|
56
|
+
if text.startswith(prefix):
|
|
57
|
+
text = text[len(prefix):]
|
|
58
|
+
break
|
|
59
|
+
for suffix in TITLE_SUFFIX:
|
|
60
|
+
if text.endswith(suffix):
|
|
61
|
+
text = text[: -len(suffix)]
|
|
62
|
+
break
|
|
63
|
+
return text
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def _first_value(item: Dict[str, Any], *keys: str) -> str:
|
|
67
|
+
for key in keys:
|
|
68
|
+
value = item.get(key)
|
|
69
|
+
if value not in (None, "", [], {}):
|
|
70
|
+
return str(value).strip()
|
|
71
|
+
return ""
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def _norm_url(raw: Any) -> str:
|
|
75
|
+
"""URL 归一(只用于去重键):去 fragment、常见 tracking 参数、尾斜杠。"""
|
|
76
|
+
text = str(raw or "").strip()
|
|
77
|
+
if not text:
|
|
78
|
+
return ""
|
|
79
|
+
try:
|
|
80
|
+
parsed = urlparse(text)
|
|
81
|
+
except ValueError:
|
|
82
|
+
return ""
|
|
83
|
+
if parsed.scheme.lower() not in ("http", "https") or not parsed.netloc:
|
|
84
|
+
return text
|
|
85
|
+
# 与 websearch_convert.py 的 TRACKING_PARAMS 保持完全一致(2026-11-09 审核):
|
|
86
|
+
# 两处归一化规则不一致会导致阶段二 ?source=wechat 与阶段三同文无法合并。
|
|
87
|
+
drop = {"utm_source", "utm_medium", "utm_campaign", "utm_term", "utm_content",
|
|
88
|
+
"gclid", "fbclid", "spm", "scm", "from", "share_token", "share_source",
|
|
89
|
+
"ref", "ref_src", "source", "campaign"}
|
|
90
|
+
query = [(k, v) for k, v in parse_qsl(parsed.query, keep_blank_values=True)
|
|
91
|
+
if k.lower() not in drop]
|
|
92
|
+
path = parsed.path.rstrip("/") or "/"
|
|
93
|
+
return urlunparse((parsed.scheme.lower(), parsed.netloc.lower(), path, "",
|
|
94
|
+
urlencode(sorted(query)), ""))
|
|
95
|
+
|
|
96
|
+
|
|
97
|
+
def _is_unofficial(article: Dict[str, Any]) -> bool:
|
|
98
|
+
"""非官方判定:外部联网标记或接口数据权威性=非权威。"""
|
|
99
|
+
return bool(article.get("非官方来源") or article.get("待核验") or article.get("pending")) or str(
|
|
100
|
+
article.get("数据权威性") or article.get("authority") or "").strip() in ("非权威", "non-authoritative")
|
|
101
|
+
|
|
102
|
+
|
|
103
|
+
def _dedupe_paragraphs(paragraphs: List[Dict[str, Any]]) -> List[Dict[str, Any]]:
|
|
104
|
+
"""段落级去重:**按归一化内容判重**,id 仅无语料时兜底,保留顺序。
|
|
105
|
+
|
|
106
|
+
1.4.3 修复(WB 实测暴露):原实现优先按 `id` 判重,而段落实为**篇内局部编号**
|
|
107
|
+
(同一篇材料被多路召回时,各路 id 都从 1 起编)——两路带回的段落只要 id 相同,
|
|
108
|
+
后一路的**不同内容**会被当作重复整段丢弃(静默丢条款,且结果依赖 --input 顺序)。
|
|
109
|
+
实测:杭州门诊待遇条款一度在合并环节丢失,Agent 靠调整 --input 顺序才找回。
|
|
110
|
+
改为内容主键后:多路召回的不同段落全部并集保留,与输入顺序无关、结果可复现。
|
|
111
|
+
"""
|
|
112
|
+
seen: set = set()
|
|
113
|
+
out: List[Dict[str, Any]] = []
|
|
114
|
+
for p in paragraphs:
|
|
115
|
+
if not isinstance(p, dict):
|
|
116
|
+
continue
|
|
117
|
+
pid = str(p.get("id") or "")
|
|
118
|
+
content = re.sub(r"\s+", "", str(p.get("内容") or p.get("content") or ""))
|
|
119
|
+
key = f"c:{content}" if content else (f"id:{pid}" if pid else "")
|
|
120
|
+
if key and key in seen:
|
|
121
|
+
continue
|
|
122
|
+
if key:
|
|
123
|
+
seen.add(key)
|
|
124
|
+
out.append(p)
|
|
125
|
+
return out
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def _merge_article(target: Dict[str, Any], incoming: Dict[str, Any]) -> None:
|
|
129
|
+
"""同材料合并(仅用于双方权威性质相同:都官方或都外部)。
|
|
130
|
+
|
|
131
|
+
官方 vs 外部的同 URL 合并不进入本函数——由调用方让官方记录整体胜出,
|
|
132
|
+
不吸收外部段落/元数据/通道/分组(2026-11-09 复审阻塞项)。
|
|
133
|
+
"""
|
|
134
|
+
para_key = "段落" if "段落" in target or "段落" in incoming else "paragraphs"
|
|
135
|
+
merged_paras = list(target.get(para_key) or []) + list(incoming.get(para_key) or [])
|
|
136
|
+
target[para_key] = _dedupe_paragraphs(merged_paras)
|
|
137
|
+
for key, value in incoming.items():
|
|
138
|
+
if key in (para_key, "_route_idx", "_para_contrib"):
|
|
139
|
+
continue
|
|
140
|
+
if value not in (None, "", [], {}):
|
|
141
|
+
if key not in target or target[key] in (None, "", [], {}):
|
|
142
|
+
target[key] = value
|
|
143
|
+
for _owner, _cnt in (incoming.get("_para_contrib") or {}).items():
|
|
144
|
+
if isinstance(_owner, int) and isinstance(_cnt, int) and _cnt > 0:
|
|
145
|
+
_tpc = target.setdefault("_para_contrib", {})
|
|
146
|
+
_tpc[_owner] = _tpc.get(_owner, 0) + _cnt
|
|
147
|
+
# 检索通道:不同则顿号拼接保留双通道(2026-10-14 修复——字段并集"非空优先"会让
|
|
148
|
+
# 先入的深搜标签吃掉补搜标签,单条材料的通道归属失真;与句子指纹去重处同款)
|
|
149
|
+
for ch_key in ("检索通道", "channel"):
|
|
150
|
+
cur_ch = str(target.get(ch_key) or "").strip()
|
|
151
|
+
inc_ch = str(incoming.get(ch_key) or "").strip()
|
|
152
|
+
# 按 token(顿号分隔)去重后拼接(2026-10-16 修复):原用子串判断,
|
|
153
|
+
# "深知可信搜索" 遇 "深知深度搜索、深知可信搜索" 时整串不是子串,会把已含的
|
|
154
|
+
# token 再拼一次,产生 "深知深度搜索、深知深度搜索、深知可信搜索" 这类重复值。
|
|
155
|
+
if inc_ch:
|
|
156
|
+
toks = [t.strip() for t in cur_ch.split("、") if t.strip()]
|
|
157
|
+
for t in inc_ch.split("、"):
|
|
158
|
+
t = t.strip()
|
|
159
|
+
if t and t not in toks:
|
|
160
|
+
toks.append(t)
|
|
161
|
+
merged_ch = "、".join(toks)
|
|
162
|
+
if merged_ch != cur_ch:
|
|
163
|
+
target[ch_key] = merged_ch
|
|
164
|
+
# 搜索条件(分组标签):**不拼接**,保留首路标签作为分组归属。
|
|
165
|
+
# 旧实现把两路标签拼成 "上海市落户政策、上海:人才引进申办条件" 这类组合标签,
|
|
166
|
+
# 会把 7 路撑成 10 个分组,过程回顾条的"检索 N 次"随之虚高,材料专库也出现怪分组
|
|
167
|
+
# (2026-09-28 WB 实测:实际 7 路、报告显示 10 次)。并集信息不丢,另存 搜索条件全部。
|
|
168
|
+
for key in ("搜索条件", "search_key"):
|
|
169
|
+
cur = str(target.get(key) or "").strip()
|
|
170
|
+
inc = str(incoming.get(key) or "").strip()
|
|
171
|
+
if not cur:
|
|
172
|
+
if inc:
|
|
173
|
+
target[key] = inc
|
|
174
|
+
continue
|
|
175
|
+
if inc and inc != cur:
|
|
176
|
+
all_key = f"{key}全部"
|
|
177
|
+
vals = list(target.get(all_key) or [cur])
|
|
178
|
+
if inc not in vals:
|
|
179
|
+
vals.append(inc)
|
|
180
|
+
target[all_key] = vals
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
def _to_article(item: Dict[str, Any], default_key: str = "") -> Dict[str, Any]:
|
|
184
|
+
"""把深度产物条目/可信产物条目归一为检索文章字段(渲染器 source_from_article 契约)。"""
|
|
185
|
+
article = {
|
|
186
|
+
"文章标题": _first_value(item, "文章标题", "title", "标题"),
|
|
187
|
+
"数据源": _first_value(item, "数据源", "source"),
|
|
188
|
+
"发布日期": _first_value(item, "发布日期", "date"),
|
|
189
|
+
"发布日期可信度": _first_value(item, "发布日期可信度", "date_confidence"),
|
|
190
|
+
"源网址": _first_value(item, "源网址", "url", "原文链接"),
|
|
191
|
+
"段落": item.get("段落") if isinstance(item.get("段落"), list) else
|
|
192
|
+
[{"内容": str(item.get("内容") or item.get("paragraph") or "").strip()}] if (item.get("内容") or item.get("paragraph")) else [],
|
|
193
|
+
}
|
|
194
|
+
for snap_key in ("screenShotPath", "快照链接", "snapshot"):
|
|
195
|
+
snap = _first_value(item, snap_key)
|
|
196
|
+
if snap:
|
|
197
|
+
article["screenShotPath"] = snap
|
|
198
|
+
break
|
|
199
|
+
# 文号键链(2026-10-23):「发文字号」是 V2 可信路文章级实际键名(policy 路实测返回,
|
|
200
|
+
# 如 商办消费函〔2021〕58号)——缺它则 V2 文号全靠 policyFiles 本地匹配兜底(writtenText
|
|
201
|
+
# 偶发为空,兜底不稳);`writtenText` 为深搜接口 10-23 新增文章级键(标题命中 policyFiles
|
|
202
|
+
# 才有,部分覆盖)。中文键优先、英文键垫底,全部缺键时行为不变
|
|
203
|
+
if _first_value(item, "文号", "发文字号", "doc_number", "writtenText"):
|
|
204
|
+
article["文号"] = _first_value(item, "文号", "发文字号", "doc_number", "writtenText")
|
|
205
|
+
# 检索通道透传(1.5.0):检索脚本在产物文章级打的 检索通道 标注,渲染按接口通道拆分计数
|
|
206
|
+
if _first_value(item, "检索通道", "channel"):
|
|
207
|
+
article["检索通道"] = _first_value(item, "检索通道", "channel")
|
|
208
|
+
# 非官方来源透传(1.6.0):联网路(searchChannel=webSearch)脚本打标 非官方来源=True,
|
|
209
|
+
# 及接口 dataAuthority 返回的 数据权威性(实测联网结果为"非权威")——渲染层按双口径打徽标
|
|
210
|
+
if item.get("非官方来源"):
|
|
211
|
+
article["非官方来源"] = True
|
|
212
|
+
if item.get("待核验") or item.get("pending"):
|
|
213
|
+
article["待核验"] = True
|
|
214
|
+
if _first_value(item, "数据权威性", "authority"):
|
|
215
|
+
article["数据权威性"] = _first_value(item, "数据权威性", "authority")
|
|
216
|
+
key = _first_value(item, "搜索条件", "search_key", "deep_group") or default_key
|
|
217
|
+
if key:
|
|
218
|
+
article["搜索条件"] = key
|
|
219
|
+
return article
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def _collect_route(input_path: Path, search_key: str,
|
|
223
|
+
articles: List[Dict[str, Any]], policy_files: List[Dict[str, Any]],
|
|
224
|
+
route_idx: int = -1) -> None:
|
|
225
|
+
"""处理一路输入:可信形态(content.data.检索文章)或深度形态(data.searches)。"""
|
|
226
|
+
data = json.loads(input_path.read_text(encoding="utf-8"))
|
|
227
|
+
policy_holder = data.get("content") if isinstance(data.get("content"), dict) else data
|
|
228
|
+
pf = (policy_holder.get("data") or {}).get("policyFiles") if isinstance(policy_holder.get("data"), dict) else []
|
|
229
|
+
for p in pf or []:
|
|
230
|
+
if isinstance(p, dict):
|
|
231
|
+
policy_files.append(dict(p))
|
|
232
|
+
|
|
233
|
+
# 1.5.1 起只处理通用形态(content.data.检索文章)——深度搜索原始产物先经
|
|
234
|
+
# deep_convert.py 转换(接口适配收敛在转换层);历史深搜形态由转换层前置解决
|
|
235
|
+
search_articles = (policy_holder.get("data") or {}).get("检索文章")
|
|
236
|
+
if isinstance(search_articles, list):
|
|
237
|
+
for item in search_articles:
|
|
238
|
+
if isinstance(item, dict):
|
|
239
|
+
_a = _to_article(item, search_key or "本次检索")
|
|
240
|
+
if route_idx >= 0:
|
|
241
|
+
_a["_route_idx"] = route_idx # 每路调用增量统计用(1.6.0),输出前剥离
|
|
242
|
+
articles.append(_a)
|
|
243
|
+
return
|
|
244
|
+
|
|
245
|
+
d = data.get("data") if isinstance(data.get("data"), dict) else {}
|
|
246
|
+
if isinstance(d.get("searches"), list):
|
|
247
|
+
raise SystemExit(
|
|
248
|
+
"错误:检测到深度搜索 v3 原始形态(data.searches)——merge 自 1.5.1 起只处理通用形态。"
|
|
249
|
+
"\n请先运行转换层:python3 scripts/deep_convert.py <深搜原始.json>,再把转换产物传给 --input。"
|
|
250
|
+
)
|
|
251
|
+
|
|
252
|
+
|
|
253
|
+
def main() -> None:
|
|
254
|
+
if hasattr(sys.stdout, "reconfigure"):
|
|
255
|
+
sys.stdout.reconfigure(encoding="utf-8")
|
|
256
|
+
parser = argparse.ArgumentParser(description="多路检索结果合并(可信/深度混合 → 渲染器统一形态)")
|
|
257
|
+
parser.add_argument("--input", action="append", required=True, help="输入 JSON(可多次传入,每路一个)")
|
|
258
|
+
parser.add_argument("--search-key", action="append", help="对应 --input 路次的搜索条件分组标签(可多次,缺省自动)")
|
|
259
|
+
parser.add_argument("--output", "-o", help="输出文件名,默认 official-docs/search-results/merged_all.json")
|
|
260
|
+
args = parser.parse_args()
|
|
261
|
+
|
|
262
|
+
if args.search_key and len(args.search_key) != len(args.input):
|
|
263
|
+
raise SystemExit("错误:--search-key 数量必须与 --input 一致(或缺省)。")
|
|
264
|
+
keys = args.search_key or [""] * len(args.input)
|
|
265
|
+
|
|
266
|
+
articles: List[Dict[str, Any]] = []
|
|
267
|
+
policy_files: List[Dict[str, Any]] = []
|
|
268
|
+
by_key: Dict[str, Dict[str, Any]] = {}
|
|
269
|
+
total_in = 0
|
|
270
|
+
|
|
271
|
+
for _ri, (input_path_str, key) in enumerate(zip(args.input, keys)):
|
|
272
|
+
raw_path = Path(input_path_str).expanduser()
|
|
273
|
+
if not raw_path.is_absolute():
|
|
274
|
+
raw_path = (SEARCH_RESULTS_DIR / raw_path.name).resolve()
|
|
275
|
+
if not raw_path.is_file():
|
|
276
|
+
raise SystemExit(f"错误:输入文件不存在: {raw_path}")
|
|
277
|
+
before = len(articles)
|
|
278
|
+
_collect_route(raw_path, key, articles, policy_files, route_idx=_ri)
|
|
279
|
+
total_in += len(articles) - before
|
|
280
|
+
|
|
281
|
+
# 每路调用增量统计(1.6.0,过程可信度):该路带来多少篇新材料(new_articles)、
|
|
282
|
+
# 给已有材料补充了多少段(added_paragraphs)——过程条按真实调用次数展示每条补搜的
|
|
283
|
+
# 实际贡献,避免按分组主通道统计时把补搜吞掉(对齐公文写作 3.9.0)。
|
|
284
|
+
call_stats: List[Dict[str, Any]] = [
|
|
285
|
+
{"label": (keys[i] or Path(args.input[i]).name), "file": Path(args.input[i]).name,
|
|
286
|
+
"articles_in": 0, "new_articles": 0, "added_paragraphs": 0}
|
|
287
|
+
for i in range(len(args.input))
|
|
288
|
+
]
|
|
289
|
+
|
|
290
|
+
# 合并式去重
|
|
291
|
+
for art in articles:
|
|
292
|
+
# 保留 _route_idx 到去重结束:官方整体替换/丢弃外部卡、近似重复丢弃外部卡时,
|
|
293
|
+
# 需要回滚该卡原先计入的 new_articles(2026-11-09 三验重要项)。
|
|
294
|
+
_ri = art.get("_route_idx")
|
|
295
|
+
if isinstance(_ri, int) and 0 <= _ri < len(call_stats):
|
|
296
|
+
call_stats[_ri]["articles_in"] += 1
|
|
297
|
+
title = str(art.get("文章标题") or "").strip()
|
|
298
|
+
url = str(art.get("源网址") or "").strip()
|
|
299
|
+
norm = _norm_title(title)
|
|
300
|
+
url_key = _norm_url(url) if url else ""
|
|
301
|
+
key = f"{norm}|{url_key}" if url_key else norm
|
|
302
|
+
if not key:
|
|
303
|
+
continue
|
|
304
|
+
if key in by_key:
|
|
305
|
+
target = by_key[key]
|
|
306
|
+
target_unofficial = _is_unofficial(target)
|
|
307
|
+
incoming_unofficial = _is_unofficial(art)
|
|
308
|
+
if target_unofficial != incoming_unofficial:
|
|
309
|
+
# 官方 vs 外部:官方记录整体胜出,且与 --input 顺序无关。
|
|
310
|
+
# 不吸收外部段落、数据源、发布日期、文号、快照、检索通道、分组。
|
|
311
|
+
if not incoming_unofficial:
|
|
312
|
+
# 官方后入:整卡替换先入的外部卡。外部卡原先计入其所属路次的
|
|
313
|
+
# new_articles 必须回滚,官方路次改为 +1,否则过程条会随
|
|
314
|
+
# --input 顺序给出不同的“新增 N 篇”(2026-11-09 三验重要项)。
|
|
315
|
+
_prev_owner = target.get("_route_idx")
|
|
316
|
+
if isinstance(_ri, int) and 0 <= _ri < len(call_stats):
|
|
317
|
+
if (isinstance(_prev_owner, int) and _prev_owner != _ri
|
|
318
|
+
and 0 <= _prev_owner < len(call_stats)):
|
|
319
|
+
call_stats[_prev_owner]["new_articles"] = max(
|
|
320
|
+
0, call_stats[_prev_owner]["new_articles"] - 1)
|
|
321
|
+
call_stats[_ri]["new_articles"] += 1
|
|
322
|
+
call_stats[_ri]["added_paragraphs"] += 0
|
|
323
|
+
# 外部卡此前由其他外部路合并进来的段落同样不进入官方卡,
|
|
324
|
+
# 其 added_paragraphs 贡献必须逐路回滚(2026-11-09 四验重要项)。
|
|
325
|
+
for _owner, _cnt in (target.get("_para_contrib") or {}).items():
|
|
326
|
+
if (isinstance(_owner, int) and 0 <= _owner < len(call_stats)
|
|
327
|
+
and isinstance(_cnt, int) and _cnt > 0):
|
|
328
|
+
call_stats[_owner]["added_paragraphs"] = max(
|
|
329
|
+
0, call_stats[_owner]["added_paragraphs"] - _cnt)
|
|
330
|
+
art["_para_contrib"] = {}
|
|
331
|
+
by_key[key] = art # 官方后入:替换掉先入的外部卡
|
|
332
|
+
# 官方先入:直接丢弃外部卡(该外部卡从未计入 new_articles,
|
|
333
|
+
# 也未获得过 added_paragraphs 贡献)
|
|
334
|
+
continue
|
|
335
|
+
# 双方权威性质相同(都官方或都外部)才做常规合并
|
|
336
|
+
_before = len(target.get("段落") or [])
|
|
337
|
+
_merge_article(target, art)
|
|
338
|
+
_delta = max(0, len(target.get("段落") or []) - _before)
|
|
339
|
+
if isinstance(_ri, int) and 0 <= _ri < len(call_stats):
|
|
340
|
+
call_stats[_ri]["new_articles"] += 0
|
|
341
|
+
call_stats[_ri]["added_paragraphs"] += _delta
|
|
342
|
+
if _delta > 0:
|
|
343
|
+
_pc = target.setdefault("_para_contrib", {})
|
|
344
|
+
_pc[_ri] = _pc.get(_ri, 0) + _delta
|
|
345
|
+
else:
|
|
346
|
+
art.setdefault("_para_contrib", {})
|
|
347
|
+
by_key[key] = art
|
|
348
|
+
if isinstance(_ri, int) and 0 <= _ri < len(call_stats):
|
|
349
|
+
call_stats[_ri]["new_articles"] += 1
|
|
350
|
+
merged = list(by_key.values())
|
|
351
|
+
|
|
352
|
+
# —— 近似重复去重(句子指纹,1.5.0)——
|
|
353
|
+
# 搜索接口对同一篇文章常返回多个标题变体条目,上面的"标题+URL"双键去重会漏掉,
|
|
354
|
+
# 表现为知识专库中同一内容出现多张卡:段落完全一致、或分段切法不同顺序略异。
|
|
355
|
+
# 按"句子集合"二次判定:一篇的句子被另一篇覆盖过半(交集≥4句且 inter/min≥0.55)
|
|
356
|
+
# 即判同一材料,保留更完整的一篇(相当时优先发文版标题),段落取并集。
|
|
357
|
+
def _norm_sent(s) -> str:
|
|
358
|
+
return re.sub(r"[\s,,、。;;::!!??「」『』“”\"'()()【】·—\[\]-]", "", str(s or ""))
|
|
359
|
+
|
|
360
|
+
def _sentence_fp(article: dict) -> set:
|
|
361
|
+
sents = set()
|
|
362
|
+
for p in (article.get("段落") or []):
|
|
363
|
+
if not isinstance(p, dict):
|
|
364
|
+
continue
|
|
365
|
+
for s in re.split(r"[。;;!??]", str(p.get("内容") or p.get("content") or "")):
|
|
366
|
+
ns = _norm_sent(s)
|
|
367
|
+
if len(ns) >= 12:
|
|
368
|
+
sents.add(ns)
|
|
369
|
+
return sents
|
|
370
|
+
|
|
371
|
+
def _is_official_title(t: str) -> bool:
|
|
372
|
+
t = t or ""
|
|
373
|
+
return ("关于印发" in t) or t.endswith("的通知") or t.endswith("的公告") or t.endswith("的意见")
|
|
374
|
+
|
|
375
|
+
_fps = [_sentence_fp(a) for a in merged]
|
|
376
|
+
_i = 0
|
|
377
|
+
_near_dup = 0
|
|
378
|
+
while _i < len(merged):
|
|
379
|
+
_j = _i + 1
|
|
380
|
+
while _j < len(merged):
|
|
381
|
+
si, sj = _fps[_i], _fps[_j]
|
|
382
|
+
inter = len(si & sj) if (si and sj) else 0
|
|
383
|
+
if inter >= 4 and inter / min(len(si), len(sj)) >= 0.55:
|
|
384
|
+
# 权威优先:官方材料 > 外部联网材料(外部近似重复不得吞掉官方卡片)
|
|
385
|
+
_i_unofficial = _is_unofficial(merged[_i])
|
|
386
|
+
_j_unofficial = _is_unofficial(merged[_j])
|
|
387
|
+
if _i_unofficial != _j_unofficial:
|
|
388
|
+
keep_idx = _j if _i_unofficial else _i
|
|
389
|
+
elif len(si) != len(sj):
|
|
390
|
+
keep_idx = _i if len(si) > len(sj) else _j
|
|
391
|
+
else:
|
|
392
|
+
ti = merged[_i].get("文章标题") or ""
|
|
393
|
+
tj = merged[_j].get("文章标题") or ""
|
|
394
|
+
keep_idx = _i if (_is_official_title(ti) or not _is_official_title(tj)) else _j
|
|
395
|
+
drop_idx = _j if keep_idx == _i else _i
|
|
396
|
+
keep, drop = merged[keep_idx], merged[drop_idx]
|
|
397
|
+
keep_unofficial = _is_unofficial(keep)
|
|
398
|
+
drop_unofficial = _is_unofficial(drop)
|
|
399
|
+
if keep_unofficial != drop_unofficial:
|
|
400
|
+
# 官方 vs 外部近似重复:官方整体胜出,外部卡直接丢弃,
|
|
401
|
+
# 不吸收外部段落/数据源/日期/文号/快照/通道/分组。
|
|
402
|
+
# 被丢弃卡原先计入其所属路次的 new_articles 同步回滚,
|
|
403
|
+
# 保证过程条“新增 N 篇”与最终材料池一致。
|
|
404
|
+
_drop_owner = drop.get("_route_idx")
|
|
405
|
+
if (isinstance(_drop_owner, int) and 0 <= _drop_owner < len(call_stats)):
|
|
406
|
+
call_stats[_drop_owner]["new_articles"] = max(
|
|
407
|
+
0, call_stats[_drop_owner]["new_articles"] - 1)
|
|
408
|
+
# 被丢弃卡此前由其他路合并进来的段落也不进入保留卡,
|
|
409
|
+
# 其 added_paragraphs 贡献逐路回滚(2026-11-09 四验重要项)。
|
|
410
|
+
for _owner, _cnt in (drop.get("_para_contrib") or {}).items():
|
|
411
|
+
if (isinstance(_owner, int) and 0 <= _owner < len(call_stats)
|
|
412
|
+
and isinstance(_cnt, int) and _cnt > 0):
|
|
413
|
+
call_stats[_owner]["added_paragraphs"] = max(
|
|
414
|
+
0, call_stats[_owner]["added_paragraphs"] - _cnt)
|
|
415
|
+
merged.pop(drop_idx)
|
|
416
|
+
_fps.pop(drop_idx)
|
|
417
|
+
_near_dup += 1
|
|
418
|
+
if drop_idx == _i:
|
|
419
|
+
_j = _i + 1
|
|
420
|
+
continue
|
|
421
|
+
# 双方权威性质相同:段落取并集(内容级去重;后续统一编号)
|
|
422
|
+
# 被并卡不再是独立材料,其所属路次已计入的 new_articles 同步回滚 -1;
|
|
423
|
+
# 否则同名分组汇总时会出现“召回 1 条 / 新增 2 篇”(2026-11-09 六验重要项)。
|
|
424
|
+
_drop_owner = drop.get("_route_idx")
|
|
425
|
+
if (isinstance(_drop_owner, int) and 0 <= _drop_owner < len(call_stats)):
|
|
426
|
+
call_stats[_drop_owner]["new_articles"] = max(
|
|
427
|
+
0, call_stats[_drop_owner]["new_articles"] - 1)
|
|
428
|
+
_seen_para = {re.sub(r"\s+", "", str(p.get("内容") or p.get("content") or ""))
|
|
429
|
+
for p in (keep.get("段落") or [])}
|
|
430
|
+
for p in (drop.get("段落") or []):
|
|
431
|
+
pc = re.sub(r"\s+", "", str(p.get("内容") or p.get("content") or ""))
|
|
432
|
+
if pc and pc not in _seen_para:
|
|
433
|
+
_seen_para.add(pc)
|
|
434
|
+
keep.setdefault("段落", []).append(p)
|
|
435
|
+
# 字段取并集:保留篇空字段用被并篇补;检索通道不同则拼接保留双通道
|
|
436
|
+
for f, v in drop.items():
|
|
437
|
+
if f in ("段落", "_route_idx", "_para_contrib"):
|
|
438
|
+
continue
|
|
439
|
+
if not keep.get(f) and v:
|
|
440
|
+
keep[f] = v
|
|
441
|
+
# 双方同为官方/外部:被并卡的段落贡献随内容一起进入保留卡,
|
|
442
|
+
# 贡献记录同步合并(后续保留卡若被官方丢弃,才能完整回滚)。
|
|
443
|
+
for _owner, _cnt in (drop.get("_para_contrib") or {}).items():
|
|
444
|
+
if isinstance(_owner, int) and isinstance(_cnt, int) and _cnt > 0:
|
|
445
|
+
_kpc = keep.setdefault("_para_contrib", {})
|
|
446
|
+
_kpc[_owner] = _kpc.get(_owner, 0) + _cnt
|
|
447
|
+
# 通道拼接同款 token 去重(2026-10-16,与字段并集处同口径)
|
|
448
|
+
_inc = (drop.get("检索通道") or "").strip()
|
|
449
|
+
if _inc:
|
|
450
|
+
_toks = [t.strip() for t in (keep.get("检索通道") or "").split("、") if t.strip()]
|
|
451
|
+
for _t in _inc.split("、"):
|
|
452
|
+
_t = _t.strip()
|
|
453
|
+
if _t and _t not in _toks:
|
|
454
|
+
_toks.append(_t)
|
|
455
|
+
_merged = "、".join(_toks)
|
|
456
|
+
if _merged != (keep.get("检索通道") or ""):
|
|
457
|
+
keep["检索通道"] = _merged
|
|
458
|
+
merged.pop(drop_idx)
|
|
459
|
+
_fps.pop(drop_idx)
|
|
460
|
+
_near_dup += 1
|
|
461
|
+
if drop_idx == _i:
|
|
462
|
+
_j = _i + 1 # _i 处已换成保留篇,重新向后比较
|
|
463
|
+
continue
|
|
464
|
+
_j += 1
|
|
465
|
+
_i += 1
|
|
466
|
+
|
|
467
|
+
# —— 段内污染段去重(1.5.0,修"一张卡里两段内容一样")——
|
|
468
|
+
# ① 内容完全一致 → 保留先出现的;
|
|
469
|
+
# ② "标题链拼进正文"的污染段——接口偶发把段落标题/上级标题文字拼进内容开头,
|
|
470
|
+
# 形成 内容=标题前缀+另一段内容 的超集段(渲染剥前缀后两段看起来完全一样)。
|
|
471
|
+
# 判定:归一化后一段是另一段的前缀或后缀超集(短段≥30字)→ 丢弃污染长段、保留干净段。
|
|
472
|
+
for article in merged:
|
|
473
|
+
paras = article.get("段落") or []
|
|
474
|
+
texts = [re.sub(r"\s+", "", str(p.get("内容") or p.get("content") or "")) for p in paras]
|
|
475
|
+
drop_set = set()
|
|
476
|
+
for a in range(len(paras)):
|
|
477
|
+
ta = texts[a]
|
|
478
|
+
if len(ta) < 30 or a in drop_set:
|
|
479
|
+
continue
|
|
480
|
+
for b in range(len(paras)):
|
|
481
|
+
tb = texts[b]
|
|
482
|
+
if a == b or b in drop_set or len(tb) < 30:
|
|
483
|
+
continue
|
|
484
|
+
if ta == tb:
|
|
485
|
+
if a < b:
|
|
486
|
+
drop_set.add(b) # 完全一致:保留先出现的
|
|
487
|
+
elif tb.startswith(ta) or tb.endswith(ta):
|
|
488
|
+
drop_set.add(b) # b 是带标题前缀的污染超集段
|
|
489
|
+
elif ta.startswith(tb) or ta.endswith(tb):
|
|
490
|
+
drop_set.add(a) # a 才是污染段
|
|
491
|
+
if drop_set:
|
|
492
|
+
article["段落"] = [p for i, p in enumerate(paras) if i not in drop_set]
|
|
493
|
+
|
|
494
|
+
# 内部路次标记与段落贡献记录不进入产物(去重/统计结束后统一剥离)
|
|
495
|
+
for _art in merged:
|
|
496
|
+
_art.pop("_route_idx", None)
|
|
497
|
+
_art.pop("_para_contrib", None)
|
|
498
|
+
|
|
499
|
+
# 显式材料编号(1.4.0):给每篇材料盖上 `编号` = 它在合并产物中的 1-based 序号。
|
|
500
|
+
# 渲染器优先读材料上的显式编号(render_trace_html.source_from_article 的 explicit_id),
|
|
501
|
+
# 答案角标 [n] 因此锚定在一个"读得到"的编号上,不必让模型去数数组位置——
|
|
502
|
+
# 多路合并后总序号与各路内部的相对编号不同(实测:WorkBuddy 需反复确认才敢写),
|
|
503
|
+
# 数错会静默绑到另一份材料且核验单不会报警,故改为显式锚点。
|
|
504
|
+
for _idx, _art in enumerate(merged, start=1):
|
|
505
|
+
_art["编号"] = _idx
|
|
506
|
+
|
|
507
|
+
# policyFiles 收集去重(writtenText+title 双键)
|
|
508
|
+
pf_seen: set = set()
|
|
509
|
+
pf_out: List[Dict[str, Any]] = []
|
|
510
|
+
for p in policy_files:
|
|
511
|
+
w = str(p.get("writtenText") or p.get("发文字号") or "").strip()
|
|
512
|
+
t = str(p.get("title") or p.get("标题") or "").strip()
|
|
513
|
+
if not w and not t:
|
|
514
|
+
continue
|
|
515
|
+
pf_key = f"{w}|{t}"
|
|
516
|
+
if pf_key in pf_seen:
|
|
517
|
+
continue
|
|
518
|
+
pf_seen.add(pf_key)
|
|
519
|
+
pf_out.append(p)
|
|
520
|
+
|
|
521
|
+
out_name = args.output or "merged_all.json"
|
|
522
|
+
out = Path(out_name).expanduser()
|
|
523
|
+
if not out.is_absolute():
|
|
524
|
+
out = (SEARCH_RESULTS_DIR / out.name).resolve()
|
|
525
|
+
try:
|
|
526
|
+
out.relative_to(SEARCH_RESULTS_DIR.resolve())
|
|
527
|
+
except ValueError:
|
|
528
|
+
raise SystemExit(f"错误:输出文件必须位于 {SEARCH_RESULTS_DIR}") from None
|
|
529
|
+
|
|
530
|
+
result = {
|
|
531
|
+
"search_meta": {"merged": True, "input_routes": len(args.input), "merged_from": [str(Path(x).name) for x in args.input]},
|
|
532
|
+
"content": {"data": {"检索文章": merged, "policyFiles": pf_out}},
|
|
533
|
+
}
|
|
534
|
+
|
|
535
|
+
# 材料池清单(1.4.3 证据侧覆盖):写答案大纲前先扫描——识别与用户情形相关、
|
|
536
|
+
# 但问题字面未覆盖的主题。清单 = 机构分布 + 文号 + 涉及金额 + 全量标题,
|
|
537
|
+
# 同时写入 search_meta.pool_summary(供程序复用)并打印到 stdout(供 Agent 扫描)。
|
|
538
|
+
def _art_org(a: Dict[str, Any]) -> str:
|
|
539
|
+
return str(a.get("数据源") or a.get("发布或实施机构") or "").strip() or "(未标注机构)"
|
|
540
|
+
|
|
541
|
+
org_counts: Dict[str, int] = {}
|
|
542
|
+
for a in merged:
|
|
543
|
+
key = _art_org(a)[:24]
|
|
544
|
+
org_counts[key] = org_counts.get(key, 0) + 1
|
|
545
|
+
doc_numbers: List[str] = []
|
|
546
|
+
for p in pf_out:
|
|
547
|
+
w = str(p.get("writtenText") or "").strip()
|
|
548
|
+
if w and w not in doc_numbers:
|
|
549
|
+
doc_numbers.append(w)
|
|
550
|
+
amounts: List[str] = []
|
|
551
|
+
for a in merged:
|
|
552
|
+
for seg in (a.get("段落") or []):
|
|
553
|
+
if not isinstance(seg, dict):
|
|
554
|
+
continue
|
|
555
|
+
for m2 in re.finditer(r"\d+(?:\.\d+)?(?:\s*[万亿])?元", str(seg.get("内容") or "")):
|
|
556
|
+
v = m2.group(0)
|
|
557
|
+
if v not in amounts:
|
|
558
|
+
amounts.append(v)
|
|
559
|
+
if len(amounts) >= 30:
|
|
560
|
+
break
|
|
561
|
+
pool_summary = {
|
|
562
|
+
"articles": len(merged),
|
|
563
|
+
"orgs": dict(sorted(org_counts.items(), key=lambda kv: -kv[1])[:20]),
|
|
564
|
+
"doc_numbers": doc_numbers[:40],
|
|
565
|
+
"amounts": amounts[:30],
|
|
566
|
+
"titles": [
|
|
567
|
+
{"编号": a.get("编号", i + 1), "标题": str(a.get("文章标题") or "")[:60],
|
|
568
|
+
"机构": _art_org(a)[:24], "日期": str(a.get("发布日期") or "")[:10],
|
|
569
|
+
# 摘要 = 首个非空段落前 60 字:主题藏在正文里(标题看不出)的材料同样能被扫到
|
|
570
|
+
"摘要": next((re.sub(r"\s+", " ", str(seg.get("内容") or "")).strip()[:60]
|
|
571
|
+
for seg in (a.get("段落") or [])
|
|
572
|
+
if isinstance(seg, dict) and str(seg.get("内容") or "").strip()), "")}
|
|
573
|
+
for i, a in enumerate(merged)
|
|
574
|
+
],
|
|
575
|
+
}
|
|
576
|
+
result["search_meta"]["pool_summary"] = pool_summary
|
|
577
|
+
# 每路调用增量(1.6.0):渲染过程条按真实调用次数展示每条补搜的实际贡献
|
|
578
|
+
result["search_meta"]["calls"] = call_stats
|
|
579
|
+
|
|
580
|
+
# official-docs/ 不随发布包携带(平台不接受 .gitkeep,目录由脚本运行期自动创建)
|
|
581
|
+
out.parent.mkdir(parents=True, exist_ok=True)
|
|
582
|
+
out.write_text(json.dumps(result, ensure_ascii=False, indent=2), encoding="utf-8")
|
|
583
|
+
print(f"✓ 合并完成:{len(args.input)} 路 / 输入 {total_in} 篇 → 合并去重后 {len(merged)} 篇"
|
|
584
|
+
f"(policyFiles {len(pf_out)} 条)→ {out}")
|
|
585
|
+
print(" 合并产物与 trusted_search.py 单路输出同构,可直接作为 render_trace_html.py 的 input_json。")
|
|
586
|
+
print("── 每路调用增量(过程可信度:该路新增 N 篇 · 为已有材料补充 K 段)──")
|
|
587
|
+
for _c in call_stats:
|
|
588
|
+
print(f" · {_c['label']}:输入 {_c['articles_in']} 篇 → 新增 {_c['new_articles']} 篇 · 补充 {_c['added_paragraphs']} 段")
|
|
589
|
+
print("── 材料池清单(证据侧覆盖:写答案大纲前先扫描,识别与用户情形相关、但问题字面未覆盖的主题)──")
|
|
590
|
+
print(" 机构分布:" + ";".join(f"{k}×{v}" for k, v in list(pool_summary["orgs"].items())[:12]))
|
|
591
|
+
if doc_numbers:
|
|
592
|
+
print(f" 文号 {len(doc_numbers)} 个:{'、'.join(doc_numbers[:12])}{'…' if len(doc_numbers) > 12 else ''}")
|
|
593
|
+
if amounts:
|
|
594
|
+
print(" 涉及金额样例:" + "、".join(amounts[:15]))
|
|
595
|
+
print(f" 标题清单({len(merged)} 篇;含首段摘要,便于识别标题看不出的主题):")
|
|
596
|
+
for t in pool_summary["titles"]:
|
|
597
|
+
line = f" {t['编号']:>3}. {t['标题']}|{t['机构']}|{t['日期']}"
|
|
598
|
+
if t.get("摘要"):
|
|
599
|
+
line += f"|{t['摘要']}"
|
|
600
|
+
print(line)
|
|
601
|
+
|
|
602
|
+
|
|
603
|
+
if __name__ == "__main__":
|
|
604
|
+
main()
|
|
@@ -9,7 +9,7 @@ svg_output/ 下全部(或指定)页面以 data URI 内嵌到单个 HTML—
|
|
|
9
9
|
用法:
|
|
10
10
|
python3 scripts/preview_slide_html.py <项目名> [--output <文件名>] [--title <标题>]
|
|
11
11
|
项目须位于 projects/<项目名>/svg_output/;输出默认 official-docs/output/<项目名>_pages_preview.html,
|
|
12
|
-
|
|
12
|
+
生成后直接向用户展示该绝对路径(dsh 会话工作区即文件视图,无需交付复制)。
|
|
13
13
|
"""
|
|
14
14
|
|
|
15
15
|
from __future__ import annotations
|
|
@@ -22,15 +22,8 @@ import sys
|
|
|
22
22
|
from pathlib import Path
|
|
23
23
|
|
|
24
24
|
SKILL_ROOT = Path(__file__).resolve().parent.parent
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
_ws = _os.environ.get("DKNWOC_WS_ROOT")
|
|
28
|
-
if not _ws:
|
|
29
|
-
_sid = _os.environ.get("DSH_SESSION_ID", "")
|
|
30
|
-
_ws = str(Path(_os.getcwd()) / "dknowc-output" / (_sid[:8] if _sid else "_default"))
|
|
31
|
-
WS_ROOT = Path(_ws).resolve()
|
|
32
|
-
PROJECTS_DIR = Path(_os.getcwd()).resolve() / "dknowc-projects"
|
|
33
|
-
OUTPUT_DIR = WS_ROOT / "official-docs" / "output"
|
|
25
|
+
PROJECTS_DIR = SKILL_ROOT / "projects"
|
|
26
|
+
OUTPUT_DIR = SKILL_ROOT / "official-docs" / "output"
|
|
34
27
|
|
|
35
28
|
|
|
36
29
|
def esc(value: str) -> str:
|
|
@@ -92,7 +85,7 @@ def main() -> int:
|
|
|
92
85
|
out.parent.mkdir(parents=True, exist_ok=True)
|
|
93
86
|
out.write_text(_page_html(title, len(svgs), "\n".join(sections)), encoding="utf-8")
|
|
94
87
|
print(f"已生成预览页({len(svgs)} 页):{out}")
|
|
95
|
-
print("
|
|
88
|
+
print("提示:dsh 下产物已在会话工作区,直接向用户展示上述绝对路径即可。")
|
|
96
89
|
return 0
|
|
97
90
|
|
|
98
91
|
|