Files
online_sys/frontend_智能制造/smart_clean_options.py

182 lines
6.7 KiB
Python
Raw Permalink Normal View History

#!/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_smart_{backup_time}'
print(f"2. 创建备份: {backup_file}")
with open(backup_file, 'w', encoding='utf-8') as f:
f.write(content)
# 3. 定义特定问题的正确答案
specific_answers = {
# 工业机器人相关
"示教器操作时,必须佩戴": "使能开关",
"机器人运行时,人员应": "站在安全围栏外",
"搬运玻璃工件时,优先选用哪种夹具": "真空吸盘",
'机器人报"过载错误",首先应': "检查是否卡住或超重",
"下列哪种情况可忽略": "机器人外壳轻微灰尘",
"机器人关节运动指令是": "MoveJ",
"安全围栏的作用是": "防止人员进入危险区域",
"机器人程序首次运行应使用": "低速(<30%",
"工具标定不准确会导致": "工件加工位置偏差",
"每日交接班必须做的是": "检查急停功能",
"机器人固定至地面通常使用": "膨胀螺栓",
"更换减速器需断开": "电源和控制线缆",
"气动夹爪的工作压力通常为": "0.4-0.7 MPa",
"ABB机器人的快速输入": "DI",
"碰撞传感器的报警信号接入": "安全输入端",
"工业机器人最常用的编程方式": "示教编程",
"机器人TCP是指": "工具中心点",
"工件坐标系的作用是": "定义工件位置",
"机器人的可达半径是指": "最大工作范围",
"伺服电机编码器故障会导致": "位置丢失",
"在车间加工碳钢时,切削液的主要作用": "冷却和润滑",
}
# 4. 处理函数
def clean_options_from_answer(answer_text, question_text=""):
"""清理答案中的选项,返回正确答案"""
# 检查是否有预定义的答案
for q_key, correct_answer in specific_answers.items():
if q_key in question_text:
return correct_answer
# 检查是否包含选项格式
if not re.search(r'[A-D][\.、]', answer_text) and 'B.' not in answer_text:
# 没有选项格式,返回原答案
return answer_text
# 提取选项
options = {}
# 处理缺少A.的格式答案直接以内容开始B.才出现)
if 'B.' in answer_text and not answer_text.strip().startswith('A.'):
first_b_pos = answer_text.find('B.')
if first_b_pos > 0:
options['A'] = answer_text[:first_b_pos].strip()
remaining = answer_text[first_b_pos:]
# 提取B, C, D选项
for match in re.finditer(r'([B-D])[\.、]\s*([^A-D]+?)(?=\s*[B-D][\.、]|$)', remaining):
letter = match.group(1)
content = match.group(2).strip()
options[letter] = content
else:
# 标准格式 A. xxx B. xxx
for match in re.finditer(r'([A-D])[\.、]\s*([^A-D]+?)(?=\s*[A-D][\.、]|$)', answer_text):
letter = match.group(1)
content = match.group(2).strip()
options[letter] = content
# 如果成功提取选项,根据规则选择答案
if options:
# 尝试智能选择
if '必须' in question_text or '应该' in question_text:
# 安全相关,选择最安全的
for opt, text in options.items():
if '安全' in text or '保护' in text or '正确' in text or '使能' in text:
return text
if '首先' in question_text:
# 选择基础检查步骤
for opt, text in options.items():
if '检查' in text or '确认' in text or '观察' in text:
return text
if '优先' in question_text or '' in question_text:
# 选择最优选项
if 'B' in options: # 经验上B选项往往是正确答案
return options['B']
# 默认返回第一个看起来合理的选项
if len(options) > 0:
# 优先选择B或C经验规则
if 'B' in options:
return options['B']
elif 'C' in options:
return options['C']
else:
# 返回第一个选项
return list(options.values())[0]
# 如果无法处理,返回原答案
return answer_text
# 5. 执行清理
print("\n3. 开始清理答案中的选项...")
cleaned_count = 0
total_count = 0
# 查找所有的问题-答案对
pattern = r'(\s*"question":\s*"([^"]+)",\s*\n\s*"answer":\s*")([^"]+)(")'
def replace_answer(match):
global cleaned_count, total_count
total_count += 1
prefix = match.group(1)
question = match.group(2)
answer = match.group(3)
suffix = match.group(4)
# 清理答案
cleaned_answer = clean_options_from_answer(answer, question)
if cleaned_answer != answer:
cleaned_count += 1
# 转义特殊字符
cleaned_answer = cleaned_answer.replace('\\', '\\\\').replace('"', '\\"')
return f'{prefix}{cleaned_answer}{suffix}'
return match.group(0)
# 执行替换
content = re.sub(pattern, replace_answer, content, flags=re.MULTILINE)
print(f"检查了 {total_count} 个答案")
print(f"清理了 {cleaned_count} 个包含选项的答案")
# 6. 保存文件
print("\n4. 保存更新后的文件...")
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
f.write(content)
# 7. 验证语法
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✅ 清理完成!成功清理 {cleaned_count} 个选项格式的答案")
# 检查是否还有剩余的选项格式
remaining = len(re.findall(r'"answer":\s*"[^"]*[B-D]\.[^"]*"', content))
if remaining > 0:
print(f"⚠️ 还有 {remaining} 个答案可能包含选项格式(这些可能是流程描述或其他非选择题内容)")
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("已恢复到备份版本")