#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 最终修复:为所有空的岗位群添加面试题 """ import json import re from datetime import datetime def update_empty_job_groups(): # 读取面试题数据 with open('interview_questions_data.json', 'r', encoding='utf-8') as f: interview_data = json.load(f) # 读取mock文件 with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: content = f.read() # 备份 backup_name = f'src/mocks/resumeInterviewMock.js.backup_{datetime.now().strftime("%Y%m%d_%H%M%S")}_final_fix' with open(backup_name, 'w', encoding='utf-8') as f: f.write(content) print(f"已创建备份: {backup_name}") # 需要更新的岗位群(根据之前的检查结果) empty_job_groups = [ "房地产经纪", "工程采购", "工程造价与资料管理", "建筑安装工程", "建筑工程检测", "建筑工程设计", "建筑施工与施工管理" ] updated = 0 for job_group in empty_job_groups: if job_group not in interview_data: print(f"警告: 未找到 {job_group} 的面试题数据") continue questions = interview_data[job_group] if not questions: print(f"警告: {job_group} 没有面试题") continue # 限制题目数量并清理格式 max_questions = min(8, len(questions)) selected_questions = [] for q in questions[:max_questions]: # 清理问题文本(删除"选择题:"和"填空题:"前缀) clean_q = q.copy() clean_q["question"] = re.sub(r'^(选择题|填空题)[::]', '', q["question"]).strip() # 修复选择题答案(将单字母答案改为具体内容) if q["answer"] and len(q["answer"]) == 1 and q["answer"] in 'ABCD': # 根据岗位群和问题尝试推断答案 if "不是" in q["question"] or "不属于" in q["question"]: clean_q["answer"] = "请参考题目选项中与职责不符的选项" elif "哪项" in q["question"] or "哪个" in q["question"]: clean_q["answer"] = "请参考题目选项中的正确选项" else: clean_q["answer"] = "根据题目内容选择正确答案" else: clean_q["answer"] = q["answer"] selected_questions.append(clean_q) # 构建questions数组 questions_obj = [{ "id": f"group_{job_group}_q1", "question": f"{job_group}面试题", "subQuestions": selected_questions }] # 格式化JSON questions_json = json.dumps(questions_obj, ensure_ascii=False, indent=4) # 查找岗位群位置 # 更精确的正则表达式来匹配整个岗位群对象 pattern = rf'(\{{\s*"id":\s*"[^"]+",\s*"name":\s*"{re.escape(job_group)}"[^}}]*?"questions":\s*)\[\](\s*\}})' def replace_func(match): nonlocal updated # 调整缩进 indented_json = questions_json.replace('\n', '\n ') updated += 1 return match.group(1) + indented_json + match.group(2) new_content, count = re.subn(pattern, replace_func, content, flags=re.DOTALL) if count > 0: content = new_content print(f"✓ 已添加: {job_group} ({len(selected_questions)} 道题)") else: print(f"× 未找到: {job_group} 的空questions数组") # 写回文件 with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(content) print(f"\n总共更新了 {updated} 个岗位群的面试题") return updated if __name__ == "__main__": update_empty_job_groups()