#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ 修复不完整的选项格式答案 根据问题类型智能选择正确答案 """ import re from datetime import datetime print("=== 修复不完整的选项格式答案 ===\n") # 1. 读取当前mock文件 print("1. 读取当前mock文件...") with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f: content = f.read() # 2. 备份 backup_time = datetime.now().strftime('%Y%m%d_%H%M%S') backup_file = f'src/mocks/resumeInterviewMock.js.backup_fix_options_{backup_time}' print(f"2. 创建备份: {backup_file}") with open(backup_file, 'w', encoding='utf-8') as f: f.write(content) # 3. 定义问题和正确答案的映射 print("\n3. 分析并修复不完整的选项...") # 需要手动处理的特殊问题和答案 manual_fixes = { "示教器操作时,必须佩戴": "使能开关", # C "机器人运行时,人员应": "站在安全围栏外", # B "搬运玻璃工件时,优先选用哪种夹具?": "真空吸盘", # B '机器人报"过载错误",首先应': "检查是否卡住或超重", # B "下列哪种情况可忽略?": "机器人外壳轻微灰尘", # C "机器人固定至地面通常使用": "膨胀螺栓", # B (假设) "更换减速器需断开": "电源", # A (假设) "气动夹爪的工作压力通常为": "0.4-0.7 MPa", # B (假设) } # 通用的智能选择规则 def smart_select_answer(question, answer_text): """根据问题智能选择最合理的答案""" # 提取选项 options = {} # 处理缺少A.的情况 if not answer_text.startswith('A.') and 'B.' in answer_text: # 第一个选项前面没有A. parts = answer_text.split('B.') if parts[0].strip(): options['A'] = parts[0].strip() remaining = 'B.' + 'B.'.join(parts[1:]) # 继续提取其他选项 for match in re.finditer(r'([B-D])\.\s*([^A-D]+?)(?=\s*[B-D]\.|$)', remaining): options[match.group(1)] = match.group(2).strip() else: # 正常格式 for match in re.finditer(r'([A-D])\.\s*([^A-D]+?)(?=\s*[A-D]\.|$)', answer_text): options[match.group(1)] = match.group(2).strip() # 如果在手动映射中,直接返回答案 for key, value in manual_fixes.items(): if key in question: return value # 智能选择规则 if "必须" in question or "应该" in question: # 安全相关问题,选择最安全的选项 for opt, text in options.items(): if "安全" in text or "保护" in text or "正确" in text: return text if "优先" in question or "最好" in question: # 选择最优的选项 for opt, text in options.items(): if "最" in text or "优" in text: return text if "首先" in question: # 选择最基础的检查或操作 for opt, text in options.items(): if "检查" in text or "确认" in text: return text if "不" in question or "忽略" in question or "错误" in question: # 否定性问题,选择例外或错误的选项 pass # 需要特殊处理 # 如果没有匹配规则,返回None表示需要保留原样 return None # 4. 执行修复 fixed_count = 0 failed_count = 0 failed_questions = [] def fix_answer(match): """修复答案中的选项格式""" global fixed_count, failed_count, failed_questions full_match = match.group(0) question = match.group(1) answer = match.group(2) # 检查是否包含选项格式 if re.search(r'[B-D]\.', answer) or (not answer.startswith('A.') and len(answer.split()) > 3): # 尝试智能选择 correct_answer = smart_select_answer(question, answer) if correct_answer: fixed_count += 1 return f'{match.group(1)}",\n "answer": "{correct_answer}"' else: # 如果无法自动决定,记录下来 failed_count += 1 failed_questions.append((question, answer)) return full_match # 匹配问题和答案对 pattern = r'"question": "([^"]+)",\n\s*"answer": "([^"]+)"' content = re.sub(pattern, fix_answer, content) print(f"\n成功修复: {fixed_count} 个答案") print(f"需要手动处理: {failed_count} 个答案") if failed_questions: print("\n以下问题需要手动确认正确答案:") for q, a in failed_questions[:10]: # 只显示前10个 print(f"问题: {q}") print(f"选项: {a}") print("-" * 50) # 5. 保存文件 print("\n4. 保存更新后的文件...") with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(content) # 6. 验证语法 print("\n5. 验证语法...") import subprocess result = subprocess.run(['node', '-c', 'src/mocks/resumeInterviewMock.js'], capture_output=True, text=True) if result.returncode == 0: print("✅ 语法检查通过!") print(f"\n✅ 修复完成!共处理 {fixed_count} 个选项格式的答案") else: print("❌ 语法错误:") print(result.stderr) print("\n正在恢复备份...") with open(backup_file, 'r', encoding='utf-8') as f: content = f.read() with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f: f.write(content) print("已恢复到备份版本")