convert2one.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #将高考题md文档分割成单独的题目
  2. import re
  3. import os
  4. def parse_document(content):
  5. """
  6. 解析文档内容,提取所有题目和对应的答案详解
  7. """
  8. # 分割题目部分和答案部分
  9. parts = content.split("**《2024年新课标全国Ⅰ卷数学真题》参考答案**")
  10. if len(parts) < 2:
  11. return []
  12. questions_part = parts[0]
  13. answers_part = parts[1]
  14. # 提取所有题目
  15. questions = extract_questions(questions_part)
  16. # 提取所有答案
  17. answers = extract_answers(answers_part)
  18. # 合并题目和答案
  19. result = []
  20. for i, (q_num, q_content) in enumerate(questions):
  21. if i < len(answers):
  22. result.append((q_num, q_content, answers[i][1]))
  23. return result
  24. def extract_questions(content):
  25. """
  26. 从题目部分提取所有题目
  27. """
  28. questions = []
  29. # 匹配题目的正则表达式(匹配 "数字." 格式)
  30. pattern = r'(\d+).(.*?)(?=\d+.|$)'
  31. matches = re.finditer(pattern, content, re.DOTALL)
  32. for match in matches:
  33. question_num = match.group(1)
  34. question_content = match.group(2).strip()
  35. questions.append((question_num, question_content))
  36. return questions
  37. def extract_answers(content):
  38. """
  39. 从答案部分提取所有答案详解
  40. """
  41. answers = []
  42. # 匹配答案的正则表达式
  43. pattern = r'(\d+).(.*?)(?=\d+.|$)'
  44. matches = re.finditer(pattern, content, re.DOTALL)
  45. for match in matches:
  46. answer_num = match.group(1)
  47. answer_content = match.group(2).strip()
  48. # 清理答案内容,移除开头的选项字母
  49. answer_content = re.sub(r'^[A-D]', '', answer_content).strip()
  50. answers.append((answer_num, answer_content))
  51. return answers
  52. def get_question_type(question_num):
  53. """
  54. 根据题号确定题目类型
  55. """
  56. num = int(question_num)
  57. if 1 <= num <= 8:
  58. return "单选"
  59. elif 9 <= num <= 11:
  60. return "多选"
  61. elif 12 <= num <= 14:
  62. return "填空"
  63. else:
  64. return "解答"
  65. def format_question_md(question_num, question_content, answer_content):
  66. """
  67. 格式化单个题目为Markdown格式,答案用callouts隐藏
  68. """
  69. question_type = get_question_type(question_num)
  70. # 添加元数据
  71. metadata = f"""---
  72. Tags:
  73. - 题库
  74. 年份: 2024
  75. 源卷: 全国1
  76. 原题号: {question_num}
  77. 题目类型: {question_type}
  78. 知识点:
  79. -
  80. 难度:
  81. ---
  82. """
  83. # 处理答案内容,每一行都加上 > 前缀
  84. formatted_answer = ""
  85. for line in answer_content.split('\n'):
  86. if line.strip(): # 非空行
  87. formatted_answer += f"> {line}\n"
  88. else: # 空行
  89. formatted_answer += ">\n"
  90. md_content = f"""{metadata}# 第{question_num}题
  91. ## 题目
  92. {question_content}
  93. ## 答案
  94. > [!question]- 点击查看答案
  95. {formatted_answer}
  96. """
  97. return md_content
  98. def save_questions(questions_data, output_dir="questions"):
  99. """
  100. 保存所有题目为单独的Markdown文件
  101. """
  102. if not os.path.exists(output_dir):
  103. os.makedirs(output_dir)
  104. for question_num, question_content, answer_content in questions_data:
  105. filename = f"{question_num}.md"
  106. filepath = os.path.join(output_dir, filename)
  107. md_content = format_question_md(question_num, question_content, answer_content)
  108. with open(filepath, 'w', encoding='utf-8') as f:
  109. f.write(md_content)
  110. print(f"已保存: {filename}")
  111. def main():
  112. # 读取文档内容(这里使用文档2的内容)
  113. with open('document2.txt', 'r', encoding='utf-8') as f:
  114. content = f.read()
  115. # 解析文档
  116. questions_data = parse_document(content)
  117. if not questions_data:
  118. print("未能解析出题目数据")
  119. return
  120. print(f"成功解析 {len(questions_data)} 道题目")
  121. # 保存为单独的Markdown文件
  122. save_questions(questions_data)
  123. if __name__ == "__main__":
  124. main()