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

fix: coordinates bug on pdf parsing #1462

Merged
merged 8 commits into from
Sep 20, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
## 0.10.16-dev2
## 0.10.16

amanda103 marked this conversation as resolved.
Show resolved Hide resolved

### Enhancements
Expand All @@ -10,6 +10,7 @@

### Fixes

* ***Fixes an issue that caused a partition error for some PDF's.** Fixes GH Issue 1460 by bypassing a coordinate check if an element has invalid coordinates.
amanda103 marked this conversation as resolved.
Show resolved Hide resolved
## 0.10.15

### Enhancements
Expand Down
Binary file added example-docs/negative-coords.pdf
Binary file not shown.
8 changes: 8 additions & 0 deletions test_unstructured/partition/pdf-image/test_pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,14 @@ def test_partition_pdf_with_fast_strategy(
assert element.metadata.filename == "layout-parser-paper-fast.pdf"


def test_partition_pdf_with_fast_neg_coordinates():
filename = "example-docs/negative-coords.pdf"
elements = pdf.partition_pdf(filename=filename, url=None, strategy="fast")
assert len(elements) == 5
assert elements[0].metadata.coordinates.points[0][0] < 0
assert elements[0].metadata.coordinates.points[1][0] < 0


def test_partition_pdf_with_fast_groups_text(
filename="example-docs/layout-parser-paper-fast.pdf",
):
Expand Down
2 changes: 1 addition & 1 deletion unstructured/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.10.16-dev2" # pragma: no cover
__version__ = "0.10.16" # pragma: no cover
7 changes: 5 additions & 2 deletions unstructured/partition/pdf.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
FileType,
add_metadata_with_filetype,
)
from unstructured.logger import logger
from unstructured.logger import logger, trace_logger
from unstructured.nlp.patterns import PARAGRAPH_PATTERN
from unstructured.partition.common import (
convert_to_bytes,
Expand Down Expand Up @@ -761,7 +761,10 @@ def check_coords_within_boundary(
a float ranges from [0,1] to scale the horizontal (x-axis) boundary
"""
if not coord_has_valid_points(coordinates) and not coord_has_valid_points(boundary):
raise ValueError("Invalid coordinates.")
trace_logger.detail( # type: ignore
f"coordinates {coordinates} and boundary {boundary} did not pass validation",
)
return False
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

worth adding a trace detail message i think, like:

trace_logger.detail( # type: ignore

.
is there a single page this fails on that could be added to a test? one could extract the problematic page with something like:

qpdf --empty --pages orignal.pdf 89 -- ~/tmp/docs/single-page-p89.pdf

finally, does not need to be addressed in this PR, but what is the why for the invalid coordinate? is it negative values? (which i think are actually legit in PDF's in certain cases)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sounds good - I'll add trace detail and a test!

And yes - the values that it was erroring on were negative.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


boundary_x_min = boundary.points[0][0]
boundary_x_max = boundary.points[2][0]
Expand Down