From ecc13f3f8161e42e78ed23e7799ee78ab6498d58 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 2 Jun 2020 18:34:19 -0400 Subject: [PATCH 01/11] remvoed USReceipt and USReceiptItem --- .../azure-ai-formrecognizer/CHANGELOG.md | 2 + .../azure/ai/formrecognizer/__init__.py | 4 - .../formrecognizer/_form_recognizer_client.py | 4 +- .../azure/ai/formrecognizer/_models.py | 105 +----------------- .../ai/formrecognizer/_response_handlers.py | 41 +------ .../aio/_form_recognizer_client_async.py | 4 +- 6 files changed, 14 insertions(+), 146 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md index a740919023f1..5617b4da42dd 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md @@ -22,6 +22,8 @@ `CustomFormModel` and `CustomFormModelInfo` models. - `models` property of `CustomFormModel` is renamed to `submodels` - `CustomFormSubModel` is renamed to `CustomFormSubmodel` +- Removed `USReceipt`. To see how to deal with the return value of `begin_recognize_receipts`, see [sample_recognize_receipts.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py) or [sample_recognize_receipts_from_url.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py) for details. +- Removed `USReceiptItem`. To see how to access the individual items on a receipt, see [sample_recognize_receipts.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py) or [sample_recognize_receipts_from_url.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py) for details. **New features** diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py index f65b9e8e92af..0bde1bc93db1 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py @@ -14,9 +14,7 @@ TrainingStatus, CustomFormModelStatus, FormContentType, - USReceipt, ReceiptType, - USReceiptItem, FormTable, FormTableCell, TrainingDocumentInfo, @@ -45,9 +43,7 @@ 'CustomFormModelStatus', 'FormContentType', 'FormContent', - 'USReceipt', 'ReceiptType', - 'USReceiptItem', 'FormTable', 'FormTableCell', 'TrainingDocumentInfo', diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py index d3d2173442aa..ac2bb7cd304f 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_form_recognizer_client.py @@ -17,7 +17,7 @@ from azure.core.polling.base_polling import LROBasePolling from ._generated._form_recognizer_client import FormRecognizerClient as FormRecognizer from ._response_handlers import ( - prepare_us_receipt, + prepare_receipt, prepare_content_result, prepare_form_result ) @@ -74,7 +74,7 @@ def __init__(self, endpoint, credential, **kwargs): def _receipt_callback(self, raw_response, _, headers): # pylint: disable=unused-argument analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response) - return prepare_us_receipt(analyze_result) + return prepare_receipt(analyze_result) @distributed_trace def begin_recognize_receipts(self, receipt, **kwargs): diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py index cf61dd1038f4..b4e6a9e7b274 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py @@ -197,80 +197,18 @@ class RecognizedReceipt(RecognizedForm): words, tables and page metadata. :ivar ~azure.ai.formrecognizer.ReceiptType receipt_type: The reciept type and confidence. - :ivar str receipt_locale: Defaults to "en-US". """ def __init__(self, **kwargs): super(RecognizedReceipt, self).__init__(**kwargs) self.receipt_type = kwargs.get("receipt_type", None) - self.receipt_locale = kwargs.get("receipt_locale", "en-US") def __repr__(self): return "RecognizedReceipt(form_type={}, fields={}, page_range={}, pages={}, " \ - "receipt_type={}, receipt_locale={})".format( + "receipt_type={})".format( self.form_type, repr(self.fields), repr(self.page_range), repr(self.pages), - repr(self.receipt_type), self.receipt_locale + repr(self.receipt_type) )[:1024] -class USReceipt(RecognizedReceipt): # pylint: disable=too-many-instance-attributes - """Extracted fields found on the US sales receipt. Provides - attributes for accessing common fields present in US sales receipts. - - :ivar ~azure.ai.formrecognizer.FormField merchant_address: - The address of the merchant. - :ivar ~azure.ai.formrecognizer.FormField merchant_name: - The name of the merchant. - :ivar ~azure.ai.formrecognizer.FormField merchant_phone_number: - The phone number associated with the merchant. - :ivar list[~azure.ai.formrecognizer.USReceiptItem] receipt_items: - The purchased items found on the receipt. - :ivar ~azure.ai.formrecognizer.FormField subtotal: - The subtotal found on the receipt - :ivar ~azure.ai.formrecognizer.FormField tax: - The tax value found on the receipt. - :ivar ~azure.ai.formrecognizer.FormField tip: - The tip value found on the receipt. - :ivar ~azure.ai.formrecognizer.FormField total: - The total amount found on the receipt. - :ivar ~azure.ai.formrecognizer.FormField transaction_date: - The transaction date of the sale. - :ivar ~azure.ai.formrecognizer.FormField transaction_time: - The transaction time of the sale. - :ivar fields: - A dictionary of the fields found on the receipt. - :vartype fields: dict[str, ~azure.ai.formrecognizer.FormField] - :ivar ~azure.ai.formrecognizer.FormPageRange page_range: - The first and last page number of the input receipt. - :ivar list[~azure.ai.formrecognizer.FormPage] pages: - Contains page metadata such as page width, length, text angle, unit. - If `include_text_content=True` is passed, contains a list - of extracted text lines for each page in the input document. - :ivar str form_type: The type of form. - """ - - def __init__(self, **kwargs): - super(USReceipt, self).__init__(**kwargs) - self.merchant_address = kwargs.get("merchant_address", None) - self.merchant_name = kwargs.get("merchant_name", None) - self.merchant_phone_number = kwargs.get("merchant_phone_number", None) - self.receipt_items = kwargs.get("receipt_items", None) - self.subtotal = kwargs.get("subtotal", None) - self.tax = kwargs.get("tax", None) - self.tip = kwargs.get("tip", None) - self.total = kwargs.get("total", None) - self.transaction_date = kwargs.get("transaction_date", None) - self.transaction_time = kwargs.get("transaction_time", None) - - def __repr__(self): - return "USReceipt(merchant_address={}, merchant_name={}, merchant_phone_number={}, " \ - "receipt_type={}, receipt_items={}, subtotal={}, tax={}, tip={}, total={}, "\ - "transaction_date={}, transaction_time={}, fields={}, page_range={}, pages={}, " \ - "form_type={}, receipt_locale={})".format( - repr(self.merchant_address), repr(self.merchant_name), repr(self.merchant_phone_number), - repr(self.receipt_type), repr(self.receipt_items), repr(self.subtotal), repr(self.tax), - repr(self.tip), repr(self.total), repr(self.transaction_date), repr(self.transaction_time), - repr(self.fields), repr(self.page_range), repr(self.pages), self.form_type, self.receipt_locale - )[:1024] - class FormField(object): """Represents a field recognized in an input form. @@ -537,45 +475,6 @@ def __repr__(self): return "ReceiptType(type={}, confidence={})".format(self.type, self.confidence)[:1024] -class USReceiptItem(object): - """A receipt item on a US sales receipt. - Contains the item name, quantity, price, and total price. - - :ivar ~azure.ai.formrecognizer.FormField name: - The name of the item. - :ivar ~azure.ai.formrecognizer.FormField quantity: - The quantity associated with this item. - :ivar ~azure.ai.formrecognizer.FormField price: - The price of a single unit of this item. - :ivar ~azure.ai.formrecognizer.FormField total_price: - The total price of this item, taking the quantity into account. - """ - - def __init__(self, **kwargs): - self.name = kwargs.get("name", None) - self.quantity = kwargs.get("quantity", None) - self.price = kwargs.get("price", None) - self.total_price = kwargs.get("total_price", None) - - @classmethod - def _from_generated(cls, items, read_result): - try: - receipt_item = items.value_array - return [cls( - name=FormField._from_generated("Name", item.value_object.get("Name"), read_result), - quantity=FormField._from_generated("Quantity", item.value_object.get("Quantity"), read_result), - price=FormField._from_generated("Price", item.value_object.get("Price"), read_result), - total_price=FormField._from_generated("TotalPrice", item.value_object.get("TotalPrice"), read_result), - ) for item in receipt_item] - except AttributeError: - return [] - - def __repr__(self): - return "USReceiptItem(name={}, quantity={}, price={}, total_price={})".format( - repr(self.name), repr(self.quantity), repr(self.price), repr(self.total_price) - )[:1024] - - class FormTable(object): """Information about the extracted table contained on a page. diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py index 08d8fa9d9461..d9a2cccd2d17 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py @@ -7,20 +7,19 @@ # pylint: disable=protected-access from ._models import ( - USReceipt, ReceiptType, FormField, - USReceiptItem, FormPage, FormLine, FormTable, FormTableCell, FormPageRange, - RecognizedForm + RecognizedForm, + RecognizedReceipt ) -def prepare_us_receipt(response): +def prepare_receipt(response): receipts = [] read_result = response.analyze_result.read_results document_result = response.analyze_result.document_results @@ -28,43 +27,15 @@ def prepare_us_receipt(response): for page in document_result: if page.fields is None: - receipt = USReceipt( + receipt = RecognizedReceipt( page_range=FormPageRange(first_page_number=page.page_range[0], last_page_number=page.page_range[1]), pages=form_page[page.page_range[0]-1:page.page_range[1]], form_type=page.doc_type, ) receipts.append(receipt) continue - receipt = USReceipt( - merchant_address=FormField._from_generated( - "MerchantAddress", page.fields.get("MerchantAddress"), read_result - ), - merchant_name=FormField._from_generated( - "MerchantName", page.fields.get("MerchantName"), read_result - ), - merchant_phone_number=FormField._from_generated( - "MerchantPhoneNumber", - page.fields.get("MerchantPhoneNumber"), - read_result, - ), + receipt = RecognizedReceipt( receipt_type=ReceiptType._from_generated(page.fields.get("ReceiptType")), - receipt_items=USReceiptItem._from_generated( - page.fields.get("Items"), read_result - ), - subtotal=FormField._from_generated( - "Subtotal", page.fields.get("Subtotal"), read_result - ), - tax=FormField._from_generated("Tax", page.fields.get("Tax"), read_result), - tip=FormField._from_generated("Tip", page.fields.get("Tip"), read_result), - total=FormField._from_generated( - "Total", page.fields.get("Total"), read_result - ), - transaction_date=FormField._from_generated( - "TransactionDate", page.fields.get("TransactionDate"), read_result - ), - transaction_time=FormField._from_generated( - "TransactionTime", page.fields.get("TransactionTime"), read_result - ), page_range=FormPageRange( first_page_number=page.page_range[0], last_page_number=page.page_range[1] ), @@ -73,7 +44,7 @@ def prepare_us_receipt(response): fields={ key: FormField._from_generated(key, value, read_result) for key, value in page.fields.items() - }, + } ) receipts.append(receipt) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py index 8e0a4dd81d56..ae35b3c275c6 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/aio/_form_recognizer_client_async.py @@ -17,7 +17,7 @@ from azure.core.polling.async_base_polling import AsyncLROBasePolling from .._generated.aio._form_recognizer_client_async import FormRecognizerClient as FormRecognizer from .._response_handlers import ( - prepare_us_receipt, + prepare_receipt, prepare_content_result, prepare_form_result ) @@ -84,7 +84,7 @@ def __init__( def _receipt_callback(self, raw_response, _, headers): # pylint: disable=unused-argument analyze_result = self._client._deserialize(AnalyzeOperationResult, raw_response) - return prepare_us_receipt(analyze_result) + return prepare_receipt(analyze_result) @distributed_trace_async async def recognize_receipts( From 7bb73e4d126623a8ad2cdf5db804dfbf16244984 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 2 Jun 2020 18:34:37 -0400 Subject: [PATCH 02/11] updated tests --- .../tests/test_receipt.py | 101 ++++++------------ .../tests/test_receipt_async.py | 101 ++++++------------ .../tests/test_receipt_from_url.py | 101 ++++++------------ .../tests/test_receipt_from_url_async.py | 101 ++++++------------ .../tests/test_repr.py | 56 +--------- .../azure-ai-formrecognizer/tests/testcase.py | 38 +++---- 6 files changed, 162 insertions(+), 336 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py index e7a605036e63..9192169c4b23 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py @@ -9,7 +9,7 @@ from azure.core.exceptions import ClientAuthenticationError, ServiceRequestError, HttpResponseError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult -from azure.ai.formrecognizer._response_handlers import prepare_us_receipt +from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer import FormRecognizerClient, FormContentType from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer @@ -136,7 +136,7 @@ def test_receipt_stream_transform_png(self, resource_group, location, form_recog def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -157,17 +157,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -188,7 +177,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -201,7 +190,7 @@ def test_receipt_stream_transform_jpg(self, resource_group, location, form_recog def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -223,17 +212,6 @@ def callback(raw_response, _, headers): document_results = raw_response.analyze_result.document_results page_results = raw_response.analyze_result.page_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -254,7 +232,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -271,21 +249,21 @@ def test_receipt_jpg(self, resource_group, location, form_recognizer_account, fo result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.merchant_phone_number.value, '+19876543210') - self.assertEqual(receipt.subtotal.value, 11.7) - self.assertEqual(receipt.tax.value, 1.17) - self.assertEqual(receipt.tip.value, 1.63) - self.assertEqual(receipt.total.value, 14.5) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+19876543210') + self.assertEqual(receipt.fields.get("Subtotal").value, 11.7) + self.assertEqual(receipt.fields.get("Tax").value, 1.17) + self.assertEqual(receipt.fields.get("Tip").value, 1.63) + self.assertEqual(receipt.fields.get("Total").value, 14.5) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized') - self.assertReceiptItemsHasValues(receipt.receipt_items, receipt.page_range.first_page_number, False) + self.assertReceiptItemsHasValues(receipt.fields['Items'].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() def test_receipt_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -297,13 +275,13 @@ def test_receipt_png(self, resource_group, location, form_recognizer_account, fo result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.subtotal.value, 1098.99) - self.assertEqual(receipt.tax.value, 104.4) - self.assertEqual(receipt.total.value, 1203.39) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("Subtotal").value, 1098.99) + self.assertEqual(receipt.fields.get("Tax").value, 104.4) + self.assertEqual(receipt.fields.get("Total").value, 1203.39) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) @@ -323,7 +301,7 @@ def test_receipt_jpg_include_text_content(self, resource_group, location, form_r self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type", "receipt_locale"]: + if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type"]: form_field = getattr(receipt, field) self.assertTextContentHasValues(form_field.value_data.text_content, receipt.page_range.first_page_number) @@ -340,22 +318,22 @@ def test_receipt_multipage(self, resource_group, location, form_recognizer_accou self.assertEqual(len(result), 3) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Bilbo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 300.0) - self.assertEqual(receipt.total.value, 100.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Bilbo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 300.0) + self.assertEqual(receipt.fields.get("Total").value, 100.0) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized') receipt = result[2] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Frodo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 3000.0) - self.assertEqual(receipt.total.value, 1000.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 3000.0) + self.assertEqual(receipt.fields.get("Total").value, 1000.0) self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) @@ -370,7 +348,7 @@ def test_receipt_multipage_transform(self, resource_group, location, form_recogn def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -395,15 +373,6 @@ def callback(raw_response, _, headers): for receipt, actual in zip(returned_model, actual): if actual.fields is None: # second page is blank continue - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.fields.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.fields.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.fields.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.fields.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.fields.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.fields.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.fields.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.fields.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.fields.get("TransactionTime"), read_results) # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.fields.get("MerchantAddress"), read_results) @@ -425,7 +394,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual.fields["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(returned_model, read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py index f496ee796fb5..d0f71f0698b7 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py @@ -9,7 +9,7 @@ from azure.core.exceptions import ServiceRequestError, ClientAuthenticationError, HttpResponseError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult -from azure.ai.formrecognizer._response_handlers import prepare_us_receipt +from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer.aio import FormRecognizerClient from azure.ai.formrecognizer import FormContentType from testcase import GlobalFormRecognizerAccountPreparer @@ -135,7 +135,7 @@ async def test_receipt_stream_transform_png(self, resource_group, location, form def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -155,17 +155,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -186,7 +175,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -199,7 +188,7 @@ async def test_receipt_stream_transform_jpg(self, resource_group, location, form def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -220,17 +209,6 @@ def callback(raw_response, _, headers): document_results = raw_response.analyze_result.document_results page_results = raw_response.analyze_result.page_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -251,7 +229,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -267,21 +245,21 @@ async def test_receipt_jpg(self, resource_group, location, form_recognizer_accou self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.merchant_phone_number.value, '+19876543210') - self.assertEqual(receipt.subtotal.value, 11.7) - self.assertEqual(receipt.tax.value, 1.17) - self.assertEqual(receipt.tip.value, 1.63) - self.assertEqual(receipt.total.value, 14.5) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+19876543210') + self.assertEqual(receipt.fields.get("Subtotal").value, 11.7) + self.assertEqual(receipt.fields.get("Tax").value, 1.17) + self.assertEqual(receipt.fields.get("Tip").value, 1.63) + self.assertEqual(receipt.fields.get("Total").value, 14.5) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized') - self.assertReceiptItemsHasValues(receipt.receipt_items, receipt.page_range.first_page_number, False) + self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() async def test_receipt_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -293,13 +271,13 @@ async def test_receipt_png(self, resource_group, location, form_recognizer_accou result = await client.recognize_receipts(receipt) self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.subtotal.value, 1098.99) - self.assertEqual(receipt.tax.value, 104.4) - self.assertEqual(receipt.total.value, 1203.39) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("Subtotal").value, 1098.99) + self.assertEqual(receipt.fields.get("Tax").value, 104.4) + self.assertEqual(receipt.fields.get("Total").value, 1203.39) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) @@ -318,7 +296,7 @@ async def test_receipt_jpg_include_text_content(self, resource_group, location, self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type", "receipt_locale"]: + if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type"]: form_field = getattr(receipt, field) self.assertTextContentHasValues(form_field.value_data.text_content, receipt.page_range.first_page_number) @@ -334,22 +312,22 @@ async def test_receipt_multipage(self, resource_group, location, form_recognizer self.assertEqual(len(result), 3) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Bilbo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 300.0) - self.assertEqual(receipt.total.value, 100.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Bilbo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 300.0) + self.assertEqual(receipt.fields.get("Total").value, 100.0) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized') receipt = result[2] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Frodo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 3000.0) - self.assertEqual(receipt.total.value, 1000.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 3000.0) + self.assertEqual(receipt.fields.get("Total").value, 1000.0) self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) @@ -364,7 +342,7 @@ async def test_receipt_multipage_transform(self, resource_group, location, form_ def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -388,15 +366,6 @@ def callback(raw_response, _, headers): for receipt, actual in zip(returned_model, actual): if actual.fields is None: # second page is blank continue - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.fields.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.fields.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.fields.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.fields.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.fields.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.fields.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.fields.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.fields.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.fields.get("TransactionTime"), read_results) # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.fields.get("MerchantAddress"), read_results) @@ -418,7 +387,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual.fields["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(returned_model, read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py index 9771d8255746..75c28c5b0c9a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py @@ -9,7 +9,7 @@ from azure.core.exceptions import HttpResponseError, ServiceRequestError, ClientAuthenticationError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult -from azure.ai.formrecognizer._response_handlers import prepare_us_receipt +from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer import FormRecognizerClient from testcase import FormRecognizerTest, GlobalFormRecognizerAccountPreparer @@ -65,7 +65,7 @@ def test_receipt_url_transform_jpg(self, resource_group, location, form_recogniz def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -83,17 +83,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -114,7 +103,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -127,7 +116,7 @@ def test_receipt_url_transform_png(self, resource_group, location, form_recogniz def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -145,17 +134,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -176,7 +154,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -196,7 +174,7 @@ def test_receipt_url_include_text_content(self, resource_group, location, form_r self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type", "receipt_locale"]: + if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type"]: field = getattr(receipt, field) self.assertTextContentHasValues(field.value_data.text_content, receipt.page_range.first_page_number) @@ -212,21 +190,21 @@ def test_receipt_url_jpg(self, resource_group, location, form_recognizer_account result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.merchant_phone_number.value, '+19876543210') - self.assertEqual(receipt.subtotal.value, 11.7) - self.assertEqual(receipt.tax.value, 1.17) - self.assertEqual(receipt.tip.value, 1.63) - self.assertEqual(receipt.total.value, 14.5) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+19876543210') + self.assertEqual(receipt.fields.get("Subtotal").value, 11.7) + self.assertEqual(receipt.fields.get("Tax").value, 1.17) + self.assertEqual(receipt.fields.get("Tip").value, 1.63) + self.assertEqual(receipt.fields.get("Total").value, 14.5) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized') - self.assertReceiptItemsHasValues(receipt.receipt_items, receipt.page_range.first_page_number, False) + self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() def test_receipt_url_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -237,13 +215,13 @@ def test_receipt_url_png(self, resource_group, location, form_recognizer_account result = poller.result() self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.subtotal.value, 1098.99) - self.assertEqual(receipt.tax.value, 104.4) - self.assertEqual(receipt.total.value, 1203.39) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("Subtotal").value, 1098.99) + self.assertEqual(receipt.fields.get("Tax").value, 104.4) + self.assertEqual(receipt.fields.get("Total").value, 1203.39) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) @@ -259,22 +237,22 @@ def test_receipt_multipage_url(self, resource_group, location, form_recognizer_a self.assertEqual(len(result), 3) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Bilbo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 300.0) - self.assertEqual(receipt.total.value, 100.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Bilbo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 300.0) + self.assertEqual(receipt.fields.get("Total").value, 100.0) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized') receipt = result[2] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Frodo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 3000.0) - self.assertEqual(receipt.total.value, 1000.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 3000.0) + self.assertEqual(receipt.fields.get("Total").value, 1000.0) self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) @@ -289,7 +267,7 @@ def test_receipt_multipage_transform_url(self, resource_group, location, form_re def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -311,15 +289,6 @@ def callback(raw_response, _, headers): for receipt, actual in zip(returned_model, actual): if actual.fields is None: # second page is blank continue - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.fields.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.fields.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.fields.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.fields.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.fields.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.fields.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.fields.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.fields.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.fields.get("TransactionTime"), read_results) # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.fields.get("MerchantAddress"), read_results) @@ -341,7 +310,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual.fields["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(returned_model, read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py index 3c95d8cf8d9d..c5c0e3665e04 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py @@ -9,7 +9,7 @@ from azure.core.exceptions import HttpResponseError, ServiceRequestError, ClientAuthenticationError from azure.core.credentials import AzureKeyCredential from azure.ai.formrecognizer._generated.models import AnalyzeOperationResult -from azure.ai.formrecognizer._response_handlers import prepare_us_receipt +from azure.ai.formrecognizer._response_handlers import prepare_receipt from azure.ai.formrecognizer.aio import FormRecognizerClient from testcase import GlobalFormRecognizerAccountPreparer from asynctestcase import AsyncFormRecognizerTest @@ -75,7 +75,7 @@ async def test_receipt_url_transform_jpg(self, resource_group, location, form_re def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -92,17 +92,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -123,7 +112,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -136,7 +125,7 @@ async def test_receipt_url_transform_png(self, resource_group, location, form_re def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -153,17 +142,6 @@ def callback(raw_response, _, headers): read_results = raw_response.analyze_result.read_results document_results = raw_response.analyze_result.document_results - # check hardcoded values - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.get("TransactionTime"), read_results) - # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.get("MerchantAddress"), read_results) self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantName"), actual.get("MerchantName"), read_results) @@ -184,7 +162,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) # Check page metadata self.assertFormPagesTransformCorrect(receipt.pages, read_results) @@ -203,7 +181,7 @@ async def test_receipt_url_include_text_content(self, resource_group, location, self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type", "receipt_locale"]: + if field not in ["receipt_type", "page_range", "pages", "fields", "form_type"]: field = getattr(receipt, field) self.assertTextContentHasValues(field.value_data.text_content, receipt.page_range.first_page_number) @@ -220,21 +198,21 @@ async def test_receipt_url_jpg(self, resource_group, location, form_recognizer_a self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.merchant_phone_number.value, '+19876543210') - self.assertEqual(receipt.subtotal.value, 11.7) - self.assertEqual(receipt.tax.value, 1.17) - self.assertEqual(receipt.tip.value, 1.63) - self.assertEqual(receipt.total.value, 14.5) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+19876543210') + self.assertEqual(receipt.fields.get("Subtotal").value, 11.7) + self.assertEqual(receipt.fields.get("Tax").value, 1.17) + self.assertEqual(receipt.fields.get("Tip").value, 1.63) + self.assertEqual(receipt.fields.get("Total").value, 14.5) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized') - self.assertReceiptItemsHasValues(receipt.receipt_items, receipt.page_range.first_page_number, False) + self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() async def test_receipt_url_png(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -244,13 +222,13 @@ async def test_receipt_url_png(self, resource_group, location, form_recognizer_a self.assertEqual(len(result), 1) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Main Street Redmond, WA 98052') - self.assertEqual(receipt.merchant_name.value, 'Contoso Contoso') - self.assertEqual(receipt.subtotal.value, 1098.99) - self.assertEqual(receipt.tax.value, 104.4) - self.assertEqual(receipt.total.value, 1203.39) - self.assertEqual(receipt.transaction_date.value, date(year=2019, month=6, day=10)) - self.assertEqual(receipt.transaction_time.value, time(hour=13, minute=59, second=0)) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Main Street Redmond, WA 98052') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Contoso Contoso') + self.assertEqual(receipt.fields.get("Subtotal").value, 1098.99) + self.assertEqual(receipt.fields.get("Tax").value, 104.4) + self.assertEqual(receipt.fields.get("Total").value, 1203.39) + self.assertEqual(receipt.fields.get("TransactionDate").value, date(year=2019, month=6, day=10)) + self.assertEqual(receipt.fields.get("TransactionTime").value, time(hour=13, minute=59, second=0)) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) @@ -265,22 +243,22 @@ async def test_receipt_multipage_url(self, resource_group, location, form_recogn self.assertEqual(len(result), 3) receipt = result[0] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Bilbo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 300.0) - self.assertEqual(receipt.total.value, 100.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Bilbo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 300.0) + self.assertEqual(receipt.fields.get("Total").value, 100.0) self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) self.assertIsNotNone(receipt.receipt_type.confidence) self.assertEqual(receipt.receipt_type.type, 'Itemized') receipt = result[2] - self.assertEqual(receipt.merchant_address.value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') - self.assertEqual(receipt.merchant_name.value, 'Frodo Baggins') - self.assertEqual(receipt.merchant_phone_number.value, '+15555555555') - self.assertEqual(receipt.subtotal.value, 3000.0) - self.assertEqual(receipt.total.value, 1000.0) + self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') + self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') + self.assertEqual(receipt.fields.get("MerchantPhoneNumber").value, '+15555555555') + self.assertEqual(receipt.fields.get("Subtotal").value, 3000.0) + self.assertEqual(receipt.fields.get("Total").value, 1000.0) self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) @@ -295,7 +273,7 @@ async def test_receipt_multipage_transform_url(self, resource_group, location, f def callback(raw_response, _, headers): analyze_result = client._client._deserialize(AnalyzeOperationResult, raw_response) - extracted_receipt = prepare_us_receipt(analyze_result) + extracted_receipt = prepare_receipt(analyze_result) responses.append(analyze_result) responses.append(extracted_receipt) @@ -316,15 +294,6 @@ def callback(raw_response, _, headers): for receipt, actual in zip(returned_model, actual): if actual.fields is None: # second page is blank continue - self.assertFormFieldTransformCorrect(receipt.merchant_address, actual.fields.get("MerchantAddress"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_name, actual.fields.get("MerchantName"), read_results) - self.assertFormFieldTransformCorrect(receipt.merchant_phone_number, actual.fields.get("MerchantPhoneNumber"), read_results) - self.assertFormFieldTransformCorrect(receipt.subtotal, actual.fields.get("Subtotal"), read_results) - self.assertFormFieldTransformCorrect(receipt.tax, actual.fields.get("Tax"), read_results) - self.assertFormFieldTransformCorrect(receipt.tip, actual.fields.get("Tip"), read_results) - self.assertFormFieldTransformCorrect(receipt.total, actual.fields.get("Total"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_date, actual.fields.get("TransactionDate"), read_results) - self.assertFormFieldTransformCorrect(receipt.transaction_time, actual.fields.get("TransactionTime"), read_results) # check dict values self.assertFormFieldTransformCorrect(receipt.fields.get("MerchantAddress"), actual.fields.get("MerchantAddress"), read_results) @@ -346,7 +315,7 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) # check receipt items - self.assertReceiptItemsTransformCorrect(receipt.receipt_items, actual.fields["Items"], read_results) + self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) # Check form pages self.assertFormPagesTransformCorrect(returned_model, read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py index ee4ab885f386..2939275bcedc 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py @@ -102,13 +102,6 @@ def us_receipt_type(): assert repr(model) == model_repr return model, model_repr -@pytest.fixture -def us_receipt_item(form_field_two): - model = _models.USReceiptItem(name=form_field_two[0], quantity=form_field_two[0], price=form_field_two[0], total_price=form_field_two[0]) - model_repr = "USReceiptItem(name={}, quantity={}, price={}, total_price={})".format(form_field_two[1], form_field_two[1], form_field_two[1], form_field_two[1])[:1024] - assert repr(model) == model_repr - return model, model_repr - @pytest.fixture def custom_form_model_field(): model = _models.CustomFormModelField(label="label", name="name", accuracy=0.99) @@ -141,7 +134,7 @@ def training_document_info(form_recognizer_error): class TestRepr(): # Not inheriting form FormRecognizerTest because that doesn't allow me to define pytest fixtures in the same file # Not worth moving pytest fixture definitions to conftest since all I would use is assertEqual and I can just use assert - def test_recognized_form(self, form_field_one, page_range, form_page, us_receipt_type, us_receipt_item): + def test_recognized_form(self, form_field_one, page_range, form_page, us_receipt_type): model = _models.RecognizedForm(form_type="receipt", fields={"one": form_field_one[0]}, page_range=page_range[0], pages=[form_page[0]]) model_repr = "RecognizedForm(form_type=receipt, fields={{'one': {}}}, page_range={}, pages=[{}])".format( form_field_one[1], page_range[1], form_page[1] @@ -151,55 +144,12 @@ def test_recognized_form(self, form_field_one, page_range, form_page, us_receipt def test_recognized_receipt(self, form_field_one, page_range, form_page, us_receipt_type): model = _models.RecognizedReceipt( form_type="receipt", fields={"one": form_field_one[0]}, page_range=page_range[0], pages=[form_page[0]], - receipt_type=us_receipt_type[0], receipt_locale="en-US") + receipt_type=us_receipt_type[0]) model_repr = "RecognizedReceipt(form_type=receipt, fields={{'one': {}}}, page_range={}, pages=[{}])".format( - form_field_one[1], page_range[1], form_page[1], us_receipt_type[0], "en-US" + form_field_one[1], page_range[1], form_page[1], us_receipt_type[0] )[:1024] assert repr(model) == model_repr - def test_us_receipt(self, form_field_one, form_field_two, us_receipt_type, us_receipt_item, page_range, form_page): - model = _models.USReceipt( - merchant_address=form_field_one[0], - merchant_name=form_field_two[0], - merchant_phone_number=form_field_one[0], - receipt_type=us_receipt_type[0], - receipt_items=[us_receipt_item[0], us_receipt_item[0]], - subtotal=form_field_two[0], - tax=form_field_one[0], - tip=form_field_two[0], - total=form_field_one[0], - transaction_date=form_field_two[0], - transaction_time=form_field_one[0], - fields={ - "one": form_field_one[0] - }, - page_range=page_range[0], - pages=[form_page[0]], - form_type="test", - receipt_locale="en-US" - ) - model_repr="USReceipt(merchant_address={}, merchant_name={}, merchant_phone_number={}, receipt_type={}, receipt_items=[{}, {}], subtotal={}, " \ - "tax={}, tip={}, total={}, transaction_date={}, transaction_time={}, fields={{'one': {}}}, page_range={}, pages=[{}], " \ - "form_type=test, receipt_locale=en-US)".format( - form_field_one[1], - form_field_two[1], - form_field_one[1], - us_receipt_type[1], - us_receipt_item[1], - us_receipt_item[1], - form_field_two[1], - form_field_one[1], - form_field_two[1], - form_field_one[1], - form_field_two[1], - form_field_one[1], - form_field_one[1], - page_range[1], - form_page[1] - )[:1024] - - - assert repr(model) == model_repr def test_custom_form_model(self, custom_form_sub_model, form_recognizer_error, training_document_info): model = _models.CustomFormModel( diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py index bdc95253117a..caeb23bb5a67 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/testcase.py @@ -262,10 +262,10 @@ def assertReceiptItemsTransformCorrect(self, items, actual_items, read_results=N actual = actual_items.value_array for r, a in zip(items, actual): - self.assertFormFieldTransformCorrect(r.name, a.value_object.get("Name"), read_results) - self.assertFormFieldTransformCorrect(r.quantity, a.value_object.get("Quantity"), read_results) - self.assertFormFieldTransformCorrect(r.total_price, a.value_object.get("TotalPrice"), read_results) - self.assertFormFieldTransformCorrect(r.price, a.value_object.get("Price"), read_results) + self.assertFormFieldTransformCorrect(r.value.get("Name"), a.value_object.get("Name"), read_results) + self.assertFormFieldTransformCorrect(r.value.get("Quantity"), a.value_object.get("Quantity"), read_results) + self.assertFormFieldTransformCorrect(r.value.get("TotalPrice"), a.value_object.get("TotalPrice"), read_results) + self.assertFormFieldTransformCorrect(r.value.get("Price"), a.value_object.get("Price"), read_results) def assertTablesTransformCorrect(self, layout, actual_layout, read_results=None, **kwargs): for table, actual_table in zip(layout, actual_layout): @@ -287,24 +287,24 @@ def assertTablesTransformCorrect(self, layout, actual_layout, read_results=None, def assertReceiptItemsHasValues(self, items, page_number, include_text_content): for item in items: - self.assertBoundingBoxHasPoints(item.name.value_data.bounding_box) - self.assertIsNotNone(item.name.confidence) - self.assertIsNotNone(item.name.value_data.text) - self.assertBoundingBoxHasPoints(item.quantity.value_data.bounding_box) - self.assertIsNotNone(item.quantity.confidence) - self.assertIsNotNone(item.quantity.value_data.text) - self.assertBoundingBoxHasPoints(item.total_price.value_data.bounding_box) - self.assertIsNotNone(item.total_price.confidence) - self.assertIsNotNone(item.total_price.value_data.text) + self.assertBoundingBoxHasPoints(item.value.get("Name").value_data.bounding_box) + self.assertIsNotNone(item.value.get("Name").confidence) + self.assertIsNotNone(item.value.get("Name").value_data.text) + self.assertBoundingBoxHasPoints(item.value.get("Quantity").value_data.bounding_box) + self.assertIsNotNone(item.value.get("Quantity").confidence) + self.assertIsNotNone(item.value.get("Quantity").value_data.text) + self.assertBoundingBoxHasPoints(item.value.get("TotalPrice").value_data.bounding_box) + self.assertIsNotNone(item.value.get("TotalPrice").confidence) + self.assertIsNotNone(item.value.get("TotalPrice").value_data.text) if include_text_content: - self.assertTextContentHasValues(item.name.value_data.text_content, page_number) - self.assertTextContentHasValues(item.name.value_data.text_content, page_number) - self.assertTextContentHasValues(item.name.value_data.text_content, page_number) + self.assertTextContentHasValues(item.value.get("Name").value_data.text_content, page_number) + self.assertTextContentHasValues(item.value.get("Name").value_data.text_content, page_number) + self.assertTextContentHasValues(item.value.get("Name").value_data.text_content, page_number) else: - self.assertIsNone(item.name.value_data.text_content) - self.assertIsNone(item.name.value_data.text_content) - self.assertIsNone(item.name.value_data.text_content) + self.assertIsNone(item.value.get("Name").value_data.text_content) + self.assertIsNone(item.value.get("Name").value_data.text_content) + self.assertIsNone(item.value.get("Name").value_data.text_content) def assertBoundingBoxHasPoints(self, box): if box is None: From b3d68f69094713aec8b40446438c6c10d5195836 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Tue, 2 Jun 2020 18:41:05 -0400 Subject: [PATCH 03/11] updated samples --- .../azure-ai-formrecognizer/README.md | 47 +++++++++++++------ .../sample_recognize_receipts_async.py | 42 ++++++++++++----- ...ample_recognize_receipts_from_url_async.py | 42 ++++++++++++----- .../samples/sample_recognize_receipts.py | 42 ++++++++++++----- .../sample_recognize_receipts_from_url.py | 42 ++++++++++++----- 5 files changed, 157 insertions(+), 58 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/README.md b/sdk/formrecognizer/azure-ai-formrecognizer/README.md index 6f562db53da0..1bf6db37712c 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/README.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/README.md @@ -227,21 +227,40 @@ with open("", "rb") as fd: poller = form_recognizer_client.begin_recognize_receipts(receipt) result = poller.result() -r = result[0] -print("Receipt contained the following values with confidences: ") -print("Receipt Type: {} has confidence: {}".format(r.receipt_type.type, r.receipt_type.confidence)) -print("Merchant Name: {} has confidence: {}".format(r.merchant_name.value, r.merchant_name.confidence)) -print("Transaction Date: {} has confidence: {}".format(r.transaction_date.value, r.transaction_date.confidence)) +receipt = result[0] +print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) +merchant_name = receipt.fields.get("MerchantName") +if merchant_name: + print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) +transaction_date = receipt.fields.get("TransactionDate") +if transaction_date: + print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") -for item in r.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) -print("Subtotal: {} has confidence: {}".format(r.subtotal.value, r.subtotal.confidence)) -print("Tax: {} has confidence: {}".format(r.tax.value, r.tax.confidence)) -print("Tip: {} has confidence: {}".format(r.tip.value, r.tip.confidence)) -print("Total: {} has confidence: {}".format(r.total.value, r.total.confidence)) +for item in receipt.fields.get("Items").value: + item_name = item.value.get("Name") + if item_name: + print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + item_quantity = item.value.get("Quantity") + if item_quantity: + print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + item_price = item.value.get("Price") + if item_price: + print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + item_total_price = item.value.get("TotalPrice") + if item_total_price: + print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) +subtotal = receipt.fields.get("Subtotal") +if subtotal: + print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) +tax = receipt.fields.get("Tax") +if tax: + print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) +tip = receipt.fields.get("Tip") +if tip: + print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) +total = receipt.fields.get("Total") +if total: + print("Total: {} has confidence: {}".format(total.value, total.confidence)) ``` ### Train a model diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py index 2e41b244a5e3..582b2ed70d74 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py @@ -46,18 +46,38 @@ async def recognize_receipts(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) - print("Merchant Name: {} has confidence: {}".format(receipt.merchant_name.value, receipt.merchant_name.confidence)) - print("Transaction Date: {} has confidence: {}".format(receipt.transaction_date.value, receipt.transaction_date.confidence)) + merchant_name = receipt.fields.get("MerchantName") + if merchant_name: + print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) + transaction_date = receipt.fields.get("TransactionDate") + if transaction_date: + print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) - print("Subtotal: {} has confidence: {}".format(receipt.subtotal.value, receipt.subtotal.confidence)) - print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) - print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) - print("Total: {} has confidence: {}".format(receipt.total.value, receipt.total.confidence)) + for item in receipt.fields.get("Items").value: + item_name = item.value.get("Name") + if item_name: + print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + item_quantity = item.value.get("Quantity") + if item_quantity: + print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + item_price = item.value.get("Price") + if item_price: + print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + item_total_price = item.value.get("TotalPrice") + if item_total_price: + print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + subtotal = receipt.fields.get("Subtotal") + if subtotal: + print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) + tax = receipt.fields.get("Tax") + if tax: + print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) + tip = receipt.fields.get("Tip") + if tip: + print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) + total = receipt.fields.get("Total") + if total: + print("Total: {} has confidence: {}".format(total.value, total.confidence)) print("--------------------------------------") # [END recognize_receipts_async] diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py index 8831e076fc69..476ca762265e 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py @@ -43,18 +43,38 @@ async def recognize_receipts_from_url(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) - print("Merchant Name: {} has confidence: {}".format(receipt.merchant_name.value, receipt.merchant_name.confidence)) - print("Transaction Date: {} has confidence: {}".format(receipt.transaction_date.value, receipt.transaction_date.confidence)) + merchant_name = receipt.fields.get("MerchantName") + if merchant_name: + print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) + transaction_date = receipt.fields.get("TransactionDate") + if transaction_date: + print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) - print("Subtotal: {} has confidence: {}".format(receipt.subtotal.value, receipt.subtotal.confidence)) - print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) - print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) - print("Total: {} has confidence: {}".format(receipt.total.value, receipt.total.confidence)) + for item in receipt.fields.get("Items").value: + item_name = item.value.get("Name") + if item_name: + print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + item_quantity = item.value.get("Quantity") + if item_quantity: + print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + item_price = item.value.get("Price") + if item_price: + print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + item_total_price = item.value.get("TotalPrice") + if item_total_price: + print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + subtotal = receipt.fields.get("Subtotal") + if subtotal: + print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) + tax = receipt.fields.get("Tax") + if tax: + print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) + tip = receipt.fields.get("Tip") + if tip: + print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) + total = receipt.fields.get("Total") + if total: + print("Total: {} has confidence: {}".format(total.value, total.confidence)) print("--------------------------------------") # [END recognize_receipts_from_url_async] diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py index b321ef76553e..193d44b72fdb 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py @@ -43,18 +43,38 @@ def recognize_receipts(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) - print("Merchant Name: {} has confidence: {}".format(receipt.merchant_name.value, receipt.merchant_name.confidence)) - print("Transaction Date: {} has confidence: {}".format(receipt.transaction_date.value, receipt.transaction_date.confidence)) + merchant_name = receipt.fields.get("MerchantName") + if merchant_name: + print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) + transaction_date = receipt.fields.get("TransactionDate") + if transaction_date: + print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) - print("Subtotal: {} has confidence: {}".format(receipt.subtotal.value, receipt.subtotal.confidence)) - print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) - print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) - print("Total: {} has confidence: {}".format(receipt.total.value, receipt.total.confidence)) + for item in receipt.fields.get("Items").value: + item_name = item.value.get("Name") + if item_name: + print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + item_quantity = item.value.get("Quantity") + if item_quantity: + print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + item_price = item.value.get("Price") + if item_price: + print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + item_total_price = item.value.get("TotalPrice") + if item_total_price: + print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + subtotal = receipt.fields.get("Subtotal") + if subtotal: + print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) + tax = receipt.fields.get("Tax") + if tax: + print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) + tip = receipt.fields.get("Tip") + if tip: + print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) + total = receipt.fields.get("Total") + if total: + print("Total: {} has confidence: {}".format(total.value, total.confidence)) print("--------------------------------------") # [END recognize_receipts] diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py index 7ccd5c58586a..c7f81b5778f9 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py @@ -43,18 +43,38 @@ def recognize_receipts_from_url(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) - print("Merchant Name: {} has confidence: {}".format(receipt.merchant_name.value, receipt.merchant_name.confidence)) - print("Transaction Date: {} has confidence: {}".format(receipt.transaction_date.value, receipt.transaction_date.confidence)) + merchant_name = receipt.fields.get("MerchantName") + if merchant_name: + print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) + transaction_date = receipt.fields.get("TransactionDate") + if transaction_date: + print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.receipt_items: - print("...Item Name: {} has confidence: {}".format(item.name.value, item.name.confidence)) - print("...Item Quantity: {} has confidence: {}".format(item.quantity.value, item.quantity.confidence)) - print("...Individual Item Price: {} has confidence: {}".format(item.price.value, item.price.confidence)) - print("...Total Item Price: {} has confidence: {}".format(item.total_price.value, item.total_price.confidence)) - print("Subtotal: {} has confidence: {}".format(receipt.subtotal.value, receipt.subtotal.confidence)) - print("Tax: {} has confidence: {}".format(receipt.tax.value, receipt.tax.confidence)) - print("Tip: {} has confidence: {}".format(receipt.tip.value, receipt.tip.confidence)) - print("Total: {} has confidence: {}".format(receipt.total.value, receipt.total.confidence)) + for item in receipt.fields.get("Items").value: + item_name = item.value.get("Name") + if item_name: + print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + item_quantity = item.value.get("Quantity") + if item_quantity: + print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + item_price = item.value.get("Price") + if item_price: + print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + item_total_price = item.value.get("TotalPrice") + if item_total_price: + print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + subtotal = receipt.fields.get("Subtotal") + if subtotal: + print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) + tax = receipt.fields.get("Tax") + if tax: + print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) + tip = receipt.fields.get("Tip") + if tip: + print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) + total = receipt.fields.get("Total") + if total: + print("Total: {} has confidence: {}".format(total.value, total.confidence)) print("--------------------------------------") # [END recognize_receipts_from_url] From 8014fe361025424b718312583c5b60be8852669f Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 3 Jun 2020 11:27:12 -0400 Subject: [PATCH 04/11] removed ReceiptType class and receipt_type property --- .../azure-ai-formrecognizer/CHANGELOG.md | 1 + .../azure/ai/formrecognizer/__init__.py | 2 -- .../azure/ai/formrecognizer/_models.py | 35 ++----------------- .../ai/formrecognizer/_response_handlers.py | 2 -- 4 files changed, 3 insertions(+), 37 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md index 5617b4da42dd..a8a1fcd7e959 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md @@ -24,6 +24,7 @@ - `CustomFormSubModel` is renamed to `CustomFormSubmodel` - Removed `USReceipt`. To see how to deal with the return value of `begin_recognize_receipts`, see [sample_recognize_receipts.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py) or [sample_recognize_receipts_from_url.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py) for details. - Removed `USReceiptItem`. To see how to access the individual items on a receipt, see [sample_recognize_receipts.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py) or [sample_recognize_receipts_from_url.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py) for details. +- Removed `ReceiptType` and the `receipt_type` property from `RecogniedReceipt`. See sample_recognize_receipts.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py) or [sample_recognize_receipts_from_url.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py) for details. **New features** diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py index 0bde1bc93db1..dd86f0f42e41 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/__init__.py @@ -14,7 +14,6 @@ TrainingStatus, CustomFormModelStatus, FormContentType, - ReceiptType, FormTable, FormTableCell, TrainingDocumentInfo, @@ -43,7 +42,6 @@ 'CustomFormModelStatus', 'FormContentType', 'FormContent', - 'ReceiptType', 'FormTable', 'FormTableCell', 'TrainingDocumentInfo', diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py index b4e6a9e7b274..b15c7d89866b 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_models.py @@ -195,18 +195,11 @@ class RecognizedReceipt(RecognizedForm): :ivar list[~azure.ai.formrecognizer.FormPage] pages: A list of pages recognized from the input document. Contains lines, words, tables and page metadata. - :ivar ~azure.ai.formrecognizer.ReceiptType receipt_type: - The reciept type and confidence. """ - def __init__(self, **kwargs): - super(RecognizedReceipt, self).__init__(**kwargs) - self.receipt_type = kwargs.get("receipt_type", None) def __repr__(self): - return "RecognizedReceipt(form_type={}, fields={}, page_range={}, pages={}, " \ - "receipt_type={})".format( - self.form_type, repr(self.fields), repr(self.page_range), repr(self.pages), - repr(self.receipt_type) + return "RecognizedReceipt(form_type={}, fields={}, page_range={}, pages={})".format( + self.form_type, repr(self.fields), repr(self.page_range), repr(self.pages) )[:1024] @@ -451,30 +444,6 @@ def __repr__(self): )[:1024] -class ReceiptType(object): - """The type of the analyzed US receipt and the confidence - value of that type. - - :ivar str type: The type of the receipt. For example, "Itemized", - "CreditCard", "Gas", "Parking", "Gas", "Other". - :ivar float confidence: - Measures the degree of certainty of the recognition result. Value is between [0.0, 1.0]. - """ - - def __init__(self, **kwargs): - self.type = kwargs.get("type", None) - self.confidence = kwargs.get("confidence", None) - - @classmethod - def _from_generated(cls, item): - return cls( - type=item.value_string, - confidence=adjust_confidence(item.confidence)) if item else None - - def __repr__(self): - return "ReceiptType(type={}, confidence={})".format(self.type, self.confidence)[:1024] - - class FormTable(object): """Information about the extracted table contained on a page. diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py index d9a2cccd2d17..90fe97582902 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/azure/ai/formrecognizer/_response_handlers.py @@ -7,7 +7,6 @@ # pylint: disable=protected-access from ._models import ( - ReceiptType, FormField, FormPage, FormLine, @@ -35,7 +34,6 @@ def prepare_receipt(response): receipts.append(receipt) continue receipt = RecognizedReceipt( - receipt_type=ReceiptType._from_generated(page.fields.get("ReceiptType")), page_range=FormPageRange( first_page_number=page.page_range[0], last_page_number=page.page_range[1] ), From 704ee27c76bbe8b199ad55402e49033af04d7ef0 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 3 Jun 2020 11:27:29 -0400 Subject: [PATCH 05/11] updated tests for ReceiptType and receipt_type removal --- .../tests/test_receipt.py | 37 +++++++++++-------- .../tests/test_receipt_async.py | 37 +++++++++++-------- .../tests/test_receipt_from_url.py | 37 +++++++++++-------- .../tests/test_receipt_from_url_async.py | 37 +++++++++++-------- .../tests/test_repr.py | 16 ++------ 5 files changed, 92 insertions(+), 72 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py index 9192169c4b23..c1296b292cbb 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt.py @@ -173,8 +173,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) @@ -228,8 +229,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) @@ -261,8 +263,9 @@ def test_receipt_jpg(self, resource_group, location, form_recognizer_account, fo self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') self.assertReceiptItemsHasValues(receipt.fields['Items'].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() @@ -285,8 +288,9 @@ def test_receipt_png(self, resource_group, location, form_recognizer_account, fo self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() def test_receipt_jpg_include_text_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -301,7 +305,7 @@ def test_receipt_jpg_include_text_content(self, resource_group, location, form_r self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type"]: + if field not in ["receipt_items", "page_range", "pages", "fields", "form_type"]: form_field = getattr(receipt, field) self.assertTextContentHasValues(form_field.value_data.text_content, receipt.page_range.first_page_number) @@ -326,8 +330,9 @@ def test_receipt_multipage(self, resource_group, location, form_recognizer_accou self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') receipt = result[2] self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') @@ -337,8 +342,9 @@ def test_receipt_multipage(self, resource_group, location, form_recognizer_accou self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() def test_receipt_multipage_transform(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -390,8 +396,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, actual.page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual.fields["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual.fields["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual.fields["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py index d0f71f0698b7..3cfd22fc875d 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_async.py @@ -171,8 +171,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) @@ -225,8 +226,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) @@ -257,8 +259,9 @@ async def test_receipt_jpg(self, resource_group, location, form_recognizer_accou self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() @@ -281,8 +284,9 @@ async def test_receipt_png(self, resource_group, location, form_recognizer_accou self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() async def test_receipt_jpg_include_text_content(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -296,7 +300,7 @@ async def test_receipt_jpg_include_text_content(self, resource_group, location, self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type"]: + if field not in ["receipt_items", "page_range", "pages", "fields", "form_type"]: form_field = getattr(receipt, field) self.assertTextContentHasValues(form_field.value_data.text_content, receipt.page_range.first_page_number) @@ -320,8 +324,9 @@ async def test_receipt_multipage(self, resource_group, location, form_recognizer self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') receipt = result[2] self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') @@ -331,8 +336,9 @@ async def test_receipt_multipage(self, resource_group, location, form_recognizer self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() async def test_receipt_multipage_transform(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -383,8 +389,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, actual.page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual.fields["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual.fields["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual.fields["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py index 75c28c5b0c9a..2ab8315d9077 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url.py @@ -99,8 +99,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) @@ -150,8 +151,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) @@ -174,7 +176,7 @@ def test_receipt_url_include_text_content(self, resource_group, location, form_r self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "receipt_items", "page_range", "pages", "fields", "form_type"]: + if field not in ["receipt_items", "page_range", "pages", "fields", "form_type"]: field = getattr(receipt, field) self.assertTextContentHasValues(field.value_data.text_content, receipt.page_range.first_page_number) @@ -202,8 +204,9 @@ def test_receipt_url_jpg(self, resource_group, location, form_recognizer_account self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() @@ -225,8 +228,9 @@ def test_receipt_url_png(self, resource_group, location, form_recognizer_account self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() def test_receipt_multipage_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -245,8 +249,9 @@ def test_receipt_multipage_url(self, resource_group, location, form_recognizer_a self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') receipt = result[2] self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') @@ -256,8 +261,9 @@ def test_receipt_multipage_url(self, resource_group, location, form_recognizer_a self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() def test_receipt_multipage_transform_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -306,8 +312,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, actual.page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual.fields["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual.fields["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual.fields["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py index c5c0e3665e04..d3981db07f21 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_receipt_from_url_async.py @@ -108,8 +108,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) @@ -158,8 +159,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, document_results[0].page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual["Items"], read_results) @@ -181,7 +183,7 @@ async def test_receipt_url_include_text_content(self, resource_group, location, self.assertFormPagesHasValues(receipt.pages) for field, value in receipt.__dict__.items(): - if field not in ["receipt_type", "page_range", "pages", "fields", "form_type"]: + if field not in ["page_range", "pages", "fields", "form_type"]: field = getattr(receipt, field) self.assertTextContentHasValues(field.value_data.text_content, receipt.page_range.first_page_number) @@ -210,8 +212,9 @@ async def test_receipt_url_jpg(self, resource_group, location, form_recognizer_a self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') self.assertReceiptItemsHasValues(receipt.fields["Items"].value, receipt.page_range.first_page_number, False) @GlobalFormRecognizerAccountPreparer() @@ -232,8 +235,9 @@ async def test_receipt_url_png(self, resource_group, location, form_recognizer_a self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() async def test_receipt_multipage_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -251,8 +255,9 @@ async def test_receipt_multipage_url(self, resource_group, location, form_recogn self.assertEqual(receipt.page_range.first_page_number, 1) self.assertEqual(receipt.page_range.last_page_number, 1) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') receipt = result[2] self.assertEqual(receipt.fields.get("MerchantAddress").value, '123 Hobbit Lane 567 Main St. Redmond, WA Redmond, WA') self.assertEqual(receipt.fields.get("MerchantName").value, 'Frodo Baggins') @@ -262,8 +267,9 @@ async def test_receipt_multipage_url(self, resource_group, location, form_recogn self.assertEqual(receipt.page_range.first_page_number, 3) self.assertEqual(receipt.page_range.last_page_number, 3) self.assertFormPagesHasValues(receipt.pages) - self.assertIsNotNone(receipt.receipt_type.confidence) - self.assertEqual(receipt.receipt_type.type, 'Itemized') + receipt_type = receipt.fields.get("ReceiptType") + self.assertIsNotNone(receipt_type.confidence) + self.assertEqual(receipt_type.value, 'Itemized') @GlobalFormRecognizerAccountPreparer() async def test_receipt_multipage_transform_url(self, resource_group, location, form_recognizer_account, form_recognizer_account_key): @@ -311,8 +317,9 @@ def callback(raw_response, _, headers): self.assertEqual(receipt.page_range.last_page_number, actual.page_range[1]) # check receipt type - self.assertEqual(receipt.receipt_type.confidence, actual.fields["ReceiptType"].confidence) - self.assertEqual(receipt.receipt_type.type, actual.fields["ReceiptType"].value_string) + receipt_type = receipt.fields.get("ReceiptType") + self.assertEqual(receipt_type.confidence, actual.fields["ReceiptType"].confidence) + self.assertEqual(receipt_type.value, actual.fields["ReceiptType"].value_string) # check receipt items self.assertReceiptItemsTransformCorrect(receipt.fields["Items"].value, actual.fields["Items"], read_results) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py index 2939275bcedc..9510f300bc59 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/tests/test_repr.py @@ -95,13 +95,6 @@ def form_page(form_table, form_line): assert repr(model) == model_repr return model, model_repr -@pytest.fixture -def us_receipt_type(): - model = _models.ReceiptType(type="Itemized", confidence=1.0) - model_repr = "ReceiptType(type=Itemized, confidence=1.0)" - assert repr(model) == model_repr - return model, model_repr - @pytest.fixture def custom_form_model_field(): model = _models.CustomFormModelField(label="label", name="name", accuracy=0.99) @@ -134,19 +127,18 @@ def training_document_info(form_recognizer_error): class TestRepr(): # Not inheriting form FormRecognizerTest because that doesn't allow me to define pytest fixtures in the same file # Not worth moving pytest fixture definitions to conftest since all I would use is assertEqual and I can just use assert - def test_recognized_form(self, form_field_one, page_range, form_page, us_receipt_type): + def test_recognized_form(self, form_field_one, page_range, form_page): model = _models.RecognizedForm(form_type="receipt", fields={"one": form_field_one[0]}, page_range=page_range[0], pages=[form_page[0]]) model_repr = "RecognizedForm(form_type=receipt, fields={{'one': {}}}, page_range={}, pages=[{}])".format( form_field_one[1], page_range[1], form_page[1] )[:1024] assert repr(model) == model_repr - def test_recognized_receipt(self, form_field_one, page_range, form_page, us_receipt_type): + def test_recognized_receipt(self, form_field_one, page_range, form_page): model = _models.RecognizedReceipt( - form_type="receipt", fields={"one": form_field_one[0]}, page_range=page_range[0], pages=[form_page[0]], - receipt_type=us_receipt_type[0]) + form_type="receipt", fields={"one": form_field_one[0]}, page_range=page_range[0], pages=[form_page[0]]) model_repr = "RecognizedReceipt(form_type=receipt, fields={{'one': {}}}, page_range={}, pages=[{}])".format( - form_field_one[1], page_range[1], form_page[1], us_receipt_type[0] + form_field_one[1], page_range[1], form_page[1] )[:1024] assert repr(model) == model_repr From 7e2a3f8cc1a2399d030e960519dc4cbd6b288300 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 3 Jun 2020 11:27:40 -0400 Subject: [PATCH 06/11] updated samples for ReceiptType and receipt_type removal --- sdk/formrecognizer/azure-ai-formrecognizer/README.md | 4 +++- .../samples/async_samples/sample_recognize_receipts_async.py | 4 +++- .../async_samples/sample_recognize_receipts_from_url_async.py | 4 +++- .../samples/sample_recognize_receipts.py | 4 +++- .../samples/sample_recognize_receipts_from_url.py | 4 +++- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/README.md b/sdk/formrecognizer/azure-ai-formrecognizer/README.md index 1bf6db37712c..0bacf6cbd261 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/README.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/README.md @@ -228,7 +228,9 @@ poller = form_recognizer_client.begin_recognize_receipts(receipt) result = poller.result() receipt = result[0] -print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) +receipt_type = receipt.fields.get("ReceiptType") +if receipt_type: + print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) merchant_name = receipt.fields.get("MerchantName") if merchant_name: print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py index 582b2ed70d74..911f99c0f75f 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py @@ -45,7 +45,9 @@ async def recognize_receipts(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) - print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) + receipt_type = receipt.fields.get("ReceiptType") + if receipt_type: + print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) merchant_name = receipt.fields.get("MerchantName") if merchant_name: print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py index 476ca762265e..ce36ed9121af 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py @@ -42,7 +42,9 @@ async def recognize_receipts_from_url(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) - print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) + receipt_type = receipt.fields.get("ReceiptType") + if receipt_type: + print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) merchant_name = receipt.fields.get("MerchantName") if merchant_name: print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py index 193d44b72fdb..d8fe5b565955 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py @@ -42,7 +42,9 @@ def recognize_receipts(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) - print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) + receipt_type = receipt.fields.get("ReceiptType") + if receipt_type: + print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) merchant_name = receipt.fields.get("MerchantName") if merchant_name: print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py index c7f81b5778f9..5d952130ddc2 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py @@ -42,7 +42,9 @@ def recognize_receipts_from_url(self): for idx, receipt in enumerate(receipts): print("--------Recognizing receipt #{}--------".format(idx)) - print("Receipt Type: {} has confidence: {}".format(receipt.receipt_type.type, receipt.receipt_type.confidence)) + receipt_type = receipt.fields.get("ReceiptType") + if receipt_type: + print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) merchant_name = receipt.fields.get("MerchantName") if merchant_name: print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) From 655efeede941a4708751859b7b2e7042a4bd9383 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 3 Jun 2020 11:49:14 -0400 Subject: [PATCH 07/11] updated changelog to link to samples directory --- sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md index a8a1fcd7e959..d3d986e88926 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md @@ -22,9 +22,9 @@ `CustomFormModel` and `CustomFormModelInfo` models. - `models` property of `CustomFormModel` is renamed to `submodels` - `CustomFormSubModel` is renamed to `CustomFormSubmodel` -- Removed `USReceipt`. To see how to deal with the return value of `begin_recognize_receipts`, see [sample_recognize_receipts.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py) or [sample_recognize_receipts_from_url.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py) for details. -- Removed `USReceiptItem`. To see how to access the individual items on a receipt, see [sample_recognize_receipts.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py) or [sample_recognize_receipts_from_url.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py) for details. -- Removed `ReceiptType` and the `receipt_type` property from `RecogniedReceipt`. See sample_recognize_receipts.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py) or [sample_recognize_receipts_from_url.py](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py) for details. +- Removed `USReceipt`. To see how to deal with the return value of `begin_recognize_receipts`, see the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. +- Removed `USReceiptItem`. To see how to access the individual items on a receipt, see the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. +- Removed `ReceiptType` and the `receipt_type` property from `RecogniedReceipt`. See the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. **New features** From 926788446182d6093462cfb961a9727510ba69f2 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 3 Jun 2020 11:50:09 -0400 Subject: [PATCH 08/11] update recognize receipts sample to loop through fields --- .../azure-ai-formrecognizer/README.md | 46 ++++--------------- 1 file changed, 10 insertions(+), 36 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/README.md b/sdk/formrecognizer/azure-ai-formrecognizer/README.md index 0bacf6cbd261..20c9f98d42e6 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/README.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/README.md @@ -227,42 +227,16 @@ with open("", "rb") as fd: poller = form_recognizer_client.begin_recognize_receipts(receipt) result = poller.result() -receipt = result[0] -receipt_type = receipt.fields.get("ReceiptType") -if receipt_type: - print("Receipt Type: {} has confidence: {}".format(receipt_type.value, receipt_type.confidence)) -merchant_name = receipt.fields.get("MerchantName") -if merchant_name: - print("Merchant Name: {} has confidence: {}".format(merchant_name.value, merchant_name.confidence)) -transaction_date = receipt.fields.get("TransactionDate") -if transaction_date: - print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) -print("Receipt items:") -for item in receipt.fields.get("Items").value: - item_name = item.value.get("Name") - if item_name: - print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) - item_quantity = item.value.get("Quantity") - if item_quantity: - print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) - item_price = item.value.get("Price") - if item_price: - print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) - item_total_price = item.value.get("TotalPrice") - if item_total_price: - print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) -subtotal = receipt.fields.get("Subtotal") -if subtotal: - print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) -tax = receipt.fields.get("Tax") -if tax: - print("Tax: {} has confidence: {}".format(tax.value, tax.confidence)) -tip = receipt.fields.get("Tip") -if tip: - print("Tip: {} has confidence: {}".format(tip.value, tip.confidence)) -total = receipt.fields.get("Total") -if total: - print("Total: {} has confidence: {}".format(total.value, total.confidence)) +for receipt in result: + for name, field in receipt.fields.items(): + if name == "Items": + print("Receipt Items:") + for idx, items in enumerate(field.value): + print("...Item #{}".format(idx)) + for item_name, item in items.value.items(): + print("......{}: {} has confidence {}".format(item_name, item.value, item.confidence)) + else: + print("{}: {} has confidence {}".format(name, field.value, field.confidence)) ``` ### Train a model From 93fd081aa98a6454da41f14a56ba7cb210ee35b6 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 3 Jun 2020 11:50:44 -0400 Subject: [PATCH 09/11] fix printing of receipt items in recognize receipt samples --- .../async_samples/sample_recognize_receipts_async.py | 11 ++++++----- .../sample_recognize_receipts_from_url_async.py | 11 ++++++----- .../samples/sample_recognize_receipts.py | 11 ++++++----- .../samples/sample_recognize_receipts_from_url.py | 11 ++++++----- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py index 911f99c0f75f..3bca0eb2549d 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_async.py @@ -55,19 +55,20 @@ async def recognize_receipts(self): if transaction_date: print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.fields.get("Items").value: + for idx, item in enumerate(receipt.fields.get("Items").value): + print("...Item #{}".format(idx)) item_name = item.value.get("Name") if item_name: - print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) item_quantity = item.value.get("Quantity") if item_quantity: - print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) item_price = item.value.get("Price") if item_price: - print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) item_total_price = item.value.get("TotalPrice") if item_total_price: - print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) subtotal = receipt.fields.get("Subtotal") if subtotal: print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py index ce36ed9121af..3559261094fb 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/async_samples/sample_recognize_receipts_from_url_async.py @@ -52,19 +52,20 @@ async def recognize_receipts_from_url(self): if transaction_date: print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.fields.get("Items").value: + for idx, item in enumerate(receipt.fields.get("Items").value): + print("...Item #{}".format(idx)) item_name = item.value.get("Name") if item_name: - print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) item_quantity = item.value.get("Quantity") if item_quantity: - print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) item_price = item.value.get("Price") if item_price: - print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) item_total_price = item.value.get("TotalPrice") if item_total_price: - print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) subtotal = receipt.fields.get("Subtotal") if subtotal: print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py index d8fe5b565955..a53c04497daa 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts.py @@ -52,19 +52,20 @@ def recognize_receipts(self): if transaction_date: print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.fields.get("Items").value: + for idx, item in enumerate(receipt.fields.get("Items").value): + print("...Item #{}".format(idx)) item_name = item.value.get("Name") if item_name: - print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) item_quantity = item.value.get("Quantity") if item_quantity: - print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) item_price = item.value.get("Price") if item_price: - print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) item_total_price = item.value.get("TotalPrice") if item_total_price: - print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) subtotal = receipt.fields.get("Subtotal") if subtotal: print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py index 5d952130ddc2..f3b333c4d7f1 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py +++ b/sdk/formrecognizer/azure-ai-formrecognizer/samples/sample_recognize_receipts_from_url.py @@ -52,19 +52,20 @@ def recognize_receipts_from_url(self): if transaction_date: print("Transaction Date: {} has confidence: {}".format(transaction_date.value, transaction_date.confidence)) print("Receipt items:") - for item in receipt.fields.get("Items").value: + for idx, item in enumerate(receipt.fields.get("Items").value): + print("...Item #{}".format(idx)) item_name = item.value.get("Name") if item_name: - print("...Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) + print("......Item Name: {} has confidence: {}".format(item_name.value, item_name.confidence)) item_quantity = item.value.get("Quantity") if item_quantity: - print("...Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) + print("......Item Quantity: {} has confidence: {}".format(item_quantity.value, item_quantity.confidence)) item_price = item.value.get("Price") if item_price: - print("...Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) + print("......Individual Item Price: {} has confidence: {}".format(item_price.value, item_price.confidence)) item_total_price = item.value.get("TotalPrice") if item_total_price: - print("...Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) + print("......Total Item Price: {} has confidence: {}".format(item_total_price.value, item_total_price.confidence)) subtotal = receipt.fields.get("Subtotal") if subtotal: print("Subtotal: {} has confidence: {}".format(subtotal.value, subtotal.confidence)) From e2eb8291c081f488ff4335f29833e1b659efbae6 Mon Sep 17 00:00:00 2001 From: iscai-msft <43154838+iscai-msft@users.noreply.github.com> Date: Wed, 3 Jun 2020 12:35:01 -0400 Subject: [PATCH 10/11] fix typo in changelog Co-authored-by: Krista Pratico --- sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md index d3d986e88926..6fe1439c6791 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/CHANGELOG.md @@ -24,7 +24,7 @@ - `CustomFormSubModel` is renamed to `CustomFormSubmodel` - Removed `USReceipt`. To see how to deal with the return value of `begin_recognize_receipts`, see the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. - Removed `USReceiptItem`. To see how to access the individual items on a receipt, see the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. -- Removed `ReceiptType` and the `receipt_type` property from `RecogniedReceipt`. See the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. +- Removed `ReceiptType` and the `receipt_type` property from `RecognizedReceipt`. See the recognize receipt samples in the [samples directory](https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/formrecognizer/azure-ai-formrecognizer/samples) for details. **New features** From eada10789cd2ca1167f75f0f8cf28dac8914acb7 Mon Sep 17 00:00:00 2001 From: iscai-msft Date: Wed, 3 Jun 2020 12:39:51 -0400 Subject: [PATCH 11/11] include link in readme to returned fields for receipt --- sdk/formrecognizer/azure-ai-formrecognizer/README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/sdk/formrecognizer/azure-ai-formrecognizer/README.md b/sdk/formrecognizer/azure-ai-formrecognizer/README.md index 20c9f98d42e6..a304148bd79a 100644 --- a/sdk/formrecognizer/azure-ai-formrecognizer/README.md +++ b/sdk/formrecognizer/azure-ai-formrecognizer/README.md @@ -210,7 +210,7 @@ for cell in table.cells: ``` ### Recognize Receipts -Recognize data from USA sales receipts using a prebuilt model. +Recognize data from USA sales receipts using a prebuilt model. [Here][service_recognize_receipt] are the fields the service returns for a recognized receipt. ```python from azure.ai.formrecognizer import FormRecognizerClient @@ -434,6 +434,7 @@ This project has adopted the [Microsoft Open Source Code of Conduct][code_of_con [cognitive_authentication_aad]: https://docs.microsoft.com/azure/cognitive-services/authentication#authenticate-with-azure-active-directory [azure_identity_credentials]: ../../identity/azure-identity#credentials [default_azure_credential]: ../../identity/azure-identity#defaultazurecredential +[service_recognize_receipt]: https://westus2.dev.cognitive.microsoft.com/docs/services/form-recognizer-api-v2-preview/operations/GetAnalyzeReceiptResult [cla]: https://cla.microsoft.com [code_of_conduct]: https://opensource.microsoft.com/codeofconduct/