MCPcopy Create free account
hub / github.com/KnowledgeXLab/LeanRAG / extract_json_from_cluster

Function extract_json_from_cluster

_cluster_utils.py:72–99  ·  view source on GitHub ↗
(s:str)

Source from the content-addressed store, hash-verified

70 logger.warning("No complete JSON object found in the input string.")
71 return None
72def extract_json_from_cluster(s:str):
73 import re
74 s=s.replace('*', '')
75 entity_name = re.search(r"Aggregate Entity Name:\s*(.+)", s).group(1).strip()
76 entity_description = re.search(
77 r"Aggregate Entity Description:\s*(.+?)\n\nFindings:", s, re.DOTALL
78 ).group(1).strip()
79
80 # 提取 findings
81 pattern = r"<summary_(\d+)>:\s*(.*?)\s*<explanation_\1>:\s*(.*?)(?=\n<summary_\d+>:|\Z)"
82 matches = re.findall(pattern, s, re.DOTALL)
83
84 findings = []
85 for _, summary, explanation in matches:
86 findings.append({
87 "summary": summary.strip().replace('\n', ' '),
88 "explanation": explanation.strip().replace('\n', ' ')
89 })
90
91 # 构造最终 JSON
92 result = {
93 "entity_name": entity_name,
94 "entity_description": entity_description,
95 "findings": findings
96 }
97
98 # 输出 JSON 字符串(可选择写入文件)
99 return result
100def parse_value(value: str):
101 """Convert a string value to its appropriate type (int, float, bool, None, or keep as string). Work as a more broad 'eval()'"""
102 value = value.strip()

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected