visualize_match_graph.py 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. 将匹配图谱数据可视化为交互式HTML文件
  5. 输入:match_graph目录下的JSON文件
  6. 输出:单个HTML文件,包含所有帖子的图谱,可通过Tab切换
  7. """
  8. import json
  9. from pathlib import Path
  10. from typing import Dict, List
  11. import sys
  12. # 添加项目根目录到路径
  13. project_root = Path(__file__).parent.parent.parent
  14. sys.path.insert(0, str(project_root))
  15. from script.data_processing.path_config import PathConfig
  16. HTML_TEMPLATE = '''<!DOCTYPE html>
  17. <html lang="zh-CN">
  18. <head>
  19. <meta charset="UTF-8">
  20. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  21. <title>匹配图谱可视化</title>
  22. <script src="https://d3js.org/d3.v7.min.js"></script>
  23. <style>
  24. * {{
  25. margin: 0;
  26. padding: 0;
  27. box-sizing: border-box;
  28. }}
  29. body {{
  30. font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  31. background: #1a1a2e;
  32. color: #eee;
  33. overflow: hidden;
  34. }}
  35. #container {{
  36. display: flex;
  37. height: 100vh;
  38. flex-direction: column;
  39. }}
  40. /* Tab样式 */
  41. .tabs {{
  42. display: flex;
  43. background: #0f3460;
  44. padding: 0 20px;
  45. overflow-x: auto;
  46. flex-shrink: 0;
  47. }}
  48. .tab {{
  49. padding: 12px 20px;
  50. cursor: pointer;
  51. border-bottom: 3px solid transparent;
  52. white-space: nowrap;
  53. font-size: 13px;
  54. color: #888;
  55. transition: all 0.2s;
  56. }}
  57. .tab:hover {{
  58. color: #fff;
  59. background: rgba(255,255,255,0.05);
  60. }}
  61. .tab.active {{
  62. color: #e94560;
  63. border-bottom-color: #e94560;
  64. background: rgba(233, 69, 96, 0.1);
  65. }}
  66. /* 主内容区 */
  67. .main-content {{
  68. display: flex;
  69. flex: 1;
  70. overflow: hidden;
  71. }}
  72. #graph {{
  73. flex: 1;
  74. position: relative;
  75. }}
  76. #sidebar {{
  77. width: 280px;
  78. background: #16213e;
  79. padding: 15px;
  80. overflow-y: auto;
  81. border-left: 1px solid #0f3460;
  82. }}
  83. h1 {{
  84. font-size: 15px;
  85. margin-bottom: 10px;
  86. color: #e94560;
  87. }}
  88. h2 {{
  89. font-size: 12px;
  90. margin: 10px 0 6px;
  91. color: #0f9b8e;
  92. }}
  93. .legend {{
  94. margin-top: 10px;
  95. }}
  96. .legend-grid {{
  97. display: grid;
  98. grid-template-columns: 1fr 1fr;
  99. gap: 4px 8px;
  100. }}
  101. .legend-item {{
  102. display: flex;
  103. align-items: center;
  104. font-size: 11px;
  105. }}
  106. .legend-color {{
  107. width: 12px;
  108. height: 12px;
  109. border-radius: 50%;
  110. margin-right: 6px;
  111. flex-shrink: 0;
  112. }}
  113. .legend-line {{
  114. width: 20px;
  115. height: 3px;
  116. margin-right: 6px;
  117. flex-shrink: 0;
  118. }}
  119. .detail-panel {{
  120. margin-top: 20px;
  121. padding: 15px;
  122. background: #0f3460;
  123. border-radius: 8px;
  124. display: none;
  125. }}
  126. .detail-panel.active {{
  127. display: block;
  128. }}
  129. .detail-panel h3 {{
  130. font-size: 14px;
  131. margin-bottom: 10px;
  132. color: #e94560;
  133. }}
  134. .detail-panel p {{
  135. font-size: 12px;
  136. line-height: 1.6;
  137. color: #ccc;
  138. margin: 5px 0;
  139. }}
  140. .detail-panel .label {{
  141. color: #888;
  142. }}
  143. .detail-panel .close-btn {{
  144. position: absolute;
  145. top: 10px;
  146. right: 10px;
  147. background: none;
  148. border: none;
  149. color: #888;
  150. cursor: pointer;
  151. font-size: 16px;
  152. }}
  153. .detail-panel .close-btn:hover {{
  154. color: #e94560;
  155. }}
  156. .detail-panel-wrapper {{
  157. position: relative;
  158. }}
  159. .similarity-score {{
  160. background: #e94560;
  161. color: #fff;
  162. padding: 2px 6px;
  163. border-radius: 4px;
  164. font-weight: bold;
  165. }}
  166. .edge-description {{
  167. background: #1a1a2e;
  168. padding: 10px;
  169. border-radius: 4px;
  170. margin-top: 8px;
  171. font-size: 11px;
  172. line-height: 1.5;
  173. }}
  174. svg {{
  175. width: 100%;
  176. height: 100%;
  177. }}
  178. .node {{
  179. cursor: pointer;
  180. }}
  181. .node circle, .node rect {{
  182. stroke-width: 3px;
  183. }}
  184. .node .post-node {{
  185. stroke: #fff;
  186. stroke-dasharray: 4,2;
  187. }}
  188. .node .persona-node {{
  189. stroke: #fff;
  190. }}
  191. .node text {{
  192. font-size: 11px;
  193. fill: #fff;
  194. pointer-events: none;
  195. }}
  196. .link {{
  197. stroke-opacity: 0.7;
  198. }}
  199. .link-hitarea {{
  200. stroke: transparent;
  201. stroke-width: 15px;
  202. cursor: pointer;
  203. fill: none;
  204. }}
  205. .link-hitarea:hover + .link {{
  206. stroke-opacity: 1;
  207. stroke-width: 3px;
  208. }}
  209. .edge-label {{
  210. font-size: 10px;
  211. fill: #fff;
  212. pointer-events: none;
  213. text-anchor: middle;
  214. }}
  215. .edge-label-bg {{
  216. fill: rgba(0,0,0,0.7);
  217. }}
  218. .link.match {{
  219. stroke: #e94560;
  220. stroke-dasharray: 5,5;
  221. }}
  222. .link.category-cross {{
  223. stroke: #2ecc71;
  224. }}
  225. .link.category-intra {{
  226. stroke: #27ae60;
  227. stroke-dasharray: 3,3;
  228. }}
  229. .link.tag-cooccur {{
  230. stroke: #f39c12;
  231. }}
  232. .link.belong {{
  233. stroke: #9b59b6;
  234. }}
  235. .link.contain {{
  236. stroke: #8e44ad;
  237. stroke-dasharray: 2,2;
  238. }}
  239. /* 镜像边样式(虚线,颜色与原边相同) */
  240. .link.mirror-category-cross {{
  241. stroke: #2ecc71;
  242. stroke-dasharray: 6,3;
  243. }}
  244. .link.mirror-category-intra {{
  245. stroke: #27ae60;
  246. stroke-dasharray: 6,3;
  247. }}
  248. .link.mirror-tag-cooccur {{
  249. stroke: #f39c12;
  250. stroke-dasharray: 6,3;
  251. }}
  252. .link.mirror-belong {{
  253. stroke: #9b59b6;
  254. stroke-dasharray: 6,3;
  255. }}
  256. .link.mirror-contain {{
  257. stroke: #8e44ad;
  258. stroke-dasharray: 6,3;
  259. }}
  260. /* 二阶镜像边样式(点划线) */
  261. .link.second-order {{
  262. stroke: #17a2b8;
  263. stroke-dasharray: 8,3,2,3;
  264. }}
  265. /* 高亮/灰化样式 */
  266. .node.dimmed circle, .node.dimmed rect {{
  267. opacity: 0.15 !important;
  268. }}
  269. .node.dimmed text {{
  270. opacity: 0.15 !important;
  271. }}
  272. .link-group.dimmed .link {{
  273. stroke-opacity: 0.08 !important;
  274. }}
  275. .link-group.dimmed .edge-label-group {{
  276. opacity: 0.15 !important;
  277. }}
  278. .node.highlighted circle, .node.highlighted rect {{
  279. stroke: #fff !important;
  280. stroke-width: 4px !important;
  281. filter: drop-shadow(0 0 8px rgba(255,255,255,0.5));
  282. }}
  283. .link-group.highlighted .link {{
  284. stroke-opacity: 1 !important;
  285. stroke-width: 3px !important;
  286. filter: drop-shadow(0 0 4px rgba(255,255,255,0.3));
  287. }}
  288. .tooltip {{
  289. position: absolute;
  290. background: rgba(0,0,0,0.9);
  291. color: #fff;
  292. padding: 10px 15px;
  293. border-radius: 6px;
  294. font-size: 12px;
  295. pointer-events: none;
  296. max-width: 300px;
  297. z-index: 1000;
  298. display: none;
  299. }}
  300. .controls {{
  301. position: absolute;
  302. top: 20px;
  303. left: 20px;
  304. background: rgba(22, 33, 62, 0.9);
  305. padding: 15px;
  306. border-radius: 8px;
  307. z-index: 100;
  308. }}
  309. .controls button {{
  310. background: #0f3460;
  311. color: #fff;
  312. border: none;
  313. padding: 8px 15px;
  314. margin: 5px;
  315. border-radius: 4px;
  316. cursor: pointer;
  317. font-size: 12px;
  318. }}
  319. .controls button:hover {{
  320. background: #e94560;
  321. }}
  322. </style>
  323. </head>
  324. <body>
  325. <div id="container">
  326. <div class="tabs" id="tabs">
  327. {tabs_html}
  328. </div>
  329. <div class="main-content">
  330. <div id="graph">
  331. <div class="controls">
  332. <button onclick="resetZoom()">重置视图</button>
  333. <button onclick="toggleLabels()">切换标签</button>
  334. </div>
  335. <div class="tooltip" id="tooltip"></div>
  336. </div>
  337. <div id="sidebar">
  338. <h1>匹配图谱</h1>
  339. <div class="detail-panel active" id="detailPanel">
  340. <h3 id="detailTitle">点击节点或边查看详情</h3>
  341. <div id="detailContent">
  342. <p style="color: #888; font-size: 11px;">点击图中的节点或边,这里会显示详细信息</p>
  343. </div>
  344. </div>
  345. <div class="legend">
  346. <h2>节点</h2>
  347. <div class="legend-grid">
  348. <div class="legend-item">
  349. <div class="legend-color" style="background: #666; border: 2px dashed #fff;"></div>
  350. <span>帖子(虚线)</span>
  351. </div>
  352. <div class="legend-item">
  353. <div class="legend-color" style="background: #666; border: 2px solid #fff;"></div>
  354. <span>人设(实线)</span>
  355. </div>
  356. <div class="legend-item">
  357. <div class="legend-color" style="background: #666; border-radius: 50%;"></div>
  358. <span>标签(圆)</span>
  359. </div>
  360. <div class="legend-item">
  361. <div class="legend-color" style="background: #666; border-radius: 2px;"></div>
  362. <span>分类(方)</span>
  363. </div>
  364. <div class="legend-item">
  365. <div class="legend-color" style="background: #666; opacity: 1;"></div>
  366. <span>直接匹配</span>
  367. </div>
  368. <div class="legend-item">
  369. <div class="legend-color" style="background: #666; opacity: 0.5;"></div>
  370. <span>扩展节点</span>
  371. </div>
  372. <div class="legend-item">
  373. <div class="legend-color" style="background: #f39c12;"></div>
  374. <span>灵感点</span>
  375. </div>
  376. <div class="legend-item">
  377. <div class="legend-color" style="background: #3498db;"></div>
  378. <span>目的点</span>
  379. </div>
  380. <div class="legend-item">
  381. <div class="legend-color" style="background: #9b59b6;"></div>
  382. <span>关键点</span>
  383. </div>
  384. </div>
  385. <h2>边(人设/实线)</h2>
  386. <div class="legend-grid">
  387. <div class="legend-item">
  388. <div class="legend-line" style="background: #e94560;"></div>
  389. <span>匹配</span>
  390. </div>
  391. <div class="legend-item">
  392. <div class="legend-line" style="background: #2ecc71;"></div>
  393. <span>分类共现(跨)</span>
  394. </div>
  395. <div class="legend-item">
  396. <div class="legend-line" style="background: #27ae60;"></div>
  397. <span>分类共现(内)</span>
  398. </div>
  399. <div class="legend-item">
  400. <div class="legend-line" style="background: #f39c12;"></div>
  401. <span>标签共现</span>
  402. </div>
  403. <div class="legend-item">
  404. <div class="legend-line" style="background: #9b59b6;"></div>
  405. <span>属于</span>
  406. </div>
  407. <div class="legend-item">
  408. <div class="legend-line" style="background: #8e44ad;"></div>
  409. <span>包含</span>
  410. </div>
  411. </div>
  412. <h2>帖子镜像边(直接)</h2>
  413. <div class="legend-grid">
  414. <div class="legend-item">
  415. <div class="legend-line" style="background: repeating-linear-gradient(90deg, #2ecc71, #2ecc71 6px, transparent 6px, transparent 9px);"></div>
  416. <span>分类共现</span>
  417. </div>
  418. <div class="legend-item">
  419. <div class="legend-line" style="background: repeating-linear-gradient(90deg, #f39c12, #f39c12 6px, transparent 6px, transparent 9px);"></div>
  420. <span>标签共现</span>
  421. </div>
  422. <div class="legend-item">
  423. <div class="legend-line" style="background: repeating-linear-gradient(90deg, #9b59b6, #9b59b6 6px, transparent 6px, transparent 9px);"></div>
  424. <span>属于</span>
  425. </div>
  426. <div class="legend-item">
  427. <div class="legend-line" style="background: repeating-linear-gradient(90deg, #8e44ad, #8e44ad 6px, transparent 6px, transparent 9px);"></div>
  428. <span>包含</span>
  429. </div>
  430. </div>
  431. <h2>帖子镜像边(二阶)</h2>
  432. <div class="legend-grid">
  433. <div class="legend-item">
  434. <div class="legend-line" style="background: repeating-linear-gradient(90deg, #17a2b8, #17a2b8 8px, transparent 8px, transparent 11px, #17a2b8 11px, #17a2b8 13px, transparent 13px, transparent 16px);"></div>
  435. <span>通过扩展节点</span>
  436. </div>
  437. </div>
  438. </div>
  439. </div>
  440. </div>
  441. </div>
  442. <script>
  443. // 所有帖子的图谱数据
  444. const allGraphData = {all_graph_data};
  445. // 当前选中的帖子索引
  446. let currentIndex = 0;
  447. let simulation = null;
  448. let svg = null;
  449. let g = null;
  450. let zoom = null;
  451. let showLabels = true;
  452. // 初始化
  453. function init() {{
  454. const container = document.getElementById("graph");
  455. const width = container.clientWidth;
  456. const height = container.clientHeight;
  457. svg = d3.select("#graph")
  458. .append("svg")
  459. .attr("width", width)
  460. .attr("height", height);
  461. g = svg.append("g");
  462. zoom = d3.zoom()
  463. .scaleExtent([0.1, 4])
  464. .on("zoom", (event) => {{
  465. g.attr("transform", event.transform);
  466. }});
  467. svg.call(zoom);
  468. // 绑定Tab点击事件
  469. document.querySelectorAll(".tab").forEach((tab, index) => {{
  470. tab.addEventListener("click", () => switchTab(index));
  471. }});
  472. // 显示第一个帖子
  473. switchTab(0);
  474. }}
  475. // 切换Tab
  476. function switchTab(index) {{
  477. currentIndex = index;
  478. // 更新Tab样式
  479. document.querySelectorAll(".tab").forEach((tab, i) => {{
  480. tab.classList.toggle("active", i === index);
  481. }});
  482. // 更新图谱
  483. renderGraph(allGraphData[index]);
  484. }}
  485. // 渲染图谱
  486. function renderGraph(data) {{
  487. // 清空现有图谱
  488. g.selectAll("*").remove();
  489. if (simulation) {{
  490. simulation.stop();
  491. }}
  492. const container = document.getElementById("graph");
  493. const width = container.clientWidth;
  494. const height = container.clientHeight;
  495. // 准备数据
  496. const nodes = data.nodes.map(n => ({{
  497. ...n,
  498. id: n.节点ID,
  499. source: n.节点ID.startsWith("帖子_") ? "帖子" : "人设",
  500. level: n.节点层级
  501. }}));
  502. const links = data.edges.map(e => ({{
  503. ...e,
  504. source: e.源节点ID,
  505. target: e.目标节点ID,
  506. type: e.边类型
  507. }}));
  508. // 分离节点类型
  509. const postNodes = nodes.filter(n => n.source === "帖子");
  510. const personaNodes = nodes.filter(n => n.source === "人设" && !n.是否扩展);
  511. const expandedNodes = nodes.filter(n => n.source === "人设" && n.是否扩展);
  512. const matchLinks = links.filter(l => l.type === "匹配");
  513. // 构建帖子节点到人设节点的映射
  514. const postToPersona = {{}};
  515. const personaToPost = {{}};
  516. matchLinks.forEach(l => {{
  517. const sid = typeof l.source === "object" ? l.source.id : l.source;
  518. const tid = typeof l.target === "object" ? l.target.id : l.target;
  519. if (!postToPersona[sid]) postToPersona[sid] = [];
  520. postToPersona[sid].push(tid);
  521. if (!personaToPost[tid]) personaToPost[tid] = [];
  522. personaToPost[tid].push(sid);
  523. }});
  524. // 找出所有连通分量
  525. function findConnectedComponents(nodes, links) {{
  526. const nodeIds = new Set(nodes.map(n => n.id));
  527. const adj = {{}};
  528. nodeIds.forEach(id => adj[id] = []);
  529. links.forEach(l => {{
  530. const sid = typeof l.source === "object" ? l.source.id : l.source;
  531. const tid = typeof l.target === "object" ? l.target.id : l.target;
  532. if (nodeIds.has(sid) && nodeIds.has(tid)) {{
  533. adj[sid].push(tid);
  534. adj[tid].push(sid);
  535. }}
  536. }});
  537. const visited = new Set();
  538. const components = [];
  539. nodeIds.forEach(startId => {{
  540. if (visited.has(startId)) return;
  541. const component = [];
  542. const queue = [startId];
  543. while (queue.length > 0) {{
  544. const id = queue.shift();
  545. if (visited.has(id)) continue;
  546. visited.add(id);
  547. component.push(id);
  548. adj[id].forEach(neighbor => {{
  549. if (!visited.has(neighbor)) queue.push(neighbor);
  550. }});
  551. }}
  552. components.push(component);
  553. }});
  554. return components;
  555. }}
  556. // 按大小排序连通分量(大的在前)
  557. const components = findConnectedComponents(nodes, links)
  558. .sort((a, b) => b.length - a.length);
  559. console.log(`找到 ${{components.length}} 个连通分量`);
  560. // 为每个节点分配连通分量ID和分量内的X范围
  561. const nodeToComponent = {{}};
  562. const componentCenters = {{}};
  563. const componentBounds = {{}};
  564. const padding = 50; // 分量之间的间距
  565. const totalPadding = padding * (components.length - 1);
  566. const availableWidth = width - totalPadding - 100; // 留边距
  567. // 根据分量大小分配宽度
  568. const totalNodes = nodes.length;
  569. let currentX = 50; // 起始边距
  570. components.forEach((comp, i) => {{
  571. const compWidth = Math.max(150, (comp.length / totalNodes) * availableWidth);
  572. const centerX = currentX + compWidth / 2;
  573. componentCenters[i] = centerX;
  574. componentBounds[i] = {{ start: currentX, end: currentX + compWidth, width: compWidth }};
  575. comp.forEach(nodeId => {{
  576. nodeToComponent[nodeId] = i;
  577. }});
  578. currentX += compWidth + padding;
  579. }});
  580. // 使用重心法(Barycenter)减少边交叉
  581. // 迭代优化:交替调整两层节点的顺序
  582. const nodeTargetX = {{}};
  583. const personaXMap = {{}};
  584. // 对每个连通分量单独处理
  585. components.forEach((comp, compIdx) => {{
  586. const bounds = componentBounds[compIdx];
  587. const compPostNodes = postNodes.filter(n => nodeToComponent[n.id] === compIdx);
  588. const compPersonaNodes = personaNodes.filter(n => nodeToComponent[n.id] === compIdx);
  589. if (compPostNodes.length === 0 || compPersonaNodes.length === 0) {{
  590. // 没有匹配关系的分量,均匀分布
  591. const spacing = bounds.width / (comp.length + 1);
  592. comp.forEach((nodeId, i) => {{
  593. const node = nodes.find(n => n.id === nodeId);
  594. if (node) {{
  595. node.x = bounds.start + spacing * (i + 1);
  596. nodeTargetX[nodeId] = node.x;
  597. if (node.source === "人设") personaXMap[nodeId] = node.x;
  598. }}
  599. }});
  600. return;
  601. }}
  602. // 初始化:给人设节点一个初始顺序
  603. let personaOrder = compPersonaNodes.map((n, i) => ({{ node: n, order: i }}));
  604. // 迭代优化(3轮)
  605. for (let iter = 0; iter < 3; iter++) {{
  606. // 1. 根据人设节点位置,计算帖子节点的重心
  607. const postBarycenter = {{}};
  608. compPostNodes.forEach(pn => {{
  609. const matched = postToPersona[pn.id] || [];
  610. if (matched.length > 0) {{
  611. const avgOrder = matched.reduce((sum, pid) => {{
  612. const po = personaOrder.find(p => p.node.id === pid);
  613. return sum + (po ? po.order : 0);
  614. }}, 0) / matched.length;
  615. postBarycenter[pn.id] = avgOrder;
  616. }} else {{
  617. postBarycenter[pn.id] = 0;
  618. }}
  619. }});
  620. // 按重心排序帖子节点
  621. const sortedPosts = [...compPostNodes].sort((a, b) =>
  622. postBarycenter[a.id] - postBarycenter[b.id]
  623. );
  624. // 2. 根据帖子节点位置,重新计算人设节点的重心
  625. const personaBarycenter = {{}};
  626. compPersonaNodes.forEach(pn => {{
  627. const matched = personaToPost[pn.id] || [];
  628. if (matched.length > 0) {{
  629. const avgOrder = matched.reduce((sum, pid) => {{
  630. const idx = sortedPosts.findIndex(p => p.id === pid);
  631. return sum + (idx >= 0 ? idx : 0);
  632. }}, 0) / matched.length;
  633. personaBarycenter[pn.id] = avgOrder;
  634. }} else {{
  635. personaBarycenter[pn.id] = personaOrder.find(p => p.node.id === pn.id)?.order || 0;
  636. }}
  637. }});
  638. // 更新人设节点顺序
  639. personaOrder = compPersonaNodes
  640. .map(n => ({{ node: n, order: personaBarycenter[n.id] }}))
  641. .sort((a, b) => a.order - b.order)
  642. .map((item, i) => ({{ node: item.node, order: i }}));
  643. }}
  644. // 最终排序
  645. const finalPersonaOrder = personaOrder.map(p => p.node);
  646. const postBarycenter = {{}};
  647. compPostNodes.forEach(pn => {{
  648. const matched = postToPersona[pn.id] || [];
  649. if (matched.length > 0) {{
  650. const avgOrder = matched.reduce((sum, pid) => {{
  651. const idx = finalPersonaOrder.findIndex(n => n.id === pid);
  652. return sum + (idx >= 0 ? idx : 0);
  653. }}, 0) / matched.length;
  654. postBarycenter[pn.id] = avgOrder;
  655. }} else {{
  656. postBarycenter[pn.id] = 0;
  657. }}
  658. }});
  659. const finalPostOrder = [...compPostNodes].sort((a, b) =>
  660. postBarycenter[a.id] - postBarycenter[b.id]
  661. );
  662. // 设置位置
  663. const personaSpacing = bounds.width / (finalPersonaOrder.length + 1);
  664. finalPersonaOrder.forEach((n, i) => {{
  665. n.x = bounds.start + personaSpacing * (i + 1);
  666. nodeTargetX[n.id] = n.x;
  667. personaXMap[n.id] = n.x;
  668. }});
  669. const postSpacing = bounds.width / (finalPostOrder.length + 1);
  670. finalPostOrder.forEach((n, i) => {{
  671. // 帖子节点用重心位置(匹配人设的平均X)
  672. const matched = postToPersona[n.id] || [];
  673. if (matched.length > 0) {{
  674. const avgX = matched.reduce((sum, pid) => sum + (personaXMap[pid] || bounds.start + bounds.width/2), 0) / matched.length;
  675. n.x = avgX;
  676. }} else {{
  677. n.x = bounds.start + postSpacing * (i + 1);
  678. }}
  679. nodeTargetX[n.id] = n.x;
  680. }});
  681. }});
  682. // 节点颜色
  683. const levelColors = {{
  684. "灵感点": "#f39c12",
  685. "目的点": "#3498db",
  686. "关键点": "#9b59b6"
  687. }};
  688. // 三层Y坐标(带倾斜:右边高,左边低)
  689. const postBaseY = height * 0.15; // 帖子节点(顶层)
  690. const personaBaseY = height * 0.45; // 直接匹配人设节点(中层)
  691. const expandedBaseY = height * 0.8; // 扩展节点(底层)
  692. const tiltAmount = height * 0.2; // 倾斜幅度
  693. // 根据X位置计算Y(右边高,左边低)
  694. function getTiltedY(baseY, x) {{
  695. const tilt = tiltAmount * (0.5 - x / width);
  696. return baseY + tilt;
  697. }}
  698. // 获取节点的基准Y
  699. function getNodeBaseY(d) {{
  700. if (d.source === "帖子") return postBaseY;
  701. if (d.是否扩展) return expandedBaseY;
  702. return personaBaseY;
  703. }}
  704. // 力导向模拟
  705. simulation = d3.forceSimulation(nodes)
  706. .force("link", d3.forceLink(links).id(d => d.id).distance(80).strength(0.1))
  707. .force("charge", d3.forceManyBody().strength(-300))
  708. // X方向:拉向目标位置,但允许被推开
  709. .force("x", d3.forceX(d => nodeTargetX[d.id] || width / 2).strength(0.15))
  710. // Y方向力:三层布局+倾斜
  711. .force("y", d3.forceY(d => {{
  712. const baseY = getNodeBaseY(d);
  713. return getTiltedY(baseY, d.x || width / 2);
  714. }}).strength(0.5))
  715. .force("collision", d3.forceCollide().radius(40));
  716. // 边类型到CSS类的映射
  717. const edgeTypeClass = {{
  718. "匹配": "match",
  719. "分类共现(跨点)": "category-cross",
  720. "分类共现(点内)": "category-intra",
  721. "标签共现": "tag-cooccur",
  722. "属于": "belong",
  723. "包含": "contain",
  724. // 镜像边(帖子节点之间,虚线)
  725. "镜像_分类共现(跨点)": "mirror-category-cross",
  726. "镜像_分类共现(点内)": "mirror-category-intra",
  727. "镜像_标签共现": "mirror-tag-cooccur",
  728. "镜像_属于": "mirror-belong",
  729. "镜像_包含": "mirror-contain"
  730. }};
  731. // 获取边的CSS类(处理二阶边)
  732. function getEdgeClass(edgeType) {{
  733. if (edgeTypeClass[edgeType]) return edgeTypeClass[edgeType];
  734. if (edgeType.startsWith("二阶_")) return "second-order";
  735. return "match";
  736. }}
  737. // 创建边的容器
  738. const linkGroup = g.append("g").attr("class", "links");
  739. // 为每条边创建组
  740. const linkG = linkGroup.selectAll("g")
  741. .data(links)
  742. .join("g")
  743. .attr("class", "link-group");
  744. // 绘制点击热区(透明宽线)
  745. const linkHitarea = linkG.append("line")
  746. .attr("class", "link-hitarea");
  747. // 绘制可见的边
  748. const link = linkG.append("line")
  749. .attr("class", d => "link " + getEdgeClass(d.type))
  750. .attr("stroke-width", d => d.type === "匹配" ? 2.5 : 1.5);
  751. // 为匹配边添加分数标签
  752. const edgeLabels = linkG.filter(d => d.type === "匹配" && d.边详情 && d.边详情.相似度)
  753. .append("g")
  754. .attr("class", "edge-label-group");
  755. edgeLabels.append("rect")
  756. .attr("class", "edge-label-bg")
  757. .attr("rx", 3)
  758. .attr("ry", 3);
  759. edgeLabels.append("text")
  760. .attr("class", "edge-label")
  761. .text(d => {{
  762. const score = d.边详情.相似度;
  763. return typeof score === "number" ? score.toFixed(2) : score;
  764. }});
  765. // 边的点击事件
  766. linkHitarea.on("click", (event, d, i) => {{
  767. event.stopPropagation();
  768. const linkIndex = links.indexOf(d);
  769. highlightEdge(d, linkIndex);
  770. showEdgeInfo(d);
  771. }})
  772. .on("mouseover", function(event, d) {{
  773. d3.select(this.parentNode).select(".link")
  774. .attr("stroke-opacity", 1)
  775. .attr("stroke-width", 4);
  776. }})
  777. .on("mouseout", function(event, d) {{
  778. d3.select(this.parentNode).select(".link")
  779. .attr("stroke-opacity", 0.7)
  780. .attr("stroke-width", d.type === "匹配" ? 2.5 : 1.5);
  781. }});
  782. // 绘制节点
  783. const node = g.append("g")
  784. .selectAll("g")
  785. .data(nodes)
  786. .join("g")
  787. .attr("class", "node")
  788. .call(d3.drag()
  789. .on("start", dragstarted)
  790. .on("drag", dragged)
  791. .on("end", dragended));
  792. // 根据节点类型绘制不同形状:标签用圆形,分类用方形
  793. // 扩展节点用较低透明度表示
  794. node.each(function(d) {{
  795. const el = d3.select(this);
  796. const isExpanded = d.是否扩展 === true;
  797. const size = d.source === "帖子" ? 12 : (isExpanded ? 8 : 10);
  798. const fill = levelColors[d.level] || "#666";
  799. const nodeClass = d.source === "帖子" ? "post-node" : "persona-node";
  800. const opacity = isExpanded ? 0.5 : 1;
  801. if (d.节点类型 === "分类") {{
  802. // 方形
  803. el.append("rect")
  804. .attr("width", size * 2)
  805. .attr("height", size * 2)
  806. .attr("x", -size)
  807. .attr("y", -size)
  808. .attr("fill", fill)
  809. .attr("class", nodeClass)
  810. .attr("rx", 3)
  811. .attr("opacity", opacity);
  812. }} else {{
  813. // 圆形(标签)
  814. el.append("circle")
  815. .attr("r", size)
  816. .attr("fill", fill)
  817. .attr("class", nodeClass)
  818. .attr("opacity", opacity);
  819. }}
  820. }});
  821. const labels = node.append("text")
  822. .attr("dx", 15)
  823. .attr("dy", 4)
  824. .text(d => d.节点名称)
  825. .style("display", showLabels ? "block" : "none");
  826. // 工具提示
  827. const tooltip = d3.select("#tooltip");
  828. node.on("mouseover", (event, d) => {{
  829. tooltip.style("display", "block")
  830. .html(`<strong>${{d.节点名称}}</strong><br/>类型: ${{d.节点类型}}<br/>层级: ${{d.节点层级}}`);
  831. }})
  832. .on("mousemove", (event) => {{
  833. tooltip.style("left", (event.pageX + 15) + "px")
  834. .style("top", (event.pageY - 10) + "px");
  835. }})
  836. .on("mouseout", () => {{
  837. tooltip.style("display", "none");
  838. }})
  839. .on("click", (event, d) => {{
  840. event.stopPropagation();
  841. highlightNode(d);
  842. showNodeInfo(d);
  843. }});
  844. // 更新位置
  845. simulation.on("tick", () => {{
  846. // 更新热区线
  847. linkHitarea
  848. .attr("x1", d => d.source.x)
  849. .attr("y1", d => d.source.y)
  850. .attr("x2", d => d.target.x)
  851. .attr("y2", d => d.target.y);
  852. // 更新可见边
  853. link
  854. .attr("x1", d => d.source.x)
  855. .attr("y1", d => d.source.y)
  856. .attr("x2", d => d.target.x)
  857. .attr("y2", d => d.target.y);
  858. // 更新边标签位置(放在边的中点)
  859. edgeLabels.attr("transform", d => {{
  860. const midX = (d.source.x + d.target.x) / 2;
  861. const midY = (d.source.y + d.target.y) / 2;
  862. return `translate(${{midX}},${{midY}})`;
  863. }});
  864. // 更新标签背景大小
  865. edgeLabels.each(function(d) {{
  866. const textEl = d3.select(this).select("text").node();
  867. if (textEl) {{
  868. const bbox = textEl.getBBox();
  869. d3.select(this).select("rect")
  870. .attr("x", bbox.x - 3)
  871. .attr("y", bbox.y - 1)
  872. .attr("width", bbox.width + 6)
  873. .attr("height", bbox.height + 2);
  874. }}
  875. }});
  876. node.attr("transform", d => `translate(${{d.x}},${{d.y}})`);
  877. }});
  878. // 拖拽函数
  879. function dragstarted(event, d) {{
  880. if (!event.active) simulation.alphaTarget(0.3).restart();
  881. d.fx = d.x;
  882. d.fy = d.y;
  883. }}
  884. function dragged(event, d) {{
  885. d.fx = event.x;
  886. d.fy = event.y;
  887. }}
  888. function dragended(event, d) {{
  889. if (!event.active) simulation.alphaTarget(0);
  890. d.fx = null;
  891. d.fy = null;
  892. }}
  893. // 清除所有高亮
  894. function clearHighlight() {{
  895. node.classed("dimmed", false).classed("highlighted", false);
  896. linkG.classed("dimmed", false).classed("highlighted", false);
  897. }}
  898. // 高亮指定的节点和边
  899. function highlightElements(highlightNodeIds, highlightLinkIndices) {{
  900. // 先灰化所有
  901. node.classed("dimmed", true).classed("highlighted", false);
  902. linkG.classed("dimmed", true).classed("highlighted", false);
  903. // 高亮指定节点
  904. node.filter(d => highlightNodeIds.has(d.id))
  905. .classed("dimmed", false)
  906. .classed("highlighted", true);
  907. // 高亮指定边
  908. linkG.filter((d, i) => highlightLinkIndices.has(i))
  909. .classed("dimmed", false)
  910. .classed("highlighted", true);
  911. }}
  912. // 点击节点时的高亮逻辑
  913. function highlightNode(clickedNode) {{
  914. const highlightNodeIds = new Set([clickedNode.id]);
  915. const highlightLinkIndices = new Set();
  916. links.forEach((link, i) => {{
  917. const sourceId = typeof link.source === "object" ? link.source.id : link.source;
  918. const targetId = typeof link.target === "object" ? link.target.id : link.target;
  919. // 与点击节点直接相连的边
  920. if (sourceId === clickedNode.id || targetId === clickedNode.id) {{
  921. highlightLinkIndices.add(i);
  922. highlightNodeIds.add(sourceId);
  923. highlightNodeIds.add(targetId);
  924. // 如果是帖子节点,还要高亮对应的镜像边
  925. if (clickedNode.source === "帖子") {{
  926. // 找到通过该帖子连接的其他帖子(镜像边)
  927. links.forEach((otherLink, j) => {{
  928. const otherType = otherLink.type;
  929. if (otherType.startsWith("镜像_") || otherType.startsWith("二阶_")) {{
  930. const oSrc = typeof otherLink.source === "object" ? otherLink.source.id : otherLink.source;
  931. const oTgt = typeof otherLink.target === "object" ? otherLink.target.id : otherLink.target;
  932. if (oSrc === clickedNode.id || oTgt === clickedNode.id) {{
  933. highlightLinkIndices.add(j);
  934. highlightNodeIds.add(oSrc);
  935. highlightNodeIds.add(oTgt);
  936. }}
  937. }}
  938. }});
  939. }}
  940. }}
  941. }});
  942. highlightElements(highlightNodeIds, highlightLinkIndices);
  943. }}
  944. // 点击边时的高亮逻辑
  945. function highlightEdge(clickedLink, clickedIndex) {{
  946. const highlightNodeIds = new Set();
  947. const highlightLinkIndices = new Set([clickedIndex]);
  948. const sourceId = typeof clickedLink.source === "object" ? clickedLink.source.id : clickedLink.source;
  949. const targetId = typeof clickedLink.target === "object" ? clickedLink.target.id : clickedLink.target;
  950. highlightNodeIds.add(sourceId);
  951. highlightNodeIds.add(targetId);
  952. // 如果是二阶边,显示完整路径
  953. if (clickedLink.type.startsWith("二阶_") && clickedLink.边详情) {{
  954. const detail = clickedLink.边详情;
  955. // 分类节点
  956. if (detail.分类节点1) highlightNodeIds.add(detail.分类节点1);
  957. if (detail.分类节点2) highlightNodeIds.add(detail.分类节点2);
  958. // 标签节点
  959. if (detail.标签节点1) highlightNodeIds.add(detail.标签节点1);
  960. if (detail.标签节点2) highlightNodeIds.add(detail.标签节点2);
  961. // 找出路径上的边
  962. links.forEach((link, i) => {{
  963. const lSrc = typeof link.source === "object" ? link.source.id : link.source;
  964. const lTgt = typeof link.target === "object" ? link.target.id : link.target;
  965. // 帖子->标签 的匹配边
  966. if (link.type === "匹配") {{
  967. if ((lSrc === sourceId && lTgt === detail.标签节点1) ||
  968. (lSrc === targetId && lTgt === detail.标签节点2)) {{
  969. highlightLinkIndices.add(i);
  970. }}
  971. }}
  972. // 标签->分类 的属于边
  973. if (link.type === "属于") {{
  974. if ((lSrc === detail.标签节点1 && lTgt === detail.分类节点1) ||
  975. (lSrc === detail.标签节点2 && lTgt === detail.分类节点2)) {{
  976. highlightLinkIndices.add(i);
  977. }}
  978. }}
  979. // 分类之间的边
  980. if ((lSrc === detail.分类节点1 && lTgt === detail.分类节点2) ||
  981. (lSrc === detail.分类节点2 && lTgt === detail.分类节点1)) {{
  982. highlightLinkIndices.add(i);
  983. }}
  984. }});
  985. }}
  986. // 如果是镜像边,显示对应的人设边
  987. else if (clickedLink.type.startsWith("镜像_") && clickedLink.边详情) {{
  988. const detail = clickedLink.边详情;
  989. if (detail.源人设节点) highlightNodeIds.add(detail.源人设节点);
  990. if (detail.目标人设节点) highlightNodeIds.add(detail.目标人设节点);
  991. // 找出对应的人设边和匹配边
  992. links.forEach((link, i) => {{
  993. const lSrc = typeof link.source === "object" ? link.source.id : link.source;
  994. const lTgt = typeof link.target === "object" ? link.target.id : link.target;
  995. // 匹配边
  996. if (link.type === "匹配") {{
  997. if ((lSrc === sourceId && lTgt === detail.源人设节点) ||
  998. (lSrc === targetId && lTgt === detail.目标人设节点)) {{
  999. highlightLinkIndices.add(i);
  1000. }}
  1001. }}
  1002. // 人设边
  1003. if ((lSrc === detail.源人设节点 && lTgt === detail.目标人设节点) ||
  1004. (lSrc === detail.目标人设节点 && lTgt === detail.源人设节点)) {{
  1005. highlightLinkIndices.add(i);
  1006. }}
  1007. }});
  1008. }}
  1009. highlightElements(highlightNodeIds, highlightLinkIndices);
  1010. }}
  1011. // 点击空白处清除高亮
  1012. svg.on("click", (event) => {{
  1013. if (event.target === svg.node()) {{
  1014. clearHighlight();
  1015. closeDetailPanel();
  1016. }}
  1017. }});
  1018. }}
  1019. // 控制函数
  1020. function resetZoom() {{
  1021. const container = document.getElementById("graph");
  1022. const width = container.clientWidth;
  1023. const height = container.clientHeight;
  1024. svg.transition().duration(750).call(
  1025. zoom.transform,
  1026. d3.zoomIdentity.translate(width/2, height/2).scale(1).translate(-width/2, -height/2)
  1027. );
  1028. }}
  1029. function toggleLabels() {{
  1030. showLabels = !showLabels;
  1031. g.selectAll(".node text").style("display", showLabels ? "block" : "none");
  1032. }}
  1033. function showNodeInfo(d) {{
  1034. const panel = document.getElementById("detailPanel");
  1035. panel.classList.add("active");
  1036. document.getElementById("detailTitle").textContent = d.source === "帖子" ? "📌 帖子节点" : "👤 人设节点";
  1037. let html = `
  1038. <p><span class="label">节点ID:</span> ${{d.节点ID}}</p>
  1039. <p><span class="label">名称:</span> <strong>${{d.节点名称}}</strong></p>
  1040. <p><span class="label">类型:</span> ${{d.节点类型}}</p>
  1041. <p><span class="label">层级:</span> ${{d.节点层级}}</p>
  1042. `;
  1043. if (d.权重) {{
  1044. html += `<p><span class="label">权重:</span> ${{d.权重}}</p>`;
  1045. }}
  1046. if (d.所属分类 && d.所属分类.length > 0) {{
  1047. html += `<p><span class="label">所属分类:</span> ${{d.所属分类.join(" > ")}}</p>`;
  1048. }}
  1049. if (d.帖子数) {{
  1050. html += `<p><span class="label">帖子数:</span> ${{d.帖子数}}</p>`;
  1051. }}
  1052. document.getElementById("detailContent").innerHTML = html;
  1053. }}
  1054. function showEdgeInfo(d) {{
  1055. const panel = document.getElementById("detailPanel");
  1056. panel.classList.add("active");
  1057. const sourceNode = typeof d.source === "object" ? d.source : {{ id: d.source }};
  1058. const targetNode = typeof d.target === "object" ? d.target : {{ id: d.target }};
  1059. // 判断是否为镜像边
  1060. const isMirror = d.type.startsWith("镜像_");
  1061. document.getElementById("detailTitle").textContent = isMirror ? "🪞 镜像边详情" : "🔗 边详情";
  1062. let html = `
  1063. <p><span class="label">边类型:</span> <strong>${{d.type}}</strong></p>
  1064. <p><span class="label">源节点:</span> ${{sourceNode.节点名称 || sourceNode.id}}</p>
  1065. <p><span class="label">目标节点:</span> ${{targetNode.节点名称 || targetNode.id}}</p>
  1066. `;
  1067. if (d.边详情) {{
  1068. if (d.边详情.相似度 !== undefined) {{
  1069. const score = typeof d.边详情.相似度 === "number" ? d.边详情.相似度.toFixed(2) : d.边详情.相似度;
  1070. html += `<p><span class="label">相似度:</span> <span class="similarity-score">${{score}}</span></p>`;
  1071. }}
  1072. if (d.边详情.说明) {{
  1073. html += `<p><span class="label">说明:</span></p><div class="edge-description">${{d.边详情.说明}}</div>`;
  1074. }}
  1075. if (d.边详情.共现次数 !== undefined) {{
  1076. html += `<p><span class="label">共现次数:</span> ${{d.边详情.共现次数}}</p>`;
  1077. }}
  1078. // 镜像边特有信息
  1079. if (d.边详情.原始边类型) {{
  1080. html += `<p><span class="label">原始边类型:</span> ${{d.边详情.原始边类型}}</p>`;
  1081. }}
  1082. if (d.边详情.源人设节点) {{
  1083. html += `<p><span class="label">源人设节点:</span> ${{d.边详情.源人设节点}}</p>`;
  1084. }}
  1085. if (d.边详情.目标人设节点) {{
  1086. html += `<p><span class="label">目标人设节点:</span> ${{d.边详情.目标人设节点}}</p>`;
  1087. }}
  1088. }}
  1089. document.getElementById("detailContent").innerHTML = html;
  1090. }}
  1091. function closeDetailPanel() {{
  1092. document.getElementById("detailPanel").classList.remove("active");
  1093. }}
  1094. // 页面加载完成后初始化
  1095. window.addEventListener("load", init);
  1096. window.addEventListener("resize", () => {{
  1097. if (currentIndex >= 0) {{
  1098. renderGraph(allGraphData[currentIndex]);
  1099. }}
  1100. }});
  1101. </script>
  1102. </body>
  1103. </html>
  1104. '''
  1105. def generate_combined_html(all_graph_data: List[Dict], output_file: Path):
  1106. """
  1107. 生成包含所有帖子图谱的HTML文件
  1108. Args:
  1109. all_graph_data: 所有帖子的图谱数据列表
  1110. output_file: 输出文件路径
  1111. """
  1112. # 生成Tab HTML
  1113. tabs_html = ""
  1114. for i, data in enumerate(all_graph_data):
  1115. post_title = data.get("postTitle", "")
  1116. # 使用帖子标题,如果太长则截断
  1117. if post_title:
  1118. tab_name = post_title[:15] + "..." if len(post_title) > 15 else post_title
  1119. else:
  1120. tab_name = f"帖子 {i+1}"
  1121. active_class = "active" if i == 0 else ""
  1122. tabs_html += f'<div class="tab {active_class}" data-index="{i}">{tab_name}</div>\n'
  1123. # 生成HTML
  1124. html_content = HTML_TEMPLATE.format(
  1125. tabs_html=tabs_html,
  1126. all_graph_data=json.dumps(all_graph_data, ensure_ascii=False)
  1127. )
  1128. with open(output_file, "w", encoding="utf-8") as f:
  1129. f.write(html_content)
  1130. def main():
  1131. # 使用路径配置
  1132. config = PathConfig()
  1133. print(f"账号: {config.account_name}")
  1134. print(f"输出版本: {config.output_version}")
  1135. print()
  1136. # 输入目录
  1137. match_graph_dir = config.intermediate_dir / "match_graph"
  1138. # 输出文件
  1139. output_file = config.intermediate_dir / "match_graph.html"
  1140. print(f"输入目录: {match_graph_dir}")
  1141. print(f"输出文件: {output_file}")
  1142. print()
  1143. # 读取所有匹配图谱文件
  1144. graph_files = sorted(match_graph_dir.glob("*_match_graph.json"))
  1145. print(f"找到 {len(graph_files)} 个匹配图谱文件")
  1146. all_graph_data = []
  1147. for i, graph_file in enumerate(graph_files, 1):
  1148. print(f" [{i}/{len(graph_files)}] 读取: {graph_file.name}")
  1149. with open(graph_file, "r", encoding="utf-8") as f:
  1150. match_graph_data = json.load(f)
  1151. # 提取需要的数据
  1152. graph_data = {
  1153. "postId": match_graph_data["说明"]["帖子ID"],
  1154. "postTitle": match_graph_data["说明"].get("帖子标题", ""),
  1155. "stats": match_graph_data["说明"]["统计"],
  1156. "nodes": match_graph_data["节点列表"],
  1157. "edges": match_graph_data["边列表"]
  1158. }
  1159. all_graph_data.append(graph_data)
  1160. # 生成HTML
  1161. print("\n生成HTML文件...")
  1162. generate_combined_html(all_graph_data, output_file)
  1163. print("\n" + "="*60)
  1164. print("处理完成!")
  1165. print(f"输出文件: {output_file}")
  1166. if __name__ == "__main__":
  1167. main()