Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include helper functions in sample snippets #36426

Merged
merged 10 commits into from
Jul 19, 2024
118 changes: 85 additions & 33 deletions sdk/documentintelligence/azure-ai-documentintelligence/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,17 @@ from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeResult

def _in_span(word, spans):
catalinaperalta marked this conversation as resolved.
Show resolved Hide resolved
for span in spans:
if word.span.offset >= span.offset and (word.span.offset + word.span.length) <= (span.offset + span.length):
return True
return False

def _format_polygon(polygon):
if not polygon:
return "N/A"
return ", ".join([f"[{polygon[i]}, {polygon[i + 1]}]" for i in range(0, len(polygon), 2)])

endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]

Expand All @@ -234,10 +245,13 @@ for page in result.pages:

if page.lines:
for line_idx, line in enumerate(page.lines):
words = get_words(page, line)
words = []
for word in page.words:
if _in_span(word, line.spans):
words.append(word)
print(
f"...Line # {line_idx} has word count {len(words)} and text '{line.content}' "
f"within bounding polygon '{format_polygon(line.polygon)}'"
f"within bounding polygon '{_format_polygon(line.polygon)}'"
)

if page.words:
Expand All @@ -248,7 +262,7 @@ for page in result.pages:
for selection_mark in page.selection_marks:
print(
f"Selection mark is '{selection_mark.state}' within bounding polygon "
f"'{format_polygon(selection_mark.polygon)}' and has a confidence of {selection_mark.confidence}"
f"'{_format_polygon(selection_mark.polygon)}' and has a confidence of {selection_mark.confidence}"
)

if result.paragraphs:
Expand All @@ -257,9 +271,16 @@ if result.paragraphs:
result.paragraphs.sort(key=lambda p: (p.spans.sort(key=lambda s: s.offset), p.spans[0].offset))
print("-----Print sorted paragraphs-----")
for paragraph in result.paragraphs:
print(
f"Found paragraph with role: '{paragraph.role}' within {format_bounding_region(paragraph.bounding_regions)} bounding region"
)
if not paragraph.bounding_regions:
print(f"Found paragraph with role: '{paragraph.role}' within N/A bounding region")
else:
print(f"Found paragraph with role: '{paragraph.role}' within")
print(
", ".join(
f" Page #{region.page_number}: {_format_polygon(region.polygon)} bounding region"
for region in paragraph.bounding_regions
)
)
print(f"...with content: '{paragraph.content}'")
print(f"...with offset: {paragraph.spans[0].offset} and length: {paragraph.spans[0].length}")

Expand All @@ -269,14 +290,14 @@ if result.tables:
if table.bounding_regions:
for region in table.bounding_regions:
print(
f"Table # {table_idx} location on page: {region.page_number} is {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(f"...Cell[{cell.row_index}][{cell.column_index}] has text '{cell.content}'")
if cell.bounding_regions:
for region in cell.bounding_regions:
print(
f"...content on page {region.page_number} is within bounding polygon '{format_polygon(region.polygon)}'"
f"...content on page {region.page_number} is within bounding polygon '{_format_polygon(region.polygon)}'"
)

print("----------------------------------------")
Expand All @@ -296,6 +317,24 @@ from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import DocumentAnalysisFeature, AnalyzeResult

def _in_span(word, spans):
for span in spans:
if word.span.offset >= span.offset and (word.span.offset + word.span.length) <= (span.offset + span.length):
return True
return False

def _format_bounding_region(bounding_regions):
if not bounding_regions:
return "N/A"
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([f"[{polygon[i]}, {polygon[i + 1]}]" for i in range(0, len(polygon), 2)])

endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]

Expand All @@ -321,12 +360,12 @@ if result.key_value_pairs:
if kv_pair.key:
print(
f"Key '{kv_pair.key.content}' found within "
f"'{format_bounding_region(kv_pair.key.bounding_regions)}' bounding regions"
f"'{_format_bounding_region(kv_pair.key.bounding_regions)}' bounding regions"
)
if kv_pair.value:
print(
f"Value '{kv_pair.value.content}' found within "
f"'{format_bounding_region(kv_pair.value.bounding_regions)}' bounding regions\n"
f"'{_format_bounding_region(kv_pair.value.bounding_regions)}' bounding regions\n"
)

for page in result.pages:
Expand All @@ -335,10 +374,13 @@ for page in result.pages:

if page.lines:
for line_idx, line in enumerate(page.lines):
words = get_words(page.words, line)
words = []
for word in page.words:
if _in_span(word, line.spans):
words.append(word)
print(
f"...Line #{line_idx} has {len(words)} words and text '{line.content}' within "
f"bounding polygon '{format_polygon(line.polygon)}'"
f"bounding polygon '{_format_polygon(line.polygon)}'"
)

if page.words:
Expand All @@ -349,7 +391,7 @@ for page in result.pages:
for selection_mark in page.selection_marks:
print(
f"Selection mark is '{selection_mark.state}' within bounding polygon "
f"'{format_polygon(selection_mark.polygon)}' and has a confidence of "
f"'{_format_polygon(selection_mark.polygon)}' and has a confidence of "
f"{selection_mark.confidence}"
)

Expand All @@ -359,14 +401,14 @@ if result.tables:
if table.bounding_regions:
for region in table.bounding_regions:
print(
f"Table # {table_idx} location on page: {region.page_number} is {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(f"...Cell[{cell.row_index}][{cell.column_index}] has text '{cell.content}'")
if cell.bounding_regions:
for region in cell.bounding_regions:
print(
f"...content on page {region.page_number} is within bounding polygon '{format_polygon(region.polygon)}'\n"
f"...content on page {region.page_number} is within bounding polygon '{_format_polygon(region.polygon)}'\n"
)
print("----------------------------------------")
```
Expand All @@ -388,6 +430,9 @@ from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeResult

def _format_price(price_dict):
return "".join([f"{p}" for p in price_dict.values()])

endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]

Expand Down Expand Up @@ -435,23 +480,23 @@ if receipts.documents:
item_total_price = item.get("valueObject").get("TotalPrice")
if item_total_price:
print(
f"......Total Item Price: {format_price(item_total_price.get('valueCurrency'))} has confidence: "
f"......Total Item Price: {_format_price(item_total_price.get('valueCurrency'))} has confidence: "
f"{item_total_price.confidence}"
)
subtotal = receipt.fields.get("Subtotal")
if subtotal:
print(
f"Subtotal: {format_price(subtotal.get('valueCurrency'))} has confidence: {subtotal.confidence}"
f"Subtotal: {_format_price(subtotal.get('valueCurrency'))} has confidence: {subtotal.confidence}"
)
tax = receipt.fields.get("TotalTax")
if tax:
print(f"Total tax: {format_price(tax.get('valueCurrency'))} has confidence: {tax.confidence}")
print(f"Total tax: {_format_price(tax.get('valueCurrency'))} has confidence: {tax.confidence}")
tip = receipt.fields.get("Tip")
if tip:
print(f"Tip: {format_price(tip.get('valueCurrency'))} has confidence: {tip.confidence}")
print(f"Tip: {_format_price(tip.get('valueCurrency'))} has confidence: {tip.confidence}")
total = receipt.fields.get("Total")
if total:
print(f"Total: {format_price(total.get('valueCurrency'))} has confidence: {total.confidence}")
print(f"Total: {_format_price(total.get('valueCurrency'))} has confidence: {total.confidence}")
print("--------------------------------------")
```

Expand Down Expand Up @@ -525,6 +570,20 @@ from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeResult

def _print_table(header_names, table_data):
# Print a two-dimensional array like a table.
max_len_list = []
for i in range(len(header_names)):
col_values = list(map(lambda row: len(str(row[i])), table_data))
col_values.append(len(str(header_names[i])))
max_len_list.append(max(col_values))

row_format_str = "".join(map(lambda len: f"{{:<{len + 4}}}", max_len_list))

print(row_format_str.format(*header_names))
for row in table_data:
print(row_format_str.format(*row))

endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
model_id = os.getenv("CUSTOM_BUILT_MODEL_ID", custom_model_id)
Expand Down Expand Up @@ -561,10 +620,7 @@ if result.documents:
if not doc.fields is None:
for field_name, field_value in doc.fields.items():
# Dynamic Table cell information store as array in document field.
if (
field_value.type == SYMBOL_OF_TABLE_TYPE
and field_value.value_array
):
if field_value.type == SYMBOL_OF_TABLE_TYPE and field_value.value_array:
col_names = []
sample_obj = field_value.value_array[0]
if KEY_OF_VALUE_OBJECT in sample_obj:
Expand All @@ -581,8 +637,8 @@ if result.documents:
)
row_data = list(map(extract_value_by_col_name, col_names))
table_rows.append(row_data)
print_table(col_names, table_rows)
_print_table(col_names, table_rows)

elif (
field_value.type == SYMBOL_OF_OBJECT_TYPE
and KEY_OF_VALUE_OBJECT in field_value
Expand All @@ -592,9 +648,7 @@ if result.documents:
is_fixed_table = all(
(
rows_of_column["type"] == SYMBOL_OF_OBJECT_TYPE
and Counter(
list(rows_by_columns[0][KEY_OF_VALUE_OBJECT].keys())
)
and Counter(list(rows_by_columns[0][KEY_OF_VALUE_OBJECT].keys()))
== Counter(list(rows_of_column[KEY_OF_VALUE_OBJECT].keys()))
)
for rows_of_column in rows_by_columns
Expand All @@ -609,17 +663,15 @@ if result.documents:
rows = rows_of_column[KEY_OF_VALUE_OBJECT]
for row_key in list(rows.keys()):
if row_key in row_dict:
row_dict[row_key].append(
rows[row_key].get(KEY_OF_CELL_CONTENT)
)
row_dict[row_key].append(rows[row_key].get(KEY_OF_CELL_CONTENT))
else:
row_dict[row_key] = [
row_key,
rows[row_key].get(KEY_OF_CELL_CONTENT),
]

col_names.insert(0, "")
print_table(col_names, list(row_dict.values()))
_print_table(col_names, list(row_dict.values()))

print("------------------------------------")
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ async def analyze_barcodes():
"sample_forms/add_ons/barcodes.jpg",
)
)
# [START analyze_barcodes]
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import DocumentAnalysisFeature, AnalyzeResult
Expand Down Expand Up @@ -91,7 +90,6 @@ async def analyze_barcodes():
print(f" Bounding regions: {format_polygon(barcode.polygon)}")

print("----------------------------------------")
# [END analyze_barcodes]


async def main():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ async def analyze_fonts():
"sample_forms/add_ons/fonts_and_languages.png",
)
)
# [START analyze_fonts]
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import DocumentAnalysisFeature, AnalyzeResult
Expand Down Expand Up @@ -134,7 +133,6 @@ async def analyze_fonts():
print(f" Text: '{get_styled_text(styles, result.content)}'")

print("----------------------------------------")
# [END analyze_fonts]


async def main():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ async def analyze_formulas():
"sample_forms/add_ons/formulas.pdf",
)
)
# [START analyze_formulas]
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import DocumentAnalysisFeature, AnalyzeResult
Expand Down Expand Up @@ -100,7 +99,6 @@ async def analyze_formulas():
print(f" Bounding regions: {format_polygon(formula.polygon)}")

print("----------------------------------------")
# [END analyze_formulas]


async def main():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ async def analyze_with_highres():
"sample_forms/add_ons/highres.png",
)
)
# [START analyze_with_highres]
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import DocumentAnalysisFeature, AnalyzeResult
Expand Down Expand Up @@ -139,7 +138,6 @@ async def analyze_with_highres():
)

print("----------------------------------------")
# [END analyze_with_highres]


async def main():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ async def analyze_languages():
"sample_forms/add_ons/fonts_and_languages.png",
)
)
# [START analyze_languages]
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import DocumentAnalysisFeature, AnalyzeResult
Expand Down Expand Up @@ -84,7 +83,6 @@ async def analyze_languages():
)

print("----------------------------------------")
# [END analyze_languages]


async def main():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@


async def analyze_query_fields():
# [START analyze_query_fields]
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence.aio import DocumentIntelligenceClient
from azure.ai.documentintelligence.models import AnalyzeDocumentRequest, DocumentAnalysisFeature, AnalyzeResult
Expand All @@ -71,7 +70,6 @@ async def analyze_query_fields():
print(f"Address: {doc.fields['Address'].value_string}")
if doc.fields and doc.fields["InvoiceNumber"]:
print(f"Invoice number: {doc.fields['InvoiceNumber'].value_string}")
# [END analyze_query_fields]


async def main():
Expand Down
Loading
Loading