i study Composite (structural design pattern). I work on project that recognises scanned…
- User
i study Composite (structural design pattern). I work on project that recognises scanned documents write code in python using this pattern. Example: I have table with bounding box. Table consists of lines with bounding boxes. and line consists of cells. i want to increase coordinates in k times.
- Assistant
Let's create a Python implementation of the Composite design pattern for your document recognition system. In this example, we'll create a hierarchy of components (Table → Lines → Cells) where each component can have its own bounding box, and we'll implement a method to scale the coordinates.
Here's the implementation:
from abc import ABC, abstractmethod from typing import List # Abstract Component class DocumentComponent(ABC): @abstractmethod def scale_coordinates(self, factor: float) -> None: pass @abstractmethod def display(self, indent: str = "") -> None: pass # Leaf class - Cell class Cell(DocumentComponent): def __init__(self, x: float, y: float, width: float, height: float): self.x = x self.y = y self.width = width self.height = height def scale_coordinates(self, factor: float) -> None: self.x *= factor self.y *= factor self.width *= factor self.height *= factor def display(self, indent: str = "") -> None: print(f"{indent}Cell: x={self.x:.2f}, y={self.y:.2f}, " f"w={self.width:.2f}, h={self.height:.2f}") # Composite class - Line class Line(DocumentComponent): def __init__(self, x: float, y: float, width: float, height: float): self.x = x self.y = y self.width = width self.height = height self.cells: List[Cell] = [] def add_cell(self, cell: Cell) -> None: self.cells.append(cell) def scale_coordinates(self, factor: float) -> None: # Scale line's own bounding box self.x *= factor self.y *= factor self.width *= factor self.height *= factor # Scale all child cells for cell in self.cells: cell.scale_coordinates(factor) def display(self, indent: str = "") -> None: print(f"{indent}Line: x={self.x:.2f}, y={self.y:.2f}, " f"w={self.width:.2f}, h={self.height:.2f}") for cell in self.cells: cell.display(indent + " ") # Composite class - Table class Table(DocumentComponent): def __init__(self, x: float, y: float, width: float, height: float): self.x = x self.y = y self.width = width self.height = height self.lines: List[Line] = [] def add_line(self, line: Line) -> None: self.lines.append(line) def scale_coordinates(self, factor: float) -> None: # Scale table's own bounding box self.x *= factor self.y *= factor self.width *= factor self.height *= factor # Scale all child lines for line in self.lines: line.scale_coordinates(factor) def display(self, indent: str = "") -> None: print(f"{indent}Table: x={self.x:.2f}, y={self.y:.2f}, " f"w={self.width:.2f}, h={self.height:.2f}") for line in self.lines: line.display(indent + " ") # Client code def main(): # Create a table table = Table(x=10.0, y=10.0, width=200.0, height=100.0) # Create first line line1 = Line(x=10.0, y=10.0, width=200.0, height=30.0) line1.add_cell(Cell(x=10.0, y=10.0, width=50.0, height=30.0)) line1.add_cell(Cell(x=60.0, y=10.0, width=50.0, height=30.0)) # Create second line line2 = Line(x=10.0, y=40.0, width=200.0, height=30.0) line2.add_cell(Cell(x=10.0, y=40.0, width=50.0, height=30.0)) line2.add_cell(Cell(x=60.0, y=40.0, width=50.0, height=30.0)) # Add lines to table table.add_line(line1) table.add_line(line2) print("Before scaling:") table.display() # Scale all coordinates by factor k=2 scale_factor = 2.0 table.scale_coordinates(scale_factor) print(f"\nAfter scaling by factor {scale_factor}:") table.display() if __name__ == "__main__": main()This implementation demonstrates the Composite pattern with the following components:
-
DocumentComponent(Abstract Component): Defines the interface for all components in the hierarchy. -
Cell(Leaf): Represents the smallest unit in the hierarchy, containing its own bounding box. -
Line(Composite): Contains multiple cells and its own bounding box. -
Table(Composite): Contains multiple lines and its own bounding box.
Key features of this implementation:
- Each component (Table, Line, Cell) has a bounding box defined by x, y, width, and height.
- The
scale_coordinatesmethod is implemented at each level, allowing uniform scaling of all coordinates. - The
displaymethod helps visualize the hierarchy and current coordinates. - The composite pattern allows us to treat individual objects (Cells) and compositions (Lines, Tables) uniformly.
When you run this code, it will output something like:
Before scaling: Table: x=10.00, y=10.00, w=200.00, h=100.00 Line: x=10.00, y=10.00, w=200.00, h=30.00 Cell: x=10.00, y=10.00, w=50.00, h=30.00 Cell: x=60.00, y=10.00, w=50.00, h=30.00 Line: x=10.00, y=40.00, w=200.00, h=30.00 Cell: x=10.00, y=40.00, w=50.00, h=30.00 Cell: x=60.00, y=40.00, w=50.00, h=30.00 After scaling by factor 2.0: Table: x=20.00, y=20.00, w=400.00, h=200.00 Line: x=20.00, y=20.00, w=400.00, h=60.00 Cell: x=20.00, y=20.00, w=100.00, h=60.00 Cell: x=120.00, y=20.00, w=100.00, h=60.00 Line: x=20.00, y=80.00, w=400.00, h=60.00 Cell: x=20.00, y=80.00, w=100.00, h=60.00 Cell: x=120.00, y=80.00, w=100.00, h=60.00You can modify this code to add more functionality specific to your document recognition system, such as:
- Adding methods to detect intersections between bounding boxes
- Implementing methods to validate the structure of tables
- Adding properties specific to document recognition (confidence scores, text content, etc.)
- Implementing methods to export the structure to different formats
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.