-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathsample_analyze_mortgage_1003.py
More file actions
297 lines (252 loc) · 11.5 KB
/
sample_analyze_mortgage_1003.py
File metadata and controls
297 lines (252 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# coding: utf-8
# -------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# --------------------------------------------------------------------------
"""
This code sample shows Prebuilt US Mortgage 1003 operations with the Azure AI Document Intelligence client library.
The async versions of the samples require Python 3.8 or later.
To learn more, please visit the documentation - Quickstart: Document Intelligence (formerly Form Recognizer) SDKs
https://learn.microsoft.com/azure/ai-services/document-intelligence/quickstarts/get-started-sdks-rest-api?pivots=programming-language-python
"""
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.documentintelligence import DocumentIntelligenceClient
from dotenv import find_dotenv, load_dotenv
"""
Remember to remove the key from your code when you're done, and never post it publicly. For production, use
secure methods to store and access your credentials. For more information, see
https://docs.microsoft.com/en-us/azure/cognitive-services/cognitive-services-security?tabs=command-line%2Ccsharp#environment-variables-and-application-configuration
"""
load_dotenv(find_dotenv())
endpoint = os.environ["DOCUMENTINTELLIGENCE_ENDPOINT"]
key = os.environ["DOCUMENTINTELLIGENCE_API_KEY"]
filepath = "YOUR_FILE_PATH"
document_intelligence_client = DocumentIntelligenceClient(
endpoint=endpoint, credential=AzureKeyCredential(key)
)
with open(filepath, "rb") as f:
poller = document_intelligence_client.begin_analyze_document(
"prebuilt-mortgage.us.1003", body=f
)
mortgage1003 = poller.result()
for idx, document in enumerate(mortgage1003.documents):
print("--------Recognizing document #{}--------".format(idx + 1))
doc_type = document.doc_type
if doc_type:
print("Document Type: {}".format(doc_type))
agency_case_number = document.fields.get("AgencyCaseNumber")
if agency_case_number:
print(
"Agency Case Number: {} has confidence: {}".format(
agency_case_number.value_string, agency_case_number.confidence
)
)
borrowers = document.fields.get("Borrowers")
if borrowers:
for borrower_idx, borrower in enumerate(borrowers.value_array):
print(
"Borrower #{}: {} has confidence: {}".format(
borrower_idx + 1, borrower.value_string, borrower.confidence
)
)
birth_date = borrower.value_object.get("BirthDate")
if birth_date:
print(
"...Birth Date: {} has confidence: {}".format(
birth_date.value_date, birth_date.confidence
)
)
cell_phone_number = borrower.value_object.get("CellPhoneNumber")
if cell_phone_number:
print(
"...Cell Phone Number: {} has confidence: {}".format(
cell_phone_number.value_phone_number, cell_phone_number.confidence
)
)
co_borrower_names = borrower.value_object.get("CoBorrowerNames")
if co_borrower_names:
print(
"...Co-Borrower Names: {} has confidence: {}".format(
co_borrower_names.value_string, co_borrower_names.confidence
)
)
num_borrowers = borrower.value_object.get("NumberOfBorrowers")
if num_borrowers:
print(
"...Number of Borrowers: {} has confidence: {}".format(
num_borrowers.value_integer, num_borrowers.confidence
)
)
current_address = borrower.value_object.get("CurrentAddress")
if current_address:
print(
"...Current Address: {} has confidence: {}".format(
current_address.value_address, current_address.confidence
)
)
current_employment = borrower.value_object.get("CurrentEmployment")
if current_employment:
print("...Current Employment:")
does_not_apply = current_employment.value_object.get("DoesNotApply")
if does_not_apply:
print(
"......Does Not Apply: {} has confidence: {}".format(
does_not_apply.value_boolean, does_not_apply.confidence
)
)
employer_address = current_employment.value_object.get("EmployerAddress")
if employer_address:
print(
"......Employer Address: {} has confidence: {}".format(
employer_address.value_address, employer_address.confidence
)
)
employer_name = current_employment.value_object.get("EmployerName")
if employer_name:
print(
"......Employer Name: {} has confidence: {}".format(
employer_name.value_string, employer_name.confidence
)
)
employer_phone_number = current_employment.value_object.get("EmployerPhoneNumber")
if employer_phone_number:
print(
"......Employer Phone Number: {} has confidence: {}".format(
employer_phone_number.value_phone_number, employer_phone_number.confidence
)
)
gross_monthly_income_total = current_employment.value_object.get("GrossMonthlyIncomeTotal")
if gross_monthly_income_total:
print(
"......Gross Monthly Income: {} has confidence: {}".format(
gross_monthly_income_total.value_number, gross_monthly_income_total.confidence
)
)
position_or_title = current_employment.value_object.get("PositionOrTitle")
if position_or_title:
print(
"......Position / Title: {} has confidence: {}".format(
position_or_title.value_string, position_or_title.confidence
)
)
start_date = current_employment.value_object.get("StartDate")
if start_date:
print(
"......Start Date: {} has confidence: {}".format(
start_date.value_date, start_date.confidence
)
)
social_security_number = borrower.value_object.get("SocialSecurityNumber")
if social_security_number:
print(
"...Social Security Number: {} has confidence: {}".format(
social_security_number.value_string, social_security_number.confidence
)
)
number_of_dependents = borrower.value_object.get("NumberOfDependents")
if number_of_dependents:
print(
"...Number of Dependents: {} has confidence: {}".format(
number_of_dependents.value_integer, number_of_dependents.confidence
)
)
marital_status = borrower.value_object.get("MaritalStatus")
if marital_status:
print(
"...Marital Status: {} has confidence: {}".format(
marital_status.value_selection_group, marital_status.confidence
)
)
lender_loan_number = document.fields.get("LenderLoanNumber")
if lender_loan_number:
print(
"Lender Loan Number: {} has confidence: {}".format(
lender_loan_number.value_string, lender_loan_number.confidence
)
)
loan = document.fields.get("Loan")
if loan:
print("Loan:")
loan_amount = loan.value_object.get("Amount")
if loan_amount:
print(
"...Loan Amount: {} has confidence: {}".format(
loan_amount.value_number, loan_amount.confidence
)
)
purpose_type = loan.value_object.get("PurposeType")
if purpose_type:
print(
"...Purpose Type: {} has confidence: {}".format(
purpose_type.value_selection_group, purpose_type.confidence
)
)
refinance_program_type = loan.value_object.get("RefinanceProgramType")
if refinance_program_type:
print(
"...Refinance Program Type: {} has confidence: {}".format(
refinance_program_type.value_selection_group, refinance_program_type.confidence
)
)
refinance_type = loan.value_object.get("RefinanceType")
if refinance_type:
print(
"...Refinance Type: {} has confidence: {}".format(
refinance_type.value_selection_group, refinance_type.confidence
)
)
property = document.fields.get("Property")
if property:
print("Property:")
address = property.value_object.get("Address")
if address:
print(
"...Address: {} has confidence: {}".format(
address.value_address, address.confidence
)
)
isFhaSecondaryResidence = property.value_object.get("IsFhaSecondaryResidence")
if isFhaSecondaryResidence:
print(
"...isFhaSecondaryResidence: {} has confidence: {}".format(
isFhaSecondaryResidence.value_boolean, isFhaSecondaryResidence.confidence
)
)
num_unit = property.value_object.get("NumberOfUnits")
if num_unit:
print(
"...Number Of Units: {} has confidence: {}".format(
num_unit.value_integer, num_unit.confidence
)
)
value = property.value_object.get("Value")
if value:
print(
"...Value: {} has confidence: {}".format(
value.value_number, value.confidence
)
)
manufactured_home = property.value_object.get("ManufacturedHome")
if manufactured_home:
print(
"...Manufactured Home: {} has confidence: {}".format(
manufactured_home.value_selection_group, manufactured_home.confidence
)
)
mixed_use_property = property.value_object.get("MixedUseProperty")
if mixed_use_property:
print(
"...Mixed Use Property: {} has confidence: {}".format(
mixed_use_property.value_selection_group, mixed_use_property.confidence
)
)
occupancy_status = property.value_object.get("OccupancyStatus")
if occupancy_status:
print(
"...Occupancy Status: {} has confidence: {}".format(
occupancy_status.value_selection_group, occupancy_status.confidence
)
)
print("--------------------------------------")