File size: 1,246 Bytes
734b5b4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
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