Skip to content
Draft
Show file tree
Hide file tree
Changes from all 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
33 changes: 33 additions & 0 deletions test_unstructured/partition/test_google_vision_paragraphs.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pytest
from google.cloud.vision import Paragraph, TextAnnotation

from unstructured.partition.utils.ocr_models.google_vision_ocr import OCRAgentGoogleVision


@pytest.mark.parametrize(
("symbols", "expected"),
[
([("final", "UNKNOWN", False)], "final"),
([("first", "SPACE", False), ("last", "UNKNOWN", False)], "first last"),
([("first", "SURE_SPACE", False), ("last", "UNKNOWN", False)], "first last"),
([("first", "EOL_SURE_SPACE", False), ("last", "UNKNOWN", False)], "first last"),
([("first", "LINE_BREAK", False), ("last", "UNKNOWN", False)], "first\nlast"),
([("hyphen", "HYPHEN", False), ("ated", "UNKNOWN", False)], "hyphen-\nated"),
([("first", "UNKNOWN", False), ("last", "SPACE", True)], "first last"),
([("whole", "LINE_BREAK", False)], "whole"),
([], ""),
],
)
def test_paragraph_keeps_all_symbols_and_detected_breaks(symbols, expected):
breaks = TextAnnotation.DetectedBreak.BreakType
words = []
for text, break_, prefix in symbols:
characters = [{"text": char} for char in text]
characters[0 if prefix else -1]["property"] = {
"detected_break": {"type_": getattr(breaks, break_), "is_prefix": prefix}
}
words.append({"symbols": characters})
paragraph = Paragraph(words=words)
agent = object.__new__(OCRAgentGoogleVision)

assert agent._get_text_from_paragraph(paragraph) == expected
30 changes: 17 additions & 13 deletions unstructured/partition/utils/ocr_models/google_vision_ocr.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,19 +98,23 @@ def _parse_regions(self, ocr_data: TextAnnotation) -> TextRegions:
return TextRegions.from_list(text_regions)

def _get_text_from_paragraph(self, paragraph: Paragraph) -> str:
"""Retain every symbol, applying the response's leading or trailing break hints."""
breaks = TextAnnotation.DetectedBreak.BreakType
para = ""
line = ""
separators = {
breaks.SPACE: " ",
breaks.SURE_SPACE: " ",
breaks.EOL_SURE_SPACE: " ",
breaks.HYPHEN: "-\n",
breaks.LINE_BREAK: "\n",
}
parts: list[str] = []
for word in paragraph.words:
for symbol in word.symbols:
line += symbol.text
if symbol.property.detected_break.type_ == breaks.SPACE:
line += " "
if symbol.property.detected_break.type_ == breaks.EOL_SURE_SPACE:
line += " "
para += line
line = ""
if symbol.property.detected_break.type_ == breaks.LINE_BREAK:
para += line
line = ""
return para
detected_break = symbol.property.detected_break
separator = separators.get(detected_break.type_, "")
if detected_break.is_prefix:
parts.append(separator)
parts.append(symbol.text)
if not detected_break.is_prefix:
parts.append(separator)
return "".join(parts).rstrip()