Skip to content

Commit 5e293e8

Browse files
authored
[form recognizer] add repr (#11150)
1 parent c815f57 commit 5e293e8

File tree

2 files changed

+320
-1
lines changed

2 files changed

+320
-1
lines changed

sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,11 @@ def __init__(self, **kwargs):
166166
self.page_range = kwargs.get("page_range", None)
167167
self.pages = kwargs.get("pages", None)
168168

169+
def __repr__(self):
170+
return "RecognizedForm(form_type={}, fields={}, page_range={}, pages={})".format(
171+
self.form_type, repr(self.fields), repr(self.page_range), repr(self.pages)
172+
)[:1024]
173+
169174

170175
class USReceipt(object): # pylint: disable=too-many-instance-attributes
171176
"""Extracted fields found on the US sales receipt. Provides
@@ -182,7 +187,7 @@ class USReceipt(object): # pylint: disable=too-many-instance-attributes
182187
:ivar list[~azure.ai.formrecognizer.USReceiptItem] receipt_items:
183188
The purchased items found on the receipt.
184189
:ivar ~azure.ai.formrecognizer.FormField subtotal:
185-
The subtotal found on the receipt.
190+
The subtotal found on the receipt
186191
:ivar ~azure.ai.formrecognizer.FormField tax:
187192
The tax value found on the receipt.
188193
:ivar ~azure.ai.formrecognizer.FormField tip:
@@ -224,6 +229,17 @@ def __init__(self, **kwargs):
224229
self.form_type = kwargs.get("form_type", None)
225230
self.receipt_locale = kwargs.get("receipt_locale", "en-US")
226231

232+
def __repr__(self):
233+
return "USReceipt(merchant_address={}, merchant_name={}, merchant_phone_number={}, " \
234+
"receipt_type={}, receipt_items={}, subtotal={}, tax={}, tip={}, total={}, "\
235+
"transaction_date={}, transaction_time={}, fields={}, page_range={}, pages={}, " \
236+
"form_type={}, receipt_locale={})".format(
237+
repr(self.merchant_address), repr(self.merchant_name), repr(self.merchant_phone_number),
238+
repr(self.receipt_type), repr(self.receipt_items), repr(self.subtotal), repr(self.tax),
239+
repr(self.tip), repr(self.total), repr(self.transaction_date), repr(self.transaction_time),
240+
repr(self.fields), repr(self.page_range), repr(self.pages), self.form_type, self.receipt_locale
241+
)[:1024]
242+
227243

228244
class FormField(object):
229245
"""Represents a field recognized in an input form.
@@ -263,6 +279,7 @@ def _from_generated(cls, field, value, read_result):
263279
page_number=value.page if value else None,
264280
)
265281

282+
266283
@classmethod
267284
def _from_generated_unlabeled(cls, field, idx, page, read_result):
268285
return cls(
@@ -274,6 +291,11 @@ def _from_generated_unlabeled(cls, field, idx, page, read_result):
274291
page_number=page,
275292
)
276293

294+
def __repr__(self):
295+
return "FormField(label_data={}, value_data={}, name={}, value={}, confidence={}, page_number={})".format(
296+
repr(self.label_data), repr(self.value_data), self.name, repr(self.value), self.confidence, self.page_number
297+
)[:1024]
298+
277299

278300
class FieldText(FormContent):
279301
"""Represents the text that is part of a form field. This includes
@@ -328,6 +350,11 @@ def _from_generated_unlabeled(cls, field, page, read_result):
328350
text_content=get_elements(field, read_result) if field.elements else None
329351
)
330352

353+
def __repr__(self):
354+
return "FieldText(page_number={}, text={}, bounding_box={}, text_content={})".format(
355+
self.page_number, self.text, self.bounding_box, repr(self.text_content)
356+
)[:1024]
357+
331358

332359
class FormPage(object):
333360
"""Represents a page recognized from the input document. Contains lines,
@@ -377,6 +404,11 @@ def _from_generated(cls, read_result):
377404
lines=[FormLine._from_generated(line, page=page.page) for line in page.lines] if page.lines else None
378405
) for page in read_result]
379406

407+
def __repr__(self):
408+
return "FormPage(page_number={}, text_angle={}, width={}, height={}, unit={}, tables={}, lines={})".format(
409+
self.page_number, self.text_angle, self.width, self.height, self.unit, repr(self.tables), repr(self.lines)
410+
)[:1024]
411+
380412

381413
class FormLine(FormContent):
382414
"""An object representing an extracted line of text.
@@ -411,6 +443,10 @@ def _from_generated(cls, line, page):
411443
words=[FormWord._from_generated(word, page) for word in line.words] if line.words else None
412444
)
413445

446+
def __repr__(self):
447+
return "FormLine(text={}, bounding_box={}, words={}, page_number={})".format(
448+
self.text, self.bounding_box, repr(self.words), self.page_number
449+
)[:1024]
414450

415451
class FormWord(FormContent):
416452
"""Represents a word recognized from the input document.
@@ -445,6 +481,11 @@ def _from_generated(cls, word, page):
445481
page_number=page
446482
)
447483

484+
def __repr__(self):
485+
return "FormWord(text={}, bounding_box={}, confidence={}, page_number={})".format(
486+
self.text, self.bounding_box, self.confidence, self.page_number
487+
)[:1024]
488+
448489

449490
class USReceiptType(object):
450491
"""The type of the analyzed US receipt and the confidence
@@ -466,6 +507,9 @@ def _from_generated(cls, item):
466507
type=item.value_string,
467508
confidence=item.confidence or 1.0) if item else None
468509

510+
def __repr__(self):
511+
return "USReceiptType(type={}, confidence={})".format(self.type, self.confidence)[:1024]
512+
469513

470514
class USReceiptItem(object):
471515
"""A receipt item on a US sales receipt.
@@ -500,6 +544,11 @@ def _from_generated(cls, items, read_result):
500544
except AttributeError:
501545
return []
502546

547+
def __repr__(self):
548+
return "USReceiptItem(name={}, quantity={}, price={}, total_price={})".format(
549+
repr(self.name), repr(self.quantity), repr(self.price), repr(self.total_price)
550+
)[:1024]
551+
503552

504553
class FormTable(object):
505554
"""Information about the extracted table contained on a page.
@@ -517,6 +566,11 @@ def __init__(self, **kwargs):
517566
self.row_count = kwargs.get("row_count", None)
518567
self.column_count = kwargs.get("column_count", None)
519568

569+
def __repr__(self):
570+
return "FormTable(cells={}, row_count={}, column_count={})".format(
571+
repr(self.cells), self.row_count, self.column_count
572+
)[:1024]
573+
520574

521575
class FormTableCell(FormContent):
522576
"""Represents a cell contained in a table recognized from the input document.
@@ -576,6 +630,13 @@ def _from_generated(cls, cell, page, read_result):
576630
text_content=get_elements(cell, read_result) if cell.elements else None
577631
)
578632

633+
def __repr__(self):
634+
return "FormTableCell(text={}, row_index={}, column_index={}, row_span={}, column_span={}, " \
635+
"bounding_box={}, confidence={}, is_header={}, is_footer={}, page_number={}, text_content={})".format(
636+
self.text, self.row_index, self.column_index, self.row_span, self.column_span, self.bounding_box,
637+
self.confidence, self.is_header, self.is_footer, self.page_number, repr(self.text_content)
638+
)[:1024]
639+
579640

580641
class CustomFormModel(object):
581642
"""Represents a model trained from custom forms.
@@ -621,6 +682,13 @@ def _from_generated(cls, model):
621682
if model.train_result else None
622683
)
623684

685+
def __repr__(self):
686+
return "CustomFormModel(model_id={}, status={}, created_on={}, last_modified={}, models={}, " \
687+
"errors={}, training_documents={})".format(
688+
self.model_id, self.status, self.created_on, self.last_modified, repr(self.models),
689+
repr(self.errors), repr(self.training_documents)
690+
)[:1024]
691+
624692

625693
class CustomFormSubModel(object):
626694
"""Represents a submodel that extracts fields from a specific type of form.
@@ -656,6 +724,11 @@ def _from_generated_labeled(cls, model):
656724
form_type="form-" + model.model_info.model_id
657725
)] if model.train_result else None
658726

727+
def __repr__(self):
728+
return "CustomFormSubModel(accuracy={}, fields={}, form_type={})".format(
729+
self.accuracy, repr(self.fields), self.form_type
730+
)[:1024]
731+
659732

660733
class CustomFormModelField(object):
661734
"""A field that the model will extract from forms it analyzes.
@@ -685,6 +758,11 @@ def _from_generated_unlabeled(cls, fields):
685758
) for idx, field_name in enumerate(fields)
686759
}
687760

761+
def __repr__(self):
762+
return "CustomFormModelField(label={}, name={}, accuracy={})".format(
763+
self.label, self.name, self.accuracy
764+
)[:1024]
765+
688766

689767
class TrainingDocumentInfo(object):
690768
"""Report for an individual document used for training
@@ -717,6 +795,11 @@ def _from_generated(cls, train_result):
717795
errors=FormRecognizerError._from_generated(doc.errors)
718796
) for doc in train_result.training_documents] if train_result.training_documents else None
719797

798+
def __repr__(self):
799+
return "TrainingDocumentInfo(document_name={}, status={}, page_count={}, errors={})".format(
800+
self.document_name, self.status, self.page_count, repr(self.errors)
801+
)[:1024]
802+
720803

721804
class FormRecognizerError(object):
722805
"""Represents an error that occurred while training.
@@ -733,6 +816,9 @@ def __init__(self, **kwargs):
733816
def _from_generated(cls, err):
734817
return [cls(code=error.code, message=error.message) for error in err] if err else []
735818

819+
def __repr__(self):
820+
return "FormRecognizerError(code={}, message={})".format(self.code, self.message)[:1024]
821+
736822

737823
class CustomFormModelInfo(object):
738824
"""Custom model information.
@@ -762,6 +848,11 @@ def _from_generated(cls, model):
762848
last_modified=model.last_updated_date_time
763849
)
764850

851+
def __repr__(self):
852+
return "CustomFormModelInfo(model_id={}, status={}, created_on={}, last_modified={})".format(
853+
self.model_id, self.status, self.created_on, self.last_modified
854+
)[:1024]
855+
765856

766857
class AccountProperties(object):
767858
"""Summary of all the custom models on the account.
@@ -780,3 +871,8 @@ def _from_generated(cls, model):
780871
custom_model_count=model.count,
781872
custom_model_limit=model.limit,
782873
)
874+
875+
def __repr__(self):
876+
return "AccountProperties(custom_model_count={}, custom_model_limit={})".format(
877+
self.custom_model_count, self.custom_model_limit
878+
)[:1024]

0 commit comments

Comments
 (0)