build_creation_pattern_layered.py 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 构建创作思维路径可视化 - 分层版本
  5. 基于原版样式,改为从上到下分层布局,去掉动画
  6. """
  7. import json
  8. import sys
  9. from pathlib import Path
  10. # 项目路径
  11. project_root = Path(__file__).parent.parent.parent
  12. sys.path.insert(0, str(project_root))
  13. from script.data_processing.path_config import PathConfig
  14. HTML_TEMPLATE = '''<!DOCTYPE html>
  15. <html lang="zh-CN">
  16. <head>
  17. <meta charset="UTF-8">
  18. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  19. <title>创作思维路径可视化</title>
  20. <script src="https://d3js.org/d3.v7.min.js"></script>
  21. <style>
  22. * { margin: 0; padding: 0; box-sizing: border-box; }
  23. body {
  24. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  25. background: #1a1a2e;
  26. color: #eee;
  27. overflow: hidden;
  28. }
  29. .container { display: flex; flex-direction: column; height: 100vh; }
  30. header {
  31. padding: 12px 20px;
  32. background: #16213e;
  33. border-bottom: 1px solid #0f3460;
  34. display: flex;
  35. align-items: center;
  36. gap: 20px;
  37. }
  38. h1 { font-size: 16px; font-weight: 500; color: #e94560; }
  39. .legend { display: flex; gap: 16px; font-size: 12px; color: #888; margin-left: auto; }
  40. .legend-item { display: flex; align-items: center; gap: 4px; }
  41. .legend-dot { width: 10px; height: 10px; border-radius: 50%; }
  42. .legend-line { width: 20px; height: 2px; }
  43. /* 主内容区:左右布局 */
  44. main { flex: 1; display: flex; overflow: hidden; }
  45. .graph-area { flex: 1; position: relative; overflow: auto; }
  46. .graph-area svg { display: block; }
  47. /* 右侧面板 */
  48. .right-panel {
  49. width: 360px;
  50. background: #16213e;
  51. border-left: 1px solid #0f3460;
  52. display: flex;
  53. flex-direction: column;
  54. overflow: hidden;
  55. }
  56. /* 帖子摘要区 */
  57. .post-summary {
  58. padding: 16px;
  59. border-bottom: 1px solid #0f3460;
  60. max-height: 200px;
  61. overflow-y: auto;
  62. }
  63. .post-title {
  64. font-size: 14px;
  65. font-weight: 600;
  66. color: #e94560;
  67. margin-bottom: 8px;
  68. line-height: 1.4;
  69. }
  70. .post-body {
  71. font-size: 12px;
  72. color: #aaa;
  73. line-height: 1.6;
  74. max-height: 100px;
  75. overflow-y: auto;
  76. }
  77. .post-meta {
  78. margin-top: 8px;
  79. font-size: 11px;
  80. color: #666;
  81. display: flex;
  82. gap: 12px;
  83. }
  84. /* 步骤时间轴 */
  85. .timeline {
  86. flex: 1;
  87. overflow-y: auto;
  88. padding: 16px;
  89. }
  90. .timeline-title {
  91. font-size: 13px;
  92. font-weight: 600;
  93. color: #888;
  94. margin-bottom: 12px;
  95. }
  96. .timeline-item {
  97. position: relative;
  98. padding-left: 24px;
  99. padding-bottom: 16px;
  100. border-left: 2px solid #0f3460;
  101. margin-left: 8px;
  102. cursor: pointer;
  103. transition: all 0.2s;
  104. }
  105. .timeline-item:hover {
  106. background: rgba(233, 69, 96, 0.1);
  107. margin-left: 6px;
  108. padding-left: 26px;
  109. }
  110. .timeline-item:last-child { border-left-color: transparent; }
  111. .timeline-item.active { border-left-color: #e94560; }
  112. .timeline-dot {
  113. position: absolute;
  114. left: -7px;
  115. top: 0;
  116. width: 12px;
  117. height: 12px;
  118. border-radius: 50%;
  119. border: 2px solid #0f3460;
  120. background: #1a1a2e;
  121. display: flex;
  122. align-items: center;
  123. justify-content: center;
  124. font-size: 8px;
  125. font-weight: bold;
  126. color: white;
  127. }
  128. .timeline-item.active .timeline-dot {
  129. border-color: #e94560;
  130. background: #e94560;
  131. }
  132. .timeline-node-name {
  133. font-size: 13px;
  134. font-weight: 500;
  135. color: #eee;
  136. margin-bottom: 4px;
  137. }
  138. .timeline-node-dim {
  139. font-size: 11px;
  140. padding: 2px 6px;
  141. border-radius: 3px;
  142. display: inline-block;
  143. margin-bottom: 6px;
  144. }
  145. .timeline-node-dim.灵感点 { background: rgba(255,107,107,0.2); color: #ff6b6b; }
  146. .timeline-node-dim.目的点 { background: rgba(78,205,196,0.2); color: #4ecdc4; }
  147. .timeline-node-dim.关键点 { background: rgba(255,230,109,0.2); color: #ffe66d; }
  148. .timeline-reason {
  149. font-size: 11px;
  150. color: #888;
  151. line-height: 1.5;
  152. background: #0f3460;
  153. padding: 8px;
  154. border-radius: 4px;
  155. }
  156. .timeline-from {
  157. font-size: 11px;
  158. color: #666;
  159. margin-bottom: 4px;
  160. }
  161. .timeline-from span { color: #e94560; }
  162. /* 节点样式 */
  163. .node { cursor: pointer; }
  164. .node circle { stroke-width: 2px; transition: all 0.3s; }
  165. .node.highlight circle { stroke-width: 4px; filter: drop-shadow(0 0 12px currentColor); }
  166. .node text { font-size: 11px; fill: #ccc; text-anchor: middle; pointer-events: none; }
  167. .node .order-badge { font-size: 10px; font-weight: bold; fill: white; }
  168. .link { stroke-opacity: 0.6; transition: all 0.3s; }
  169. .link.highlight { stroke-opacity: 1; stroke-width: 3px; }
  170. .link-label { font-size: 9px; fill: #888; pointer-events: none; }
  171. /* 层级标签 */
  172. .layer-label {
  173. font-size: 11px;
  174. fill: #555;
  175. }
  176. </style>
  177. </head>
  178. <body>
  179. <div class="container">
  180. <header>
  181. <h1>创作思维路径</h1>
  182. <div class="legend">
  183. <div class="legend-item"><span class="legend-dot" style="background: #ff6b6b;"></span><span>灵感点</span></div>
  184. <div class="legend-item"><span class="legend-dot" style="background: #4ecdc4;"></span><span>目的点</span></div>
  185. <div class="legend-item"><span class="legend-dot" style="background: #ffe66d;"></span><span>关键点</span></div>
  186. <div class="legend-item"><span class="legend-line" style="background: #e94560;"></span><span>AI推导</span></div>
  187. </div>
  188. </header>
  189. <main>
  190. <div class="graph-area">
  191. <svg id="graph"></svg>
  192. </div>
  193. <div class="right-panel">
  194. <div class="post-summary" id="postSummary"></div>
  195. <div class="timeline" id="timeline">
  196. <div class="timeline-title">推导过程</div>
  197. </div>
  198. </div>
  199. </main>
  200. </div>
  201. <script>
  202. const DATA = __DATA_PLACEHOLDER__;
  203. const dimColors = { '灵感点': '#ff6b6b', '目的点': '#4ecdc4', '关键点': '#ffe66d' };
  204. // 提取数据
  205. const postDetail = DATA['帖子详情'];
  206. const steps = DATA['步骤列表'];
  207. const lastStep = steps[steps.length - 1];
  208. const nodes = lastStep['输出']['节点列表'];
  209. const edges = lastStep['输出']['边列表'].filter(e => e['关系类型'] === 'AI推导');
  210. // 构建边的查找表(目标节点 -> 边)
  211. const edgeByTarget = {};
  212. edges.forEach(e => { edgeByTarget[e['目标']] = e; });
  213. const nodeById = {};
  214. const graphNodes = nodes.filter(n => n['发现编号'] !== undefined && n['发现编号'] !== null).map(n => {
  215. const node = {
  216. id: n['节点ID'],
  217. name: n['节点名称'],
  218. dimension: n['节点维度'],
  219. category: n['节点分类'],
  220. description: n['节点描述'],
  221. order: n['发现编号'],
  222. isKnown: n['是否已知']
  223. };
  224. nodeById[node.id] = node;
  225. return node;
  226. });
  227. const graphLinks = edges.map(e => ({
  228. source: e['来源'],
  229. target: e['目标'],
  230. type: e['关系类型'],
  231. score: e['可能性分数'],
  232. reason: e['推理说明']
  233. })).filter(e => nodeById[e.source] && nodeById[e.target]);
  234. // 渲染帖子摘要
  235. function renderPostSummary() {
  236. const container = document.getElementById('postSummary');
  237. const bodyText = postDetail.body_text || '';
  238. const shortBody = bodyText.length > 150 ? bodyText.slice(0, 150) + '...' : bodyText;
  239. container.innerHTML = `
  240. <div class="post-title">${postDetail.postTitle || '无标题'}</div>
  241. <div class="post-body">${shortBody}</div>
  242. <div class="post-meta">
  243. <span>❤️ ${postDetail.like_count || 0}</span>
  244. <span>⭐ ${postDetail.collect_count || 0}</span>
  245. <span>${postDetail.publish_time || ''}</span>
  246. </div>
  247. `;
  248. }
  249. // 渲染时间轴
  250. function renderTimeline() {
  251. const container = document.getElementById('timeline');
  252. const sortedNodes = [...graphNodes].sort((a, b) => a.order - b.order);
  253. let html = '<div class="timeline-title">推导过程</div>';
  254. sortedNodes.forEach(n => {
  255. const edge = edgeByTarget[n.id];
  256. const fromNode = edge ? nodeById[edge.source] : null;
  257. html += `
  258. <div class="timeline-item" data-order="${n.order}" data-node-id="${n.id}">
  259. <div class="timeline-dot">${n.order}</div>
  260. <div class="timeline-node-name">${n.name}</div>
  261. <div class="timeline-node-dim ${n.dimension}">${n.dimension}</div>
  262. ${fromNode ? `<div class="timeline-from">← 从 <span>${fromNode.name}</span> 推导</div>` : '<div class="timeline-from">起点</div>'}
  263. ${edge ? `<div class="timeline-reason">${edge.reason || ''}</div>` : ''}
  264. </div>
  265. `;
  266. });
  267. container.innerHTML = html;
  268. // 绑定事件
  269. container.querySelectorAll('.timeline-item').forEach(item => {
  270. item.addEventListener('click', () => highlightNode(item.dataset.nodeId));
  271. item.addEventListener('mouseenter', () => highlightNode(item.dataset.nodeId));
  272. item.addEventListener('mouseleave', () => clearHighlight());
  273. });
  274. }
  275. // ========== 分层布局 ==========
  276. // 按 order 分层
  277. const layers = {};
  278. graphNodes.forEach(n => {
  279. if (!layers[n.order]) layers[n.order] = [];
  280. layers[n.order].push(n);
  281. });
  282. const layerKeys = Object.keys(layers).map(Number).sort((a, b) => a - b);
  283. // 布局参数
  284. const nodeRadius = 18;
  285. const layerGap = 100;
  286. const nodeGap = 100;
  287. const padding = 60;
  288. // 计算每层宽度
  289. let maxLayerWidth = 0;
  290. layerKeys.forEach(layer => {
  291. const layerWidth = layers[layer].length * nodeGap;
  292. maxLayerWidth = Math.max(maxLayerWidth, layerWidth);
  293. });
  294. const svgWidth = Math.max(maxLayerWidth + padding * 2, 600);
  295. const svgHeight = layerKeys.length * layerGap + padding * 2;
  296. // 计算节点位置
  297. layerKeys.forEach((layer, layerIndex) => {
  298. const layerNodes = layers[layer];
  299. const layerWidth = (layerNodes.length - 1) * nodeGap;
  300. const startX = (svgWidth - layerWidth) / 2;
  301. layerNodes.forEach((node, nodeIndex) => {
  302. node.x = startX + nodeIndex * nodeGap;
  303. node.y = padding + layerIndex * layerGap;
  304. });
  305. });
  306. // 创建 SVG
  307. const svg = d3.select('#graph')
  308. .attr('width', svgWidth)
  309. .attr('height', svgHeight)
  310. .style('background', '#222'); // 调试:添加背景色
  311. const g = svg.append('g');
  312. // 调试:画一个测试圆
  313. g.append('circle').attr('cx', 100).attr('cy', 100).attr('r', 20).attr('fill', 'red');
  314. // 缩放
  315. const zoom = d3.zoom()
  316. .scaleExtent([0.3, 3])
  317. .on('zoom', (event) => g.attr('transform', event.transform));
  318. svg.call(zoom);
  319. // 箭头
  320. svg.append('defs').append('marker')
  321. .attr('id', 'arrow')
  322. .attr('viewBox', '0 -5 10 10')
  323. .attr('refX', 25)
  324. .attr('refY', 0)
  325. .attr('markerWidth', 6)
  326. .attr('markerHeight', 6)
  327. .attr('orient', 'auto')
  328. .append('path')
  329. .attr('d', 'M0,-5L10,0L0,5')
  330. .attr('fill', '#e94560');
  331. // 层级标签
  332. layerKeys.forEach((layer, layerIndex) => {
  333. g.append('text')
  334. .attr('class', 'layer-label')
  335. .attr('x', 20)
  336. .attr('y', padding + layerIndex * layerGap + 4)
  337. .text(`#${layer}`);
  338. });
  339. // 绘制边(曲线)
  340. const link = g.append('g')
  341. .selectAll('path')
  342. .data(graphLinks)
  343. .join('path')
  344. .attr('class', 'link')
  345. .attr('stroke', '#e94560')
  346. .attr('stroke-width', 2)
  347. .attr('fill', 'none')
  348. .attr('marker-end', 'url(#arrow)')
  349. .attr('d', d => {
  350. const source = nodeById[d.source];
  351. const target = nodeById[d.target];
  352. if (!source || !target) return '';
  353. const midY = (source.y + target.y) / 2;
  354. return `M${source.x},${source.y + nodeRadius} C${source.x},${midY} ${target.x},${midY} ${target.x},${target.y - nodeRadius}`;
  355. });
  356. // 边标签
  357. const linkLabel = g.append('g')
  358. .selectAll('text')
  359. .data(graphLinks)
  360. .join('text')
  361. .attr('class', 'link-label')
  362. .attr('x', d => {
  363. const source = nodeById[d.source];
  364. const target = nodeById[d.target];
  365. return source && target ? (source.x + target.x) / 2 : 0;
  366. })
  367. .attr('y', d => {
  368. const source = nodeById[d.source];
  369. const target = nodeById[d.target];
  370. return source && target ? (source.y + target.y) / 2 : 0;
  371. })
  372. .attr('text-anchor', 'middle')
  373. .text(d => d.score?.toFixed(2) || '');
  374. // 绘制节点
  375. const node = g.append('g')
  376. .selectAll('g')
  377. .data(graphNodes)
  378. .join('g')
  379. .attr('class', 'node')
  380. .attr('transform', d => `translate(${d.x},${d.y})`)
  381. .on('click', (event, d) => highlightNode(d.id))
  382. .on('mouseenter', (event, d) => highlightNode(d.id))
  383. .on('mouseleave', () => clearHighlight());
  384. node.append('circle')
  385. .attr('r', nodeRadius)
  386. .attr('fill', d => dimColors[d.dimension] || '#888')
  387. .attr('stroke', d => dimColors[d.dimension] || '#888');
  388. node.append('text')
  389. .attr('class', 'order-badge')
  390. .attr('dy', 4)
  391. .text(d => d.order || '');
  392. node.append('text')
  393. .attr('dy', 35)
  394. .text(d => d.name.length > 6 ? d.name.slice(0, 6) + '…' : d.name);
  395. // 高亮函数
  396. function highlightNode(nodeId) {
  397. node.classed('highlight', d => d.id === nodeId);
  398. link.classed('highlight', d => d.target === nodeId);
  399. document.querySelectorAll('.timeline-item').forEach(item => {
  400. item.classList.toggle('active', item.dataset.nodeId === nodeId);
  401. });
  402. }
  403. function clearHighlight() {
  404. node.classed('highlight', false);
  405. link.classed('highlight', false);
  406. document.querySelectorAll('.timeline-item').forEach(item => {
  407. item.classList.remove('active');
  408. });
  409. }
  410. // 调试信息
  411. console.log('=== 调试 ===');
  412. console.log('graphNodes:', graphNodes.length, graphNodes);
  413. console.log('graphLinks:', graphLinks.length, graphLinks);
  414. console.log('layers:', layers);
  415. console.log('layerKeys:', layerKeys);
  416. console.log('svgWidth:', svgWidth, 'svgHeight:', svgHeight);
  417. // 初始化
  418. renderPostSummary();
  419. renderTimeline();
  420. // 居中显示
  421. setTimeout(() => {
  422. const graphArea = document.querySelector('.graph-area');
  423. const areaWidth = graphArea.clientWidth;
  424. const areaHeight = graphArea.clientHeight;
  425. const scale = Math.min(areaWidth / svgWidth, areaHeight / svgHeight, 1) * 0.9;
  426. const tx = (areaWidth - svgWidth * scale) / 2;
  427. const ty = (areaHeight - svgHeight * scale) / 2;
  428. svg.call(zoom.transform, d3.zoomIdentity.translate(tx, ty).scale(scale));
  429. }, 100);
  430. </script>
  431. </body>
  432. </html>
  433. '''
  434. def main():
  435. import argparse
  436. parser = argparse.ArgumentParser(description='构建创作思维路径可视化(分层布局)')
  437. parser.add_argument('input_dir', nargs='?', help='输入目录路径(可选,默认使用 PathConfig)')
  438. args = parser.parse_args()
  439. if args.input_dir:
  440. input_dir = Path(args.input_dir)
  441. print(f"输入目录: {input_dir}")
  442. else:
  443. config = PathConfig()
  444. input_dir = config.intermediate_dir / "creation_pattern"
  445. print(f"账号: {config.account_name}")
  446. print(f"输入目录: {input_dir}")
  447. if not input_dir.exists():
  448. print(f"错误: 目录不存在!")
  449. sys.exit(1)
  450. # 查找所有创作模式文件
  451. pattern_files = sorted(input_dir.glob("*_创作模式.json"))
  452. print(f"找到 {len(pattern_files)} 个创作模式文件")
  453. if not pattern_files:
  454. print("错误: 没有找到创作模式文件!")
  455. sys.exit(1)
  456. # 为每个文件生成 HTML
  457. for pattern_file in pattern_files:
  458. post_id = pattern_file.stem.replace("_创作模式", "")
  459. print(f"\n处理: {post_id}")
  460. # 读取数据
  461. with open(pattern_file, "r", encoding="utf-8") as f:
  462. data = json.load(f)
  463. # 生成 HTML
  464. data_json = json.dumps(data, ensure_ascii=False)
  465. html_content = HTML_TEMPLATE.replace("__DATA_PLACEHOLDER__", data_json)
  466. # 输出文件
  467. output_file = input_dir / f"{post_id}_分层图谱.html"
  468. with open(output_file, "w", encoding="utf-8") as f:
  469. f.write(html_content)
  470. print(f"输出: {output_file}")
  471. print("\n完成!")
  472. if __name__ == "__main__":
  473. main()