bidmaster-cli/src/bidmaster/tools/table.py
sladro c1292fcacc feat: add validation and toc pipeline upgrades
Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
2025-11-19 10:11:21 +08:00

110 lines
3.6 KiB
Python

"""表格生成器
生成响应表和偏离表的工具类。
"""
import json
from pathlib import Path
class TableGenerator:
"""表格生成器类"""
def __init__(self) -> None:
"""初始化表格生成器"""
pass
def assemble_tables(self, project_path: str, **kwargs):
"""组装响应表和偏离表"""
project_dir = Path(project_path)
if not project_dir.exists():
raise FileNotFoundError(f"项目目录不存在: {project_path}")
# 直接实现核心功能
analysis_file = project_dir / "analysis_result.json"
tasks_file = project_dir / "tasks.json"
if not analysis_file.exists():
raise FileNotFoundError(f"分析结果不存在: {analysis_file}")
if not tasks_file.exists():
raise FileNotFoundError(f"任务清单不存在: {tasks_file}")
# 执行组装
with open(analysis_file, 'r', encoding='utf-8') as f:
analysis = json.load(f)
with open(tasks_file, 'r', encoding='utf-8') as f:
tasks = json.load(f)
response_table = self.generate_response_table(
analysis.get('technical_criteria', []),
tasks
)
deviation_table = self.generate_deviation_table(
analysis.get('deviation_items', [])
)
# 保存结果
output = project_dir / "tables_result.json"
with open(output, 'w', encoding='utf-8') as f:
json.dump({
'response_table': response_table,
'deviation_table': deviation_table
}, f, ensure_ascii=False, indent=2)
def generate_response_table(self, criteria: list[dict], tasks: list[dict]) -> list[dict]:
"""生成响应表"""
response_table = []
for criterion in criteria:
task_content = self._find_task_content(criterion, tasks)
response_table.append({
'item': criterion.get('item_name', ''),
'requirement': criterion.get('description', ''),
'response': task_content,
'score': criterion.get('max_score', 0),
'status': '完全响应' if task_content != "待生成" else '待响应'
})
return response_table
def generate_deviation_table(self, deviation_items: list[dict]) -> list[dict]:
"""生成偏离表"""
deviation_table = []
for item in deviation_items:
deviation_table.append({
'requirement': item.get('requirement', ''),
'response_type': item.get('response_type', '完全响应'),
'deviation_reason': self._get_deviation_reason(item)
})
return deviation_table
def _find_task_content(self, criterion: dict, tasks: list[dict]) -> str:
"""查找任务内容"""
chapter_id = criterion.get('chapter_id')
for task in tasks:
if task.get('chapter_id') == chapter_id:
content = task.get('content', '')
if content and len(content) > 100:
return content[:100] + "..."
return content if content else "待生成"
return "待生成"
def _get_deviation_reason(self, item: dict) -> str:
"""获取偏离原因"""
response_type = item.get('response_type', '完全响应')
if response_type == '完全响应':
return '无偏离'
elif response_type == '部分响应':
return '部分功能通过替代方案实现'
else:
return '技术方案调整说明'