build_creation_pattern_v3.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 构建创作思维路径可视化 V3
  5. 新版特性:
  6. - 按节点发现编号分层布局(同一编号在同一层)
  7. - 同一个发现编号的节点使用相同的填充颜色
  8. - Tab 切换展示目录下所有帖子
  9. 读取 creation_pattern 目录下的 JSON 数据,输出单文件 HTML
  10. """
  11. import json
  12. import sys
  13. from pathlib import Path
  14. # 项目路径
  15. project_root = Path(__file__).parent.parent.parent
  16. sys.path.insert(0, str(project_root))
  17. from script.data_processing.path_config import PathConfig
  18. HTML_TEMPLATE = '''<!DOCTYPE html>
  19. <html lang="zh-CN">
  20. <head>
  21. <meta charset="UTF-8">
  22. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  23. <title>创作思维路径可视化</title>
  24. <script src="https://d3js.org/d3.v7.min.js"></script>
  25. <style>
  26. * { margin: 0; padding: 0; box-sizing: border-box; }
  27. body {
  28. font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  29. background: #1a1a2e;
  30. color: #eee;
  31. overflow: hidden;
  32. }
  33. .container { display: flex; flex-direction: column; height: 100vh; }
  34. /* Tab 样式 */
  35. .tab-bar {
  36. display: flex;
  37. background: #0f3460;
  38. border-bottom: 1px solid #16213e;
  39. overflow-x: auto;
  40. flex-shrink: 0;
  41. }
  42. .tab-bar::-webkit-scrollbar { height: 4px; }
  43. .tab-bar::-webkit-scrollbar-thumb { background: #e94560; border-radius: 2px; }
  44. .tab-item {
  45. padding: 10px 16px;
  46. font-size: 12px;
  47. color: #888;
  48. cursor: pointer;
  49. border-bottom: 2px solid transparent;
  50. white-space: nowrap;
  51. transition: all 0.2s;
  52. }
  53. .tab-item:hover { color: #ccc; background: rgba(233, 69, 96, 0.1); }
  54. .tab-item.active {
  55. color: #e94560;
  56. border-bottom-color: #e94560;
  57. background: rgba(233, 69, 96, 0.1);
  58. }
  59. header {
  60. padding: 10px 20px;
  61. background: #16213e;
  62. border-bottom: 1px solid #0f3460;
  63. display: flex;
  64. align-items: center;
  65. gap: 20px;
  66. }
  67. h1 { font-size: 16px; font-weight: 500; color: #e94560; }
  68. .controls { display: flex; align-items: center; gap: 12px; flex: 1; }
  69. .slider-container {
  70. display: flex;
  71. align-items: center;
  72. gap: 8px;
  73. background: #0f3460;
  74. padding: 6px 12px;
  75. border-radius: 6px;
  76. }
  77. .slider-container label { font-size: 13px; color: #aaa; }
  78. #stepSlider { width: 200px; cursor: pointer; }
  79. #stepDisplay { font-size: 14px; font-weight: 600; color: #e94560; min-width: 60px; }
  80. .play-btn {
  81. background: #e94560;
  82. border: none;
  83. color: white;
  84. padding: 6px 14px;
  85. border-radius: 4px;
  86. cursor: pointer;
  87. font-size: 13px;
  88. }
  89. .play-btn:hover { background: #ff6b6b; }
  90. .legend {
  91. display: flex;
  92. gap: 12px;
  93. font-size: 11px;
  94. color: #888;
  95. margin-left: auto;
  96. background: #0f3460;
  97. padding: 6px 12px;
  98. border-radius: 6px;
  99. }
  100. .legend-item { display: flex; align-items: center; gap: 4px; }
  101. .legend-dot { width: 10px; height: 10px; border-radius: 50%; }
  102. main { flex: 1; display: flex; overflow: hidden; }
  103. .graph-area { flex: 1; position: relative; }
  104. .graph-area svg { width: 100%; height: 100%; }
  105. .right-panel {
  106. width: 340px;
  107. background: #16213e;
  108. border-left: 1px solid #0f3460;
  109. display: flex;
  110. flex-direction: column;
  111. overflow: hidden;
  112. }
  113. .post-summary {
  114. padding: 16px;
  115. border-bottom: 1px solid #0f3460;
  116. max-height: 180px;
  117. overflow-y: auto;
  118. }
  119. .post-title {
  120. font-size: 14px;
  121. font-weight: 600;
  122. color: #e94560;
  123. margin-bottom: 8px;
  124. line-height: 1.4;
  125. }
  126. .post-body {
  127. font-size: 12px;
  128. color: #aaa;
  129. line-height: 1.6;
  130. max-height: 80px;
  131. overflow-y: auto;
  132. }
  133. .post-meta {
  134. margin-top: 8px;
  135. font-size: 11px;
  136. color: #666;
  137. display: flex;
  138. gap: 12px;
  139. }
  140. .timeline {
  141. flex: 1;
  142. overflow-y: auto;
  143. padding: 16px;
  144. }
  145. .timeline-title {
  146. font-size: 13px;
  147. font-weight: 600;
  148. color: #888;
  149. margin-bottom: 12px;
  150. }
  151. .timeline-item {
  152. position: relative;
  153. padding-left: 24px;
  154. padding-bottom: 16px;
  155. border-left: 2px solid #0f3460;
  156. margin-left: 8px;
  157. cursor: pointer;
  158. transition: all 0.2s;
  159. }
  160. .timeline-item:hover {
  161. background: rgba(233, 69, 96, 0.1);
  162. margin-left: 6px;
  163. padding-left: 26px;
  164. }
  165. .timeline-item:last-child { border-left-color: transparent; }
  166. .timeline-item.active { border-left-color: #e94560; }
  167. .timeline-item.future { opacity: 0.4; }
  168. .timeline-dot {
  169. position: absolute;
  170. left: -7px;
  171. top: 0;
  172. width: 12px;
  173. height: 12px;
  174. border-radius: 50%;
  175. display: flex;
  176. align-items: center;
  177. justify-content: center;
  178. font-size: 8px;
  179. font-weight: bold;
  180. color: white;
  181. }
  182. .timeline-node-name {
  183. font-size: 13px;
  184. font-weight: 500;
  185. color: #eee;
  186. margin-bottom: 4px;
  187. }
  188. .timeline-node-dim {
  189. font-size: 11px;
  190. padding: 2px 6px;
  191. border-radius: 3px;
  192. display: inline-block;
  193. margin-bottom: 6px;
  194. }
  195. .timeline-node-dim.灵感点 { background: rgba(255,107,107,0.2); color: #ff6b6b; }
  196. .timeline-node-dim.目的点 { background: rgba(78,205,196,0.2); color: #4ecdc4; }
  197. .timeline-node-dim.关键点 { background: rgba(255,230,109,0.2); color: #ffe66d; }
  198. .timeline-reason {
  199. font-size: 11px;
  200. color: #888;
  201. line-height: 1.5;
  202. background: #0f3460;
  203. padding: 8px;
  204. border-radius: 4px;
  205. }
  206. .timeline-from {
  207. font-size: 11px;
  208. color: #666;
  209. margin-bottom: 4px;
  210. }
  211. .timeline-from span { color: #e94560; }
  212. .layer-label { font-size: 12px; fill: #555; font-weight: 600; }
  213. .layer-line { stroke: #2a2a4a; stroke-width: 1; stroke-dasharray: 4,4; }
  214. .node { cursor: pointer; }
  215. .node circle { stroke-width: 0; transition: all 0.3s; }
  216. .node.unknown circle { fill: #2a2a4a !important; opacity: 0.4; }
  217. .node.highlight circle { filter: drop-shadow(0 0 8px rgba(255,255,255,0.5)); }
  218. .node text { font-size: 11px; fill: #ccc; text-anchor: middle; pointer-events: none; }
  219. .node.unknown text { fill: #555; }
  220. .node .order-badge { font-size: 10px; font-weight: bold; fill: white; }
  221. .node .constant-badge { font-size: 12px; fill: #ffd700; }
  222. .node.constant circle:first-child { filter: drop-shadow(0 0 4px rgba(255, 215, 0, 0.6)); }
  223. .link { stroke-opacity: 0.5; transition: all 0.3s; cursor: pointer; }
  224. .link.hidden { stroke-opacity: 0 !important; pointer-events: none; }
  225. .link.highlight { stroke-opacity: 0.8; stroke-width: 2px; }
  226. .link:hover { stroke-opacity: 0.9; stroke-width: 2.5px; }
  227. .link-label {
  228. font-size: 10px;
  229. fill: #f39c12;
  230. font-weight: 600;
  231. text-anchor: middle;
  232. pointer-events: none;
  233. }
  234. .link-label.hidden { opacity: 0; }
  235. .link-label-group.hidden { opacity: 0; pointer-events: none; }
  236. .link-label-bg {
  237. fill: #16213e;
  238. opacity: 0.85;
  239. }
  240. .edge-detail {
  241. position: fixed;
  242. background: #16213e;
  243. border: 1px solid #0f3460;
  244. border-radius: 8px;
  245. padding: 12px 16px;
  246. max-width: 320px;
  247. box-shadow: 0 4px 20px rgba(0,0,0,0.4);
  248. z-index: 1000;
  249. display: none;
  250. }
  251. .edge-detail.show { display: block; }
  252. .edge-detail-header {
  253. display: flex;
  254. justify-content: space-between;
  255. align-items: center;
  256. margin-bottom: 10px;
  257. }
  258. .edge-detail-type {
  259. font-size: 12px;
  260. padding: 3px 8px;
  261. border-radius: 4px;
  262. font-weight: 500;
  263. }
  264. .edge-detail-type.AI推导 { background: rgba(233,69,96,0.2); color: #e94560; }
  265. .edge-detail-type.共现推导 { background: rgba(52,152,219,0.2); color: #3498db; }
  266. .edge-detail-close {
  267. background: none;
  268. border: none;
  269. color: #888;
  270. cursor: pointer;
  271. font-size: 16px;
  272. }
  273. .edge-detail-close:hover { color: #fff; }
  274. .edge-detail-nodes {
  275. font-size: 13px;
  276. color: #ccc;
  277. margin-bottom: 8px;
  278. }
  279. .edge-detail-nodes span { color: #e94560; font-weight: 500; }
  280. .edge-detail-arrow { color: #666; margin: 0 6px; }
  281. .edge-detail-score {
  282. font-size: 11px;
  283. color: #888;
  284. margin-bottom: 8px;
  285. }
  286. .edge-detail-reason {
  287. font-size: 12px;
  288. color: #aaa;
  289. line-height: 1.6;
  290. background: #0f3460;
  291. padding: 10px;
  292. border-radius: 4px;
  293. }
  294. .edge-detail-path {
  295. margin-top: 10px;
  296. font-size: 11px;
  297. color: #888;
  298. background: #0f3460;
  299. padding: 10px;
  300. border-radius: 4px;
  301. max-height: 200px;
  302. overflow-y: auto;
  303. }
  304. .edge-detail-path-title {
  305. font-weight: 500;
  306. color: #e94560;
  307. margin-bottom: 6px;
  308. }
  309. .edge-path-item {
  310. display: flex;
  311. align-items: center;
  312. gap: 4px;
  313. padding: 4px 0;
  314. border-bottom: 1px solid #1a1a2e;
  315. }
  316. .edge-path-item:last-child { border-bottom: none; }
  317. .path-node {
  318. display: inline-flex;
  319. align-items: center;
  320. gap: 4px;
  321. background: rgba(233, 69, 96, 0.15);
  322. padding: 2px 6px;
  323. border-radius: 3px;
  324. }
  325. .path-node-domain {
  326. color: #888;
  327. font-size: 10px;
  328. margin-right: 4px;
  329. }
  330. .path-node-shape {
  331. margin-right: 4px;
  332. font-size: 10px;
  333. }
  334. .path-node-shape.circle { color: #e94560; }
  335. .path-node-shape.square { color: #3498db; }
  336. .path-node-name { color: #ccc; font-weight: 500; }
  337. .path-edge {
  338. display: inline-flex;
  339. align-items: center;
  340. gap: 4px;
  341. color: #3498db;
  342. }
  343. .path-edge-type { font-size: 10px; }
  344. .path-edge-score { color: #f39c12; font-weight: 500; }
  345. </style>
  346. </head>
  347. <body>
  348. <div class="edge-detail" id="edgeDetail">
  349. <div class="edge-detail-header">
  350. <span class="edge-detail-type" id="edgeType"></span>
  351. <button class="edge-detail-close" id="edgeClose">×</button>
  352. </div>
  353. <div class="edge-detail-nodes" id="edgeNodes"></div>
  354. <div class="edge-detail-score" id="edgeScore"></div>
  355. <div class="edge-detail-reason" id="edgeReason"></div>
  356. <div class="edge-detail-path" id="edgePath" style="display: none;"></div>
  357. </div>
  358. <div class="container">
  359. <div class="tab-bar" id="tabBar"></div>
  360. <header>
  361. <h1>创作思维路径</h1>
  362. <div class="controls">
  363. <div class="slider-container">
  364. <label>步骤:</label>
  365. <input type="range" id="stepSlider" min="0" max="9" value="9">
  366. <span id="stepDisplay">9/9</span>
  367. </div>
  368. <button class="play-btn" id="playBtn">▶ 播放</button>
  369. </div>
  370. <div class="legend">
  371. <div class="legend-item"><span class="legend-dot" style="background: #e94560;"></span><span>AI推导</span></div>
  372. <div class="legend-item"><span class="legend-dot" style="background: #3498db;"></span><span>共现推导</span></div>
  373. <div class="legend-item"><span style="color: #ffd700; font-size: 14px;">★</span><span>人设常量</span></div>
  374. </div>
  375. </header>
  376. <main>
  377. <div class="graph-area">
  378. <svg id="graph"></svg>
  379. </div>
  380. <div class="right-panel">
  381. <div class="post-summary" id="postSummary"></div>
  382. <div class="timeline" id="timeline"></div>
  383. </div>
  384. </main>
  385. </div>
  386. <script>
  387. const ALL_DATA = __DATA_PLACEHOLDER__;
  388. const stepColors = [
  389. '#e94560', '#9b59b6', '#3498db', '#1abc9c', '#2ecc71',
  390. '#f39c12', '#e74c3c', '#8e44ad', '#2980b9', '#16a085',
  391. '#27ae60', '#d35400', '#c0392b', '#7b1fa2', '#1565c0',
  392. ];
  393. function getStepColor(order) {
  394. if (!order || order <= 0) return '#2a2a4a';
  395. return stepColors[(order - 1) % stepColors.length];
  396. }
  397. let currentPostIndex = 0;
  398. let postDetail, graphNodes, graphLinks, nodeById, edgeByTarget, maxOrder;
  399. let currentStep, playing = false, playInterval = null;
  400. let svg, g, zoom, link, node;
  401. const slider = document.getElementById('stepSlider');
  402. const stepDisplay = document.getElementById('stepDisplay');
  403. const playBtn = document.getElementById('playBtn');
  404. // 边类型颜色
  405. const edgeColors = {
  406. 'AI推导': '#e94560',
  407. '共现推导': '#3498db',
  408. '支撑': '#666',
  409. '关联': '#666'
  410. };
  411. function loadPostData(index) {
  412. const DATA = ALL_DATA[index];
  413. postDetail = DATA['帖子详情'];
  414. const steps = DATA['步骤列表'];
  415. const lastStep = steps[steps.length - 1];
  416. const nodes = lastStep['输出']['节点列表'];
  417. const edges = lastStep['输出']['边列表'] || [];
  418. maxOrder = Math.max(...nodes.map(n => n['发现编号'] || 0), 1);
  419. // 只保留 AI推导 和 共现推导 用于 timeline 显示
  420. edgeByTarget = {};
  421. edges.filter(e => e['关系类型'] === 'AI推导' || e['关系类型'] === '共现推导')
  422. .forEach(e => { edgeByTarget[e['目标']] = e; });
  423. nodeById = {};
  424. graphNodes = nodes.map(n => {
  425. const node = {
  426. id: n['节点ID'],
  427. name: n['节点名称'],
  428. dimension: n['节点维度'],
  429. order: n['发现编号'],
  430. isKnown: n['是否已知'],
  431. isConstant: n['是人设常量'] || false
  432. };
  433. nodeById[node.id] = node;
  434. return node;
  435. });
  436. // 只加载 AI推导 和 共现推导 边(支撑/关联不展示)
  437. graphLinks = edges
  438. .filter(e => e['关系类型'] === 'AI推导' || e['关系类型'] === '共现推导')
  439. .map(e => ({
  440. source: e['来源'],
  441. target: e['目标'],
  442. type: e['关系类型'],
  443. score: e['score'] || e['可能性分数'],
  444. reason: e['推理说明'],
  445. path: e['推导路径']
  446. }));
  447. }
  448. function renderTabs() {
  449. const tabBar = document.getElementById('tabBar');
  450. tabBar.innerHTML = ALL_DATA.map((data, i) => {
  451. const title = data['帖子详情']?.postTitle || `帖子${i + 1}`;
  452. const shortTitle = title.length > 20 ? title.slice(0, 20) + '...' : title;
  453. return `<div class="tab-item ${i === 0 ? 'active' : ''}" data-index="${i}">${shortTitle}</div>`;
  454. }).join('');
  455. tabBar.querySelectorAll('.tab-item').forEach(tab => {
  456. tab.addEventListener('click', () => {
  457. const index = parseInt(tab.dataset.index);
  458. if (index !== currentPostIndex) {
  459. if (playing) { clearInterval(playInterval); playing = false; playBtn.textContent = '▶ 播放'; }
  460. currentPostIndex = index;
  461. tabBar.querySelectorAll('.tab-item').forEach(t => t.classList.remove('active'));
  462. tab.classList.add('active');
  463. loadPostData(index);
  464. renderAll();
  465. }
  466. });
  467. });
  468. }
  469. function renderPostSummary() {
  470. const container = document.getElementById('postSummary');
  471. const bodyText = postDetail.body_text || '';
  472. const shortBody = bodyText.length > 150 ? bodyText.slice(0, 150) + '...' : bodyText;
  473. container.innerHTML = `
  474. <div class="post-title">${postDetail.postTitle || '无标题'}</div>
  475. <div class="post-body">${shortBody}</div>
  476. <div class="post-meta">
  477. <span>❤️ ${postDetail.like_count || 0}</span>
  478. <span>⭐ ${postDetail.collect_count || 0}</span>
  479. </div>
  480. `;
  481. }
  482. function renderTimeline() {
  483. const container = document.getElementById('timeline');
  484. const sortedNodes = [...graphNodes].filter(n => n.order).sort((a, b) => a.order - b.order);
  485. let html = '<div class="timeline-title">推导过程</div>';
  486. sortedNodes.forEach(n => {
  487. const edge = edgeByTarget[n.id];
  488. const fromNode = edge ? nodeById[edge.source] : null;
  489. const stepColor = getStepColor(n.order);
  490. html += `
  491. <div class="timeline-item" data-order="${n.order}" data-node-id="${n.id}">
  492. <div class="timeline-dot" style="background: ${stepColor};">${n.order}</div>
  493. <div class="timeline-node-name">${n.name}</div>
  494. <div class="timeline-node-dim ${n.dimension}">${n.dimension}</div>
  495. ${fromNode ? `<div class="timeline-from">← <span>${fromNode.name}</span></div>` : '<div class="timeline-from">起点</div>'}
  496. ${edge?.reason ? `<div class="timeline-reason">${edge.reason}</div>` : ''}
  497. </div>
  498. `;
  499. });
  500. container.innerHTML = html;
  501. container.querySelectorAll('.timeline-item').forEach(item => {
  502. item.addEventListener('click', () => {
  503. const order = parseInt(item.dataset.order);
  504. slider.value = order;
  505. updateDisplay(order);
  506. highlightNode(item.dataset.nodeId);
  507. });
  508. item.addEventListener('mouseenter', () => highlightNode(item.dataset.nodeId));
  509. item.addEventListener('mouseleave', clearHighlight);
  510. });
  511. }
  512. function renderGraph() {
  513. const graphArea = document.querySelector('.graph-area');
  514. const width = graphArea.clientWidth;
  515. const height = graphArea.clientHeight;
  516. // 清空并重建 SVG
  517. d3.select('#graph').selectAll('*').remove();
  518. svg = d3.select('#graph');
  519. g = svg.append('g');
  520. zoom = d3.zoom()
  521. .scaleExtent([0.2, 3])
  522. .on('zoom', (event) => g.attr('transform', event.transform));
  523. svg.call(zoom);
  524. // 定义不同颜色的箭头
  525. const defs = svg.append('defs');
  526. Object.entries(edgeColors).forEach(([type, color]) => {
  527. defs.append('marker')
  528. .attr('id', `arrow-${type}`)
  529. .attr('viewBox', '0 -5 10 10')
  530. .attr('refX', 8)
  531. .attr('refY', 0)
  532. .attr('markerWidth', 6)
  533. .attr('markerHeight', 6)
  534. .attr('orient', 'auto')
  535. .append('path')
  536. .attr('d', 'M0,-4L8,0L0,4')
  537. .attr('fill', color);
  538. });
  539. // 分层布局
  540. const layerHeight = 100;
  541. const nodeSpacing = 140;
  542. const startY = 60;
  543. const nodesByOrder = {};
  544. graphNodes.forEach(n => {
  545. const order = n.order || 0;
  546. if (!nodesByOrder[order]) nodesByOrder[order] = [];
  547. nodesByOrder[order].push(n);
  548. });
  549. const orders = Object.keys(nodesByOrder).map(Number).sort((a, b) => a - b);
  550. orders.forEach((order, layerIndex) => {
  551. const layerNodes = nodesByOrder[order];
  552. const layerWidth = (layerNodes.length - 1) * nodeSpacing;
  553. const layerStartX = 80 + (width - 400 - layerWidth) / 2;
  554. layerNodes.forEach((n, i) => {
  555. n.x = layerStartX + i * nodeSpacing;
  556. n.y = startY + layerIndex * layerHeight;
  557. });
  558. });
  559. // 层级标签
  560. const layerGroup = g.append('g');
  561. orders.filter(o => o > 0).forEach((order, layerIndex) => {
  562. const y = startY + layerIndex * layerHeight;
  563. const stepColor = getStepColor(order);
  564. layerGroup.append('line')
  565. .attr('class', 'layer-line')
  566. .attr('x1', 20).attr('y1', y)
  567. .attr('x2', width - 360).attr('y2', y)
  568. .attr('stroke', stepColor).attr('stroke-opacity', 0.2);
  569. layerGroup.append('text')
  570. .attr('class', 'layer-label')
  571. .attr('x', 25).attr('y', y + 4)
  572. .attr('fill', stepColor)
  573. .text(`${order}`);
  574. });
  575. // 边(按类型着色)
  576. link = g.append('g').selectAll('path').data(graphLinks).join('path')
  577. .attr('class', d => `link link-${d.type}`)
  578. .attr('stroke', d => edgeColors[d.type] || '#666')
  579. .attr('stroke-width', d => (d.type === 'AI推导' || d.type === '共现推导') ? 1.5 : 1)
  580. .attr('fill', 'none')
  581. .attr('marker-end', d => d.type === 'AI推导' ? `url(#arrow-${d.type})` : null)
  582. .attr('d', d => {
  583. const s = nodeById[d.source], t = nodeById[d.target];
  584. if (!s || !t) return '';
  585. const sy = s.y + 20, ty = t.y - 20;
  586. const offset = Math.min(Math.abs(ty - sy) * 0.4, 40);
  587. return `M ${s.x} ${sy} C ${s.x} ${sy + offset} ${t.x} ${ty - offset} ${t.x} ${ty}`;
  588. })
  589. .on('click', (event, d) => showEdgeDetail(event, d));
  590. // 边分数标签
  591. const linkLabels = g.append('g').selectAll('g')
  592. .data(graphLinks.filter(d => d.score > 0))
  593. .join('g')
  594. .attr('class', 'link-label-group')
  595. .attr('transform', d => {
  596. const s = nodeById[d.source], t = nodeById[d.target];
  597. if (!s || !t) return '';
  598. const mx = (s.x + t.x) / 2;
  599. const my = (s.y + 20 + t.y - 20) / 2;
  600. return `translate(${mx}, ${my})`;
  601. });
  602. linkLabels.append('rect')
  603. .attr('class', 'link-label-bg')
  604. .attr('x', -16).attr('y', -8)
  605. .attr('width', 32).attr('height', 14)
  606. .attr('rx', 3);
  607. linkLabels.append('text')
  608. .attr('class', 'link-label')
  609. .attr('dy', 3)
  610. .text(d => d.score.toFixed(2));
  611. // 节点
  612. node = g.append('g').selectAll('g').data(graphNodes).join('g')
  613. .attr('class', d => `node${d.isConstant ? ' constant' : ''}`)
  614. .attr('transform', d => `translate(${d.x},${d.y})`)
  615. .on('click', (e, d) => { slider.value = d.order || maxOrder; updateDisplay(d.order || maxOrder); })
  616. .on('mouseenter', (e, d) => highlightNode(d.id))
  617. .on('mouseleave', clearHighlight);
  618. // 常量节点外环
  619. node.filter(d => d.isConstant).append('circle')
  620. .attr('r', 24)
  621. .attr('fill', 'none')
  622. .attr('stroke', '#ffd700')
  623. .attr('stroke-width', 2)
  624. .attr('stroke-dasharray', '4,2');
  625. node.append('circle').attr('r', 18).attr('fill', d => getStepColor(d.order));
  626. node.append('text').attr('class', 'order-badge').attr('dy', 4).text(d => d.order || '');
  627. // 常量节点显示星号
  628. node.filter(d => d.isConstant).append('text')
  629. .attr('class', 'constant-badge')
  630. .attr('dy', -22)
  631. .attr('text-anchor', 'middle')
  632. .text('★');
  633. node.append('text').attr('dy', 35).text(d => d.name);
  634. // 初始视图
  635. setTimeout(() => {
  636. const bounds = g.node().getBBox();
  637. const scale = Math.min((width - 380) / bounds.width, (height - 20) / bounds.height, 1);
  638. const tx = (width - 340 - bounds.width * scale) / 2 - bounds.x * scale;
  639. const ty = (height - bounds.height * scale) / 2 - bounds.y * scale;
  640. svg.transition().duration(300).call(zoom.transform, d3.zoomIdentity.translate(tx, ty).scale(scale));
  641. }, 50);
  642. }
  643. function highlightNode(nodeId) {
  644. node.classed('highlight', d => d.id === nodeId);
  645. link.classed('highlight', d => d.target === nodeId);
  646. document.querySelectorAll('.timeline-item').forEach(item => {
  647. item.classList.toggle('active', item.dataset.nodeId === nodeId);
  648. });
  649. }
  650. function clearHighlight() {
  651. node.classed('highlight', false);
  652. link.classed('highlight', false);
  653. }
  654. // 边详情弹窗
  655. const edgeDetail = document.getElementById('edgeDetail');
  656. const edgeType = document.getElementById('edgeType');
  657. const edgeNodes = document.getElementById('edgeNodes');
  658. const edgeScore = document.getElementById('edgeScore');
  659. const edgeReason = document.getElementById('edgeReason');
  660. const edgePath = document.getElementById('edgePath');
  661. const edgeClose = document.getElementById('edgeClose');
  662. function renderPath(pathData) {
  663. if (!pathData || pathData.length === 0) return '';
  664. let html = '<div class="edge-detail-path-title">推导路径</div>';
  665. pathData.forEach((item, idx) => {
  666. if (item['类型'] === '节点') {
  667. const domain = item['节点域'] || '';
  668. const dimension = item['节点维度'] || '';
  669. const nodeType = item['节点类型'] || '';
  670. const domainDim = [domain, dimension].filter(Boolean).join('-');
  671. const domainTag = domainDim ? `<span class="path-node-domain">[${domainDim}]</span>` : '';
  672. const shapeIcon = nodeType === '分类' ? '<span class="path-node-shape square">■</span>' : '<span class="path-node-shape circle">●</span>';
  673. html += `<div class="edge-path-item"><span class="path-node">${domainTag}${shapeIcon}<span class="path-node-name">${item['节点名称'] || ''}</span></span></div>`;
  674. } else if (item['类型'] === '边') {
  675. const scoreText = item['分数'] != null ? `<span class="path-edge-score">${item['分数']}</span>` : '';
  676. html += `<div class="edge-path-item"><span class="path-edge">↓ <span class="path-edge-type">${item['边类型'] || ''}</span> ${scoreText}</span></div>`;
  677. }
  678. });
  679. return html;
  680. }
  681. function showEdgeDetail(event, d) {
  682. event.stopPropagation();
  683. const sourceNode = nodeById[d.source];
  684. const targetNode = nodeById[d.target];
  685. edgeType.textContent = d.type;
  686. edgeType.className = `edge-detail-type ${d.type}`;
  687. const arrow = d.type === 'AI推导' ? '→' : '—';
  688. edgeNodes.innerHTML = `<span>${sourceNode?.name || d.source}</span><span class="edge-detail-arrow">${arrow}</span><span>${targetNode?.name || d.target}</span>`;
  689. edgeScore.textContent = d.score ? `分数: ${d.score}` : '';
  690. edgeReason.textContent = d.reason || '无推理说明';
  691. // 显示路径
  692. if (d.path && d.path.length > 0) {
  693. edgePath.innerHTML = renderPath(d.path);
  694. edgePath.style.display = 'block';
  695. } else {
  696. edgePath.style.display = 'none';
  697. }
  698. // 定位弹窗
  699. const x = Math.min(event.clientX + 10, window.innerWidth - 340);
  700. const y = Math.min(event.clientY + 10, window.innerHeight - 200);
  701. edgeDetail.style.left = x + 'px';
  702. edgeDetail.style.top = y + 'px';
  703. edgeDetail.classList.add('show');
  704. }
  705. function hideEdgeDetail() {
  706. edgeDetail.classList.remove('show');
  707. }
  708. edgeClose.addEventListener('click', hideEdgeDetail);
  709. document.addEventListener('click', (e) => {
  710. if (!edgeDetail.contains(e.target)) hideEdgeDetail();
  711. });
  712. function updateDisplay(step) {
  713. currentStep = step;
  714. stepDisplay.textContent = `${step}/${maxOrder}`;
  715. node.classed('known', d => d.order && d.order <= step);
  716. node.classed('unknown', d => !d.order || d.order > step);
  717. link.classed('hidden', d => {
  718. const s = nodeById[d.source], t = nodeById[d.target];
  719. return !s || !t || s.order > step || t.order > step;
  720. });
  721. // 同步控制边标签的显示
  722. d3.selectAll('.link-label-group').classed('hidden', function(d) {
  723. const s = nodeById[d.source], t = nodeById[d.target];
  724. return !s || !t || s.order > step || t.order > step;
  725. });
  726. document.querySelectorAll('.timeline-item').forEach(item => {
  727. item.classList.toggle('future', parseInt(item.dataset.order) > step);
  728. });
  729. }
  730. function renderAll() {
  731. slider.max = maxOrder;
  732. slider.value = maxOrder;
  733. currentStep = maxOrder;
  734. renderPostSummary();
  735. renderTimeline();
  736. renderGraph();
  737. updateDisplay(maxOrder);
  738. }
  739. slider.addEventListener('input', e => updateDisplay(parseInt(e.target.value)));
  740. playBtn.addEventListener('click', () => {
  741. if (playing) {
  742. clearInterval(playInterval);
  743. playBtn.textContent = '▶ 播放';
  744. playing = false;
  745. } else {
  746. if (currentStep >= maxOrder) { currentStep = 0; slider.value = 0; updateDisplay(0); }
  747. playing = true;
  748. playBtn.textContent = '⏸ 暂停';
  749. playInterval = setInterval(() => {
  750. currentStep++;
  751. slider.value = currentStep;
  752. updateDisplay(currentStep);
  753. const item = document.querySelector(`.timeline-item[data-order="${currentStep}"]`);
  754. if (item) item.scrollIntoView({ behavior: 'smooth', block: 'center' });
  755. if (currentStep >= maxOrder) {
  756. clearInterval(playInterval);
  757. playBtn.textContent = '▶ 播放';
  758. playing = false;
  759. }
  760. }, 1200);
  761. }
  762. });
  763. // 初始化
  764. renderTabs();
  765. loadPostData(0);
  766. renderAll();
  767. </script>
  768. </body>
  769. </html>
  770. '''
  771. def main():
  772. import argparse
  773. parser = argparse.ArgumentParser(description='构建创作思维路径可视化 V3(分层布局 + Tab切换)')
  774. parser.add_argument('input_dir', nargs='?', help='输入目录路径(可选,默认使用 PathConfig)')
  775. parser.add_argument('--version', '-v', type=str, default='v5',
  776. help='版本号 (v2/v3/v4/v5),默认 v5')
  777. parser.add_argument('--type', '-t', type=str, default='point_order',
  778. help='目录类型前缀 (creation_pattern/point_order),默认 point_order')
  779. args = parser.parse_args()
  780. if args.input_dir:
  781. input_dir = Path(args.input_dir)
  782. print(f"输入目录: {input_dir}")
  783. else:
  784. config = PathConfig()
  785. input_dir = config.intermediate_dir / f"{args.type}_{args.version}"
  786. print(f"账号: {config.account_name}")
  787. print(f"输入目录: {input_dir}")
  788. if not input_dir.exists():
  789. print(f"错误: 目录不存在: {input_dir}")
  790. sys.exit(1)
  791. # 查找所有分析文件(支持多种命名)
  792. pattern_files = sorted(input_dir.glob("*_点顺序.json"))
  793. if not pattern_files:
  794. pattern_files = sorted(input_dir.glob("*_创作模式.json"))
  795. print(f"找到 {len(pattern_files)} 个分析文件")
  796. if not pattern_files:
  797. print("错误: 没有找到分析文件 (*_点顺序.json 或 *_创作模式.json)!")
  798. sys.exit(1)
  799. # 读取所有帖子数据
  800. all_data = []
  801. for pattern_file in pattern_files:
  802. post_id = pattern_file.stem.replace("_点顺序", "").replace("_创作模式", "")
  803. print(f" 读取: {post_id}")
  804. with open(pattern_file, "r", encoding="utf-8") as f:
  805. data = json.load(f)
  806. all_data.append(data)
  807. # 生成单个 HTML 文件
  808. data_json = json.dumps(all_data, ensure_ascii=False)
  809. html_content = HTML_TEMPLATE.replace("__DATA_PLACEHOLDER__", data_json)
  810. output_file = input_dir / "创作思维路径可视化.html"
  811. with open(output_file, "w", encoding="utf-8") as f:
  812. f.write(html_content)
  813. print(f"\n输出: {output_file}")
  814. print("完成!")
  815. if __name__ == "__main__":
  816. main()