Skip to content

Commit

Permalink
[formrecognizer] Remove business card ContactNames page_number workar…
Browse files Browse the repository at this point in the history
…ound (#15275)

* revert setting business card ContactNames page number now that service returns this value

* remove workaround in tests
  • Loading branch information
kristapratico authored Nov 12, 2020
1 parent 04d33d2 commit 6be8888
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ class FormRecognizerClient(FormRecognizerClientBase):
:caption: Creating the FormRecognizerClient with a token credential.
"""

def _prebuilt_callback(self, raw_response, _, headers, **kwargs): # pylint: disable=unused-argument
def _prebuilt_callback(self, raw_response, _, headers): # pylint: disable=unused-argument
analyze_result = self._deserialize(self._generated_models.AnalyzeOperationResult, raw_response)
return prepare_prebuilt_models(analyze_result, **kwargs)
return prepare_prebuilt_models(analyze_result)

@distributed_trace
def begin_recognize_receipts(self, receipt, **kwargs):
Expand Down Expand Up @@ -247,9 +247,7 @@ def begin_recognize_business_cards(
file_stream=business_card,
content_type=content_type,
include_text_details=include_field_elements,
cls=kwargs.pop("cls", lambda pipeline_response, _, response_headers: self._prebuilt_callback(
pipeline_response, _, response_headers, business_card=True
)),
cls=kwargs.pop("cls", self._prebuilt_callback),
polling=True,
**kwargs
)
Expand Down Expand Up @@ -298,9 +296,7 @@ def begin_recognize_business_cards_from_url(
return self._client.begin_analyze_business_card_async( # type: ignore
file_stream={"source": business_card_url},
include_text_details=include_field_elements,
cls=kwargs.pop("cls", lambda pipeline_response, _, response_headers: self._prebuilt_callback(
pipeline_response, _, response_headers, business_card=True
)),
cls=kwargs.pop("cls", self._prebuilt_callback),
polling=True,
**kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,18 +86,6 @@ def adjust_text_angle(text_angle):
return text_angle


def adjust_page_number(value):
"""Adjusts the page number on the business card field
`ContactNames` to be set to the page number value found on `FirstName`
"""
for val in value.value_array:
if val.value_object.get("FirstName", None) and val.value_object.get("LastName", None):
if val.value_object["FirstName"].page == val.value_object["LastName"].page:
page_number = val.value_object["FirstName"].page
val.page = page_number
return value


def get_authentication_policy(credential):
authentication_policy = None
if credential is None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
from ._helpers import (
adjust_value_type,
adjust_confidence,
adjust_page_number,
get_element
)

Expand All @@ -36,7 +35,7 @@ def resolve_element(element, read_result):
raise ValueError("Failed to parse element reference.")


def get_field_value(field, value, read_result, **kwargs): # pylint: disable=too-many-return-statements
def get_field_value(field, value, read_result): # pylint: disable=too-many-return-statements
if value is None:
return value
if value.type == "string":
Expand All @@ -52,16 +51,13 @@ def get_field_value(field, value, read_result, **kwargs): # pylint: disable=too
if value.type == "time":
return value.value_time
if value.type == "array":
# business cards pre-built model doesn't return a page number for the `ContactNames` field
if "business_card" in kwargs and field == "ContactNames":
value = adjust_page_number(value)
return [
FormField._from_generated(field, value, read_result, **kwargs)
FormField._from_generated(field, value, read_result)
for value in value.value_array
]
if value.type == "object":
return {
key: FormField._from_generated(key, value, read_result, **kwargs)
key: FormField._from_generated(key, value, read_result)
for key, value in value.value_object.items()
}
if value.type == "selectionMark":
Expand Down Expand Up @@ -251,12 +247,12 @@ def __init__(self, **kwargs):
self.confidence = kwargs.get("confidence", None)

@classmethod
def _from_generated(cls, field, value, read_result, **kwargs):
def _from_generated(cls, field, value, read_result):
return cls(
value_type=adjust_value_type(value.type) if value else None,
label_data=None, # not returned with receipt/supervised
value_data=FieldData._from_generated(value, read_result),
value=get_field_value(field, value, read_result, **kwargs),
value=get_field_value(field, value, read_result),
name=field,
confidence=adjust_confidence(value.confidence) if value else None,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
)


def prepare_prebuilt_models(response, **kwargs):
def prepare_prebuilt_models(response):
prebuilt_models = []
read_result = response.analyze_result.read_results
document_result = response.analyze_result.document_results
Expand All @@ -36,7 +36,7 @@ def prepare_prebuilt_models(response, **kwargs):
pages=form_page[page.page_range[0]-1:page.page_range[1]],
form_type=page.doc_type,
fields={
key: FormField._from_generated(key, value, read_result, **kwargs)
key: FormField._from_generated(key, value, read_result)
for key, value in page.fields.items()
} if page.fields else None,
form_type_confidence=doc_type_confidence,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,9 @@ class FormRecognizerClient(FormRecognizerClientBaseAsync):
:caption: Creating the FormRecognizerClient with a token credential.
"""

def _prebuilt_callback(self, raw_response, _, headers, **kwargs): # pylint: disable=unused-argument
def _prebuilt_callback(self, raw_response, _, headers): # pylint: disable=unused-argument
analyze_result = self._deserialize(self._generated_models.AnalyzeOperationResult, raw_response)
return prepare_prebuilt_models(analyze_result, **kwargs)
return prepare_prebuilt_models(analyze_result)

@distributed_trace_async
async def begin_recognize_receipts(
Expand Down Expand Up @@ -250,9 +250,7 @@ async def begin_recognize_business_cards(
file_stream=business_card,
content_type=content_type,
include_text_details=include_field_elements,
cls=kwargs.pop("cls", lambda pipeline_response, _, response_headers: self._prebuilt_callback(
pipeline_response, _, response_headers, business_card=True
)),
cls=kwargs.pop("cls", self._prebuilt_callback),
polling=True,
**kwargs
)
Expand Down Expand Up @@ -299,9 +297,7 @@ async def begin_recognize_business_cards_from_url(
return await self._client.begin_analyze_business_card_async( # type: ignore
file_stream={"source": business_card_url},
include_text_details=include_field_elements,
cls=kwargs.pop("cls", lambda pipeline_response, _, response_headers: self._prebuilt_callback(
pipeline_response, _, response_headers, business_card=True
)),
cls=kwargs.pop("cls", self._prebuilt_callback),
polling=True,
**kwargs
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_business_card_stream_transform_png(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down Expand Up @@ -177,7 +177,7 @@ def test_business_card_stream_transform_jpg(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down Expand Up @@ -215,7 +215,7 @@ def test_business_card_stream_multipage_transform_pdf(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ async def test_business_card_stream_transform_png(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down Expand Up @@ -191,7 +191,7 @@ async def test_business_card_stream_transform_jpg(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down Expand Up @@ -230,7 +230,7 @@ async def test_business_card_stream_multipage_transform_pdf(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def test_business_card_url_transform_png(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down Expand Up @@ -114,7 +114,7 @@ def test_business_card_url_transform_jpg(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down Expand Up @@ -149,7 +149,7 @@ def test_business_card_url_multipage_transform_pdf(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ async def test_business_card_url_transform_png(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down Expand Up @@ -124,7 +124,7 @@ async def test_business_card_url_transform_jpg(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down Expand Up @@ -160,7 +160,7 @@ async def test_business_card_url_multipage_transform_pdf(self, client):

def callback(raw_response, _, headers):
analyze_result = client._deserialize(AnalyzeOperationResult, raw_response)
extracted_business_card = prepare_prebuilt_models(analyze_result, business_card=True)
extracted_business_card = prepare_prebuilt_models(analyze_result)
responses.append(analyze_result)
responses.append(extracted_business_card)

Expand Down

0 comments on commit 6be8888

Please sign in to comment.