110 lines
3.8 KiB
Python
110 lines
3.8 KiB
Python
|
|
#!/usr/bin/env python3
|
||
|
|
import json
|
||
|
|
import re
|
||
|
|
from datetime import datetime
|
||
|
|
|
||
|
|
print("正在更新所有面试题数据(最终版)...")
|
||
|
|
|
||
|
|
# 读取提取好的面试题数据
|
||
|
|
with open('all_interview_questions_complete.json', 'r', encoding='utf-8') as f:
|
||
|
|
job_group_questions = json.load(f)
|
||
|
|
|
||
|
|
# 统计信息
|
||
|
|
total_questions = sum(len(qs) for qs in job_group_questions.values())
|
||
|
|
print(f"准备更新 {len(job_group_questions)} 个岗位群,共 {total_questions} 道面试题")
|
||
|
|
|
||
|
|
# 读取现有mock文件
|
||
|
|
with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f:
|
||
|
|
content = f.read()
|
||
|
|
|
||
|
|
# 备份
|
||
|
|
timestamp = datetime.now().strftime('%Y%m%d_%H%M%S')
|
||
|
|
backup_file = f'src/mocks/resumeInterviewMock.js.backup_final_{timestamp}'
|
||
|
|
with open(backup_file, 'w', encoding='utf-8') as f:
|
||
|
|
f.write(content)
|
||
|
|
print(f"已创建备份: {backup_file}")
|
||
|
|
|
||
|
|
# 更新每个岗位群
|
||
|
|
updated_count = 0
|
||
|
|
for job_group, questions in job_group_questions.items():
|
||
|
|
if not questions:
|
||
|
|
continue
|
||
|
|
|
||
|
|
# 为每个问题创建正确的格式
|
||
|
|
formatted_questions = []
|
||
|
|
for i, q in enumerate(questions, 1):
|
||
|
|
# 清理答案中的特殊字符
|
||
|
|
answer = q['answer']
|
||
|
|
# 将多个连续换行符替换为单个
|
||
|
|
answer = re.sub(r'\n+', ' ', answer)
|
||
|
|
# 将列表符号后的换行替换为空格
|
||
|
|
answer = re.sub(r'[-•]\s*', '• ', answer)
|
||
|
|
# 清理多余空格
|
||
|
|
answer = re.sub(r'\s+', ' ', answer).strip()
|
||
|
|
|
||
|
|
formatted_questions.append({
|
||
|
|
"id": f"q{i}",
|
||
|
|
"question": q['question'].strip(),
|
||
|
|
"answer": answer
|
||
|
|
})
|
||
|
|
|
||
|
|
# 手动构建JSON字符串以确保格式正确
|
||
|
|
sub_questions_parts = []
|
||
|
|
for q in formatted_questions:
|
||
|
|
# 使用json.dumps确保字符串正确转义
|
||
|
|
q_str = json.dumps(q['question'], ensure_ascii=False)
|
||
|
|
a_str = json.dumps(q['answer'], ensure_ascii=False)
|
||
|
|
|
||
|
|
sub_questions_parts.append(f''' {{
|
||
|
|
"id": "{q['id']}",
|
||
|
|
"question": {q_str},
|
||
|
|
"answer": {a_str}
|
||
|
|
}}''')
|
||
|
|
|
||
|
|
sub_questions_str = ',\n'.join(sub_questions_parts)
|
||
|
|
sub_questions_array = f"[\n{sub_questions_str}\n ]"
|
||
|
|
|
||
|
|
# 替换对应岗位群的subQuestions
|
||
|
|
# 使用更精确的模式匹配
|
||
|
|
pattern = rf'("question"\s*:\s*"{re.escape(job_group)}岗位群面试题"\s*,\s*"subQuestions"\s*:\s*)\[[^\]]*?\]'
|
||
|
|
|
||
|
|
def replacement_func(match):
|
||
|
|
return match.group(1) + sub_questions_array
|
||
|
|
|
||
|
|
new_content = re.sub(pattern, replacement_func, content, flags=re.DOTALL)
|
||
|
|
|
||
|
|
if new_content != content:
|
||
|
|
content = new_content
|
||
|
|
updated_count += 1
|
||
|
|
print(f"✓ 已更新 {job_group}: {len(questions)} 道题")
|
||
|
|
|
||
|
|
# 保存更新后的文件
|
||
|
|
if updated_count > 0:
|
||
|
|
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
|
||
|
|
f.write(content)
|
||
|
|
print(f"\n✅ 成功更新了 {updated_count} 个岗位群")
|
||
|
|
|
||
|
|
# 验证语法
|
||
|
|
import subprocess
|
||
|
|
try:
|
||
|
|
result = subprocess.run(['node', '-c', 'src/mocks/resumeInterviewMock.js'],
|
||
|
|
capture_output=True, text=True, encoding='utf-8')
|
||
|
|
if result.returncode == 0:
|
||
|
|
print("✓ 语法检查通过")
|
||
|
|
print(f"\n🎉 所有面试题更新成功!共 {total_questions} 道题")
|
||
|
|
|
||
|
|
# 显示各岗位群题目数量
|
||
|
|
print("\n各岗位群题目数量:")
|
||
|
|
for group, qs in sorted(job_group_questions.items()):
|
||
|
|
print(f" {group}: {len(qs)} 道题")
|
||
|
|
|
||
|
|
else:
|
||
|
|
print(f"✗ 语法检查失败: {result.stderr}")
|
||
|
|
# 恢复备份
|
||
|
|
with open(backup_file, 'r', encoding='utf-8') as f:
|
||
|
|
backup_content = f.read()
|
||
|
|
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
|
||
|
|
f.write(backup_content)
|
||
|
|
print("已从备份恢复")
|
||
|
|
except Exception as e:
|
||
|
|
print(f"错误: {e}")
|