#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import re from datetime import datetime # 读取面试题数据 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")}_direct' with open(backup_name, 'w', encoding='utf-8') as f: f.write(content) print(f"已创建备份: {backup_name}") # 为每个空岗位群生成面试题 def generate_questions(job_group_name): if job_group_name not in interview_data: return [] questions = interview_data[job_group_name][:8] # 限制8道题 cleaned = [] for q in questions: clean_q = { "id": q["id"], "question": re.sub(r'^(选择题|填空题)[::]', '', q["question"]).strip(), "answer": q["answer"] } # 修复单字母答案 if len(q["answer"]) == 1 and q["answer"] in 'ABCD': if "不是" in q["question"] or "不属于" in q["question"]: clean_q["answer"] = "请参考题目选项中与职责不符的选项" else: clean_q["answer"] = "请参考题目选项中的正确答案" cleaned.append(clean_q) return [{ "id": f"group_{job_group_name}_q1", "question": f"{job_group_name}面试题", "subQuestions": cleaned }] # 手动替换每个空questions updates = [ (184, "房地产经纪"), (251, "工程采购"), (422, "工程造价与资料管理"), (529, "建筑安装工程"), (740, "建筑工程检测"), (867, "建筑工程设计"), (1142, "建筑施工与施工管理") ] lines = content.split('\n') for line_no, job_group in updates: questions_obj = generate_questions(job_group) if questions_obj: # 格式化JSON questions_json = json.dumps(questions_obj, ensure_ascii=False, indent=4) # 调整缩进(4个空格) questions_json = questions_json.replace('\n', '\n ') # 替换对应行 lines[line_no - 1] = ' "questions": ' + questions_json print(f"✓ 已更新: {job_group} (行 {line_no})") # 写回文件 with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write('\n'.join(lines)) print("\n完成所有更新!")