techprotrade's picture
Add src directory
734b5b4 verified
Raw
History Blame Contribute Delete
1.25 kB
from PIL import Image
from typing import Dict, List, Any
import io
class TableDetectionAgent:
"""Detects table structures in PDF pages."""
def process(
self, pdf_page: Image.Image, extracted_text: str = None
) -> Dict[str, Any]:
"""Detect tables in the page."""
return {
"tables_found": self._find_tables(extracted_text or ""),
"table_count": 0,
"table_regions": [],
}
def _find_tables(self, text: str) -> List[Dict]:
"""Find table-like structures."""
tables = []
lines = text.split("\n")
in_table = False
current_table = []
for line in lines:
if "|" in line or "\t" in line:
if not in_table:
in_table = True
current_table = []
current_table.append(line)
else:
if in_table and current_table:
tables.append({"rows": len(current_table), "data": current_table})
in_table = False
current_table = []
if in_table and current_table:
tables.append({"rows": len(current_table), "data": current_table})
return tables