generate_visualize_data.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. """
  100. all_keys = {_topic_point_key(t) for t in topic_points}
  101. topic_by_key = {_topic_point_key(t): t for t in topic_points}
  102. result = []
  103. derived_names_so_far: set[str] = set()
  104. for i, (derivation, eval_data) in enumerate(zip(derivations, evals)):
  105. round_num = derivation.get("round", i + 1)
  106. eval_results = eval_data.get("eval_results") or []
  107. matched_post_points = set()
  108. for er in eval_results:
  109. # 新格式: is_matched;旧格式: match_result == "匹配"
  110. if not (er.get("is_matched") is True or er.get("match_result") == "匹配"):
  111. continue
  112. mp = er.get("matched_post_point") or er.get("matched_post_topic") or er.get("match_post_point") or ""
  113. if mp and str(mp).strip():
  114. matched_post_points.add(str(mp).strip())
  115. new_derived_names = matched_post_points - derived_names_so_far
  116. derived_names_so_far |= matched_post_points
  117. # 推导成功的选题点:name 在 derived_names_so_far 中的选题点(每 name 取一条,与 topic_points 顺序一致)
  118. derived_keys = {k for k in all_keys if topic_by_key[k]["name"] in derived_names_so_far}
  119. new_derived_keys = {k for k in all_keys if topic_by_key[k]["name"] in new_derived_names}
  120. not_derived_keys = all_keys - derived_keys
  121. derived_list = [dict(topic_by_key[k]) for k in sorted(derived_keys, key=lambda k: (topic_by_key[k]["name"], k[1], k[2]))]
  122. new_list = [dict(topic_by_key[k]) for k in sorted(new_derived_keys, key=lambda k: (topic_by_key[k]["name"], k[1], k[2]))]
  123. not_derived_list = [dict(topic_by_key[k]) for k in sorted(not_derived_keys, key=lambda k: (topic_by_key[k]["name"], k[1], k[2]))]
  124. result.append({
  125. "轮次": round_num,
  126. "推导成功的选题点": derived_list,
  127. "未推导成功的选题点": not_derived_list,
  128. "本次新推导成功的选题点": new_list,
  129. })
  130. return result
  131. def _tree_node_display_name(raw: str) -> str:
  132. """人设节点可能是 a.b.c 路径形式,实际需要的是最后一段节点名 c。"""
  133. s = (raw or "").strip()
  134. if "." in s:
  135. return s.rsplit(".", 1)[-1].strip() or s
  136. return s
  137. def _to_tree_node(name: str, extra: dict | None = None) -> dict:
  138. d = {"name": name}
  139. if extra:
  140. d.update(extra)
  141. return d
  142. def _to_pattern_node(pattern_name: str) -> dict:
  143. """将 pattern 字符串转为 input_pattern_nodes 的一项(简化版)。"""
  144. items = [x.strip() for x in pattern_name.replace("+", " ").split() if x.strip()]
  145. return {
  146. "items": [{"name": x, "point": "关键点", "dimension": "形式", "type": "标签"} for x in items],
  147. "match_items": items,
  148. }
  149. def build_visualize_edges(
  150. derivations: list[dict],
  151. evals: list[dict],
  152. topic_points: list[dict],
  153. ) -> tuple[list[dict], list[dict]]:
  154. """
  155. 生成 node_list(所有评估通过的帖子选题点)和 edge_list(只保留评估通过的推导路径)。
  156. 按轮次从小到大处理,保证每个输出节点最多只出现在一条边的 output_nodes 里,且保留的是前面轮次的数据。
  157. """
  158. # 按轮次从小到大排序,确保优先使用前面轮次的输出节点
  159. derivations = sorted(derivations, key=lambda d: d.get("round", 0))
  160. evals = sorted(evals, key=lambda e: e.get("round", 0))
  161. topic_by_name = {}
  162. for t in topic_points:
  163. name = t["name"]
  164. if name not in topic_by_name:
  165. topic_by_name[name] = t
  166. # 不依赖 id,仅用 (round, derivation_output_point) 与推导的 output 节点名匹配关联评估结果
  167. # key=(round_num, derivation_output_point) -> (matched_post_point, matched_reason);同轮同节点取首次匹配
  168. match_by_round_output: dict[tuple[int, str], tuple[str, str]] = {}
  169. for round_idx, eval_data in enumerate(evals):
  170. round_num = eval_data.get("round", round_idx + 1)
  171. for er in eval_data.get("eval_results") or []:
  172. if not (er.get("is_matched") is True or er.get("match_result") == "匹配"):
  173. continue
  174. out_point = (er.get("derivation_output_point") or "").strip()
  175. mp = er.get("matched_post_point") or er.get("matched_post_topic") or er.get("match_post_point") or ""
  176. matched_reason = er.get("matched_reason") or er.get("match_reason") or ""
  177. if out_point and mp and str(mp).strip():
  178. mp = str(mp).strip()
  179. k = (round_num, out_point)
  180. if k not in match_by_round_output:
  181. match_by_round_output[k] = (mp, str(matched_reason).strip() if matched_reason else "")
  182. node_list = []
  183. seen_nodes = set()
  184. edge_list = []
  185. level_by_name = {}
  186. output_nodes_seen: set[str] = set() # 已在之前边的 output_nodes 中出现过的节点,避免同一输出节点对应多条边
  187. for round_idx, derivation in enumerate(derivations):
  188. round_num = derivation.get("round", round_idx + 1)
  189. for dr in derivation.get("derivation_results") or []:
  190. output_list = dr.get("output") or []
  191. matched_outputs = []
  192. matched_reasons = []
  193. matched_derivation_outputs = []
  194. for out_item in output_list:
  195. key = (round_num, out_item)
  196. pair = match_by_round_output.get(key)
  197. if not pair:
  198. continue
  199. mp, reason = pair
  200. matched_outputs.append(mp)
  201. matched_reasons.append(reason)
  202. matched_derivation_outputs.append(out_item)
  203. if mp not in seen_nodes:
  204. seen_nodes.add(mp)
  205. node = dict(topic_by_name.get(mp, {"name": mp, "point": "", "dimension": "", "root_source": "", "root_sources_desc": ""}))
  206. node["level"] = round_num
  207. if "original_word" not in node:
  208. node["original_word"] = node.get("name", mp)
  209. node["derivation_type"] = dr.get("method", "")
  210. level_by_name[mp] = round_num
  211. node_list.append(node)
  212. if not matched_outputs:
  213. continue
  214. # 只保留尚未在之前边的 output_nodes 中出现过的节点,避免同一输出节点对应多条边
  215. output_names_this_edge = [x for x in matched_outputs if x not in output_nodes_seen]
  216. if not output_names_this_edge:
  217. continue
  218. output_nodes_seen.update(output_names_this_edge)
  219. input_data = dr.get("input") or {}
  220. derived_nodes = input_data.get("derived_nodes") or []
  221. tree_nodes = input_data.get("tree_nodes") or []
  222. patterns = input_data.get("patterns") or []
  223. input_post_nodes = [{"name": x} for x in derived_nodes]
  224. input_tree_nodes = [_to_tree_node(_tree_node_display_name(x)) for x in tree_nodes]
  225. if patterns and isinstance(patterns[0], str):
  226. input_pattern_nodes = [_to_pattern_node(p) for p in patterns]
  227. elif patterns and isinstance(patterns[0], dict):
  228. input_pattern_nodes = patterns
  229. else:
  230. input_pattern_nodes = []
  231. output_nodes = [{"name": x} for x in output_names_this_edge]
  232. # 与 output_names_this_edge 顺序对应的匹配理由、推导输出节点名
  233. mp_to_reason = dict(zip(matched_outputs, matched_reasons))
  234. mp_to_derivation_out = dict(zip(matched_outputs, matched_derivation_outputs))
  235. reason_for_this_edge = [mp_to_reason.get(name, "") for name in output_names_this_edge]
  236. derivation_points_this_edge = [mp_to_derivation_out.get(name, "") for name in output_names_this_edge]
  237. detail = {
  238. "reason": dr.get("reason", ""),
  239. "评估结果": "匹配成功",
  240. }
  241. if any(reason_for_this_edge):
  242. detail["匹配理由"] = reason_for_this_edge
  243. detail["待比对的推导选题点"] = derivation_points_this_edge
  244. if dr.get("tools"):
  245. detail["tools"] = dr["tools"]
  246. edge_list.append({
  247. "name": dr.get("method", "") or f"推导-{round_num}",
  248. "input_post_nodes": input_post_nodes,
  249. "input_tree_nodes": input_tree_nodes,
  250. "input_pattern_nodes": input_pattern_nodes,
  251. "output_nodes": output_nodes,
  252. "detail": detail,
  253. })
  254. return node_list, edge_list
  255. def generate_visualize_data(account_name: str, post_id: str, log_id: str, base_dir: Path | None = None) -> None:
  256. """
  257. 主流程:读取解构内容与推导日志,生成整体推导结果与整体推导路径可视化两个 JSON。
  258. """
  259. if base_dir is None:
  260. base_dir = Path(__file__).resolve().parent
  261. input_dir = base_dir / "input" / account_name / "原始数据" / "解构内容"
  262. log_dir = base_dir / "output" / account_name / "推导日志" / post_id / log_id
  263. result_dir = base_dir / "output" / account_name / "整体推导结果"
  264. visualize_dir = base_dir / "output" / account_name / "整体推导路径可视化"
  265. deconstruct_path = input_dir / f"{post_id}.json"
  266. topic_points = parse_topic_points_from_deconstruct(deconstruct_path)
  267. derivations, evals = load_derivation_logs(log_dir)
  268. if not derivations or not evals:
  269. raise ValueError(f"推导或评估数据为空: {log_dir}")
  270. # 2.1 整体推导结果
  271. derivation_result = build_derivation_result(topic_points, derivations, evals)
  272. result_dir.mkdir(parents=True, exist_ok=True)
  273. result_path = result_dir / f"{post_id}.json"
  274. with open(result_path, "w", encoding="utf-8") as f:
  275. json.dump(derivation_result, f, ensure_ascii=False, indent=4)
  276. print(f"已写入整体推导结果: {result_path}")
  277. # 2.2 整体推导路径可视化
  278. node_list, edge_list = build_visualize_edges(derivations, evals, topic_points)
  279. visualize_path = visualize_dir / f"{post_id}.json"
  280. visualize_dir.mkdir(parents=True, exist_ok=True)
  281. with open(visualize_path, "w", encoding="utf-8") as f:
  282. json.dump({"node_list": node_list, "edge_list": edge_list}, f, ensure_ascii=False, indent=4)
  283. print(f"已写入整体推导路径可视化: {visualize_path}")
  284. def main(account_name, post_id, log_id):
  285. # parser = argparse.ArgumentParser(description="生成推导可视化数据")
  286. # parser.add_argument("account_name", help="账号名,如 家有大志")
  287. # parser.add_argument("post_id", help="帖子 ID")
  288. # parser.add_argument("log_id", help="推导日志 ID,如 20260303204232")
  289. # parser.add_argument("--base-dir", type=Path, default=None, help="项目根目录,默认为本脚本所在目录")
  290. # args = parser.parse_args()
  291. generate_visualize_data(account_name=account_name, post_id=post_id, log_id=log_id)
  292. if __name__ == "__main__":
  293. account_name="家有大志"
  294. post_id = "68fb6a5c000000000302e5de"
  295. log_id="20260309180228"
  296. main(account_name, post_id, log_id)