#!/usr/bin/env python3 # -*- coding: utf-8 -*- import json import re from datetime import datetime # 读取智能制造岗位简历数据 print("正在读取智能制造岗位简历.json...") with open('网页未导入数据/智能制造产业/智能制造岗位简历.json', 'r', encoding='utf-8') as f: smart_mfg_data = json.load(f) # 读取当前mock文件 print("正在读取当前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_sections_fix_{backup_time}' with open(backup_file, 'w', encoding='utf-8') as f: f.write(mock_content) print(f"已创建备份文件: {backup_file}") # 解析智能制造岗位简历数据,构建完整的面试题映射 interview_data_map = {} 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) for i, section in enumerate(sections): if not section.strip(): continue # 处理第一个章节(可能没有前置换行符) if i == 0 and not section.startswith('一、'): section = section[2:] if section.startswith('# ') else section lines = section.split('\n') if not lines: continue # 第一行是章节标题 section_title = lines[0].strip() if not section_title: continue current_section_questions = [] # 解析该章节中的问题 j = 1 # 跳过标题行 while j < len(lines): line = lines[j].strip() if not line: j += 1 continue # 查找问题编号(如 1. xxx 或 1、xxx) 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_lines = [] j += 1 # 移到下一行开始查找答案 while j < len(lines): next_line = lines[j].strip() # 如果遇到下一个问题,停止 if re.match(r'^\d+[\.\、]\s*', next_line): j -= 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 next_line.startswith('-') or not re.match(r'^答案', next_line): # 其他内容作为答案的一部分 answer_lines.append(next_line) j += 1 # 整理答案 answer = ' '.join(answer_lines).strip() if not answer: answer = "请根据实际情况回答" current_section_questions.append({ 'question': question_text, 'answer': answer }) else: j += 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共解析 {len(interview_data_map)} 个岗位群的面试题数据") # 统计解析结果 for group, data in interview_data_map.items(): total_questions = sum(len(section['questions']) for section in data) print(f" - {group}: {len(data)} 个章节,{total_questions} 道题") # 更新mock文件中的面试题 def update_industry_questions(match): full_match = match.group(0) industry_name = match.group(1) # 查找对应的面试题数据 if industry_name not in interview_data_map: print(f" - {industry_name}: 保持原样(未找到对应数据)") return full_match 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("\n正在更新面试题数据...") pattern = r'"name":\s*"([^"]+)"[^}]*?"questions":\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("\n✅ 面试题数据更新完成!") # 验证语法 import subprocess result = subprocess.run(['node', '-c', 'src/mocks/resumeInterviewMock.js'], capture_output=True, text=True) if result.returncode == 0: print("✅ 语法检查通过!") else: print("❌ 语法错误:") print(result.stderr)