Repository: QuivrHQ/MegaParse
File: libs/megaparse/src/megaparse/parser/unstructured_parser.py (Python)

Code before:
            )
            cleaned_content = re.sub(r"^```.*$\n?", "", content_str, flags=re.MULTILINE)
            markdown_line = f"[TABLE]\n{cleaned_content}\n[/TABLE]\n\n"

        return markdown_line

    def get_strategy(
        self,
        page: PdfPage,
        threshold=0.5,
    ) -> StrategyEnum:
        if self.strategy != StrategyEnum.AUTO:
            raise ValueError("Strategy must be AUTO to use get_strategy")

        # Get the dim of the page
        total_page_area = page.get_width() * page.get_height()
        total_image_area = 0
        images_coords = []
        # Get all the images in the page
        for obj in page.get_objects():
            if isinstance(obj, PdfImage):
                images_coords.append(obj.get_pos())

        canva = np.zeros((int(page.get_height()), int(page.get_width())))
        for coords in images_coords:
            canva[int(coords[1]) : int(coords[3]), int(coords[0]) : int(coords[2])] = 1

        # Get the total area of the images
        total_image_area = np.sum(canva)

        if total_image_area / total_page_area > threshold:
            return StrategyEnum.HI_RES
        return StrategyEnum.FAST

    async def convert(
        self,
        file_path: str | Path | None = None,
        file: IO[bytes] | None = None,
        file_extensions: str = "",
        **kwargs,
    ) -> str:
        strategies = {}
        if file_extensions == ".pdf" and self.strategy == StrategyEnum.AUTO:
            print("Determining strategy...")
            document = (
                pdfium.PdfDocument(file_path) if file_path else pdfium.PdfDocument(file)
            )
            for i, page in enumerate(document):
                strategies[i] = self.get_strategy(page)

            # count number of pages needing HI_RES

Review comment (suggestion):
should probably clamp values to avoid out of bounds error
```suggestion
            p_width, p_high = page.get_width() * page.get_height()
            x1 = max(0, min(p_width, int(coords[0])))
            y1 = max(0, min(p_height, int(coords[1])))
            x2 = max(0, min(p_width, int(coords[2])))
            y2 = max(0, min(p_height, int(coords[3])))
            canva[y1:y2, x1:x2] = 1
```

Code after:
            )
            cleaned_content = re.sub(r"^```.*$\n?", "", content_str, flags=re.MULTILINE)
            markdown_line = f"[TABLE]\n{cleaned_content}\n[/TABLE]\n\n"

        return markdown_line

    def get_strategy(
        self,
        page: PdfPage,
        threshold=0.5,
    ) -> StrategyEnum:
        if self.strategy != StrategyEnum.AUTO:
            raise ValueError("Strategy must be AUTO to use get_strategy")

        # Get the dim of the page
        total_page_area = page.get_width() * page.get_height()
        total_image_area = 0
        images_coords = []
        # Get all the images in the page
        for obj in page.get_objects():
            if isinstance(obj, PdfImage):
                images_coords.append(obj.get_pos())

        canva = np.zeros((int(page.get_height()), int(page.get_width())))
        for coords in images_coords:
            p_width, p_height = int(page.get_width()), int(page.get_height())
            x1 = max(0, min(p_width, int(coords[0])))
            y1 = max(0, min(p_height, int(coords[1])))
            x2 = max(0, min(p_width, int(coords[2])))
            y2 = max(0, min(p_height, int(coords[3])))
            canva[y1:y2, x1:x2] = 1
        # Get the total area of the images
        total_image_area = np.sum(canva)

        if total_image_area / total_page_area > threshold:
            return StrategyEnum.HI_RES
        return StrategyEnum.FAST

    async def convert(
        self,
        file_path: str | Path | None = None,
        file: IO[bytes] | None = None,
        file_extensions: str = "",
        **kwargs,
    ) -> str:
        strategies = {}
        if file_extensions == ".pdf" and self.strategy == StrategyEnum.AUTO:
            print("Determining strategy...")
            document = (
                pdfium.PdfDocument(file_path) if file_path else pdfium.PdfDocument(file)
            )