feat: implement Office document direct extraction (Section 2.4)
- Update DocumentTypeDetector._analyze_office to convert Office to PDF first - Analyze converted PDF for text extractability before routing - Route text-based Office documents to direct track (10x faster) - Update OCR service to convert Office files for DirectExtractionEngine - Add unit tests for Office → PDF → Direct extraction flow - Handle conversion failures with fallback to OCR track This optimization reduces Office document processing from >300s to ~2-5s for text-based documents by avoiding unnecessary OCR processing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -1025,12 +1025,46 @@ class OCRService:
|
||||
logger.info(f"Reason: {recommendation.reason}")
|
||||
|
||||
# Route to appropriate processing track
|
||||
unified_doc = None
|
||||
|
||||
if recommendation.track == "direct":
|
||||
# Use direct extraction for editable PDFs
|
||||
logger.info("Using DIRECT extraction track (PyMuPDF)")
|
||||
unified_doc = self.direct_extraction_engine.extract(file_path, output_dir)
|
||||
unified_doc.document_id = document_id
|
||||
else:
|
||||
|
||||
# Check if file is Office document - needs conversion to PDF first
|
||||
actual_file_path = file_path
|
||||
temp_pdf_path = None
|
||||
|
||||
if self.office_converter.is_office_document(file_path):
|
||||
# Convert Office to PDF for direct extraction
|
||||
logger.info(f"Converting Office document to PDF for direct extraction: {file_path.name}")
|
||||
try:
|
||||
# Convert to output directory or file parent
|
||||
convert_dir = output_dir if output_dir else file_path.parent
|
||||
temp_pdf_path = self.office_converter.convert_to_pdf(file_path, convert_dir)
|
||||
actual_file_path = temp_pdf_path
|
||||
logger.info(f"Office document converted to PDF: {temp_pdf_path.name}")
|
||||
except OfficeConverterError as e:
|
||||
logger.error(f"Office conversion failed, falling back to OCR: {e}")
|
||||
# Fallback to OCR if conversion fails
|
||||
recommendation = ProcessingTrackRecommendation(
|
||||
track="ocr",
|
||||
confidence=0.7,
|
||||
reason=f"Office conversion failed ({str(e)}), using OCR as fallback",
|
||||
document_type=recommendation.document_type
|
||||
)
|
||||
|
||||
# Only proceed with direct extraction if track is still "direct"
|
||||
if recommendation.track == "direct":
|
||||
unified_doc = self.direct_extraction_engine.extract(actual_file_path, output_dir)
|
||||
unified_doc.document_id = document_id
|
||||
|
||||
# Update metadata with original filename if Office was converted
|
||||
if temp_pdf_path:
|
||||
unified_doc.metadata.original_filename = file_path.name
|
||||
|
||||
# Use OCR track (either by recommendation or fallback)
|
||||
if recommendation.track == "ocr":
|
||||
# Use OCR for scanned documents, images, etc.
|
||||
logger.info("Using OCR track (PaddleOCR)")
|
||||
ocr_result = self.process_file_traditional(
|
||||
|
||||
Reference in New Issue
Block a user