build_creation_pattern.py 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 构建创作思维路径可视化
  5. 读取 creation_pattern 目录下的 JSON 数据,输出单文件 HTML
  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. .controls { display: flex; align-items: center; gap: 12px; flex: 1; }
  40. .slider-container {
  41. display: flex;
  42. align-items: center;
  43. gap: 8px;
  44. background: #0f3460;
  45. padding: 6px 12px;
  46. border-radius: 6px;
  47. }
  48. .slider-container label { font-size: 13px; color: #aaa; }
  49. #stepSlider { width: 200px; cursor: pointer; }
  50. #stepDisplay { font-size: 14px; font-weight: 600; color: #e94560; min-width: 60px; }
  51. .play-btn {
  52. background: #e94560;
  53. border: none;
  54. color: white;
  55. padding: 6px 14px;
  56. border-radius: 4px;
  57. cursor: pointer;
  58. font-size: 13px;
  59. }
  60. .play-btn:hover { background: #ff6b6b; }
  61. .legend { display: flex; gap: 16px; font-size: 12px; color: #888; margin-left: auto; }
  62. .legend-item { display: flex; align-items: center; gap: 4px; }
  63. .legend-dot { width: 10px; height: 10px; border-radius: 50%; }
  64. .legend-line { width: 20px; height: 2px; }
  65. /* 主内容区:左右布局 */
  66. main { flex: 1; display: flex; overflow: hidden; }
  67. .graph-area { flex: 1; position: relative; }
  68. .graph-area svg { width: 100%; height: 100%; }
  69. /* 右侧面板 */
  70. .right-panel {
  71. width: 360px;
  72. background: #16213e;
  73. border-left: 1px solid #0f3460;
  74. display: flex;
  75. flex-direction: column;
  76. overflow: hidden;
  77. }
  78. /* 帖子摘要区 */
  79. .post-summary {
  80. padding: 16px;
  81. border-bottom: 1px solid #0f3460;
  82. max-height: 200px;
  83. overflow-y: auto;
  84. }
  85. .post-title {
  86. font-size: 14px;
  87. font-weight: 600;
  88. color: #e94560;
  89. margin-bottom: 8px;
  90. line-height: 1.4;
  91. }
  92. .post-body {
  93. font-size: 12px;
  94. color: #aaa;
  95. line-height: 1.6;
  96. max-height: 100px;
  97. overflow-y: auto;
  98. }
  99. .post-meta {
  100. margin-top: 8px;
  101. font-size: 11px;
  102. color: #666;
  103. display: flex;
  104. gap: 12px;
  105. }
  106. /* 步骤时间轴 */
  107. .timeline {
  108. flex: 1;
  109. overflow-y: auto;
  110. padding: 16px;
  111. }
  112. .timeline-title {
  113. font-size: 13px;
  114. font-weight: 600;
  115. color: #888;
  116. margin-bottom: 12px;
  117. }
  118. .timeline-item {
  119. position: relative;
  120. padding-left: 24px;
  121. padding-bottom: 16px;
  122. border-left: 2px solid #0f3460;
  123. margin-left: 8px;
  124. cursor: pointer;
  125. transition: all 0.2s;
  126. }
  127. .timeline-item:hover {
  128. background: rgba(233, 69, 96, 0.1);
  129. margin-left: 6px;
  130. padding-left: 26px;
  131. }
  132. .timeline-item:last-child { border-left-color: transparent; }
  133. .timeline-item.active { border-left-color: #e94560; }
  134. .timeline-item.future { opacity: 0.4; }
  135. .timeline-dot {
  136. position: absolute;
  137. left: -7px;
  138. top: 0;
  139. width: 12px;
  140. height: 12px;
  141. border-radius: 50%;
  142. border: 2px solid #0f3460;
  143. background: #1a1a2e;
  144. display: flex;
  145. align-items: center;
  146. justify-content: center;
  147. font-size: 8px;
  148. font-weight: bold;
  149. color: white;
  150. }
  151. .timeline-item.active .timeline-dot {
  152. border-color: #e94560;
  153. background: #e94560;
  154. }
  155. .timeline-node-name {
  156. font-size: 13px;
  157. font-weight: 500;
  158. color: #eee;
  159. margin-bottom: 4px;
  160. }
  161. .timeline-node-dim {
  162. font-size: 11px;
  163. padding: 2px 6px;
  164. border-radius: 3px;
  165. display: inline-block;
  166. margin-bottom: 6px;
  167. }
  168. .timeline-node-dim.灵感点 { background: rgba(255,107,107,0.2); color: #ff6b6b; }
  169. .timeline-node-dim.目的点 { background: rgba(78,205,196,0.2); color: #4ecdc4; }
  170. .timeline-node-dim.关键点 { background: rgba(255,230,109,0.2); color: #ffe66d; }
  171. .timeline-reason {
  172. font-size: 11px;
  173. color: #888;
  174. line-height: 1.5;
  175. background: #0f3460;
  176. padding: 8px;
  177. border-radius: 4px;
  178. }
  179. .timeline-from {
  180. font-size: 11px;
  181. color: #666;
  182. margin-bottom: 4px;
  183. }
  184. .timeline-from span { color: #e94560; }
  185. /* 节点样式 */
  186. .node { cursor: pointer; }
  187. .node circle { stroke-width: 2px; transition: all 0.3s; }
  188. .node.unknown circle { fill: #2a2a4a !important; stroke: #444 !important; opacity: 0.4; }
  189. .node.known circle { filter: drop-shadow(0 0 6px currentColor); }
  190. .node.highlight circle { stroke-width: 4px; filter: drop-shadow(0 0 12px currentColor); }
  191. .node text { font-size: 11px; fill: #ccc; text-anchor: middle; pointer-events: none; }
  192. .node.unknown text { fill: #555; }
  193. .node .order-badge { font-size: 10px; font-weight: bold; fill: white; }
  194. .link { stroke-opacity: 0.6; transition: all 0.3s; }
  195. .link.hidden { stroke-opacity: 0 !important; }
  196. .link.highlight { stroke-opacity: 1; stroke-width: 3px; }
  197. .link-label { font-size: 9px; fill: #888; pointer-events: none; }
  198. .link-label.hidden { opacity: 0; }
  199. </style>
  200. </head>
  201. <body>
  202. <div class="container">
  203. <header>
  204. <h1>创作思维路径</h1>
  205. <div class="controls">
  206. <div class="slider-container">
  207. <label>步骤:</label>
  208. <input type="range" id="stepSlider" min="0" max="9" value="9">
  209. <span id="stepDisplay">9/9</span>
  210. </div>
  211. <button class="play-btn" id="playBtn">▶ 播放</button>
  212. </div>
  213. <div class="legend">
  214. <div class="legend-item"><span class="legend-dot" style="background: #ff6b6b;"></span><span>灵感点</span></div>
  215. <div class="legend-item"><span class="legend-dot" style="background: #4ecdc4;"></span><span>目的点</span></div>
  216. <div class="legend-item"><span class="legend-dot" style="background: #ffe66d;"></span><span>关键点</span></div>
  217. <div class="legend-item"><span class="legend-line" style="background: #e94560;"></span><span>AI推导</span></div>
  218. </div>
  219. </header>
  220. <main>
  221. <div class="graph-area">
  222. <svg id="graph"></svg>
  223. </div>
  224. <div class="right-panel">
  225. <div class="post-summary" id="postSummary"></div>
  226. <div class="timeline" id="timeline">
  227. <div class="timeline-title">推导过程</div>
  228. </div>
  229. </div>
  230. </main>
  231. </div>
  232. <script>
  233. const DATA = __DATA_PLACEHOLDER__;
  234. const dimColors = { '灵感点': '#ff6b6b', '目的点': '#4ecdc4', '关键点': '#ffe66d' };
  235. // 提取数据
  236. const postDetail = DATA['帖子详情'];
  237. const steps = DATA['步骤列表'];
  238. const lastStep = steps[steps.length - 1];
  239. const nodes = lastStep['输出']['节点列表'];
  240. const edges = lastStep['输出']['边列表'].filter(e => e['关系类型'] === 'AI推导');
  241. const maxOrder = Math.max(...nodes.map(n => n['发现编号'] || 0));
  242. // 构建边的查找表(目标节点 -> 边)
  243. const edgeByTarget = {};
  244. edges.forEach(e => { edgeByTarget[e['目标']] = e; });
  245. const nodeById = {};
  246. const graphNodes = nodes.map(n => {
  247. const node = {
  248. id: n['节点ID'],
  249. name: n['节点名称'],
  250. dimension: n['节点维度'],
  251. category: n['节点分类'],
  252. description: n['节点描述'],
  253. order: n['发现编号'],
  254. isKnown: n['是否已知']
  255. };
  256. nodeById[node.id] = node;
  257. return node;
  258. });
  259. const graphLinks = edges.map(e => ({
  260. source: e['来源'],
  261. target: e['目标'],
  262. type: e['关系类型'],
  263. score: e['可能性分数'],
  264. reason: e['推理说明']
  265. }));
  266. // 渲染帖子摘要
  267. function renderPostSummary() {
  268. const container = document.getElementById('postSummary');
  269. const bodyText = postDetail.body_text || '';
  270. const shortBody = bodyText.length > 150 ? bodyText.slice(0, 150) + '...' : bodyText;
  271. container.innerHTML = `
  272. <div class="post-title">${postDetail.postTitle || '无标题'}</div>
  273. <div class="post-body">${shortBody}</div>
  274. <div class="post-meta">
  275. <span>❤️ ${postDetail.like_count || 0}</span>
  276. <span>⭐ ${postDetail.collect_count || 0}</span>
  277. <span>${postDetail.publish_time || ''}</span>
  278. </div>
  279. `;
  280. }
  281. // 渲染时间轴
  282. function renderTimeline() {
  283. const container = document.getElementById('timeline');
  284. // 按发现顺序排序节点
  285. const sortedNodes = [...graphNodes].filter(n => n.order).sort((a, b) => a.order - b.order);
  286. let html = '<div class="timeline-title">推导过程</div>';
  287. sortedNodes.forEach(n => {
  288. const edge = edgeByTarget[n.id];
  289. const fromNode = edge ? nodeById[edge.source] : null;
  290. html += `
  291. <div class="timeline-item" data-order="${n.order}" data-node-id="${n.id}">
  292. <div class="timeline-dot">${n.order}</div>
  293. <div class="timeline-node-name">${n.name}</div>
  294. <div class="timeline-node-dim ${n.dimension}">${n.dimension}</div>
  295. ${fromNode ? `<div class="timeline-from">← 从 <span>${fromNode.name}</span> 推导</div>` : '<div class="timeline-from">起点</div>'}
  296. ${edge ? `<div class="timeline-reason">${edge.reason || ''}</div>` : ''}
  297. </div>
  298. `;
  299. });
  300. container.innerHTML = html;
  301. // 绑定点击事件
  302. container.querySelectorAll('.timeline-item').forEach(item => {
  303. item.addEventListener('click', () => {
  304. const order = parseInt(item.dataset.order);
  305. const nodeId = item.dataset.nodeId;
  306. slider.value = order;
  307. updateDisplay(order);
  308. highlightNode(nodeId);
  309. });
  310. item.addEventListener('mouseenter', () => {
  311. const nodeId = item.dataset.nodeId;
  312. highlightNode(nodeId);
  313. });
  314. item.addEventListener('mouseleave', () => {
  315. clearHighlight();
  316. });
  317. });
  318. }
  319. let currentStep = maxOrder;
  320. const slider = document.getElementById('stepSlider');
  321. const stepDisplay = document.getElementById('stepDisplay');
  322. const playBtn = document.getElementById('playBtn');
  323. slider.max = maxOrder;
  324. slider.value = maxOrder;
  325. const svg = d3.select('#graph');
  326. const graphArea = document.querySelector('.graph-area');
  327. const width = graphArea.clientWidth;
  328. const height = graphArea.clientHeight;
  329. const g = svg.append('g');
  330. const zoom = d3.zoom()
  331. .scaleExtent([0.2, 3])
  332. .on('zoom', (event) => g.attr('transform', event.transform));
  333. svg.call(zoom);
  334. svg.append('defs').append('marker')
  335. .attr('id', 'arrow')
  336. .attr('viewBox', '0 -5 10 10')
  337. .attr('refX', 20)
  338. .attr('refY', 0)
  339. .attr('markerWidth', 6)
  340. .attr('markerHeight', 6)
  341. .attr('orient', 'auto')
  342. .append('path')
  343. .attr('d', 'M0,-5L10,0L0,5')
  344. .attr('fill', '#e94560');
  345. const simulation = d3.forceSimulation(graphNodes)
  346. .force('link', d3.forceLink(graphLinks).id(d => d.id).distance(120))
  347. .force('charge', d3.forceManyBody().strength(-400))
  348. .force('center', d3.forceCenter(width / 2, height / 2))
  349. .force('collision', d3.forceCollide().radius(50));
  350. const link = g.append('g')
  351. .selectAll('line')
  352. .data(graphLinks)
  353. .join('line')
  354. .attr('class', 'link')
  355. .attr('stroke', '#e94560')
  356. .attr('stroke-width', 2)
  357. .attr('marker-end', 'url(#arrow)');
  358. const linkLabel = g.append('g')
  359. .selectAll('text')
  360. .data(graphLinks)
  361. .join('text')
  362. .attr('class', 'link-label')
  363. .text(d => d.score?.toFixed(2) || '');
  364. const node = g.append('g')
  365. .selectAll('g')
  366. .data(graphNodes)
  367. .join('g')
  368. .attr('class', d => `node dim-${d.dimension}`)
  369. .call(d3.drag()
  370. .on('start', dragstarted)
  371. .on('drag', dragged)
  372. .on('end', dragended))
  373. .on('click', (event, d) => {
  374. slider.value = d.order || maxOrder;
  375. updateDisplay(d.order || maxOrder);
  376. })
  377. .on('mouseenter', (event, d) => highlightNode(d.id))
  378. .on('mouseleave', () => clearHighlight());
  379. node.append('circle')
  380. .attr('r', 18)
  381. .attr('fill', d => dimColors[d.dimension] || '#888')
  382. .attr('stroke', d => dimColors[d.dimension] || '#888');
  383. node.append('text')
  384. .attr('class', 'order-badge')
  385. .attr('dy', 4)
  386. .text(d => d.order || '');
  387. node.append('text')
  388. .attr('dy', 35)
  389. .text(d => d.name.length > 6 ? d.name.slice(0, 6) + '…' : d.name);
  390. simulation.on('tick', () => {
  391. link.attr('x1', d => d.source.x).attr('y1', d => d.source.y)
  392. .attr('x2', d => d.target.x).attr('y2', d => d.target.y);
  393. linkLabel.attr('x', d => (d.source.x + d.target.x) / 2)
  394. .attr('y', d => (d.source.y + d.target.y) / 2 - 8);
  395. node.attr('transform', d => `translate(${d.x},${d.y})`);
  396. });
  397. function dragstarted(event, d) {
  398. if (!event.active) simulation.alphaTarget(0.3).restart();
  399. d.fx = d.x; d.fy = d.y;
  400. }
  401. function dragged(event, d) { d.fx = event.x; d.fy = event.y; }
  402. function dragended(event, d) {
  403. if (!event.active) simulation.alphaTarget(0);
  404. d.fx = null; d.fy = null;
  405. }
  406. // 高亮节点
  407. function highlightNode(nodeId) {
  408. node.classed('highlight', d => d.id === nodeId);
  409. // 高亮相关边
  410. link.classed('highlight', d => d.target.id === nodeId || d.target === nodeId);
  411. // 高亮时间轴项
  412. document.querySelectorAll('.timeline-item').forEach(item => {
  413. item.classList.toggle('active', item.dataset.nodeId === nodeId);
  414. });
  415. }
  416. function clearHighlight() {
  417. node.classed('highlight', false);
  418. link.classed('highlight', false);
  419. }
  420. function updateDisplay(step) {
  421. currentStep = step;
  422. stepDisplay.textContent = `${step}/${maxOrder}`;
  423. node.classed('known', d => d.order && d.order <= step);
  424. node.classed('unknown', d => !d.order || d.order > step);
  425. link.classed('hidden', d => {
  426. const srcKnown = nodeById[d.source.id || d.source]?.order <= step;
  427. const tgtKnown = nodeById[d.target.id || d.target]?.order <= step;
  428. return !srcKnown || !tgtKnown;
  429. });
  430. linkLabel.classed('hidden', d => {
  431. const srcKnown = nodeById[d.source.id || d.source]?.order <= step;
  432. const tgtKnown = nodeById[d.target.id || d.target]?.order <= step;
  433. return !srcKnown || !tgtKnown;
  434. });
  435. // 更新时间轴状态
  436. document.querySelectorAll('.timeline-item').forEach(item => {
  437. const order = parseInt(item.dataset.order);
  438. item.classList.toggle('future', order > step);
  439. });
  440. }
  441. slider.addEventListener('input', (e) => updateDisplay(parseInt(e.target.value)));
  442. let playing = false, playInterval = null;
  443. playBtn.addEventListener('click', () => {
  444. if (playing) {
  445. clearInterval(playInterval);
  446. playBtn.textContent = '▶ 播放';
  447. playing = false;
  448. } else {
  449. if (currentStep >= maxOrder) { currentStep = 0; slider.value = 0; updateDisplay(0); }
  450. playing = true;
  451. playBtn.textContent = '⏸ 暂停';
  452. playInterval = setInterval(() => {
  453. currentStep++;
  454. slider.value = currentStep;
  455. updateDisplay(currentStep);
  456. // 滚动时间轴到当前项
  457. const currentItem = document.querySelector(`.timeline-item[data-order="${currentStep}"]`);
  458. if (currentItem) currentItem.scrollIntoView({ behavior: 'smooth', block: 'center' });
  459. if (currentStep >= maxOrder) {
  460. clearInterval(playInterval);
  461. playBtn.textContent = '▶ 播放';
  462. playing = false;
  463. }
  464. }, 1200);
  465. }
  466. });
  467. // 初始化
  468. renderPostSummary();
  469. renderTimeline();
  470. updateDisplay(maxOrder);
  471. setTimeout(() => {
  472. const bounds = g.node().getBBox();
  473. const scale = Math.min((width - 60) / bounds.width, (height - 60) / bounds.height, 1.2);
  474. const tx = (width - bounds.width * scale) / 2 - bounds.x * scale;
  475. const ty = (height - bounds.height * scale) / 2 - bounds.y * scale;
  476. svg.transition().duration(500).call(zoom.transform, d3.zoomIdentity.translate(tx, ty).scale(scale));
  477. }, 1000);
  478. </script>
  479. </body>
  480. </html>
  481. '''
  482. def main():
  483. import argparse
  484. parser = argparse.ArgumentParser(description='构建创作思维路径可视化')
  485. parser.add_argument('input_dir', nargs='?', help='输入目录路径(可选,默认使用 PathConfig)')
  486. args = parser.parse_args()
  487. if args.input_dir:
  488. input_dir = Path(args.input_dir)
  489. print(f"输入目录: {input_dir}")
  490. else:
  491. config = PathConfig()
  492. input_dir = config.intermediate_dir / "creation_pattern"
  493. print(f"账号: {config.account_name}")
  494. print(f"输入目录: {input_dir}")
  495. if not input_dir.exists():
  496. print(f"错误: 目录不存在!")
  497. sys.exit(1)
  498. # 查找所有创作模式文件
  499. pattern_files = sorted(input_dir.glob("*_创作模式.json"))
  500. print(f"找到 {len(pattern_files)} 个创作模式文件")
  501. if not pattern_files:
  502. print("错误: 没有找到创作模式文件!")
  503. sys.exit(1)
  504. # 为每个文件生成 HTML
  505. for pattern_file in pattern_files:
  506. post_id = pattern_file.stem.replace("_创作模式", "")
  507. print(f"\n处理: {post_id}")
  508. # 读取数据
  509. with open(pattern_file, "r", encoding="utf-8") as f:
  510. data = json.load(f)
  511. # 生成 HTML
  512. data_json = json.dumps(data, ensure_ascii=False)
  513. html_content = HTML_TEMPLATE.replace("__DATA_PLACEHOLDER__", data_json)
  514. # 输出文件
  515. output_file = input_dir / f"{post_id}_创作思维路径.html"
  516. with open(output_file, "w", encoding="utf-8") as f:
  517. f.write(html_content)
  518. print(f"输出: {output_file}")
  519. print("\n完成!")
  520. if __name__ == "__main__":
  521. main()