| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- """
- 关键点检索工具 - 根据输入的点在图数据库中查找所有关联的点
- 用于 Agent 执行时自主调取关联关键点数据。
- """
- import json
- import os
- from pathlib import Path
- from typing import Any, Dict, Optional
- # 完整图数据库文件路径(包含 edges)
- GRAPH_FULL_DATA_PATH = os.getenv(
- "GRAPH_FULL_DATA_PATH",
- # str(Path(__file__).parent.parent / "data/library/item_graph/item_graph_full_all_levels.json")
- str(Path(__file__).parent.parent / "data/library/apriori_analysis_post_level/frequent_itemsets_multi_depth.json")
- )
- # 缓存图数据,避免重复加载
- _graph_full_cache: Optional[Dict[str, Any]] = None
- def _load_graph_full() -> Dict[str, Any]:
- """加载完整图数据(带缓存,包含 edges)"""
- global _graph_full_cache
- if _graph_full_cache is None:
- with open(GRAPH_FULL_DATA_PATH, 'r', encoding='utf-8') as f:
- _graph_full_cache = json.load(f)
- return _graph_full_cache
- def build_element_index(data):
- """
- 预先建立元素到组合的索引
-
- Returns:
- dict: {元素: [包含该元素的组合索引列表]}
- """
- index = {}
-
- data = data.get("dimension_modes", {}).get("full", {}).get("results_by_depth", {}).get("depth_4", {}).get("frequent_itemsets_by_type", {}).get("关键点×灵感点×目的点_混合", {}).get("itemsets", [])
- items = {}
- index = {}
- for group_idx, group in enumerate(data):
- itemset = group.get("itemset", [])
- items[group_idx] = itemset
- for element in itemset:
- if element not in index:
- index[element] = []
- index[element].append(str(group_idx))
- return index, items
- if __name__ == "__main__":
- graph = _load_graph_full()
- index, items = build_element_index(graph)
- # 输出到文件
- output_path = Path(__file__).parent.parent / "data/library/apriori_analysis_post_level/frequent_itemsets_multi_depth_index.json"
- with open(output_path, 'w', encoding='utf-8') as f:
- json.dump({
- "index": index,
- "items": items
- }, f, ensure_ascii=False, indent=2)
- print(f"数据已输出到: {output_path}")
-
|