summaryrefslogtreecommitdiff
path: root/python-amazon-textract-geofinder.spec
blob: dfdbcd09ec5520ec0b90f76f3bf42e39a2de989b (plain)
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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
%global _empty_manifest_terminate_build 0
Name:		python-amazon-textract-geofinder
Version:	0.0.7
Release:	1
Summary:	Amazon Textract package to easier access data through geometric information
License:	Apache License Version 2.0
URL:		https://github.com/aws-samples/amazon-textract-textractor/tpipelinegeofinder
Source0:	https://mirrors.aliyun.com/pypi/web/packages/fc/03/356baee260345378f7dfd56784341e3ac102843b86e0fc68bca17e0f90ff/amazon-textract-geofinder-0.0.7.tar.gz
BuildArch:	noarch

Requires:	python3-amazon-textract-response-parser

%description
# Textract-Pipeline-GeoFinder

Provides functions to use geometric information to extract information.

Use cases include:
* Give context to key/value pairs from the Amazon Textract AnalyzeDocument API for FORMS
* Find values in specific areas

# Install

```bash
> python -m pip install amazon-textract-geofinder
```

Make sure your environment is setup with AWS credentials through configuration files or environment variables or an attached role. (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)

# Concept

To find information in a document based on geometry with this library the main advantage over defining x,y coordinates where the expected value should be is the concept of an area.

An area is ultimately defined by a box with x_min, y_min, x_max, y_max coordinates but can be defined by finding words/phrases in the document and then use to create the area.

From there functions to parse the information in the area help to extract the information. E. g. by defining the area based on the question like 'Did you feel fever or feverish lately?' we can associate the answers to it and create a new key/value pair specific to this question.


# Samples

## Get context for key value pairs

Sample image:

<img src="./tests/data/patient_intake_form_sample.jpg" width=300> 

The Amazon Textract AnalyzeDocument API with the FORMS feature returns the following keys:

| Key                                          | Value          |
|----------------------------------------------|----------------|
| First Name:                                  | ALEJANDRO      |
| First Name:                                  | CARLOS         |
| Relationship to Patient:                     | BROTHER        |
| First Name:                                  | JANE           |
| Marital Status:                              | MARRIED        |
| Phone:                                       | 646-555-0111   |
| Last Name:                                   | SALAZAR        |
| Phone:                                       | 212-555-0150   |
| Relationship to Patient:                     | FRIEND         |
| Last Name:                                   | ROSALEZ        |
| City:                                        | ANYTOWN        |
| Phone:                                       | 650-555-0123   |
| Address:                                     | 123 ANY STREET |
| Yes                                          | SELECTED       |
| Yes                                          | NOT_SELECTED   |
| Date of Birth:                               | 10/10/1982     |
| Last Name:                                   | DOE            |
| Sex:                                         | M              |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| State:                                       | CA             |
| Zip Code:                                    | 12345          |
| Email Address:                               |                |
| No                                           | NOT_SELECTED   |
| No                                           | SELECTED       |
| No                                           | NOT_SELECTED   |
| Yes                                          | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |


But the information to which section of the document the individual keys belong is not obvious. Most keys appear multiple times and we want to give them context to associate them with the 'Patient', 'Emergency Contact 1', 'Emergency Contact 2' or specific questions.


This Jupyter notebook that walks through the sample: [sample notebook](./geofinder-sample-notebook.ipynb)
Make sure to have AWS credentials setup when starting the notebook locally or use a SageMaker notebook with a role including permissions for Amazon Textract. 

This code snippet is take from the notebook.

```bash
python -m pip install amazon-textract-helper amazon-textract-geofinder
```

```python
from textractgeofinder.ocrdb import AreaSelection
from textractgeofinder.tgeofinder import KeyValue, TGeoFinder, AreaSelection, SelectionElement
from textractprettyprinter.t_pretty_print import get_forms_string
from textractcaller import call_textract
from textractcaller.t_call import Textract_Features

import trp.trp2 as t2

image_filename='./tests/data/patient_intake_form_sample.jpg'

j = call_textract(input_document=image_filename, features=[Textract_Features.FORMS])


t_document = t2.TDocumentSchema().load(j)
doc_height = 1000
doc_width = 1000
geofinder_doc = TGeoFinder(j, doc_height=doc_height, doc_width=doc_width)

def set_hierarchy_kv(list_kv: list[KeyValue], t_document: t2.TDocument, page_block: t2.TBlock, prefix="BORROWER"):
    for x in list_kv:
        t_document.add_virtual_key_for_existing_key(key_name=f"{prefix}_{x.key.text}",
                                                    existing_key=t_document.get_block_by_id(x.key.id),
                                                    page_block=page_block)
# patient information
patient_information = geofinder_doc.find_phrase_on_page("patient information")[0]
emergency_contact_1 = geofinder_doc.find_phrase_on_page("emergency contact 1:", min_textdistance=0.99)[0]
top_left = t2.TPoint(y=patient_information.ymax, x=0)
lower_right = t2.TPoint(y=emergency_contact_1.ymin, x=doc_width)
form_fields = geofinder_doc.get_form_fields_in_area(
    area_selection=AreaSelection(top_left=top_left, lower_right=lower_right))
set_hierarchy_kv(list_kv=form_fields, t_document=t_document, prefix='PATIENT', page_block=t_document.pages[0])

set_hierarchy_kv(list_kv=form_fields, t_document=t_document, prefix='PATIENT', page_block=t_document.pages[0])

print(get_forms_string(t2.TDocumentSchema().dump(t_document)))
```

| Key                     | Value          |
|-------------------------|----------------|
| ...                     | ...            |
| PATIENT_first name:     | ALEJANDRO      |
| PATIENT_address:        | 123 ANY STREET |
| PATIENT_sex:            | M              |
| PATIENT_state:          | CA             |
| PATIENT_zip code:       | 12345          |
| PATIENT_marital status: | MARRIED        |
| PATIENT_last name:      | ROSALEZ        |
| PATIENT_phone:          | 646-555-0111   |
| PATIENT_email address:  |                |
| PATIENT_city:           | ANYTOWN        |
| PATIENT_date of birth:  | 10/10/1982     |

## Using the Amazon Textact Helper command line tool with the sample

This will show the full result, like the notebook.

```bash
> python -m pip install amazon-textract-helper amazon-textract-geofinder
> cat tests/data/patient_intake_form_sample.json| bin/amazon-textract-geofinder | amazon-textract --stdin --pretty-print FORMS
```

| Key                     | Value          |
|-------------------------|----------------|
| First Name:                                  | ALEJANDRO      |
| First Name:                                  | CARLOS         |
| Relationship to Patient:                     | BROTHER        |
| First Name:                                  | JANE           |
| Marital Status:                              | MARRIED        |
| Phone:                                       | 646-555-0111   |
| Last Name:                                   | SALAZAR        |
| Phone:                                       | 212-555-0150   |
| Relationship to Patient:                     | FRIEND         |
| Last Name:                                   | ROSALEZ        |
| City:                                        | ANYTOWN        |
| Phone:                                       | 650-555-0123   |
| Address:                                     | 123 ANY STREET |
| Yes                                          | SELECTED       |
| Yes                                          | NOT_SELECTED   |
| Date of Birth:                               | 10/10/1982     |
| Last Name:                                   | DOE            |
| Sex:                                         | M              |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| State:                                       | CA             |
| Zip Code:                                    | 12345          |
| Email Address:                               |                |
| No                                           | NOT_SELECTED   |
| No                                           | SELECTED       |
| No                                           | NOT_SELECTED   |
| Yes                                          | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |
| PATIENT_first name:                          | ALEJANDRO      |
| PATIENT_address:                             | 123 ANY STREET |
| PATIENT_sex:                                 | M              |
| PATIENT_state:                               | CA             |
| PATIENT_zip code:                            | 12345          |
| PATIENT_marital status:                      | MARRIED        |
| PATIENT_last name:                           | ROSALEZ        |
| PATIENT_phone:                               | 646-555-0111   |
| PATIENT_email address:                       |                |
| PATIENT_city:                                | ANYTOWN        |
| PATIENT_date of birth:                       | 10/10/1982     |
| EMERGENCY_CONTACT_1_first name:              | CARLOS         |
| EMERGENCY_CONTACT_1_phone:                   | 212-555-0150   |
| EMERGENCY_CONTACT_1_relationship to patient: | BROTHER        |
| EMERGENCY_CONTACT_1_last name:               | SALAZAR        |
| EMERGENCY_CONTACT_2_first name:              | JANE           |
| EMERGENCY_CONTACT_2_phone:                   | 650-555-0123   |
| EMERGENCY_CONTACT_2_last name:               | DOE            |
| EMERGENCY_CONTACT_2_relationship to patient: | FRIEND         |
| FEVER->YES                                   | SELECTED       |
| FEVER->NO                                    | NOT_SELECTED   |
| SHORTNESS->YES                               | NOT_SELECTED   |
| SHORTNESS->NO                                | SELECTED       |
| COUGH->YES                                   | NOT_SELECTED   |
| COUGH->NO                                    | SELECTED       |
| LOSS_OF_TASTE->YES                           | NOT_SELECTED   |
| LOSS_OF_TASTE->NO                            | SELECTED       |
| COVID_CONTACT->YES                           | SELECTED       |
| COVID_CONTACT->NO                            | NOT_SELECTED   |
| TRAVEL->YES                                  | NOT_SELECTED   |
| TRAVEL->NO                                   | SELECTED       |




%package -n python3-amazon-textract-geofinder
Summary:	Amazon Textract package to easier access data through geometric information
Provides:	python-amazon-textract-geofinder
BuildRequires:	python3-devel
BuildRequires:	python3-setuptools
BuildRequires:	python3-pip
%description -n python3-amazon-textract-geofinder
# Textract-Pipeline-GeoFinder

Provides functions to use geometric information to extract information.

Use cases include:
* Give context to key/value pairs from the Amazon Textract AnalyzeDocument API for FORMS
* Find values in specific areas

# Install

```bash
> python -m pip install amazon-textract-geofinder
```

Make sure your environment is setup with AWS credentials through configuration files or environment variables or an attached role. (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)

# Concept

To find information in a document based on geometry with this library the main advantage over defining x,y coordinates where the expected value should be is the concept of an area.

An area is ultimately defined by a box with x_min, y_min, x_max, y_max coordinates but can be defined by finding words/phrases in the document and then use to create the area.

From there functions to parse the information in the area help to extract the information. E. g. by defining the area based on the question like 'Did you feel fever or feverish lately?' we can associate the answers to it and create a new key/value pair specific to this question.


# Samples

## Get context for key value pairs

Sample image:

<img src="./tests/data/patient_intake_form_sample.jpg" width=300> 

The Amazon Textract AnalyzeDocument API with the FORMS feature returns the following keys:

| Key                                          | Value          |
|----------------------------------------------|----------------|
| First Name:                                  | ALEJANDRO      |
| First Name:                                  | CARLOS         |
| Relationship to Patient:                     | BROTHER        |
| First Name:                                  | JANE           |
| Marital Status:                              | MARRIED        |
| Phone:                                       | 646-555-0111   |
| Last Name:                                   | SALAZAR        |
| Phone:                                       | 212-555-0150   |
| Relationship to Patient:                     | FRIEND         |
| Last Name:                                   | ROSALEZ        |
| City:                                        | ANYTOWN        |
| Phone:                                       | 650-555-0123   |
| Address:                                     | 123 ANY STREET |
| Yes                                          | SELECTED       |
| Yes                                          | NOT_SELECTED   |
| Date of Birth:                               | 10/10/1982     |
| Last Name:                                   | DOE            |
| Sex:                                         | M              |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| State:                                       | CA             |
| Zip Code:                                    | 12345          |
| Email Address:                               |                |
| No                                           | NOT_SELECTED   |
| No                                           | SELECTED       |
| No                                           | NOT_SELECTED   |
| Yes                                          | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |


But the information to which section of the document the individual keys belong is not obvious. Most keys appear multiple times and we want to give them context to associate them with the 'Patient', 'Emergency Contact 1', 'Emergency Contact 2' or specific questions.


This Jupyter notebook that walks through the sample: [sample notebook](./geofinder-sample-notebook.ipynb)
Make sure to have AWS credentials setup when starting the notebook locally or use a SageMaker notebook with a role including permissions for Amazon Textract. 

This code snippet is take from the notebook.

```bash
python -m pip install amazon-textract-helper amazon-textract-geofinder
```

```python
from textractgeofinder.ocrdb import AreaSelection
from textractgeofinder.tgeofinder import KeyValue, TGeoFinder, AreaSelection, SelectionElement
from textractprettyprinter.t_pretty_print import get_forms_string
from textractcaller import call_textract
from textractcaller.t_call import Textract_Features

import trp.trp2 as t2

image_filename='./tests/data/patient_intake_form_sample.jpg'

j = call_textract(input_document=image_filename, features=[Textract_Features.FORMS])


t_document = t2.TDocumentSchema().load(j)
doc_height = 1000
doc_width = 1000
geofinder_doc = TGeoFinder(j, doc_height=doc_height, doc_width=doc_width)

def set_hierarchy_kv(list_kv: list[KeyValue], t_document: t2.TDocument, page_block: t2.TBlock, prefix="BORROWER"):
    for x in list_kv:
        t_document.add_virtual_key_for_existing_key(key_name=f"{prefix}_{x.key.text}",
                                                    existing_key=t_document.get_block_by_id(x.key.id),
                                                    page_block=page_block)
# patient information
patient_information = geofinder_doc.find_phrase_on_page("patient information")[0]
emergency_contact_1 = geofinder_doc.find_phrase_on_page("emergency contact 1:", min_textdistance=0.99)[0]
top_left = t2.TPoint(y=patient_information.ymax, x=0)
lower_right = t2.TPoint(y=emergency_contact_1.ymin, x=doc_width)
form_fields = geofinder_doc.get_form_fields_in_area(
    area_selection=AreaSelection(top_left=top_left, lower_right=lower_right))
set_hierarchy_kv(list_kv=form_fields, t_document=t_document, prefix='PATIENT', page_block=t_document.pages[0])

set_hierarchy_kv(list_kv=form_fields, t_document=t_document, prefix='PATIENT', page_block=t_document.pages[0])

print(get_forms_string(t2.TDocumentSchema().dump(t_document)))
```

| Key                     | Value          |
|-------------------------|----------------|
| ...                     | ...            |
| PATIENT_first name:     | ALEJANDRO      |
| PATIENT_address:        | 123 ANY STREET |
| PATIENT_sex:            | M              |
| PATIENT_state:          | CA             |
| PATIENT_zip code:       | 12345          |
| PATIENT_marital status: | MARRIED        |
| PATIENT_last name:      | ROSALEZ        |
| PATIENT_phone:          | 646-555-0111   |
| PATIENT_email address:  |                |
| PATIENT_city:           | ANYTOWN        |
| PATIENT_date of birth:  | 10/10/1982     |

## Using the Amazon Textact Helper command line tool with the sample

This will show the full result, like the notebook.

```bash
> python -m pip install amazon-textract-helper amazon-textract-geofinder
> cat tests/data/patient_intake_form_sample.json| bin/amazon-textract-geofinder | amazon-textract --stdin --pretty-print FORMS
```

| Key                     | Value          |
|-------------------------|----------------|
| First Name:                                  | ALEJANDRO      |
| First Name:                                  | CARLOS         |
| Relationship to Patient:                     | BROTHER        |
| First Name:                                  | JANE           |
| Marital Status:                              | MARRIED        |
| Phone:                                       | 646-555-0111   |
| Last Name:                                   | SALAZAR        |
| Phone:                                       | 212-555-0150   |
| Relationship to Patient:                     | FRIEND         |
| Last Name:                                   | ROSALEZ        |
| City:                                        | ANYTOWN        |
| Phone:                                       | 650-555-0123   |
| Address:                                     | 123 ANY STREET |
| Yes                                          | SELECTED       |
| Yes                                          | NOT_SELECTED   |
| Date of Birth:                               | 10/10/1982     |
| Last Name:                                   | DOE            |
| Sex:                                         | M              |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| State:                                       | CA             |
| Zip Code:                                    | 12345          |
| Email Address:                               |                |
| No                                           | NOT_SELECTED   |
| No                                           | SELECTED       |
| No                                           | NOT_SELECTED   |
| Yes                                          | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |
| PATIENT_first name:                          | ALEJANDRO      |
| PATIENT_address:                             | 123 ANY STREET |
| PATIENT_sex:                                 | M              |
| PATIENT_state:                               | CA             |
| PATIENT_zip code:                            | 12345          |
| PATIENT_marital status:                      | MARRIED        |
| PATIENT_last name:                           | ROSALEZ        |
| PATIENT_phone:                               | 646-555-0111   |
| PATIENT_email address:                       |                |
| PATIENT_city:                                | ANYTOWN        |
| PATIENT_date of birth:                       | 10/10/1982     |
| EMERGENCY_CONTACT_1_first name:              | CARLOS         |
| EMERGENCY_CONTACT_1_phone:                   | 212-555-0150   |
| EMERGENCY_CONTACT_1_relationship to patient: | BROTHER        |
| EMERGENCY_CONTACT_1_last name:               | SALAZAR        |
| EMERGENCY_CONTACT_2_first name:              | JANE           |
| EMERGENCY_CONTACT_2_phone:                   | 650-555-0123   |
| EMERGENCY_CONTACT_2_last name:               | DOE            |
| EMERGENCY_CONTACT_2_relationship to patient: | FRIEND         |
| FEVER->YES                                   | SELECTED       |
| FEVER->NO                                    | NOT_SELECTED   |
| SHORTNESS->YES                               | NOT_SELECTED   |
| SHORTNESS->NO                                | SELECTED       |
| COUGH->YES                                   | NOT_SELECTED   |
| COUGH->NO                                    | SELECTED       |
| LOSS_OF_TASTE->YES                           | NOT_SELECTED   |
| LOSS_OF_TASTE->NO                            | SELECTED       |
| COVID_CONTACT->YES                           | SELECTED       |
| COVID_CONTACT->NO                            | NOT_SELECTED   |
| TRAVEL->YES                                  | NOT_SELECTED   |
| TRAVEL->NO                                   | SELECTED       |




%package help
Summary:	Development documents and examples for amazon-textract-geofinder
Provides:	python3-amazon-textract-geofinder-doc
%description help
# Textract-Pipeline-GeoFinder

Provides functions to use geometric information to extract information.

Use cases include:
* Give context to key/value pairs from the Amazon Textract AnalyzeDocument API for FORMS
* Find values in specific areas

# Install

```bash
> python -m pip install amazon-textract-geofinder
```

Make sure your environment is setup with AWS credentials through configuration files or environment variables or an attached role. (https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-configure.html)

# Concept

To find information in a document based on geometry with this library the main advantage over defining x,y coordinates where the expected value should be is the concept of an area.

An area is ultimately defined by a box with x_min, y_min, x_max, y_max coordinates but can be defined by finding words/phrases in the document and then use to create the area.

From there functions to parse the information in the area help to extract the information. E. g. by defining the area based on the question like 'Did you feel fever or feverish lately?' we can associate the answers to it and create a new key/value pair specific to this question.


# Samples

## Get context for key value pairs

Sample image:

<img src="./tests/data/patient_intake_form_sample.jpg" width=300> 

The Amazon Textract AnalyzeDocument API with the FORMS feature returns the following keys:

| Key                                          | Value          |
|----------------------------------------------|----------------|
| First Name:                                  | ALEJANDRO      |
| First Name:                                  | CARLOS         |
| Relationship to Patient:                     | BROTHER        |
| First Name:                                  | JANE           |
| Marital Status:                              | MARRIED        |
| Phone:                                       | 646-555-0111   |
| Last Name:                                   | SALAZAR        |
| Phone:                                       | 212-555-0150   |
| Relationship to Patient:                     | FRIEND         |
| Last Name:                                   | ROSALEZ        |
| City:                                        | ANYTOWN        |
| Phone:                                       | 650-555-0123   |
| Address:                                     | 123 ANY STREET |
| Yes                                          | SELECTED       |
| Yes                                          | NOT_SELECTED   |
| Date of Birth:                               | 10/10/1982     |
| Last Name:                                   | DOE            |
| Sex:                                         | M              |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| State:                                       | CA             |
| Zip Code:                                    | 12345          |
| Email Address:                               |                |
| No                                           | NOT_SELECTED   |
| No                                           | SELECTED       |
| No                                           | NOT_SELECTED   |
| Yes                                          | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |


But the information to which section of the document the individual keys belong is not obvious. Most keys appear multiple times and we want to give them context to associate them with the 'Patient', 'Emergency Contact 1', 'Emergency Contact 2' or specific questions.


This Jupyter notebook that walks through the sample: [sample notebook](./geofinder-sample-notebook.ipynb)
Make sure to have AWS credentials setup when starting the notebook locally or use a SageMaker notebook with a role including permissions for Amazon Textract. 

This code snippet is take from the notebook.

```bash
python -m pip install amazon-textract-helper amazon-textract-geofinder
```

```python
from textractgeofinder.ocrdb import AreaSelection
from textractgeofinder.tgeofinder import KeyValue, TGeoFinder, AreaSelection, SelectionElement
from textractprettyprinter.t_pretty_print import get_forms_string
from textractcaller import call_textract
from textractcaller.t_call import Textract_Features

import trp.trp2 as t2

image_filename='./tests/data/patient_intake_form_sample.jpg'

j = call_textract(input_document=image_filename, features=[Textract_Features.FORMS])


t_document = t2.TDocumentSchema().load(j)
doc_height = 1000
doc_width = 1000
geofinder_doc = TGeoFinder(j, doc_height=doc_height, doc_width=doc_width)

def set_hierarchy_kv(list_kv: list[KeyValue], t_document: t2.TDocument, page_block: t2.TBlock, prefix="BORROWER"):
    for x in list_kv:
        t_document.add_virtual_key_for_existing_key(key_name=f"{prefix}_{x.key.text}",
                                                    existing_key=t_document.get_block_by_id(x.key.id),
                                                    page_block=page_block)
# patient information
patient_information = geofinder_doc.find_phrase_on_page("patient information")[0]
emergency_contact_1 = geofinder_doc.find_phrase_on_page("emergency contact 1:", min_textdistance=0.99)[0]
top_left = t2.TPoint(y=patient_information.ymax, x=0)
lower_right = t2.TPoint(y=emergency_contact_1.ymin, x=doc_width)
form_fields = geofinder_doc.get_form_fields_in_area(
    area_selection=AreaSelection(top_left=top_left, lower_right=lower_right))
set_hierarchy_kv(list_kv=form_fields, t_document=t_document, prefix='PATIENT', page_block=t_document.pages[0])

set_hierarchy_kv(list_kv=form_fields, t_document=t_document, prefix='PATIENT', page_block=t_document.pages[0])

print(get_forms_string(t2.TDocumentSchema().dump(t_document)))
```

| Key                     | Value          |
|-------------------------|----------------|
| ...                     | ...            |
| PATIENT_first name:     | ALEJANDRO      |
| PATIENT_address:        | 123 ANY STREET |
| PATIENT_sex:            | M              |
| PATIENT_state:          | CA             |
| PATIENT_zip code:       | 12345          |
| PATIENT_marital status: | MARRIED        |
| PATIENT_last name:      | ROSALEZ        |
| PATIENT_phone:          | 646-555-0111   |
| PATIENT_email address:  |                |
| PATIENT_city:           | ANYTOWN        |
| PATIENT_date of birth:  | 10/10/1982     |

## Using the Amazon Textact Helper command line tool with the sample

This will show the full result, like the notebook.

```bash
> python -m pip install amazon-textract-helper amazon-textract-geofinder
> cat tests/data/patient_intake_form_sample.json| bin/amazon-textract-geofinder | amazon-textract --stdin --pretty-print FORMS
```

| Key                     | Value          |
|-------------------------|----------------|
| First Name:                                  | ALEJANDRO      |
| First Name:                                  | CARLOS         |
| Relationship to Patient:                     | BROTHER        |
| First Name:                                  | JANE           |
| Marital Status:                              | MARRIED        |
| Phone:                                       | 646-555-0111   |
| Last Name:                                   | SALAZAR        |
| Phone:                                       | 212-555-0150   |
| Relationship to Patient:                     | FRIEND         |
| Last Name:                                   | ROSALEZ        |
| City:                                        | ANYTOWN        |
| Phone:                                       | 650-555-0123   |
| Address:                                     | 123 ANY STREET |
| Yes                                          | SELECTED       |
| Yes                                          | NOT_SELECTED   |
| Date of Birth:                               | 10/10/1982     |
| Last Name:                                   | DOE            |
| Sex:                                         | M              |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| Yes                                          | NOT_SELECTED   |
| State:                                       | CA             |
| Zip Code:                                    | 12345          |
| Email Address:                               |                |
| No                                           | NOT_SELECTED   |
| No                                           | SELECTED       |
| No                                           | NOT_SELECTED   |
| Yes                                          | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |
| No                                           | SELECTED       |
| PATIENT_first name:                          | ALEJANDRO      |
| PATIENT_address:                             | 123 ANY STREET |
| PATIENT_sex:                                 | M              |
| PATIENT_state:                               | CA             |
| PATIENT_zip code:                            | 12345          |
| PATIENT_marital status:                      | MARRIED        |
| PATIENT_last name:                           | ROSALEZ        |
| PATIENT_phone:                               | 646-555-0111   |
| PATIENT_email address:                       |                |
| PATIENT_city:                                | ANYTOWN        |
| PATIENT_date of birth:                       | 10/10/1982     |
| EMERGENCY_CONTACT_1_first name:              | CARLOS         |
| EMERGENCY_CONTACT_1_phone:                   | 212-555-0150   |
| EMERGENCY_CONTACT_1_relationship to patient: | BROTHER        |
| EMERGENCY_CONTACT_1_last name:               | SALAZAR        |
| EMERGENCY_CONTACT_2_first name:              | JANE           |
| EMERGENCY_CONTACT_2_phone:                   | 650-555-0123   |
| EMERGENCY_CONTACT_2_last name:               | DOE            |
| EMERGENCY_CONTACT_2_relationship to patient: | FRIEND         |
| FEVER->YES                                   | SELECTED       |
| FEVER->NO                                    | NOT_SELECTED   |
| SHORTNESS->YES                               | NOT_SELECTED   |
| SHORTNESS->NO                                | SELECTED       |
| COUGH->YES                                   | NOT_SELECTED   |
| COUGH->NO                                    | SELECTED       |
| LOSS_OF_TASTE->YES                           | NOT_SELECTED   |
| LOSS_OF_TASTE->NO                            | SELECTED       |
| COVID_CONTACT->YES                           | SELECTED       |
| COVID_CONTACT->NO                            | NOT_SELECTED   |
| TRAVEL->YES                                  | NOT_SELECTED   |
| TRAVEL->NO                                   | SELECTED       |




%prep
%autosetup -n amazon-textract-geofinder-0.0.7

%build
%py3_build

%install
%py3_install
install -d -m755 %{buildroot}/%{_pkgdocdir}
if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi
if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi
if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi
if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi
pushd %{buildroot}
if [ -d usr/lib ]; then
	find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/lib64 ]; then
	find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/bin ]; then
	find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
if [ -d usr/sbin ]; then
	find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst
fi
touch doclist.lst
if [ -d usr/share/man ]; then
	find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst
fi
popd
mv %{buildroot}/filelist.lst .
mv %{buildroot}/doclist.lst .

%files -n python3-amazon-textract-geofinder -f filelist.lst
%dir %{python3_sitelib}/*

%files help -f doclist.lst
%{_docdir}/*

%changelog
* Tue Jun 20 2023 Python_Bot <Python_Bot@openeuler.org> - 0.0.7-1
- Package Spec generated