feat: 过滤综合分析题主观题,重新生成客观题库共756题
This commit is contained in:
+25
-11
@@ -1,14 +1,21 @@
|
||||
import json
|
||||
import os
|
||||
import pdfplumber
|
||||
import re
|
||||
|
||||
def extract_from_pdf(pdf_path, output_json_path):
|
||||
def extract_from_pdf(pdf_path, output_json_paths):
|
||||
questions = []
|
||||
current_q = None
|
||||
global_id = 1
|
||||
|
||||
with pdfplumber.open(pdf_path) as pdf:
|
||||
for page_num, page in enumerate(pdf.pages):
|
||||
text = page.extract_text() or ""
|
||||
# 当遇到综合分析题或页码超过142页时停止抽取
|
||||
if "大数据工程技术人员技能竞赛综合分析题" in text or page_num >= 142:
|
||||
print(f"在第 {page_num + 1} 页检测到综合分析题或达到客观题边界,停止解析。")
|
||||
break
|
||||
|
||||
tables = page.extract_tables()
|
||||
for table in tables:
|
||||
for row in table:
|
||||
@@ -22,7 +29,6 @@ def extract_from_pdf(pdf_path, output_json_path):
|
||||
is_new_q = False
|
||||
if col0.isdigit():
|
||||
is_new_q = True
|
||||
# q_id = int(col0)
|
||||
|
||||
title = row[1] if row[1] else ""
|
||||
options_raw = row[2] if row[2] else ""
|
||||
@@ -30,7 +36,7 @@ def extract_from_pdf(pdf_path, output_json_path):
|
||||
q_answer = row[4] if row[4] else ""
|
||||
source_file = row[5] if len(row) > 5 and row[5] else ""
|
||||
|
||||
# Split options
|
||||
# 拆分选项
|
||||
opts = re.split(r'(?=[A-H][\.、])', options_raw)
|
||||
parsed_options = [opt.strip().replace('\n', '') for opt in opts if opt.strip()]
|
||||
|
||||
@@ -44,7 +50,7 @@ def extract_from_pdf(pdf_path, output_json_path):
|
||||
"options": parsed_options,
|
||||
"type": q_type.replace('\n', '').strip(),
|
||||
"answer": q_answer.replace('\n', '').strip(),
|
||||
"source_id": col0, # keeping original id for reference
|
||||
"source_id": col0,
|
||||
"page_number": page_num + 1,
|
||||
"source_file": source_file.replace('\n', '').strip()
|
||||
}
|
||||
@@ -64,13 +70,21 @@ def extract_from_pdf(pdf_path, output_json_path):
|
||||
current_q["id"] = global_id
|
||||
questions.append(current_q)
|
||||
|
||||
print(f"Extracted {len(questions)} questions via table parsing.")
|
||||
print(f"共提取题目 {len(questions)} 道。")
|
||||
single_count = sum(1 for q in questions if q['type'] == '单选')
|
||||
multi_count = sum(1 for q in questions if q['type'] == '多选')
|
||||
print(f"其中单选 {single_count} 道,多选 {multi_count} 道。")
|
||||
|
||||
bad = [x for x in questions if len(x['options']) < 2]
|
||||
print(f"Questions with <2 options: {len(bad)}")
|
||||
|
||||
with open(output_json_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(questions, f, ensure_ascii=False, indent=2)
|
||||
for out_path in output_json_paths:
|
||||
os.makedirs(os.path.dirname(os.path.abspath(out_path)), exist_ok=True)
|
||||
with open(out_path, 'w', encoding='utf-8') as f:
|
||||
json.dump(questions, f, ensure_ascii=False, indent=2)
|
||||
print(f"已保存题库至:{out_path}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
extract_from_pdf("d:/code/bigdata/题库.pdf", "d:/code/bigdata/questions.json")
|
||||
targets = [
|
||||
"d:/code/bigdata/questions.json",
|
||||
"d:/code/bigdata/quiz-app/src/questions.json"
|
||||
]
|
||||
extract_from_pdf("d:/code/bigdata/题库.pdf", targets)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user