Files
ALL-teach_sys/frontend_智能制造/fix_interview_answers_properly.py

187 lines
6.8 KiB
Python
Raw Permalink Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import json
import re
# 读取智能制造岗位简历数据
with open('网页未导入数据/智能制造产业/智能制造岗位简历.json', 'r', encoding='utf-8') as f:
smart_mfg_data = json.load(f)
# 读取当前的mock文件
with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f:
mock_content = f.read()
# 备份原文件
import datetime
backup_time = datetime.datetime.now().strftime('%Y%m%d_%H%M%S')
with open(f'src/mocks/resumeInterviewMock.js.backup_final_{backup_time}', 'w', encoding='utf-8') as f:
f.write(mock_content)
print(f"已创建备份文件: src/mocks/resumeInterviewMock.js.backup_final_{backup_time}")
# 创建岗位群到面试题的映射
interview_map = {}
for item in smart_mfg_data:
group = item.get('简历岗位群', '')
questions = item.get('面试题内容', '')
if group and questions:
# 解析面试题内容
parsed_questions = []
lines = questions.split('\n')
current_section = None
current_q = None
current_a = []
for line in lines:
line = line.strip()
if not line:
continue
# 大标题
if line.startswith('# '):
if current_q and current_section:
answer = ' '.join(current_a).strip()
if answer.startswith('示例答案:') or answer.startswith('答案:'):
answer = answer.split('', 1)[1].strip()
current_section['questions'].append({
'question': current_q,
'answer': answer
})
if current_section:
parsed_questions.append(current_section)
current_section = {
'title': line[2:].strip(),
'questions': []
}
current_q = None
current_a = []
# 问题编号
elif re.match(r'^\d+\.\s+', line):
if current_q and current_section:
answer = ' '.join(current_a).strip()
if answer.startswith('示例答案:') or answer.startswith('答案:'):
answer = answer.split('', 1)[1].strip()
current_section['questions'].append({
'question': current_q,
'answer': answer
})
current_q = re.sub(r'^\d+\.\s+', '', line)
current_a = []
# 答案内容
else:
if current_q:
current_a.append(line)
# 保存最后一个
if current_q and current_section:
answer = ' '.join(current_a).strip()
if answer.startswith('示例答案:') or answer.startswith('答案:'):
answer = answer.split('', 1)[1].strip()
current_section['questions'].append({
'question': current_q,
'answer': answer
})
if current_section:
parsed_questions.append(current_section)
interview_map[group] = parsed_questions
# 需要更新的岗位群
target_groups = ['PLC', '工业机器人', '焊接工艺', '电气设计', '机器视觉', '非标自动化设计']
# 更新每个岗位群
for group_name in target_groups:
if group_name not in interview_map:
print(f"未找到 {group_name} 的面试题数据")
continue
# 查找岗位群位置
pattern = rf'"name":\s*"{re.escape(group_name)}"'
match = re.search(pattern, mock_content)
if not match:
print(f"未找到岗位群: {group_name}")
continue
# 找到questions数组
start_pos = match.end()
questions_pattern = r'"questions":\s*\['
questions_match = re.search(questions_pattern, mock_content[start_pos:start_pos+5000])
if not questions_match:
continue
questions_start = start_pos + questions_match.end()
# 找到questions数组的结束
bracket_count = 1
i = questions_start
in_string = False
escape_next = False
while i < len(mock_content) and bracket_count > 0:
char = mock_content[i]
if escape_next:
escape_next = False
elif char == '\\':
escape_next = True
elif char == '"' and not escape_next:
in_string = not in_string
elif not in_string:
if char == '[':
bracket_count += 1
elif char == ']':
bracket_count -= 1
i += 1
if bracket_count == 0:
questions_end = i - 1
# 构建新的questions数组
new_questions = []
for idx, section in enumerate(interview_map[group_name]):
sub_questions = []
for q_idx, q in enumerate(section['questions']):
# 转义特殊字符
question = q['question'].replace('\\', '\\\\').replace('"', '\\"')
answer = q['answer'].replace('\\', '\\\\').replace('"', '\\"').replace('\n', '\\n')
sub_questions.append(f'''{{
"id": "q{idx}_{q_idx+1}",
"question": "{question}",
"answer": "{answer}"
}}''')
# 转义section标题
section_title = section['title'].replace('\\', '\\\\').replace('"', '\\"')
new_questions.append(f'''{{
"id": "group_q{idx+1}",
"question": "# {section_title}",
"subQuestions": [
{','.join(sub_questions)}
]
}}''')
# 替换内容
new_questions_str = ',\n '.join(new_questions)
mock_content = mock_content[:questions_start] + '\n ' + new_questions_str + '\n ' + mock_content[questions_end:]
print(f"✓ 已更新 {group_name} 的面试题")
# 额外修复电气设计最后一个空答案
mock_content = re.sub(
r'"在设计电气系统时,如何选择合适的电缆[?]",\s*"answer":\s*""',
'"在设计电气系统时,如何选择合适的电缆?",\n "answer": "选择电缆需要考虑1)载流量-根据负载电流选择合适截面积2)电压等级-确保绝缘等级满足系统电压3)环境条件-考虑温度、湿度、腐蚀性等4)敷设方式-架空、直埋、穿管等影响散热5)机械强度-满足拉伸、弯曲要求6)经济性-在满足技术要求前提下选择经济方案。"',
mock_content
)
# 保存更新后的文件
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
f.write(mock_content)
print("\n✅ 面试题更新完成!")