Skip to content

Commit

Permalink
[formrecognizer] flatten style properties on text appearance model (A…
Browse files Browse the repository at this point in the history
…zure#18562)

* flatten style properties on text appearance model

* fix docstring

* adjust tests
  • Loading branch information
kristapratico authored May 10, 2021
1 parent 1f8779a commit 802cbaa
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 53 deletions.
2 changes: 2 additions & 0 deletions sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

- `begin_recognize_id_documents` renamed to `begin_recognize_identity_documents`
- `begin_recognize_id_documents_from_url` renamed to `begin_recognize_identity_documents_from_url`
- The model `TextAppearance` now includes the properties `style_name` and `style_confidence` that were part of the `TextStyle` object.
- Removed the model `TextStyle`.

## 3.1.0b4 (2021-04-06)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
CustomFormModelProperties,
FormSelectionMark,
TextAppearance,
TextStyle,
)
from ._api_versions import FormRecognizerApiVersion

Expand Down Expand Up @@ -69,7 +68,6 @@
"CustomFormModelProperties",
"FormSelectionMark",
"TextAppearance",
"TextStyle",
]

__VERSION__ = VERSION
Original file line number Diff line number Diff line change
Expand Up @@ -1307,48 +1307,29 @@ def to_dict(self):
class TextAppearance(object):
"""An object representing the appearance of the text line.
:param style: An object representing the style of the text line.
:type style: ~azure.ai.formrecognizer.TextStyle
:ivar str style_name: The text line style name.
Possible values include: "other", "handwriting".
:ivar float style_confidence: The confidence of text line style.
"""

def __init__(self, **kwargs):
self.style = kwargs.get("style", None)
self.style_name = kwargs.get("style_name", None)
self.style_confidence = kwargs.get("style_confidence", None)

@classmethod
def _from_generated(cls, appearance):
if appearance is None:
return appearance
return cls(
style=TextStyle(
name=appearance.style.name, confidence=appearance.style.confidence
)
style_name=appearance.style.name,
style_confidence=appearance.style.confidence
)

def __repr__(self):
return "TextAppearance(style={})".format(repr(self.style))
return "TextAppearance(style_name={}, style_confidence={})".format(self.style_name, self.style_confidence)

def to_dict(self):
return {
"style": self.style.to_dict() if self.style else None
"style_name": self.style_name,
"style_confidence": self.style_confidence
}


class TextStyle(object):
"""An object representing the style of the text line.
:param name: The text line style name.
Possible values include: "other", "handwriting".
:type name: str
:param confidence: The confidence of text line style.
:type confidence: float
"""

def __init__(self, **kwargs):
self.name = kwargs.get("name", None)
self.confidence = kwargs.get("confidence", None)

def __repr__(self):
return "TextStyle(name={}, confidence={})".format(self.name, self.confidence)

def to_dict(self):
return {"name": self.name, "confidence": self.confidence}
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ async def recognize_content(self):
format_bounding_box(line.bounding_box)
))
if line.appearance:
if line.appearance.style.name == "handwriting" and line.appearance.style.confidence > 0.8:
if line.appearance.style_name == "handwriting" and line.appearance.style_confidence > 0.8:
print("Text line '{}' is handwritten and might be a signature.".format(line.text))
for word in line.words:
print("...Word '{}' has a confidence of {}".format(word.text, word.confidence))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def recognize_content(self):
format_bounding_box(line.bounding_box)
))
if line.appearance:
if line.appearance.style.name == "handwriting" and line.appearance.style.confidence > 0.8:
if line.appearance.style_name == "handwriting" and line.appearance.style_confidence > 0.8:
print("Text line '{}' is handwritten and might be a signature.".format(line.text))
for word in line.words:
print("...Word '{}' has a confidence of {}".format(word.text, word.confidence))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def form_word(bounding_box):

@pytest.fixture
def form_line(bounding_box, form_word):
appearance = _models.TextAppearance(style=_models.TextStyle(name="other", confidence=1.0))
appearance = _models.TextAppearance(style_name="other", style_confidence=1.0)
model = _models.FormLine(text="Word Word", bounding_box=bounding_box[0], words=[form_word[0], form_word[0]], page_number=1, appearance=appearance)
model_repr = "FormLine(text=Word Word, bounding_box={}, words=[{}, {}], page_number=1, kind=line, appearance={})".format(bounding_box[1], form_word[1], form_word[1], appearance)[:1024]
assert repr(model) == model_repr
Expand Down
23 changes: 8 additions & 15 deletions sdk/formrecognizer/azure-ai-formrecognizer/tests/test_to_dict.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def test_form_line_to_dict(self):
],
page_number=2,
appearance=_models.TextAppearance(
style=_models.TextStyle(name="other", confidence=0.90)
style_name="other", style_confidence=0.90
),
)

Expand Down Expand Up @@ -133,7 +133,7 @@ def test_form_line_to_dict(self):
],
"page_number": 2,
"kind": "line",
"appearance": {"style": {"name": "other", "confidence": 0.90}},
"appearance": {"style_name": "other", "style_confidence": 0.90},
}
assert d == final

Expand Down Expand Up @@ -196,18 +196,11 @@ def test_form_element_to_dict(self):

def test_text_appearance_to_dict(self):
model = _models.TextAppearance(
style=_models.TextStyle(name="other", confidence=0.98)
style_name="other", style_confidence=0.98
)

d = model.to_dict()
final = {"style": {"name": "other", "confidence": 0.98}}
assert d == final

def test_text_style_to_dict(self):
model = _models.TextStyle(name="other", confidence=0.98)

d = model.to_dict()
final = {"name": "other", "confidence": 0.98}
final = {"style_name": "other", "style_confidence": 0.98}
assert d == final

def test_field_data_to_dict(self):
Expand Down Expand Up @@ -395,7 +388,7 @@ def test_recognized_form_to_dict(self):
],
page_number=2,
appearance=_models.TextAppearance(
style=_models.TextStyle(name="other", confidence=0.90)
style_name="other", style_confidence=0.90
),
)],
)
Expand Down Expand Up @@ -480,7 +473,7 @@ def test_recognized_form_to_dict(self):
],
"page_number": 2,
"kind": "line",
"appearance": {"style": {"name": "other", "confidence": 0.90}},
"appearance": {"style_name": "other", "style_confidence": 0.90},
}],
"selection_marks": [],
"tables": [],
Expand Down Expand Up @@ -574,7 +567,7 @@ def test_form_page_to_dict(self):
],
page_number=2,
appearance=_models.TextAppearance(
style=_models.TextStyle(name="other", confidence=0.90)
style_name="other", style_confidence=0.90
),
),
],
Expand Down Expand Up @@ -680,7 +673,7 @@ def test_form_page_to_dict(self):
],
"page_number": 2,
"kind": "line",
"appearance": {"style": {"name": "other", "confidence": 0.90}},
"appearance": {"style_name": "other", "style_confidence": 0.90},
}],
"selection_marks": [{
"text": "checkbox",
Expand Down
8 changes: 4 additions & 4 deletions sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,8 @@ def assertFormLineTransformCorrect(self, line, expected):
self.assertEqual(line.text, expected.text)
self.assertBoundingBoxTransformCorrect(line.bounding_box, expected.bounding_box)
if expected.appearance:
self.assertEqual(line.appearance.style.name, expected.appearance.style.name)
self.assertEqual(line.appearance.style.confidence, expected.appearance.style.confidence)
self.assertEqual(line.appearance.style_name, expected.appearance.style.name)
self.assertEqual(line.appearance.style_confidence, expected.appearance.style.confidence)
for word, expected_word in zip(line.words, expected.words):
self.assertFormWordTransformCorrect(word, expected_word)

Expand Down Expand Up @@ -558,8 +558,8 @@ def assertFormLineHasValues(self, line, page_number):
self.assertIsNotNone(line.text)
self.assertBoundingBoxHasPoints(line.bounding_box)
if line.appearance:
self.assertIsNotNone(line.appearance.style.name)
self.assertIsNotNone(line.appearance.style.confidence)
self.assertIsNotNone(line.appearance.style_name)
self.assertIsNotNone(line.appearance.style_confidence)
self.assertEqual(line.page_number, page_number)
for word in line.words:
self.assertFormWordHasValues(word, page_number)
Expand Down

0 comments on commit 802cbaa

Please sign in to comment.