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

119 lines
3.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import json
from datetime import datetime
# 读取mock文件
with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f:
content = f.read()
# 备份原文件
backup_time = 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(content)
print(f"已创建备份文件: src/mocks/resumeInterviewMock.js.backup_{backup_time}")
def process_questions_array(questions_str):
"""处理questions数组将多个section合并成一个"""
# 提取所有的subQuestions
all_questions = []
# 找到所有的subQuestions数组
pattern = r'"subQuestions":\s*\[(.*?)\]'
matches = re.finditer(pattern, questions_str, re.DOTALL)
question_counter = 1
for match in matches:
sub_questions_content = match.group(1)
# 提取每个问题和答案
q_pattern = r'"question":\s*"([^"\\]*(?:\\.[^"\\]*)*)"[,\s]*"answer":\s*"([^"\\]*(?:\\.[^"\\]*)*)"'
q_matches = re.finditer(q_pattern, sub_questions_content)
for q_match in q_matches:
question = q_match.group(1)
answer = q_match.group(2)
# 处理答案格式
# 1. 单个字母答案(选择题)保持原样
# 2. 其他答案处理换行为<br/>
if len(answer.strip()) == 1 and answer.strip() in 'ABCDEFGH':
formatted_answer = answer
else:
# 替换换行符
formatted_answer = answer.replace('\\n', '<br/>')
# 如果有选项,只保留正确答案内容
if 'A.' in formatted_answer or 'B.' in formatted_answer:
# 移除选项标记
formatted_answer = re.sub(r'[A-H]\.\s*', '', formatted_answer)
all_questions.append({
'id': f'q_{question_counter}',
'question': question,
'answer': formatted_answer
})
question_counter += 1
# 构建新的questions数组字符串
result = '[\n {\n'
result += ' "id": "all_questions",\n'
result += ' "question": "面试题集",\n'
result += ' "subQuestions": [\n'
for i, q in enumerate(all_questions):
# 转义引号
question_escaped = q['question'].replace('"', '\\"')
answer_escaped = q['answer'].replace('"', '\\"')
result += ' {\n'
result += f' "id": "{q["id"]}",\n'
result += f' "question": "{question_escaped}",\n'
result += f' "answer": "{answer_escaped}"\n'
result += ' }'
if i < len(all_questions) - 1:
result += ','
result += '\n'
result += ' ]\n'
result += ' }\n ]'
return result
# 处理每个岗位群的questions数组
def process_content(content):
# 找到所有的questions数组
pattern = r'("questions":\s*\[)(.*?)(\]\s*\})'
def replacer(match):
prefix = match.group(1)
questions_content = match.group(2)
suffix = match.group(3)
# 如果已经是合并后的格式,跳过
if '"id": "all_questions"' in questions_content:
return match.group(0)
# 处理questions内容
new_questions = process_questions_array(questions_content)
return prefix + new_questions[1:-1] + suffix
# 替换所有的questions数组
result = re.sub(pattern, replacer, content, flags=re.DOTALL)
return result
# 处理文件
processed_content = process_content(content)
# 保存处理后的文件
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
f.write(processed_content)
print("✅ 面试题结构修改完成!")
print("- 所有题目已合并到同一张卡片")
print("- 答案换行已转换为<br/>标签")
print("- 选择题答案已处理为只显示正确答案")