Files
ALL-teach_sys/frontend_视觉设计/precise_answer_update.py

164 lines
6.3 KiB
Python
Raw Normal View History

#!/usr/bin/env python3
import json
import os
from datetime import datetime
# 读取视觉设计岗位简历.json文件
json_file = '网页未导入数据/视觉设计产业/视觉设计岗位简历.json'
mock_file = 'src/mocks/resumeInterviewMock.js'
print("正在读取所有岗位的面试题和答案...")
with open(json_file, 'r', encoding='utf-8') as f:
positions_data = json.load(f)
# 提取UI设计师的面试题和答案
ui_qa_list = []
for position in positions_data:
if position.get("岗位名称") == "UI设计师":
interview_content = position.get("面试题内容", "")
lines = interview_content.split('\n')
current_question = None
current_answer = ''
collecting_answer = False
for line in lines:
line_stripped = line.strip()
# 检测问题行
if line_stripped and line_stripped[0].isdigit() and '. 问题' in line_stripped:
# 保存上一个问题
if current_question:
# 清理答案格式
answer = current_answer.strip()
# 限制长度并保持可读性
if len(answer) > 400:
# 截取到句号或分号位置
cut_point = 400
for sep in ['', '', '. ']:
pos = answer[:400].rfind(sep)
if pos > 200:
cut_point = pos + 1
break
answer = answer[:cut_point] + "..."
ui_qa_list.append({
'question': current_question,
'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。"
})
# 提取新问题
parts = line_stripped.split('问题:', 1)
if len(parts) > 1:
current_question = parts[1].strip()
else:
parts = line_stripped.split('问题:', 1)
if len(parts) > 1:
current_question = parts[1].strip()
else:
current_question = line_stripped
current_answer = ''
collecting_answer = False
# 检测答案开始
elif any(marker in line for marker in ['参考回答:', '参考答案:', '答案:']):
collecting_answer = True
# 提取答案内容
for marker in ['参考回答:', '参考答案:', '答案:']:
if marker in line:
answer_part = line.split(marker, 1)[1].strip()
if answer_part:
current_answer = answer_part
break
# 收集答案内容
elif collecting_answer and line.strip():
# 检查是否是新问题或新章节
if (line_stripped and line_stripped[0].isdigit() and '. 问题' in line_stripped) or line_stripped.startswith('#'):
collecting_answer = False
else:
if current_answer:
current_answer += ' '
current_answer += line.strip()
# 保存最后一个问题
if current_question:
answer = current_answer.strip()
if len(answer) > 400:
cut_point = 400
for sep in ['', '', '. ']:
pos = answer[:400].rfind(sep)
if pos > 200:
cut_point = pos + 1
break
answer = answer[:cut_point] + "..."
ui_qa_list.append({
'question': current_question,
'answer': answer if answer else "请根据您的实际经验和项目情况进行回答。"
})
break
print(f"找到UI设计师的 {len(ui_qa_list)} 个面试题")
# 创建备份
backup_file = f"{mock_file}.backup_precise_{datetime.now().strftime('%Y%m%d_%H%M%S')}"
with open(mock_file, 'r', encoding='utf-8') as f:
original_content = f.read()
with open(backup_file, 'w', encoding='utf-8') as f:
f.write(original_content)
print(f"已创建备份:{backup_file}")
# 逐个更新答案
print("\n开始更新UI设计面试题答案...")
content = original_content
updated_count = 0
for i, qa in enumerate(ui_qa_list, 1):
question_escaped = qa['question'].replace('?', r'\?').replace('(', r'\(').replace(')', r'\)')
# 构建查找模式
search_pattern = f'"question": "{qa["question"]}",'
if search_pattern in content:
# 找到问题位置
q_pos = content.find(search_pattern)
if q_pos > 0:
# 找到对应的answer行
# 从问题位置开始,找到下一个"answer":
answer_start = content.find('"answer":', q_pos)
if answer_start > 0 and answer_start < q_pos + 500: # 确保在同一个问题范围内
# 找到答案的引号位置
quote_start = content.find('"', answer_start + 9)
quote_end = content.find('"', quote_start + 1)
if quote_start > 0 and quote_end > 0:
# 替换答案内容
old_answer = content[quote_start+1:quote_end]
new_answer = qa['answer'].replace('"', '\\"').replace('\n', ' ')
content = content[:quote_start+1] + new_answer + content[quote_end:]
updated_count += 1
print(f"✓ 更新问题 {i}: {qa['question'][:50]}...")
# 写入文件
if updated_count > 0:
with open(mock_file, 'w', encoding='utf-8') as f:
f.write(content)
# 验证语法
print(f"\n验证语法...")
result = os.popen(f'node -c {mock_file} 2>&1').read()
if result:
print(f"❌ 语法错误:{result}")
# 恢复备份
with open(backup_file, 'r', encoding='utf-8') as f:
content = f.read()
with open(mock_file, 'w', encoding='utf-8') as f:
f.write(content)
print("已恢复备份")
else:
print("✓ 语法验证通过")
print(f"\n成功更新 {updated_count} 个UI设计面试题答案")
else:
print("未能更新任何答案")