Skip to content

Commit

Permalink
[formrecognizer] Update to use f-strings in samples (Azure#30675)
Browse files Browse the repository at this point in the history
* update to fstrings

* update layout and read async samples

* fix remaining async samples

* add more async samples fixes

* add sync sample updates

* run black

* fix changed header
  • Loading branch information
catalinaperalta authored Jun 8, 2023
1 parent f5c682e commit 63fc24d
Show file tree
Hide file tree
Showing 46 changed files with 1,234 additions and 1,460 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,103 +55,69 @@ async def analyze_business_card_async():
business_cards = await poller.result()

for idx, business_card in enumerate(business_cards.documents):
print("--------Analyzing business card #{}--------".format(idx + 1))
print(f"--------Analyzing business card #{idx + 1}--------")
contact_names = business_card.fields.get("ContactNames")
if contact_names:
for contact_name in contact_names.value:
print(
"Contact First Name: {} has confidence: {}".format(
contact_name.value["FirstName"].value,
contact_name.value[
"FirstName"
].confidence,
)
f"Contact First Name: {contact_name.value['FirstName'].value} "
f"has confidence: {contact_name.value['FirstName'].confidence}"
)
print(
"Contact Last Name: {} has confidence: {}".format(
contact_name.value["LastName"].value,
contact_name.value[
"LastName"
].confidence,
)
f"Contact Last Name: {contact_name.value['LastName'].value} has"
f" confidence: {contact_name.value['LastName'].confidence}"
)
company_names = business_card.fields.get("CompanyNames")
if company_names:
for company_name in company_names.value:
print(
"Company Name: {} has confidence: {}".format(
company_name.value, company_name.confidence
)
f"Company Name: {company_name.value} has confidence: {company_name.confidence}"
)
departments = business_card.fields.get("Departments")
if departments:
for department in departments.value:
print(
"Department: {} has confidence: {}".format(
department.value, department.confidence
)
f"Department: {department.value} has confidence: {department.confidence}"
)
job_titles = business_card.fields.get("JobTitles")
if job_titles:
for job_title in job_titles.value:
print(
"Job Title: {} has confidence: {}".format(
job_title.value, job_title.confidence
)
f"Job Title: {job_title.value} has confidence: {job_title.confidence}"
)
emails = business_card.fields.get("Emails")
if emails:
for email in emails.value:
print(
"Email: {} has confidence: {}".format(email.value, email.confidence)
)
print(f"Email: {email.value} has confidence: {email.confidence}")
websites = business_card.fields.get("Websites")
if websites:
for website in websites.value:
print(
"Website: {} has confidence: {}".format(
website.value, website.confidence
)
)
print(f"Website: {website.value} has confidence: {website.confidence}")
addresses = business_card.fields.get("Addresses")
if addresses:
for address in addresses.value:
print(
"Address: {} has confidence: {}".format(
address.value, address.confidence
)
)
print(f"Address: {address.value} has confidence: {address.confidence}")
mobile_phones = business_card.fields.get("MobilePhones")
if mobile_phones:
for phone in mobile_phones.value:
print(
"Mobile phone number: {} has confidence: {}".format(
phone.content, phone.confidence
)
f"Mobile phone number: {phone.content} has confidence: {phone.confidence}"
)
faxes = business_card.fields.get("Faxes")
if faxes:
for fax in faxes.value:
print(
"Fax number: {} has confidence: {}".format(
fax.content, fax.confidence
)
)
print(f"Fax number: {fax.content} has confidence: {fax.confidence}")
work_phones = business_card.fields.get("WorkPhones")
if work_phones:
for work_phone in work_phones.value:
print(
"Work phone number: {} has confidence: {}".format(
work_phone.content, work_phone.confidence
)
f"Work phone number: {work_phone.content} has confidence: {work_phone.confidence}"
)
other_phones = business_card.fields.get("OtherPhones")
if other_phones:
for other_phone in other_phones.value:
print(
"Other phone number: {} has confidence: {}".format(
other_phone.value, other_phone.confidence
)
f"Other phone number: {other_phone.value} has confidence: {other_phone.confidence}"
)


Expand All @@ -162,11 +128,14 @@ async def main():
if __name__ == "__main__":
import sys
from azure.core.exceptions import HttpResponseError

try:
asyncio.run(main())
except HttpResponseError as error:
print("For more information about troubleshooting errors, see the following guide: "
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
print(
"For more information about troubleshooting errors, see the following guide: "
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting"
)
# Examples of how to check an HttpResponseError
# Check by error code:
if error.error is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,43 +65,37 @@ async def analyze_custom_documents_async(custom_model_id):
result = await poller.result()

for idx, document in enumerate(result.documents):
print("--------Analyzing document #{}--------".format(idx + 1))
print("Document has type {}".format(document.doc_type))
print("Document has document type confidence {}".format(document.confidence))
print("Document was analyzed with model with ID {}".format(result.model_id))
print(f"--------Analyzing document #{idx + 1}--------")
print(f"Document has type {document.doc_type}")
print(f"Document has document type confidence {document.confidence}")
print(f"Document was analyzed with model with ID {result.model_id}")
for name, field in document.fields.items():
field_value = field.value if field.value else field.content
print("......found field of type '{}' with value '{}' and with confidence {}".format(field.value_type, field_value, field.confidence))
print(
f"......found field of type '{field.value_type}' with value '{field_value}' and with confidence {field.confidence}"
)

# iterate over tables, lines, and selection marks on each page
for page in result.pages:
print("\nLines found on page {}".format(page.page_number))
print(f"\nLines found on page {page.page_number}")
for line in page.lines:
print("...Line '{}'".format(line.content))
print(f"...Line '{line.content}'")
for word in page.words:
print(
"...Word '{}' has a confidence of {}".format(
word.content, word.confidence
)
)
print(f"...Word '{word.content}' has a confidence of {word.confidence}")
if page.selection_marks:
print("\nSelection marks found on page {}".format(page.page_number))
print(f"\nSelection marks found on page {page.page_number}")
for selection_mark in page.selection_marks:
print(
"...Selection mark is '{}' and has a confidence of {}".format(
selection_mark.state, selection_mark.confidence
)
f"...Selection mark is '{selection_mark.state}' and has a confidence of {selection_mark.confidence}"
)

for i, table in enumerate(result.tables):
print("\nTable {} can be found on page:".format(i + 1))
print(f"\nTable {i + 1} can be found on page:")
for region in table.bounding_regions:
print("...{}".format(region.page_number))
print(f"...{region.page_number}")
for cell in table.cells:
print(
"...Cell[{}][{}] has text '{}'".format(
cell.row_index, cell.column_index, cell.content
)
f"...Cell[{cell.row_index}][{cell.column_index}] has text '{cell.content}'"
)
print("-----------------------------------")
# [END analyze_custom_documents_async]
Expand All @@ -110,7 +104,6 @@ async def analyze_custom_documents_async(custom_model_id):
async def main():
model_id = None
if os.getenv("CONTAINER_SAS_URL") and not os.getenv("CUSTOM_BUILT_MODEL_ID"):

from azure.core.credentials import AzureKeyCredential
from azure.ai.formrecognizer.aio import DocumentModelAdministrationClient
from azure.ai.formrecognizer import ModelBuildMode
Expand Down Expand Up @@ -139,11 +132,14 @@ async def main():
if __name__ == "__main__":
import sys
from azure.core.exceptions import HttpResponseError

try:
asyncio.run(main())
except HttpResponseError as error:
print("For more information about troubleshooting errors, see the following guide: "
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
print(
"For more information about troubleshooting errors, see the following guide: "
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting"
)
# Examples of how to check an HttpResponseError
# Check by error code:
if error.error is not None:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,20 @@
import os
import asyncio


def format_bounding_region(bounding_regions):
if not bounding_regions:
return "N/A"
return ", ".join("Page #{}: {}".format(region.page_number, format_polygon(region.polygon)) for region in bounding_regions)
return ", ".join(
f"Page #{region.page_number}: {format_polygon(region.polygon)}"
for region in bounding_regions
)


def format_polygon(polygon):
if not polygon:
return "N/A"
return ", ".join(["[{}, {}]".format(p.x, p.y) for p in polygon])
return ", ".join([f"[{p.x}, {p.y}]" for p in polygon])


async def analyze_general_documents():
Expand Down Expand Up @@ -66,105 +71,89 @@ async def analyze_general_documents():
for style in result.styles:
if style.is_handwritten:
print("Document contains handwritten content: ")
print(",".join([result.content[span.offset:span.offset + span.length] for span in style.spans]))
print(
",".join(
[
result.content[span.offset : span.offset + span.length]
for span in style.spans
]
)
)

print("----Key-value pairs found in document----")
for kv_pair in result.key_value_pairs:
if kv_pair.common_name:
print(f"Common name for key value pair: {kv_pair.common_name}")
if kv_pair.key:
print(
"Key '{}' found within '{}' bounding regions".format(
kv_pair.key.content,
format_bounding_region(kv_pair.key.bounding_regions),
)
)
f"Key '{kv_pair.key.content}' found within "
f"'{format_bounding_region(kv_pair.key.bounding_regions)}' bounding regions"
)
if kv_pair.value:
print(
"Value '{}' found within '{}' bounding regions\n".format(
kv_pair.value.content,
format_bounding_region(kv_pair.value.bounding_regions),
)
)
f"Value '{kv_pair.value.content}' found within "
f"'{format_bounding_region(kv_pair.value.bounding_regions)}' bounding regions\n"
)

for page in result.pages:
print("----Analyzing document from page #{}----".format(page.page_number))
print(f"----Analyzing document from page #{page.page_number}----")
print(
"Page has width: {} and height: {}, measured with unit: {}".format(
page.width, page.height, page.unit
)
f"Page has width: {page.width} and height: {page.height}, measured with unit: {page.unit}"
)

for line_idx, line in enumerate(page.lines):
words = line.get_words()
print(
"...Line # {} has {} words and text '{}' within bounding polygon '{}'".format(
line_idx,
len(words),
line.content,
format_polygon(line.polygon),
)
f"...Line #{line_idx} has {len(words)} words and text '{line.content}' within "
f"bounding polygon '{format_polygon(line.polygon)}'"
)

for word in words:
print(
"......Word '{}' has a confidence of {}".format(
word.content, word.confidence
)
f"......Word '{word.content}' has a confidence of {word.confidence}"
)

for selection_mark in page.selection_marks:
print(
"Selection mark is '{}' within bounding polygon '{}' and has a confidence of {}".format(
selection_mark.state,
format_polygon(selection_mark.polygon),
selection_mark.confidence,
)
f"Selection mark is '{selection_mark.state}' within bounding polygon "
f"'{format_polygon(selection_mark.polygon)}' and has a confidence of "
f"{selection_mark.confidence}"
)

for table_idx, table in enumerate(result.tables):
print(
"Table # {} has {} rows and {} columns".format(
table_idx, table.row_count, table.column_count
)
f"Table # {table_idx} has {table.row_count} rows and {table.column_count} columns"
)
for region in table.bounding_regions:
print(
"Table # {} location on page: {} is {}".format(
table_idx,
region.page_number,
format_polygon(region.polygon),
)
f"Table # {table_idx} location on page: {region.page_number} is {format_polygon(region.polygon)}"
)
for cell in table.cells:
print(
"...Cell[{}][{}] has text '{}'".format(
cell.row_index,
cell.column_index,
cell.content,
)
f"...Cell[{cell.row_index}][{cell.column_index}] has text '{cell.content}'"
)
for region in cell.bounding_regions:
print(
"...content on page {} is within bounding polygon '{}'\n".format(
region.page_number,
format_polygon(region.polygon),
)
f"...content on page {region.page_number} is within bounding polygon '{format_polygon(region.polygon)}'\n"
)
print("----------------------------------------")


async def main():
await analyze_general_documents()


if __name__ == "__main__":
import sys
from azure.core.exceptions import HttpResponseError

try:
asyncio.run(main())
except HttpResponseError as error:
print("For more information about troubleshooting errors, see the following guide: "
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting")
print(
"For more information about troubleshooting errors, see the following guide: "
"https://aka.ms/azsdk/python/formrecognizer/troubleshooting"
)
# Examples of how to check an HttpResponseError
# Check by error code:
if error.error is not None:
Expand Down
Loading

0 comments on commit 63fc24d

Please sign in to comment.