- 包含4个产业方向的前端项目:智能开发、智能制造、大健康、财经商贸 - 已清理node_modules、.yoyo等大文件,项目大小从2.6GB优化至631MB - 配置完善的.gitignore文件 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
189 lines
6.5 KiB
Python
189 lines
6.5 KiB
Python
#!/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_merge_{backup_time}', 'w', encoding='utf-8') as f:
|
||
f.write(content)
|
||
print(f"已创建备份文件: src/mocks/resumeInterviewMock.js.backup_merge_{backup_time}")
|
||
|
||
def merge_questions_in_industry(industry_data):
|
||
"""将一个岗位群的多个questions组合并成一个"""
|
||
|
||
# 找到questions数组的起始和结束位置
|
||
questions_start = industry_data.find('"questions": [')
|
||
if questions_start == -1:
|
||
return industry_data
|
||
|
||
questions_start += len('"questions": [')
|
||
|
||
# 找到questions数组的结束
|
||
bracket_count = 1
|
||
i = questions_start
|
||
in_string = False
|
||
escape_next = False
|
||
|
||
while i < len(industry_data) and bracket_count > 0:
|
||
char = industry_data[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:
|
||
return industry_data
|
||
|
||
questions_end = i - 1
|
||
questions_content = industry_data[questions_start:questions_end]
|
||
|
||
# 解析questions数组内容
|
||
# 提取所有subQuestions
|
||
all_sub_questions = []
|
||
question_counter = 1
|
||
|
||
# 查找所有的subQuestions
|
||
pattern = r'"subQuestions":\s*\[(.*?)\](?:\s*}\s*]|\s*}\s*,)'
|
||
matches = re.finditer(pattern, questions_content, re.DOTALL)
|
||
|
||
for match in matches:
|
||
sub_questions_str = match.group(1)
|
||
|
||
# 解析每个子问题
|
||
sub_pattern = r'"question":\s*"([^"\\]*(?:\\.[^"\\]*)*)",\s*"answer":\s*"([^"\\]*(?:\\.[^"\\]*)*)"'
|
||
sub_matches = re.finditer(sub_pattern, sub_questions_str)
|
||
|
||
for sub_match in sub_matches:
|
||
question_text = sub_match.group(1)
|
||
answer_text = sub_match.group(2)
|
||
|
||
# 处理答案格式
|
||
# 1. 如果是选择题答案(如 "A" 或 "B" 等单个字母),保持原样
|
||
# 2. 其他答案需要处理换行
|
||
if len(answer_text.strip()) == 1 and answer_text.strip() in 'ABCDEFGH':
|
||
# 选择题答案,保持原样
|
||
formatted_answer = answer_text
|
||
else:
|
||
# 处理换行符,确保正确显示
|
||
formatted_answer = answer_text.replace('\\n', '<br/>')
|
||
# 如果答案包含选项(A. B. C. 等),只保留文字说明
|
||
if re.search(r'\s*[A-H]\.\s*', formatted_answer):
|
||
# 移除选项标识,保留内容
|
||
formatted_answer = re.sub(r'\s*[A-H]\.\s*', '', formatted_answer)
|
||
|
||
all_sub_questions.append({
|
||
'id': f'q_{question_counter}',
|
||
'question': question_text,
|
||
'answer': formatted_answer
|
||
})
|
||
question_counter += 1
|
||
|
||
# 构建新的questions数组(只包含一个大的question对象)
|
||
merged_question = {
|
||
'id': 'merged_questions',
|
||
'question': '综合面试题集',
|
||
'subQuestions': all_sub_questions
|
||
}
|
||
|
||
# 生成新的questions数组字符串
|
||
new_questions_str = '\n {\n'
|
||
new_questions_str += ' "id": "merged_questions",\n'
|
||
new_questions_str += ' "question": "综合面试题集",\n'
|
||
new_questions_str += ' "subQuestions": [\n'
|
||
|
||
for idx, q in enumerate(all_sub_questions):
|
||
question_escaped = q['question'].replace('"', '\\"')
|
||
answer_escaped = q['answer'].replace('"', '\\"')
|
||
|
||
new_questions_str += ' {\n'
|
||
new_questions_str += f' "id": "{q["id"]}",\n'
|
||
new_questions_str += f' "question": "{question_escaped}",\n'
|
||
new_questions_str += f' "answer": "{answer_escaped}"\n'
|
||
new_questions_str += ' }'
|
||
|
||
if idx < len(all_sub_questions) - 1:
|
||
new_questions_str += ','
|
||
new_questions_str += '\n'
|
||
|
||
new_questions_str += ' ]\n'
|
||
new_questions_str += ' }\n '
|
||
|
||
# 替换原来的questions数组内容
|
||
before_questions = industry_data[:questions_start]
|
||
after_questions = industry_data[questions_end:]
|
||
|
||
return before_questions + new_questions_str + after_questions
|
||
|
||
# 处理每个岗位群
|
||
industries_pattern = r'(\{\s*"id":\s*"manufacturing_\d+".*?"questions":\s*\[.*?\]\s*\})'
|
||
|
||
def replace_industry(match):
|
||
industry_data = match.group(1)
|
||
return merge_questions_in_industry(industry_data)
|
||
|
||
# 分段处理以避免递归深度问题
|
||
lines = content.split('\n')
|
||
in_industry = False
|
||
industry_lines = []
|
||
result_lines = []
|
||
industry_start = -1
|
||
|
||
for i, line in enumerate(lines):
|
||
if '"id": "manufacturing_' in line and not in_industry:
|
||
in_industry = True
|
||
industry_start = i
|
||
industry_lines = [line]
|
||
elif in_industry:
|
||
industry_lines.append(line)
|
||
# 检查是否是industry的结束
|
||
if ' },' in line or ' }' in line:
|
||
# 如果下一行是positions或questions,说明还没结束
|
||
if i + 1 < len(lines):
|
||
next_line = lines[i + 1]
|
||
if '"positions":' in next_line or '"questions":' in next_line:
|
||
continue
|
||
|
||
# 处理这个industry
|
||
industry_text = '\n'.join(industry_lines)
|
||
processed_industry = merge_questions_in_industry(industry_text)
|
||
|
||
# 将处理后的内容添加到结果
|
||
result_lines.extend(processed_industry.split('\n'))
|
||
|
||
in_industry = False
|
||
industry_lines = []
|
||
else:
|
||
result_lines.append(line)
|
||
|
||
# 如果最后还有未处理的industry
|
||
if industry_lines:
|
||
industry_text = '\n'.join(industry_lines)
|
||
processed_industry = merge_questions_in_industry(industry_text)
|
||
result_lines.extend(processed_industry.split('\n'))
|
||
|
||
# 合并结果
|
||
final_content = '\n'.join(result_lines)
|
||
|
||
# 保存处理后的文件
|
||
with open('src/mocks/resumeInterviewMock.js', 'w', encoding='utf-8') as f:
|
||
f.write(final_content)
|
||
|
||
print("✅ 面试题合并完成!")
|
||
print("- 所有题目已合并到同一张卡片")
|
||
print("- 答案换行已转换为<br/>标签")
|
||
print("- 选择题答案已处理为只显示正确答案") |