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

238 lines
8.3 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)
# 创建面试题映射字典
interview_questions_map = {}
for item in smart_mfg_data:
job_group = item.get('简历岗位群', '')
interview_content = item.get('面试题内容', '')
if job_group and interview_content:
# 存储完整的面试题内容
interview_questions_map[job_group] = interview_content
# 打印找到的岗位群
print("找到的岗位群面试题:")
for key in interview_questions_map.keys():
print(f"- {key}")
# 读取当前的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_{backup_time}', 'w', encoding='utf-8') as f:
f.write(mock_content)
print(f"\n已创建备份文件: src/mocks/resumeInterviewMock.js.backup_{backup_time}")
# 需要更新的岗位群映射
job_group_mapping = {
'PLC': 'PLC类岗位面试题',
'工业机器人': '工业机器人类岗位面试题',
'焊接工艺': '焊接工艺类岗位面试题',
'电气设计': '电气设计类岗位面试题',
'机器视觉': '机器视觉类岗位面试题',
'非标自动化设计': '非标自动化类岗位面试题'
}
# 解析面试题内容为结构化数据
def parse_interview_content(content):
"""解析面试题内容为结构化数据"""
questions = []
lines = content.split('\n')
current_section = None
current_question = None
current_answer = []
for line in lines:
line = line.strip()
if not line:
continue
# 检查是否是大标题(如 # 一、xxx
if line.startswith('# '):
# 保存之前的问题
if current_question and current_section:
answer_text = '\\n'.join(current_answer).strip()
if not answer_text.startswith('示例答案') and not answer_text.startswith('答案'):
answer_text = answer_text
else:
# 去掉"示例答案:"或"答案:"前缀
answer_text = re.sub(r'^(示例答案[:]|答案[:])\s*', '', answer_text)
current_section['subQuestions'].append({
'question': current_question,
'answer': answer_text
})
# 创建新section
if current_section:
questions.append(current_section)
section_title = line.replace('#', '').strip()
current_section = {
'title': section_title,
'subQuestions': []
}
current_question = None
current_answer = []
# 检查是否是问题编号(如 1. xxx
elif re.match(r'^\d+\.\s+', line):
# 保存之前的问题
if current_question and current_section:
answer_text = '\\n'.join(current_answer).strip()
if answer_text.startswith('示例答案') or answer_text.startswith('答案'):
answer_text = re.sub(r'^(示例答案[:]|答案[:])\s*', '', answer_text)
current_section['subQuestions'].append({
'question': current_question,
'answer': answer_text
})
# 提取新问题
current_question = re.sub(r'^\d+\.\s+', '', line)
current_answer = []
# 否则是答案内容
else:
if current_question:
current_answer.append(line)
# 保存最后一个问题
if current_question and current_section:
answer_text = '\\n'.join(current_answer).strip()
if answer_text.startswith('示例答案') or answer_text.startswith('答案'):
answer_text = re.sub(r'^(示例答案[:]|答案[:])\s*', '', answer_text)
current_section['subQuestions'].append({
'question': current_question,
'answer': answer_text
})
# 保存最后一个section
if current_section:
questions.append(current_section)
return questions
# 更新mock文件中的面试题
def update_mock_questions(mock_content, job_group_name, interview_content):
"""更新指定岗位群的面试题"""
# 解析面试题内容
parsed_questions = parse_interview_content(interview_content)
if not parsed_questions:
print(f" 警告: 无法解析 {job_group_name} 的面试题内容")
return mock_content
# 查找岗位群的位置
pattern = rf'"name":\s*"{re.escape(job_group_name)}"'
matches = list(re.finditer(pattern, mock_content))
if not matches:
print(f" 未找到岗位群: {job_group_name}")
return mock_content
for match in matches:
# 找到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(parsed_questions):
sub_questions = []
for q_idx, q in enumerate(section['subQuestions']):
answer = q['answer'].replace('"', '\\"').replace('\n', '\\n')
sub_questions.append(f'''{{
"id": "q{idx}_{q_idx+1}",
"question": "{q['question']}",
"answer": "{answer}"
}}''')
new_questions.append(f'''{{
"id": "group_q{idx+1}",
"question": "{section['title']}",
"subQuestions": [
{','.join(sub_questions)}
]
}}''')
# 替换原有的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" ✓ 已更新 {job_group_name} 的面试题")
break
return mock_content
# 执行更新
print("\n开始更新面试题:")
updated_content = mock_content
for mock_group_name, json_interview_name in job_group_mapping.items():
print(f"\n处理 {mock_group_name}:")
# 查找对应的面试题内容
found = False
for item in smart_mfg_data:
if item.get('面试题', '') == json_interview_name:
interview_content = item.get('面试题内容', '')
if interview_content:
updated_content = update_mock_questions(updated_content, mock_group_name, interview_content)
found = True
break
if not found:
print(f" 未找到 {json_interview_name} 的面试题数据")
# 保存更新后的文件
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
f.write(updated_content)
print("\n✅ 面试题更新完成!")
print("已更新的岗位群:")
for group in job_group_mapping.keys():
print(f" - {group}")