- Implemented enhanced PDF parsing with V2 parser for better question extraction - Added improved question formatting and display components - Integrated question navigation and progress tracking features - Added JSON export functionality for paper questions - Enhanced image extraction and association with questions - Improved database configuration with environment variables - Updated UI with responsive question cards and navigation sidebar - Added mathematical symbol rendering and question type detection Co-authored-by: Qwen-Coder <[email protected]>
219 lines
7.2 KiB
Python
219 lines
7.2 KiB
Python
#!/usr/bin/env python3
|
|
"""
|
|
学习助手 Pro - 功能演示脚本
|
|
展示完整的PDF解析和题目提取流程
|
|
"""
|
|
|
|
import os
|
|
import sys
|
|
import json
|
|
import time
|
|
from pathlib import Path
|
|
|
|
# 添加项目路径
|
|
sys.path.append('/Users/brunogao/work/codes/pingan/pager/backend')
|
|
|
|
def demo_pdf_parsing():
|
|
"""演示PDF解析功能"""
|
|
print("🎯 学习助手 Pro - PDF解析演示")
|
|
print("=" * 60)
|
|
|
|
# 检查测试文件
|
|
pdf_path = "/Users/brunogao/work/codes/pingan/pager/backend/uploads/1/7c6df72c-3386-4909-8e6e-0471930d1284_2025年9月2日初中数学作业.pdf"
|
|
|
|
if not os.path.exists(pdf_path):
|
|
print(f"❌ 测试PDF文件不存在: {pdf_path}")
|
|
return
|
|
|
|
print(f"📄 PDF文件: {os.path.basename(pdf_path)}")
|
|
print(f"📏 文件大小: {os.path.getsize(pdf_path) / 1024:.1f} KB")
|
|
|
|
try:
|
|
# 导入V2解析器
|
|
from pdf_exam_parser_enhanced_v2 import PDFExamParserEnhancedV2
|
|
|
|
# 创建解析器实例
|
|
parser = PDFExamParserEnhancedV2()
|
|
parser.debug = True # 开启调试模式
|
|
|
|
print("\n🔍 开始PDF解析...")
|
|
start_time = time.time()
|
|
|
|
# 执行解析
|
|
result = parser.process_pdf(pdf_path)
|
|
|
|
end_time = time.time()
|
|
processing_time = end_time - start_time
|
|
|
|
if result.get("success"):
|
|
questions = result.get("questions", [])
|
|
stats = result.get("stats", {})
|
|
|
|
print(f"\n✅ 解析成功!")
|
|
print(f"📊 处理时间: {processing_time:.3f}秒")
|
|
print(f"📝 识别题目: {len(questions)}道")
|
|
print(f"🔧 处理方法: {stats.get('method', 'unknown')}")
|
|
print(f"📄 原始行数: {stats.get('original_lines', 0)}")
|
|
|
|
# 显示前3道题目
|
|
print(f"\n📋 前3道题目预览:")
|
|
for i, question in enumerate(questions[:3]):
|
|
print(f"\n--- 题目{question.get('number', i+1)} ---")
|
|
print(f"类型: {question.get('type', 'unknown')}")
|
|
print(f"页码: {question.get('page', 1)}")
|
|
print(f"题干: {question.get('text', '')[:100]}...")
|
|
|
|
if question.get('options'):
|
|
print("选项:")
|
|
for key, value in question.get('options', {}).items():
|
|
print(f" {key}. {value}")
|
|
|
|
return questions
|
|
|
|
else:
|
|
error = result.get("error", "未知错误")
|
|
print(f"\n❌ 解析失败: {error}")
|
|
return []
|
|
|
|
except Exception as e:
|
|
print(f"\n❌ 解析异常: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return []
|
|
|
|
def demo_json_formatting(questions):
|
|
"""演示JSON格式化功能"""
|
|
if not questions:
|
|
print("\n⚠️ 没有题目数据,跳过JSON格式化演示")
|
|
return
|
|
|
|
print(f"\n🏗️ JSON格式化演示")
|
|
print("=" * 60)
|
|
|
|
try:
|
|
# 导入格式化器
|
|
from utils.question_formatter import QuestionFormatter
|
|
|
|
# 创建格式化器
|
|
formatter = QuestionFormatter()
|
|
|
|
# 构建试卷数据
|
|
paper_data = {
|
|
"paper_id": 1,
|
|
"title": "2025年9月2日初中数学作业",
|
|
"subject": "数学",
|
|
"grade": "grade8",
|
|
"questions": questions
|
|
}
|
|
|
|
print("📝 开始格式化...")
|
|
|
|
# 格式化数据
|
|
formatted_data = formatter.format_paper_questions(paper_data)
|
|
|
|
print(f"✅ 格式化完成!")
|
|
print(f"📊 格式化题目: {len(formatted_data.get('questions', []))}道")
|
|
|
|
# 显示统计信息
|
|
stats = formatted_data.get('statistics', {})
|
|
print(f"\n📈 试卷统计:")
|
|
print(f" 题目总数: {stats.get('total_questions', 0)}")
|
|
print(f" 预计用时: {stats.get('estimated_time_formatted', '未知')}")
|
|
print(f" 总分值: {stats.get('total_score', 0)}")
|
|
|
|
# 题目类型分布
|
|
types = stats.get('question_types', {})
|
|
if types:
|
|
print(f" 题型分布: {types}")
|
|
|
|
# 难度分布
|
|
difficulty = stats.get('difficulty_distribution', {})
|
|
if difficulty:
|
|
print(f" 难度分布: {difficulty}")
|
|
|
|
# 知识点
|
|
knowledge_points = stats.get('knowledge_points', [])
|
|
if knowledge_points:
|
|
print(f" 知识点: {', '.join(knowledge_points[:5])}")
|
|
if len(knowledge_points) > 5:
|
|
print(f" 等{len(knowledge_points)}个知识点")
|
|
|
|
# 导出JSON文件
|
|
output_file = "demo_output.json"
|
|
if formatter.export_to_json_file(formatted_data, output_file):
|
|
file_size = os.path.getsize(output_file)
|
|
print(f"\n💾 导出成功: {output_file}")
|
|
print(f"📏 文件大小: {file_size} 字节")
|
|
|
|
# 显示JSON结构预览
|
|
with open(output_file, 'r', encoding='utf-8') as f:
|
|
data = json.load(f)
|
|
print(f"\n📄 JSON结构预览:")
|
|
print(f" paper_info: {list(data.get('paper_info', {}).keys())}")
|
|
print(f" questions: {len(data.get('questions', []))}项")
|
|
print(f" statistics: {list(data.get('statistics', {}).keys())}")
|
|
print(f" export_info: {list(data.get('export_info', {}).keys())}")
|
|
|
|
return output_file
|
|
else:
|
|
print("\n❌ JSON导出失败")
|
|
return None
|
|
|
|
except Exception as e:
|
|
print(f"\n❌ 格式化异常: {e}")
|
|
import traceback
|
|
traceback.print_exc()
|
|
return None
|
|
|
|
def demo_api_features():
|
|
"""演示API功能"""
|
|
print(f"\n🌐 API功能演示")
|
|
print("=" * 60)
|
|
|
|
api_endpoints = [
|
|
"GET /api/papers/{paper_id}/export/json - 导出试卷JSON数据",
|
|
"GET /api/questions/{question_id}/format - 获取格式化题目",
|
|
"POST /api/questions/batch/export - 批量导出题目",
|
|
"POST /api/papers/{paper_id}/split - 拆分试卷题目",
|
|
"GET /api/docs - API文档"
|
|
]
|
|
|
|
print("📋 可用API端点:")
|
|
for endpoint in api_endpoints:
|
|
print(f" {endpoint}")
|
|
|
|
print(f"\n💡 使用示例:")
|
|
print(f" curl -X GET 'http://localhost:8888/api/papers/1/export/json'")
|
|
print(f" curl -X GET 'http://localhost:8888/api/questions/1/format'")
|
|
|
|
def main():
|
|
"""主演示函数"""
|
|
print("🎉 欢迎使用学习助手 Pro!")
|
|
print("这是一个完整的PDF试卷解析和题目提取系统")
|
|
|
|
# 演示PDF解析
|
|
questions = demo_pdf_parsing()
|
|
|
|
# 演示JSON格式化
|
|
json_file = demo_json_formatting(questions)
|
|
|
|
# 演示API功能
|
|
demo_api_features()
|
|
|
|
print(f"\n🎊 演示完成!")
|
|
print("=" * 60)
|
|
print("✅ 主要功能:")
|
|
print(" 🔧 增强版PDF解析 - 解决题号丢失问题")
|
|
print(" 🎨 现代化UI设计 - 学而思风格界面")
|
|
print(" 📊 标准化JSON输出 - 完整的数据格式")
|
|
print(" 🌐 RESTful API - 完整的后端接口")
|
|
print(" 📱 响应式设计 - 移动端适配")
|
|
|
|
if json_file and os.path.exists(json_file):
|
|
print(f"\n📁 生成文件: {json_file}")
|
|
print(" 您可以查看完整的JSON数据结构")
|
|
|
|
print(f"\n🚀 系统已就绪,可以开始使用!")
|
|
|
|
if __name__ == "__main__":
|
|
main() |