generate_visualize_data.py 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. #!/usr/bin/env python3
  2. """
  3. 生成推导可视化数据。
  4. 输入参数:account_name, post_id, log_id
  5. - 从 input/{account_name}/解构内容/{post_id}.json 解析选题点列表
  6. - 从 output/{account_name}/推导日志/{post_id}/{log_id}/ 读取推导与评估 JSON,生成:
  7. 1. output/{account_name}/整体推导结果/{post_id}.json
  8. 2. output/{account_name}/整体推导路径可视化/{post_id}.json
  9. """
  10. import argparse
  11. import json
  12. import re
  13. from pathlib import Path
  14. from typing import Any
  15. def _collect_dimension_names(point_data: dict) -> dict[str, str]:
  16. """从点的 实质/形式/意图 中收集 名称 -> dimension。"""
  17. name_to_dim = {}
  18. if "实质" in point_data and point_data["实质"]:
  19. for key in ("具体元素", "具象概念", "抽象概念"):
  20. for item in (point_data["实质"].get(key) or []):
  21. n = item.get("名称")
  22. if n:
  23. name_to_dim[n] = "实质"
  24. if "形式" in point_data and point_data["形式"]:
  25. for key in ("具体元素形式", "具象概念形式", "整体形式"):
  26. for item in (point_data["形式"].get(key) or []):
  27. n = item.get("名称")
  28. if n:
  29. name_to_dim[n] = "形式"
  30. if point_data.get("意图"):
  31. for item in point_data["意图"]:
  32. n = item.get("名称")
  33. if n:
  34. name_to_dim[n] = "意图"
  35. return name_to_dim
  36. def parse_topic_points_from_deconstruct(deconstruct_path: Path) -> list[dict[str, Any]]:
  37. """
  38. 从 input/{account_name}/解构内容/{post_id}.json 解析选题点列表。
  39. 选题点来自分词结果中的「词」,字段:name, point, dimension, root_source, root_sources_desc。
  40. """
  41. if not deconstruct_path.exists():
  42. raise FileNotFoundError(f"解构内容文件不存在: {deconstruct_path}")
  43. with open(deconstruct_path, "r", encoding="utf-8") as f:
  44. data = json.load(f)
  45. result = []
  46. for point_type in ("灵感点", "目的点", "关键点"):
  47. for point in data.get(point_type) or []:
  48. root_source = point.get("点", "")
  49. root_sources_desc = point.get("点描述", "")
  50. name_to_dim = _collect_dimension_names(point)
  51. for word_item in point.get("分词结果") or []:
  52. name = word_item.get("词", "").strip()
  53. if not name:
  54. continue
  55. dimension = name_to_dim.get(name, "实质")
  56. result.append({
  57. "name": name,
  58. "point": point_type,
  59. "dimension": dimension,
  60. "root_source": root_source,
  61. "root_sources_desc": root_sources_desc,
  62. })
  63. return result
  64. def _topic_point_key(t: dict) -> tuple:
  65. return (t["name"], t["point"], t["dimension"])
  66. def load_derivation_logs(log_dir: Path) -> tuple[list[dict], list[dict]]:
  67. """
  68. 从 output/{account_name}/推导日志/{post_id}/{log_id}/ 读取所有 {轮次}_推导.json 与 {轮次}_评估.json。
  69. 返回 (推导列表按轮次序, 评估列表按轮次序)。
  70. """
  71. if not log_dir.is_dir():
  72. raise FileNotFoundError(f"推导日志目录不存在: {log_dir}")
  73. derivation_by_round = {}
  74. eval_by_round = {}
  75. for p in log_dir.glob("*.json"):
  76. base = p.stem
  77. m = re.match(r"^(\d+)_(推导|评估)$", base)
  78. if not m:
  79. continue
  80. round_num = int(m.group(1))
  81. with open(p, "r", encoding="utf-8") as f:
  82. content = json.load(f)
  83. if m.group(2) == "推导":
  84. derivation_by_round[round_num] = content
  85. else:
  86. eval_by_round[round_num] = content
  87. rounds = sorted(set(derivation_by_round) | set(eval_by_round))
  88. derivations = [derivation_by_round[r] for r in rounds if r in derivation_by_round]
  89. evals = [eval_by_round[r] for r in rounds if r in eval_by_round]
  90. return derivations, evals
  91. def build_derivation_result(
  92. topic_points: list[dict],
  93. derivations: list[dict],
  94. evals: list[dict],
  95. ) -> list[dict]:
  96. """
  97. 生成整体推导结果:每轮 轮次、推导成功的选题点、未推导成功的选题点、本次新推导成功的选题点。
  98. 选题点用 topic_points 中的完整信息;按 name 判定是否被推导(评估中的 match_post_point)。
  99. 若之前推导成功的选题点 is_fully_derived=false,本轮变为 is_fully_derived=true,则算本次新推导成功的选题点,
  100. 且 matched_score、is_fully_derived 在本轮后更新为该轮评估值。
  101. 推导成功的选题点:使用当前已更新的 best (matched_score, is_fully_derived)。
  102. 本次新推导成功的选题点:用当轮评估的 matched_score、is_fully_derived。
  103. 未推导成功的选题点:不包含 matched_score、is_fully_derived。
  104. """
  105. all_keys = {_topic_point_key(t) for t in topic_points}
  106. topic_by_key = {_topic_point_key(t): t for t in topic_points}
  107. # 分轮次收集 (round_num, name) -> (matched_score, is_fully_derived),同一轮同名取首次出现
  108. score_by_round_name: dict[tuple[int, str], tuple[float, bool]] = {}
  109. for round_idx, eval_data in enumerate(evals):
  110. round_num = eval_data.get("round", round_idx + 1)
  111. for er in eval_data.get("eval_results") or []:
  112. if not (er.get("is_matched") is True or er.get("match_result") == "匹配"):
  113. continue
  114. mp = (er.get("matched_post_point") or er.get("matched_post_topic") or er.get("match_post_point") or "").strip()
  115. if not mp:
  116. continue
  117. key = (round_num, mp)
  118. if key in score_by_round_name:
  119. continue
  120. score = er.get("matched_score")
  121. if score is None:
  122. score = 1.0
  123. else:
  124. try:
  125. score = float(score)
  126. except (TypeError, ValueError):
  127. score = 1.0
  128. is_fully = er.get("is_fully_derived", True)
  129. score_by_round_name[key] = (score, bool(is_fully))
  130. result = []
  131. derived_names_so_far: set[str] = set()
  132. fully_derived_names_so_far: set[str] = set() # 已出现过 is_fully_derived=true 的选题点
  133. best_score_by_name: dict[str, tuple[float, bool]] = {} # name -> (matched_score, is_fully_derived),遇 is_fully=true 时更新
  134. for i, (derivation, eval_data) in enumerate(zip(derivations, evals)):
  135. round_num = derivation.get("round", i + 1)
  136. eval_results = eval_data.get("eval_results") or []
  137. matched_post_points = set()
  138. for er in eval_results:
  139. if not (er.get("is_matched") is True or er.get("match_result") == "匹配"):
  140. continue
  141. mp = er.get("matched_post_point") or er.get("matched_post_topic") or er.get("match_post_point") or ""
  142. if mp and str(mp).strip():
  143. matched_post_points.add(str(mp).strip())
  144. # 本轮每个匹配名的 (score, is_fully)
  145. this_round_scores: dict[str, tuple[float, bool]] = {}
  146. for name in matched_post_points:
  147. val = score_by_round_name.get((round_num, name))
  148. if val is not None:
  149. this_round_scores[name] = val
  150. # 本次新推导成功:首次匹配 或 之前 is_fully=false 且本轮 is_fully=true
  151. new_derived_names = set()
  152. for name in matched_post_points:
  153. score, is_fully = this_round_scores.get(name, (None, False))
  154. if name not in derived_names_so_far:
  155. new_derived_names.add(name)
  156. elif name not in fully_derived_names_so_far and is_fully:
  157. new_derived_names.add(name)
  158. # 更新推导集合与 best:首次出现或本轮 is_fully=true 时更新 best
  159. derived_names_so_far |= matched_post_points
  160. for name in matched_post_points:
  161. val = this_round_scores.get(name)
  162. if val is None:
  163. continue
  164. score, is_fully = val
  165. if name not in best_score_by_name:
  166. best_score_by_name[name] = (score, is_fully)
  167. elif is_fully:
  168. best_score_by_name[name] = (score, is_fully)
  169. if is_fully:
  170. fully_derived_names_so_far.add(name)
  171. derived_keys = {k for k in all_keys if topic_by_key[k]["name"] in derived_names_so_far}
  172. new_derived_keys = {k for k in all_keys if topic_by_key[k]["name"] in new_derived_names}
  173. not_derived_keys = all_keys - derived_keys
  174. sort_derived = sorted(derived_keys, key=lambda k: (topic_by_key[k]["name"], k[1], k[2]))
  175. sort_new = sorted(new_derived_keys, key=lambda k: (topic_by_key[k]["name"], k[1], k[2]))
  176. sort_not = sorted(not_derived_keys, key=lambda k: (topic_by_key[k]["name"], k[1], k[2]))
  177. def add_score_fields(keys: set, sort_keys: list, round_for_score: int | None) -> list[dict]:
  178. """round_for_score: 用该轮评估的分数;若为 None 则不添加 score 字段。"""
  179. out = []
  180. for k in sort_keys:
  181. if k not in keys:
  182. continue
  183. obj = dict(topic_by_key[k])
  184. if round_for_score is not None:
  185. name = obj.get("name", "")
  186. val = score_by_round_name.get((round_for_score, name))
  187. if val is not None:
  188. obj["matched_score"] = val[0]
  189. obj["is_fully_derived"] = val[1]
  190. else:
  191. obj["matched_score"] = None
  192. obj["is_fully_derived"] = False
  193. out.append(obj)
  194. return out
  195. # 推导成功的选题点:用当前已更新的 best (matched_score, is_fully_derived)
  196. derived_list = []
  197. for k in sort_derived:
  198. if k not in derived_keys:
  199. continue
  200. obj = dict(topic_by_key[k])
  201. name = obj.get("name", "")
  202. val = best_score_by_name.get(name)
  203. if val is not None:
  204. obj["matched_score"] = val[0]
  205. obj["is_fully_derived"] = val[1]
  206. else:
  207. obj["matched_score"] = None
  208. obj["is_fully_derived"] = False
  209. derived_list.append(obj)
  210. new_list = add_score_fields(new_derived_keys, sort_new, round_for_score=round_num)
  211. not_derived_list = [dict(topic_by_key[k]) for k in sort_not] # 不带 matched_score、is_fully_derived
  212. result.append({
  213. "轮次": round_num,
  214. "推导成功的选题点": derived_list,
  215. "未推导成功的选题点": not_derived_list,
  216. "本次新推导成功的选题点": new_list,
  217. })
  218. return result
  219. def _tree_node_display_name(raw: str) -> str:
  220. """人设节点可能是 a.b.c 路径形式,实际需要的是最后一段节点名 c。"""
  221. s = (raw or "").strip()
  222. if "." in s:
  223. return s.rsplit(".", 1)[-1].strip() or s
  224. return s
  225. def _to_tree_node(name: str, extra: dict | None = None) -> dict:
  226. d = {"name": name}
  227. if extra:
  228. d.update(extra)
  229. return d
  230. def _to_pattern_node(pattern_name: str) -> dict:
  231. """将 pattern 字符串转为 input_pattern_nodes 的一项(简化版)。"""
  232. items = [x.strip() for x in pattern_name.replace("+", " ").split() if x.strip()]
  233. return {
  234. "items": [{"name": x, "point": "关键点", "dimension": "形式", "type": "标签"} for x in items],
  235. "match_items": items,
  236. }
  237. def build_visualize_edges(
  238. derivations: list[dict],
  239. evals: list[dict],
  240. topic_points: list[dict],
  241. ) -> tuple[list[dict], list[dict]]:
  242. """
  243. 生成 node_list(所有评估通过的帖子选题点)和 edge_list(只保留评估通过的推导路径)。
  244. - node_list:同一轮内节点不重复,重复时保留 matched_score 更高的;节点带 matched_score、is_fully_derived。
  245. - edge_list:边带 level(与 output 节点 level 一致);同一轮内 output 节点不重复;若前面轮次该节点匹配分更高则本轮不保留该节点。
  246. 评估数据支持 path_id(对应推导 derivation_results[].id)、item_id(output 中元素从 1 起的序号)、matched_score、is_fully_derived。
  247. """
  248. derivations = sorted(derivations, key=lambda d: d.get("round", 0))
  249. evals = sorted(evals, key=lambda e: e.get("round", 0))
  250. topic_by_name = {t["name"]: t for t in topic_points}
  251. # 评估匹配:(round_num, path_id, item_id) -> (matched_post_point, matched_reason, matched_score, is_fully_derived)
  252. # path_id = 推导中 derivation_results[].id,item_id = output 中元素从 1 起的序号
  253. match_by_path_item: dict[tuple[int, int, int], tuple[str, str, float, bool]] = {}
  254. match_by_round_output: dict[tuple[int, str], tuple[str, str, float, bool]] = {} # 兼容无 path_id/item_id
  255. for round_idx, eval_data in enumerate(evals):
  256. round_num = eval_data.get("round", round_idx + 1)
  257. for er in eval_data.get("eval_results") or []:
  258. if not (er.get("is_matched") is True or er.get("match_result") == "匹配"):
  259. continue
  260. mp = (er.get("matched_post_point") or er.get("matched_post_topic") or er.get("match_post_point") or "").strip()
  261. if not mp:
  262. continue
  263. out_point = (er.get("derivation_output_point") or "").strip()
  264. reason = (er.get("matched_reason") or er.get("match_reason") or "").strip()
  265. score = er.get("matched_score")
  266. if score is None:
  267. score = 1.0
  268. else:
  269. try:
  270. score = float(score)
  271. except (TypeError, ValueError):
  272. score = 1.0
  273. is_fully = er.get("is_fully_derived", True)
  274. val = (mp, reason, score, bool(is_fully))
  275. path_id = er.get("path_id")
  276. item_id = er.get("item_id")
  277. if path_id is not None and item_id is not None:
  278. try:
  279. match_by_path_item[(round_num, int(path_id), int(item_id))] = val
  280. except (TypeError, ValueError):
  281. pass
  282. if out_point:
  283. k = (round_num, out_point)
  284. if k not in match_by_round_output:
  285. match_by_round_output[k] = val
  286. # 按 (round_num, mp) 收集节点候选,同轮同节点保留 matched_score 最高的一条
  287. node_candidates: dict[tuple[int, str], dict] = {} # (round_num, mp) -> node_dict (含 score, is_fully_derived)
  288. def get_match(round_num: int, path_id: int | None, item_id: int | None, out_item: str) -> tuple[str, str, float, bool] | None:
  289. if path_id is not None and item_id is not None:
  290. v = match_by_path_item.get((round_num, path_id, item_id))
  291. if v is not None:
  292. return v
  293. return match_by_round_output.get((round_num, out_item))
  294. edge_list = []
  295. round_output_seen: set[tuple[int, str]] = set() # (round_num, node_name) 本轮已作为某边的 output
  296. best_score_by_node: dict[str, float] = {} # node_name -> 已出现过的最高 matched_score
  297. for round_idx, derivation in enumerate(derivations):
  298. round_num = derivation.get("round", round_idx + 1)
  299. for dr in derivation.get("derivation_results") or []:
  300. output_list = dr.get("output") or []
  301. path_id = dr.get("id")
  302. matched: list[tuple[str, str, float, bool, str]] = [] # (mp, reason, score, is_fully, derivation_out)
  303. for i, out_item in enumerate(output_list):
  304. item_id = i + 1
  305. v = get_match(round_num, path_id, item_id, out_item)
  306. if not v:
  307. continue
  308. mp, reason, score, is_fully = v
  309. matched.append((mp, reason, score, is_fully, out_item))
  310. if not matched:
  311. continue
  312. # 同一轮内 output 节点不重复;若前面轮次该节点匹配分更高则本轮不保留
  313. output_names_this_edge = []
  314. for mp, reason, score, is_fully, out_item in matched:
  315. if (round_num, mp) in round_output_seen:
  316. continue
  317. if score <= best_score_by_node.get(mp, -1.0):
  318. continue
  319. output_names_this_edge.append((mp, reason, score, is_fully, out_item))
  320. if not output_names_this_edge:
  321. continue
  322. for mp, _r, score, _f, _o in output_names_this_edge:
  323. round_output_seen.add((round_num, mp))
  324. best_score_by_node[mp] = max(best_score_by_node.get(mp, -1.0), score)
  325. # 节点候选:同轮同节点保留匹配分更高的
  326. for mp, _reason, score, is_fully, _out_item in output_names_this_edge:
  327. key = (round_num, mp)
  328. if key not in node_candidates or node_candidates[key].get("matched_score", 0) < score:
  329. node = dict(topic_by_name.get(mp, {"name": mp, "point": "", "dimension": "", "root_source": "", "root_sources_desc": ""}))
  330. node["level"] = round_num
  331. node.setdefault("original_word", node.get("name", mp))
  332. node["derivation_type"] = dr.get("method", "")
  333. node["matched_score"] = score
  334. node["is_fully_derived"] = is_fully
  335. node_candidates[key] = node
  336. input_data = dr.get("input") or {}
  337. derived_nodes = input_data.get("derived_nodes") or []
  338. tree_nodes = input_data.get("tree_nodes") or []
  339. patterns = input_data.get("patterns") or []
  340. input_post_nodes = [{"name": x} for x in derived_nodes]
  341. input_tree_nodes = [_to_tree_node(_tree_node_display_name(x)) for x in tree_nodes]
  342. if patterns and isinstance(patterns[0], str):
  343. input_pattern_nodes = [_to_pattern_node(p) for p in patterns]
  344. elif patterns and isinstance(patterns[0], dict):
  345. input_pattern_nodes = patterns
  346. else:
  347. input_pattern_nodes = []
  348. output_nodes = []
  349. reasons_list = []
  350. derivation_points_list = []
  351. for mp, reason, score, is_fully, out_item in output_names_this_edge:
  352. output_nodes.append({"name": mp, "matched_score": score, "is_fully_derived": is_fully})
  353. reasons_list.append(reason)
  354. derivation_points_list.append(out_item)
  355. detail = {
  356. "reason": dr.get("reason", ""),
  357. "评估结果": "匹配成功",
  358. }
  359. if any(reasons_list):
  360. detail["匹配理由"] = reasons_list
  361. detail["待比对的推导选题点"] = derivation_points_list
  362. if dr.get("tools"):
  363. detail["tools"] = dr["tools"]
  364. edge_list.append({
  365. "name": dr.get("method", "") or f"推导-{round_num}",
  366. "level": round_num,
  367. "input_post_nodes": input_post_nodes,
  368. "input_tree_nodes": input_tree_nodes,
  369. "input_pattern_nodes": input_pattern_nodes,
  370. "output_nodes": output_nodes,
  371. "detail": detail,
  372. })
  373. node_list = list(node_candidates.values())
  374. return node_list, edge_list
  375. def _find_project_root() -> Path:
  376. """从脚本所在目录向上查找包含 .git 的项目根目录。"""
  377. p = Path(__file__).resolve().parent
  378. while p != p.parent:
  379. if (p / ".git").is_dir():
  380. return p
  381. p = p.parent
  382. return Path(__file__).resolve().parent
  383. def generate_visualize_data(account_name: str, post_id: str, log_id: str, base_dir: Path | None = None) -> None:
  384. """
  385. 主流程:读取解构内容与推导日志,生成整体推导结果与整体推导路径可视化两个 JSON。
  386. base_dir 默认为脚本所在目录;若其下 output/.../推导日志 不存在,则尝试项目根目录下的 output/...(兼容从项目根运行)。
  387. """
  388. if base_dir is None:
  389. base_dir = Path(__file__).resolve().parent
  390. input_dir = base_dir / "input" / account_name / "原始数据" / "解构内容"
  391. log_dir = base_dir / "output" / account_name / "推导日志" / post_id / log_id
  392. result_dir = base_dir / "output" / account_name / "整体推导结果"
  393. visualize_dir = base_dir / "output" / account_name / "整体推导路径可视化"
  394. # 兼容:若推导日志不在 base_dir 下,尝试项目根目录下的 output/
  395. if not log_dir.is_dir():
  396. project_root = _find_project_root()
  397. if project_root != base_dir:
  398. alt_log = project_root / "output" / account_name / "推导日志" / post_id / log_id
  399. if alt_log.is_dir():
  400. log_dir = alt_log
  401. result_dir = project_root / "output" / account_name / "整体推导结果"
  402. visualize_dir = project_root / "output" / account_name / "整体推导路径可视化"
  403. deconstruct_path = input_dir / f"{post_id}.json"
  404. topic_points = parse_topic_points_from_deconstruct(deconstruct_path)
  405. derivations, evals = load_derivation_logs(log_dir)
  406. if not derivations or not evals:
  407. raise ValueError(f"推导或评估数据为空: {log_dir}")
  408. # 2.1 整体推导结果
  409. derivation_result = build_derivation_result(topic_points, derivations, evals)
  410. result_dir.mkdir(parents=True, exist_ok=True)
  411. result_path = result_dir / f"{post_id}.json"
  412. with open(result_path, "w", encoding="utf-8") as f:
  413. json.dump(derivation_result, f, ensure_ascii=False, indent=4)
  414. print(f"已写入整体推导结果: {result_path}")
  415. # 2.2 整体推导路径可视化
  416. node_list, edge_list = build_visualize_edges(derivations, evals, topic_points)
  417. visualize_path = visualize_dir / f"{post_id}.json"
  418. visualize_dir.mkdir(parents=True, exist_ok=True)
  419. with open(visualize_path, "w", encoding="utf-8") as f:
  420. json.dump({"node_list": node_list, "edge_list": edge_list}, f, ensure_ascii=False, indent=4)
  421. print(f"已写入整体推导路径可视化: {visualize_path}")
  422. def main(account_name, post_id, log_id):
  423. # parser = argparse.ArgumentParser(description="生成推导可视化数据")
  424. # parser.add_argument("account_name", help="账号名,如 家有大志")
  425. # parser.add_argument("post_id", help="帖子 ID")
  426. # parser.add_argument("log_id", help="推导日志 ID,如 20260303204232")
  427. # parser.add_argument("--base-dir", type=Path, default=None, help="项目根目录,默认为本脚本所在目录")
  428. # args = parser.parse_args()
  429. generate_visualize_data(account_name=account_name, post_id=post_id, log_id=log_id)
  430. if __name__ == "__main__":
  431. account_name="家有大志"
  432. post_id = "68fb6a5c000000000302e5de"
  433. log_id="20260310220945"
  434. main(account_name, post_id, log_id)