#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import re from datetime import datetime print("=== 完整面试题更新工具 ===") print("此工具将完整导入所有岗位群的所有章节面试题") # 读取智能制造岗位简历数据 print("\n1. 读取源数据...") with open('网页未导入数据/智能制造产业/智能制造岗位简历.json', 'r', encoding='utf-8') as f: smart_mfg_data = json.load(f) # 读取当前mock文件 print("2. 读取当前mock文件...") with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: mock_content = f.read() # 备份原文件 backup_time = datetime.now().strftime('%Y%m%d_%H%M%S') backup_file = f'src/mocks/resumeInterviewMock.js.backup_complete_update_{backup_time}' with open(backup_file, 'w', encoding='utf-8') as f: f.write(mock_content) print(f"3. 已创建备份: {backup_file}") # 解析智能制造岗位简历数据 interview_data_map = {} print("\n4. 解析面试题数据...") for item in smart_mfg_data: job_group = item.get('简历岗位群', '') interview_content = item.get('面试题内容', '') if job_group and interview_content: questions_data = [] # 按 # 分割章节,但保留 # 标记 sections = re.split(r'\n(?=# )', interview_content.strip()) for section in sections: if not section.strip(): continue lines = section.split('\n') if not lines: continue # 第一行是章节标题 section_title = lines[0].strip() if section_title.startswith('# '): section_title = section_title[2:].strip() if not section_title: continue current_section_questions = [] # 解析该章节中的问题 i = 1 # 跳过标题行 while i < len(lines): line = lines[i].strip() if not line: i += 1 continue # 查找问题编号(如 1. xxx 或 1、xxx) question_match = re.match(r'^(\d+)[\.\、]\s*(.+)', line) if question_match: question_text = question_match.group(2).strip() # 查找答案 answer_lines = [] i += 1 # 移到下一行开始查找答案 while i < len(lines): next_line = lines[i].strip() # 如果遇到下一个问题,停止 if re.match(r'^\d+[\.\、]\s*', next_line): i -= 1 # 回退,让外层循环处理 break # 处理答案内容 if next_line: # 移除答案标记 if re.match(r'^答案[::]?\s*', next_line): answer_text = re.sub(r'^答案[::]?\s*', '', next_line) if answer_text: answer_lines.append(answer_text) elif not re.match(r'^答案', next_line): # 其他内容作为答案的一部分 answer_lines.append(next_line) i += 1 # 整理答案 answer = ' '.join(answer_lines).strip() if not answer: answer = "请根据实际情况回答" current_section_questions.append({ 'question': question_text, 'answer': answer }) else: i += 1 # 如果该章节有问题,添加到结果中 if current_section_questions: questions_data.append({ 'id': f'group_q{len(questions_data) + 1}', 'title': section_title, 'questions': current_section_questions }) interview_data_map[job_group] = questions_data print(f"\n解析结果:") total_sections = 0 total_questions = 0 for group, data in interview_data_map.items(): section_count = len(data) question_count = sum(len(section['questions']) for section in data) total_sections += section_count total_questions += question_count print(f" - {group}: {section_count} 个章节,{question_count} 道题") print(f"\n总计: {len(interview_data_map)} 个岗位群,{total_sections} 个章节,{total_questions} 道题") # 更新mock文件中的面试题 def update_industry_questions(match): industry_name = match.group(1) # 查找对应的面试题数据 if industry_name not in interview_data_map: return match.group(0) # 保持原样 questions_data = interview_data_map[industry_name] # 构建新的questions数组 new_questions = [] for section_data in questions_data: sub_questions = [] for idx, q in enumerate(section_data['questions']): # 转义特殊字符 question_text = q['question'].replace('\\', '\\\\').replace('"', '\\"') answer_text = q['answer'].replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n') sub_questions.append('{\\n "id": "q{}_{}", \\n "question": "{}", \\n "answer": "{}"\\n }'.format( section_data['id'][7:], idx+1, question_text, answer_text)) section_title = section_data['title'].replace('\\', '\\\\').replace('"', '\\"') new_questions.append('{\\n "id": "{}", \\n "question": "# {}", \\n "subQuestions": [\\n {}\\n ]\\n }'.format( section_data['id'], section_title, ','.join(sub_questions))) # 替换questions部分 questions_str = ',\\n '.join(new_questions) # 构建完整的替换内容 before_questions = match.group(0).split('"questions": [')[0] result = before_questions + '"questions": [\\n ' + questions_str + '\\n ]' # 统计题目数量 total_questions = sum(len(section['questions']) for section in questions_data) print(f" - {industry_name}: 更新为 {len(questions_data)} 个章节,{total_questions} 道题") return result # 执行更新 print("\n5. 正在更新面试题数据...") pattern = r'"name":\s*"([^"]+)"[^}]*?"questions":\s*\[.*?\]\s*(?=\s*\})' new_content = re.sub(pattern, update_industry_questions, mock_content, flags=re.DOTALL) # 保存更新后的文件 with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(new_content) print("\n6. ✅ 面试题数据更新完成!") # 验证语法 print("7. 验证语法...") 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() final_questions = len(re.findall(r'"id": "q[0-9]', final_content)) final_sections = len(re.findall(r'"question": "# ', final_content)) print(f"\n=== 最终统计 ===") print(f"总章节数: {final_sections}") print(f"总题目数: {final_questions}") print(f"\n现在用户可以在页面看到完整的面试题了!") else: print("❌ 语法错误:") print(result.stderr)