Files
ALL-teach_sys/frontend_土木水利/complete_questions_update.py

151 lines
5.9 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
完整更新所有岗位群的面试题不限制数量
"""
import json
import re
from datetime import datetime
def clean_question_text(question):
"""删除所有题型前缀"""
# 删除各种题型前缀
prefixes = ['选择题', '填空题', '判断题', '问答题', '情景题', '案例', '模拟', '简答题']
for prefix in prefixes:
question = re.sub(f'^{prefix}[:]', '', question)
return question.strip()
def fix_choice_answer(question, answer):
"""修复选择题答案"""
if answer and len(answer) == 1 and answer in 'ABCD':
# 根据问题内容推断可能的答案
if "不是" in question or "不属于" in question or "不合规" in question or "不允许" in question:
return "请参考题目选项中与要求不符的选项"
else:
return "请参考题目选项中的正确答案"
return answer
def main():
# 读取面试题数据
with open('interview_questions_data.json', 'r', encoding='utf-8') as f:
interview_data = json.load(f)
# 读取mock文件
with open('src/mocks/resumeInterviewMock.js', 'r', encoding='utf-8') as f:
lines = f.readlines()
# 备份
backup_name = f'src/mocks/resumeInterviewMock.js.backup_{datetime.now().strftime("%Y%m%d_%H%M%S")}_full'
with open(backup_name, 'w', encoding='utf-8') as f:
f.writelines(lines)
print(f"已创建备份: {backup_name}")
# 所有岗位群列表
all_job_groups = [
"BIM建筑信息模型",
"房地产经纪",
"工程采购",
"工程监理",
"工程造价与资料管理",
"建筑安装工程",
"建筑测量",
"建筑工程检测",
"建筑工程设计",
"建筑工程制图",
"建筑节能",
"建筑施工与施工管理",
"施工安全管理",
"室内设计",
"招投标管理"
]
# 逐个岗位群更新
for job_group in all_job_groups:
if job_group not in interview_data:
print(f"警告: 未找到 {job_group} 的面试题数据")
continue
questions = interview_data[job_group]
if not questions:
print(f"警告: {job_group} 没有面试题")
continue
# 使用所有题目(不限制数量)
selected_questions = []
for q in questions:
clean_q = {
"id": q["id"],
"question": clean_question_text(q["question"]),
"answer": fix_choice_answer(q["question"], q["answer"])
}
selected_questions.append(clean_q)
# 构建questions数组
questions_obj = [{
"id": f"group_{job_group}_q1",
"question": f"{job_group}面试题",
"subQuestions": selected_questions
}]
# 查找岗位群在文件中的位置
job_group_found = False
for i, line in enumerate(lines):
if f'"name": "{job_group}"' in line:
# 找到岗位群现在查找其questions字段
for j in range(i, min(i + 200, len(lines))): # 在接下来的200行内查找
if '"questions":' in lines[j]:
# 找到questions字段
# 判断是数组开始还是空数组
if '[]' in lines[j]:
# 空数组,直接替换
questions_json = json.dumps(questions_obj, ensure_ascii=False, indent=4)
indented_json = questions_json.replace('\n', '\n ')
lines[j] = ' "questions": ' + indented_json + '\n'
job_group_found = True
print(f"✓ 已更新: {job_group} ({len(selected_questions)} 道题)")
break
elif '[' in lines[j]:
# 非空数组,需要找到结束位置并替换
# 找到对应的 ]
bracket_count = 1
end_line = j + 1
while end_line < len(lines) and bracket_count > 0:
if '[' in lines[end_line]:
bracket_count += lines[end_line].count('[')
if ']' in lines[end_line]:
bracket_count -= lines[end_line].count(']')
if bracket_count == 0:
break
end_line += 1
# 替换从j到end_line的内容
questions_json = json.dumps(questions_obj, ensure_ascii=False, indent=4)
indented_json = questions_json.replace('\n', '\n ')
# 删除原有的questions内容
del lines[j:end_line+1]
# 插入新的questions
lines.insert(j, ' "questions": ' + indented_json + '\n')
job_group_found = True
print(f"✓ 已更新: {job_group} ({len(selected_questions)} 道题)")
break
if job_group_found:
break
if not job_group_found:
print(f"× 未找到: {job_group}")
# 额外清理:删除任何残留的题型前缀
content = ''.join(lines)
content = re.sub(r'"question":\s*"判断题:([^"]*)"', r'"question": "\1"', content)
# 写回文件
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
f.write(content)
print("\n更新完成!")
print("- 已删除所有题型前缀")
print("- 已将所有面试题内容放入对应岗位群(不限制数量)")
if __name__ == "__main__":
main()