Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add BBoxAnnotation to TabbyPDF reader #312

Merged
merged 3 commits into from
Aug 24, 2023
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions dedoc/readers/pdf_reader/pdf_txtlayer_reader/pdf_tabby_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from dedoc.common.exceptions.java_not_found_error import JavaNotFoundError
from dedoc.common.exceptions.tabby_pdf_error import TabbyPdfError
from dedoc.data_structures import BBoxAnnotation
sunveil marked this conversation as resolved.
Show resolved Hide resolved
from dedoc.data_structures.bbox import BBox
from dedoc.data_structures.concrete_annotations.bold_annotation import BoldAnnotation
from dedoc.data_structures.concrete_annotations.indentation_annotation import IndentationAnnotation
Expand Down Expand Up @@ -148,16 +149,18 @@ def __get_tables(self, page: dict, file_hash: str) -> List[ScanTable]:
def __get_lines_with_location(self, page: dict, file_hash: str) -> List[LineWithLocation]:
lines = []
page_number = page["number"]
page_width = int(page["width"])
page_height = int(page["height"])
prev_line = None

for block in page["blocks"]:
annotations = []
order = block["order"]
block_text = block["text"]
bx_top_left = block["x_top_left"]
by_top_left = block["y_top_left"]
bx_bottom_right = bx_top_left + block["width"]
by_bottom_right = by_top_left + block["height"]
bx_top_left = int(block["x_top_left"])
by_top_left = int(block["y_top_left"])
bx_bottom_right = bx_top_left + int(block["width"])
by_bottom_right = by_top_left + int(block["height"])
indent = block["indent"]
spacing = block["spacing"]
len_block = len(block_text)
Expand All @@ -173,7 +176,12 @@ def __get_lines_with_location(self, page: dict, file_hash: str) -> List[LineWith
url = annotation["url"]
start = annotation["start"]
end = annotation["end"]

x_top_left = int(annotation["x_top_left"])
y_top_left = int(annotation["y_top_left"])
x_bottom_right = bx_top_left + int(annotation["width"])
y_bottom_right = by_top_left + int(annotation["height"])
box = BBox.from_two_points((x_top_left, y_top_left), (x_bottom_right, y_bottom_right))
annotations.append(BBoxAnnotation(start, end, box, page_width=page_width, page_height=page_height))
annotations.append(SizeAnnotation(start, end, str(font_size)))
annotations.append(StyleAnnotation(start, end, font_name))

Expand All @@ -189,6 +197,7 @@ def __get_lines_with_location(self, page: dict, file_hash: str) -> List[LineWith
meta = block["metadata"].lower()
uid = f"txt_{file_hash}_{order}"
bbox = BBox.from_two_points((bx_top_left, by_top_left), (bx_bottom_right, by_bottom_right))
annotations.append(BBoxAnnotation(0, len_block, bbox, page_width=page_width, page_height=page_height))

metadata = LineMetadata(page_id=page_number, line_id=order)
line_with_location = LineWithLocation(line=block_text,
Expand Down