documentExtractionag051/ExtractDocument
0
1"""2validation_utils.py3===================4 5Utility functions for validation data operations.6 7This module provides helper functions for working with validation data structures.8"""9 10from typing import Dict, Any11import logging12 13logger = logging.getLogger(__name__)14 15 16def extract_page_dimensions(data: Dict[str, Any]) -> Dict[int, tuple]:17 """18 Extract page dimensions from input data.19 20 Supports both simplified schema and original engine output format.21 22 Args:23 data (dict): Input data containing page information.24 25 Returns:26 dict: {page_num: (width, height)}27 """28 # Check for simplified schema29 if "pages" in data and isinstance(data.get("pages"), list):30 pages = data["pages"]31 # Check if pages have pageNum (simplified schema)32 if pages and "pageNum" in pages[0]:33 return {34 p.get("pageNum", idx + 1): (p.get("width", 0), p.get("height", 0))35 for idx, p in enumerate(pages)36 }37 38 # Check for engine output format39 if "engineData" in data:40 pages = (41 data.get("engineData", {})42 .get("imagePreprocessingResult", {})43 .get("pages", [])44 )45 return {46 idx + 1: (p.get("width", 0), p.get("height", 0))47 for idx, p in enumerate(pages)48 }49 50 logger.warning("Could not extract page dimensions from input data.")51 return {}52 53 54def check_empty_values(data_dict: Dict[str, Any]) -> bool:55 """56 Check if all 'value' fields in a dictionary are empty strings.57 58 Args:59 data_dict (dict): Dictionary with nested value fields.60 61 Returns:62 bool: True if all values are empty, False otherwise.63 """64 return all(65 isinstance(v, dict) and v.get("value", "") == ""66 for v in data_dict.values()67 )68 69 70def calculate_cumulative_heights(page_dimensions: Dict[int, tuple]) -> Dict[int, int]:71 """72 Calculate cumulative page heights for multi-page documents.73 74 Args:75 page_dimensions (dict): {page_num: (width, height)}76 77 Returns:78 dict: {page_num: cumulative_height_offset}79 """80 cumulative_heights = {}81 cumulative_height = 082 83 for page_num in sorted(page_dimensions.keys()):84 cumulative_heights[page_num] = cumulative_height85 _, height = page_dimensions[page_num]86 cumulative_height += height87 88 return cumulative_heights89 