Skip to content
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -2749,8 +2749,10 @@ def __init__(self, **kwargs):
self.lines = kwargs.get("lines", None)

@classmethod
def _from_generated(cls, page):
def _from_generated(cls, page, parent):
return cls(
# TODO will the parent need to be assigned to documentPage at some point?
# And will that assignment change the way DocumentWord accesses AnalyzeResult?
page_number=page.page_number,
angle=adjust_text_angle(page.angle),
width=page.width,
Expand All @@ -2759,7 +2761,7 @@ def _from_generated(cls, page):
lines=[DocumentLine._from_generated(line) for line in page.lines]
if page.lines
else [],
words=[DocumentWord._from_generated(word) for word in page.words]
words=[DocumentWord._from_generated(word, parent) for word in page.words]
if page.words
else [],
selection_marks=[
Expand Down Expand Up @@ -3427,10 +3429,12 @@ def __init__(self, **kwargs):
super(DocumentWord, self).__init__(kind="word", **kwargs)
self.span = kwargs.get("span", None)
self.confidence = kwargs.get("confidence", None)
self._parent = kwargs.get("_parent", None)

@classmethod
def _from_generated(cls, word):
def _from_generated(cls, word, parent):
return cls(
_parent=parent,
content=word.content,
bounding_box=get_bounding_box(word),
span=DocumentSpan._from_generated(word.span)
Expand Down Expand Up @@ -3483,6 +3487,13 @@ def from_dict(cls, data):
confidence=data.get("confidence", None),
)

def get_styles(self):
# type () -> [DocumentStyle]
styles = []
for style in self._parent.styles:
if in_span(self, style):

@catalinaperalta catalinaperalta Oct 19, 2021

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

in_span() is defined in the get_words() PR (#21335). For ease the code is:

def in_span(element, spans):
    # type: (Any, list[Point]) -> bool
    if hasattr(element, "span"):
        for span in spans:
            if element.span.offset >= span.offset and (
                element.span.offset + element.span.length
            ) <= (span.offset + span.length):
                return True
    return False

styles.append(style)
return styles

class AnalyzeResult(object):
"""Document analysis result.
Expand Down Expand Up @@ -3527,7 +3538,7 @@ def _from_generated(cls, response):
api_version=response.api_version,
model_id=response.model_id,
content=response.content,
pages=[DocumentPage._from_generated(page) for page in response.pages]
pages=[DocumentPage._from_generated(page, response) for page in response.pages]
if response.pages
else [],
tables=[DocumentTable._from_generated(table) for table in response.tables]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@

class TestDocumentFromStream(FormRecognizerTest):

@FormRecognizerPreparer()
@DocumentAnalysisClientPreparer()
def test_document_word_get_styles(self, client):
with open(self.invoice_pdf, "rb") as fd:
document = fd.read()

poller = client.begin_analyze_document("prebuilt-document", document)
result = poller.result()

styles = result.pages[0].words[0].get_styles()
assert len(styles) == 1
assert styles[0].is_handwritten == False

@FormRecognizerPreparer()
@DocumentAnalysisClientPreparer()
def test_document_stream_transform_pdf(self, client):
Expand Down