read_file_998abacd.txt 4.6 KB

12345678
  1. {
  2. "success": true,
  3. "content": "\"\"\"\n结果处理组件实现\n\"\"\"\nimport json\nimport re\nfrom typing import Dict, List, Any\nimport sys\nfrom pathlib import Path\n\n# 添加项目根目录到路径,支持相对导入\n_root = Path(__file__).parent.parent\nif str(_root) not in sys.path:\n sys.path.insert(0, str(_root))\n\nfrom interfaces import IResultProcessor\n\n\nclass ResultProcessor(IResultProcessor):\n \"\"\"结果处理器\"\"\"\n \n def parse_result(self, llm_response: str, requirements: List[Dict[str, str]]) -> Dict[str, List[int]]:\n \"\"\"\n 解析LLM返回结果\n \n Args:\n llm_response: LLM返回的文本\n requirements: 审查要求列表(用于验证二级目录名称)\n \n Returns:\n 字典,key为二级目录名称,value为包含的要点编号列表\n 格式: {\"法律法规\": [1, 2], \"标准规范\": [1], \"文件制度\": []}\n \"\"\"\n text = (llm_response or \"\").strip()\n if not text:\n return {}\n\n # 提取JSON部分\n json_str = self._extract_json(text)\n if not json_str:\n # 如果无法提取JSON,返回空字典\n return {}\n\n try:\n result = json.loads(json_str)\n except json.JSONDecodeError:\n # JSON解析失败,返回空字典\n return {}\n\n # 验证结果格式:应该是字典,字段名为二级目录,值为整数列表\n if not isinstance(result, dict):\n return {}\n\n # 规范化结果:确保所有值都是整数列表\n normalized_result = {}\n for key, value in result.items():\n if isinstance(value, list):\n # 过滤出整数,忽略非整数项\n int_list = [int(item) for item in value if isinstance(item, (int, str)) and str(item).isdigit()]\n normalized_result[key] = int_list\n elif isinstance(value, (int, str)) and str(value).isdigit():\n # 单个数字,转换为列表\n normalized_result[key] = [int(value)]\n else:\n # 其他类型,设为空列表\n normalized_result[key] = []\n\n return normalized_result\n \n def _extract_json(self, text: str) -> str:\n \"\"\"\n 从文本中提取JSON字符串\n \n Args:\n text: 原始文本\n \n Returns:\n JSON字符串\n \"\"\"\n text = text.strip()\n \n # 首先尝试查找代码块中的JSON\n code_block_pattern = r'```(?:json)?\\s*(\\{.*?\\})\\s*```'\n code_matches = re.findall(code_block_pattern, text, re.DOTALL)\n if code_matches:\n # 尝试解析,找到第一个有效的JSON\n for match in code_matches:\n try:\n json.loads(match)\n return match\n except json.JSONDecodeError:\n continue\n \n # 尝试直接解析整个文本\n try:\n json.loads(text)\n return text\n except json.JSONDecodeError:\n pass\n \n # 使用更智能的方法:从第一个 { 开始,找到匹配的 }\n start_idx = text.find('{')\n if start_idx == -1:\n return text\n \n # 从开始位置查找匹配的结束大括号\n brace_count = 0\n in_string = False\n escape_next = False\n \n for i in range(start_idx, len(text)):\n char = text[i]\n \n if escape_next:\n escape_next = False\n continue\n \n if char == '\\\\':\n escape_next = True\n continue\n \n if char == '\"' and not escape_next:\n in_string = not in_string\n continue\n \n if not in_string:\n if char == '{':\n brace_count += 1\n elif char == '}':\n brace_count -= 1\n if brace_count == 0:\n # 找到匹配的结束大括号\n json_str = text[start_idx:i+1]\n try:\n json.loads(json_str)\n return json_str\n except json.JSONDecodeError:\n pass\n \n return text\n\n",
  4. "start_line": 1,
  5. "end_line": 140,
  6. "total_lines": 140,
  7. "has_more": false
  8. }