#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 完整导入所有岗位群的面试题 将智能制造岗位简历.json中的所有面试题导入到mock文件 """ import json import re from datetime import datetime print("=== 完整面试题导入工具 ===\n") # 1. 读取源数据 print("1. 读取智能制造岗位简历.json...") with open('网页未导入数据/智能制造产业/智能制造岗位简历.json', 'r', encoding='utf-8') as f: source_data = json.load(f) # 2. 解析所有面试题 print("2. 解析面试题数据...") interview_questions_map = {} for item in source_data: job_group = item.get('简历岗位群', '') interview_content = item.get('面试题内容', '') if not job_group or not interview_content: continue # 收集所有问题(不分章节,统一放在一起) all_questions = [] # 使用更宽松的正则表达式匹配问题 lines = interview_content.split('\n') i = 0 while i < len(lines): line = lines[i].strip() # 匹配问题编号(1. 或 1、) question_match = re.match(r'^(\d+)[\.、]\s*(.+)', line) if question_match: question_num = question_match.group(1) question_text = question_match.group(2).strip() # 查找答案 answer = "" i += 1 # 继续查找答案,直到遇到下一个问题或章节 while i < len(lines): next_line = lines[i].strip() # 如果遇到下一个问题编号或章节标题,停止 if re.match(r'^\d+[\.、]\s*', next_line) or next_line.startswith('#'): i -= 1 # 回退,让外层循环处理 break # 如果是答案标记 if next_line and (next_line.startswith('答案') or next_line.startswith('示例答案')): # 提取答案内容 answer = re.sub(r'^(答案|示例答案)[::]?\s*', '', next_line).strip() if not answer: # 答案可能在下一行 i += 1 if i < len(lines): answer = lines[i].strip() break elif next_line and not answer: # 如果没有答案标记,下一个非空行可能就是答案 if ':' not in next_line or not next_line.startswith('A.'): answer = next_line break i += 1 # 如果没有找到答案,提供默认答案 if not answer: answer = "请根据实际情况回答" all_questions.append({ 'question': question_text, 'answer': answer }) i += 1 if all_questions: interview_questions_map[job_group] = all_questions print(f" - {job_group}: 解析到 {len(all_questions)} 道题") print(f"\n共解析 {len(interview_questions_map)} 个岗位群的面试题") # 3. 读取当前mock文件 print("\n3. 读取当前mock文件...") with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: mock_content = f.read() # 4. 备份 backup_time = datetime.now().strftime('%Y%m%d_%H%M%S') backup_file = f'src/mocks/resumeInterviewMock.js.backup_full_import_{backup_time}' print(f"4. 创建备份: {backup_file}") with open(backup_file, 'w', encoding='utf-8') as f: f.write(mock_content) # 5. 更新每个岗位群的面试题 print("\n5. 更新mock文件...") def update_questions(match): industry_name = match.group(1) if industry_name not in interview_questions_map: return match.group(0) questions = interview_questions_map[industry_name] # 构建新的questions数组(统一放在一个章节下) sub_questions = [] for idx, q in enumerate(questions, 1): # 转义特殊字符 question_text = q['question'].replace('\\', '\\\\').replace('"', '\\"') answer_text = q['answer'].replace('\\', '\\\\').replace('"', '\\"').replace('\n', ' ') sub_questions.append(f'''{{ "id": "q1_{idx}", "question": "{question_text}", "answer": "{answer_text}" }}''') # 构建完整的questions结构 questions_str = f'''[ {{ "id": "group_q1", "question": "# {industry_name}岗位面试题", "subQuestions": [ {','.join(sub_questions)} ] }} ]''' # 构建替换内容 before = match.group(0).split('"questions": [')[0] result = before + '"questions": ' + questions_str print(f" - {industry_name}: 更新为 {len(questions)} 道题") return result # 执行替换 pattern = r'"name":\s*"([^"]+)"[^}]*?"questions":\s*\[.*?\]\s*(?=\s*\})' new_content = re.sub(pattern, update_questions, mock_content, flags=re.DOTALL) # 6. 保存文件 print("\n6. 保存更新后的文件...") with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(new_content) # 7. 验证 print("\n7. 验证结果...") import subprocess result = subprocess.run(['node', '-c', 'src/mocks/resumeInterviewMock.js'], capture_output=True, text=True) if result.returncode == 0: print("✅ 语法检查通过!") # 统计最终结果 with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: final_content = f.read() print("\n=== 最终统计 ===") for group in interview_questions_map.keys(): pattern = rf'"name": "{re.escape(group)}".*?"questions": \[(.*?)\]\s*\}}' match = re.search(pattern, final_content, re.DOTALL) if match: count = len(re.findall(r'"id": "q1_\d+', match.group(1))) print(f" - {group}: {count} 道题") print("\n✅ 所有岗位群的面试题已完整导入!") else: print("❌ 语法错误:") print(result.stderr) print("\n正在恢复备份...") with open(backup_file, 'r', encoding='utf-8') as f: mock_content = f.read() with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(mock_content) print("已恢复到备份版本")