|
@@ -18,6 +18,9 @@ import leidenalg
|
|
|
import igraph as ig
|
|
|
import sys,os
|
|
|
|
|
|
+from db.session import get_db
|
|
|
+from service.kg_node_service import KGNodeService
|
|
|
+
|
|
|
# 当前工作路径
|
|
|
current_path = os.getcwd()
|
|
|
sys.path.append(current_path)
|
|
@@ -26,7 +29,7 @@ sys.path.append(current_path)
|
|
|
RESOLUTION = 0.07
|
|
|
|
|
|
# 图谱数据缓存路径(由dump_graph_data.py生成)
|
|
|
-CACHED_DATA_PATH = f"{current_path}\\web\\cached_data"
|
|
|
+CACHED_DATA_PATH = os.path.join(current_path, 'community', 'web', 'cached_data')
|
|
|
|
|
|
def load_entity_data():
|
|
|
"""
|
|
@@ -36,7 +39,7 @@ def load_entity_data():
|
|
|
list: 实体数据列表,每个元素格式为[node_id, attributes_dict]
|
|
|
"""
|
|
|
print("load entity data")
|
|
|
- with open(f"{CACHED_DATA_PATH}\\entities_med.json", "r", encoding="utf-8") as f:
|
|
|
+ with open(os.path.join(CACHED_DATA_PATH,'entities_med.json'), "r", encoding="utf-8") as f:
|
|
|
entities = json.load(f)
|
|
|
return entities
|
|
|
|
|
@@ -51,18 +54,19 @@ def load_relation_data(g):
|
|
|
1. 支持分块加载多个关系文件(relationship_med_0.json ~ relationship_med_29.json)
|
|
|
2. 每个关系项格式为[source, target, {relation_attrs}]
|
|
|
"""
|
|
|
- #for i in range(30):
|
|
|
- #if os.path.exists(f"{CACHED_DATA_PATH}\\relationship_med_{i}.json"):
|
|
|
- #print("load entity data", f"{CACHED_DATA_PATH}\\relationship_med_{i}.json")
|
|
|
- #with open(f"{CACHED_DATA_PATH}\\relationship_med_{i}.json", "r", encoding="utf-8") as f:
|
|
|
- if os.path.exists(f"{CACHED_DATA_PATH}\\relationship_med.json"):
|
|
|
- print("load entity data", f"{CACHED_DATA_PATH}\\relationship_med_.json")
|
|
|
- with open(f"{CACHED_DATA_PATH}\\relationship_med.json", "r", encoding="utf-8") as f:
|
|
|
- relations = json.load(f)
|
|
|
- for item in relations:
|
|
|
- # 添加带权重的边,并存储关系属性
|
|
|
- weight = int(item[2].pop('weight').replace('权重:', ''))
|
|
|
- g.add_edge(item[0], item[1], weight=weight, **item[2])
|
|
|
+ for i in range(90):
|
|
|
+ if os.path.exists(os.path.join(CACHED_DATA_PATH, f"relationship_med_{i}.json")):
|
|
|
+ print("load entity data", os.path.join(CACHED_DATA_PATH, f"relationship_med_{i}.json"))
|
|
|
+ with open(os.path.join(CACHED_DATA_PATH, f"relationship_med_{i}.json"), "r", encoding="utf-8") as f:
|
|
|
+
|
|
|
+ relations = json.load(f)
|
|
|
+ for item in relations:
|
|
|
+ # 添加带权重的边,并存储关系属性
|
|
|
+ weight = int(item[2].pop('weight', '8').replace('权重:', ''))
|
|
|
+ #如果item[0]或者item[1]为空或null,则跳过
|
|
|
+ if item[0] is None or item[1] is None:
|
|
|
+ continue
|
|
|
+ g.add_edge(item[0], item[1], weight=weight, **item[2])
|
|
|
|
|
|
class GraphHelper:
|
|
|
"""
|
|
@@ -110,6 +114,23 @@ class GraphHelper:
|
|
|
# 加载边数据(疾病-症状关系等)
|
|
|
load_relation_data(self.graph)
|
|
|
|
|
|
+ def node_search2(self, node_id=None, node_type=None, filters=None, limit=1, min_degree=None):
|
|
|
+ """节点检索功能"""
|
|
|
+
|
|
|
+ kg_node_service = KGNodeService(next(get_db()))
|
|
|
+ es_result = kg_node_service.search_title_index("graph_entity_index", node_id, limit)
|
|
|
+ results = []
|
|
|
+ for item in es_result:
|
|
|
+ n = self.graph.nodes.get(item["title"])
|
|
|
+ score = item["score"]
|
|
|
+ if n:
|
|
|
+ results.append({
|
|
|
+ 'id': item["title"],
|
|
|
+ 'score': score,
|
|
|
+ **n
|
|
|
+ })
|
|
|
+ return results
|
|
|
+
|
|
|
def node_search(self, node_id=None, node_type=None, filters=None):
|
|
|
"""节点检索
|
|
|
|
|
@@ -161,25 +182,29 @@ class GraphHelper:
|
|
|
relations = []
|
|
|
|
|
|
while queue:
|
|
|
- current = queue.pop(0)
|
|
|
- current_hop = visited[current]
|
|
|
-
|
|
|
- if current_hop >= hops:
|
|
|
- continue
|
|
|
-
|
|
|
- # 遍历相邻节点
|
|
|
- for neighbor in self.graph.neighbors(current):
|
|
|
- if neighbor not in visited:
|
|
|
- visited[neighbor] = current_hop + 1
|
|
|
- queue.append(neighbor)
|
|
|
+ try:
|
|
|
+ current = queue.pop(0)
|
|
|
+ current_hop = visited[current]
|
|
|
|
|
|
- # 记录边关系
|
|
|
- edge_data = self.graph.get_edge_data(current, neighbor)
|
|
|
- relations.append({
|
|
|
- 'src_name': current,
|
|
|
- 'dest_name': neighbor,
|
|
|
- **edge_data
|
|
|
- })
|
|
|
+ if current_hop >= hops:
|
|
|
+ continue
|
|
|
+
|
|
|
+ # 遍历相邻节点
|
|
|
+ for neighbor in self.graph.neighbors(current):
|
|
|
+ if neighbor not in visited:
|
|
|
+ visited[neighbor] = current_hop + 1
|
|
|
+ queue.append(neighbor)
|
|
|
+
|
|
|
+ # 记录边关系
|
|
|
+ edge_data = self.graph.get_edge_data(current, neighbor)
|
|
|
+ relations.append({
|
|
|
+ 'src_name': current,
|
|
|
+ 'dest_name': neighbor,
|
|
|
+ **edge_data
|
|
|
+ })
|
|
|
+ except Exception as e:
|
|
|
+ print(f"Error processing node {current}: {str(e)}")
|
|
|
+ continue
|
|
|
|
|
|
# 提取邻居实体(排除中心节点)
|
|
|
entities = [
|