98 lines
7.2 KiB
JSON
98 lines
7.2 KiB
JSON
{
|
||
"file_path": "tweet/topic_parser.py",
|
||
"file_size": 1544,
|
||
"line_count": 58,
|
||
"functions": [
|
||
{
|
||
"name": "parse",
|
||
"line_start": 22,
|
||
"line_end": 59,
|
||
"args": [
|
||
{
|
||
"name": "raw_text",
|
||
"type_hint": "str"
|
||
}
|
||
],
|
||
"return_type": "List[Dict[str, Any]]",
|
||
"docstring": "从原始文本解析、修复和验证JSON\n\nArgs:\n raw_text: AI模型返回的原始字符串\n\nReturns:\n 一个字典列表,每个字典代表一个有效的选题",
|
||
"is_async": false,
|
||
"decorators": [
|
||
"staticmethod"
|
||
],
|
||
"code": " def parse(raw_text: str) -> List[Dict[str, Any]]:\n \"\"\"\n 从原始文本解析、修复和验证JSON\n\n Args:\n raw_text: AI模型返回的原始字符串\n\n Returns:\n 一个字典列表,每个字典代表一个有效的选题\n \"\"\"\n logger.info(\"开始解析AI生成的选题...\")\n \n # 使用通用JSON解析函数解析原始文本\n parsed_json = process_llm_json_text(raw_text)\n \n if not parsed_json:\n logger.error(\"解析AI响应失败,无法获取JSON数据\")\n return []\n \n if not isinstance(parsed_json, list):\n logger.error(f\"解析结果不是列表,而是 {type(parsed_json)}\")\n return []\n \n logger.info(f\"成功解析 {len(parsed_json)} 个选题对象。开始验证...\")\n \n # 验证每个选题是否包含所有必需的键\n valid_topics = []\n required_keys = {\"index\", \"date\", \"logic\", \"object\", \"product\", \"style\", \"targetAudience\"}\n optional_keys = {\"productLogic\", \"styleLogic\", \"targetAudienceLogic\"}\n \n for i, item in enumerate(parsed_json):\n if isinstance(item, dict) and required_keys.issubset(item.keys()):\n valid_topics.append(item)\n else:\n logger.warning(f\"第 {i+1} 个选题缺少必需键或格式不正确: {item}\")\n \n logger.info(f\"验证完成,获得 {len(valid_topics)} 个有效选题。\")\n return valid_topics ",
|
||
"code_hash": "b764577bd884f9a327e661e58d893403"
|
||
}
|
||
],
|
||
"classes": [
|
||
{
|
||
"name": "TopicParser",
|
||
"line_start": 16,
|
||
"line_end": 59,
|
||
"bases": [],
|
||
"methods": [
|
||
{
|
||
"name": "parse",
|
||
"line_start": 22,
|
||
"line_end": 59,
|
||
"args": [
|
||
{
|
||
"name": "raw_text",
|
||
"type_hint": "str"
|
||
}
|
||
],
|
||
"return_type": "List[Dict[str, Any]]",
|
||
"docstring": "从原始文本解析、修复和验证JSON\n\nArgs:\n raw_text: AI模型返回的原始字符串\n\nReturns:\n 一个字典列表,每个字典代表一个有效的选题",
|
||
"is_async": false,
|
||
"decorators": [
|
||
"staticmethod"
|
||
],
|
||
"code": " def parse(raw_text: str) -> List[Dict[str, Any]]:\n \"\"\"\n 从原始文本解析、修复和验证JSON\n\n Args:\n raw_text: AI模型返回的原始字符串\n\n Returns:\n 一个字典列表,每个字典代表一个有效的选题\n \"\"\"\n logger.info(\"开始解析AI生成的选题...\")\n \n # 使用通用JSON解析函数解析原始文本\n parsed_json = process_llm_json_text(raw_text)\n \n if not parsed_json:\n logger.error(\"解析AI响应失败,无法获取JSON数据\")\n return []\n \n if not isinstance(parsed_json, list):\n logger.error(f\"解析结果不是列表,而是 {type(parsed_json)}\")\n return []\n \n logger.info(f\"成功解析 {len(parsed_json)} 个选题对象。开始验证...\")\n \n # 验证每个选题是否包含所有必需的键\n valid_topics = []\n required_keys = {\"index\", \"date\", \"logic\", \"object\", \"product\", \"style\", \"targetAudience\"}\n optional_keys = {\"productLogic\", \"styleLogic\", \"targetAudienceLogic\"}\n \n for i, item in enumerate(parsed_json):\n if isinstance(item, dict) and required_keys.issubset(item.keys()):\n valid_topics.append(item)\n else:\n logger.warning(f\"第 {i+1} 个选题缺少必需键或格式不正确: {item}\")\n \n logger.info(f\"验证完成,获得 {len(valid_topics)} 个有效选题。\")\n return valid_topics ",
|
||
"code_hash": "b764577bd884f9a327e661e58d893403"
|
||
}
|
||
],
|
||
"docstring": "解析和验证由AI模型生成的选题列表",
|
||
"decorators": [],
|
||
"code": "class TopicParser:\n \"\"\"\n 解析和验证由AI模型生成的选题列表\n \"\"\"\n\n @staticmethod\n def parse(raw_text: str) -> List[Dict[str, Any]]:\n \"\"\"\n 从原始文本解析、修复和验证JSON\n\n Args:\n raw_text: AI模型返回的原始字符串\n\n Returns:\n 一个字典列表,每个字典代表一个有效的选题\n \"\"\"\n logger.info(\"开始解析AI生成的选题...\")\n \n # 使用通用JSON解析函数解析原始文本\n parsed_json = process_llm_json_text(raw_text)\n \n if not parsed_json:\n logger.error(\"解析AI响应失败,无法获取JSON数据\")\n return []\n \n if not isinstance(parsed_json, list):\n logger.error(f\"解析结果不是列表,而是 {type(parsed_json)}\")\n return []\n \n logger.info(f\"成功解析 {len(parsed_json)} 个选题对象。开始验证...\")\n \n # 验证每个选题是否包含所有必需的键\n valid_topics = []\n required_keys = {\"index\", \"date\", \"logic\", \"object\", \"product\", \"style\", \"targetAudience\"}\n optional_keys = {\"productLogic\", \"styleLogic\", \"targetAudienceLogic\"}\n \n for i, item in enumerate(parsed_json):\n if isinstance(item, dict) and required_keys.issubset(item.keys()):\n valid_topics.append(item)\n else:\n logger.warning(f\"第 {i+1} 个选题缺少必需键或格式不正确: {item}\")\n \n logger.info(f\"验证完成,获得 {len(valid_topics)} 个有效选题。\")\n return valid_topics ",
|
||
"code_hash": "8ea78a1b3f8cfa61023ecbaf1638bfd5"
|
||
}
|
||
],
|
||
"imports": [
|
||
{
|
||
"type": "import",
|
||
"modules": [
|
||
"logging"
|
||
],
|
||
"aliases": []
|
||
},
|
||
{
|
||
"type": "import",
|
||
"modules": [
|
||
"json"
|
||
],
|
||
"aliases": []
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "typing",
|
||
"names": [
|
||
"List",
|
||
"Dict",
|
||
"Any"
|
||
],
|
||
"aliases": [],
|
||
"level": 0
|
||
},
|
||
{
|
||
"type": "from_import",
|
||
"module": "utils.file_io",
|
||
"names": [
|
||
"process_llm_json_text"
|
||
],
|
||
"aliases": [],
|
||
"level": 0
|
||
}
|
||
],
|
||
"constants": [],
|
||
"docstring": "AI响应解析器模块",
|
||
"content_hash": "ef155d2ff5e2ef181d534b9d314c4cee"
|
||
} |