Notes
Notes - notes.io |
import time
def lambda_handler(event, context):
# create a boto3 client
client = boto3.client('textract')
# the name of the S3 bucket
documentName = event['documentName']
bucketName = event['bucketName']
documentLocation = {
'S3Object': {
'Bucket': bucketName,
'Name': documentName
}
}
response = client.start_document_analysis(
DocumentLocation=documentLocation,
FeatureTypes=["FORMS"]
)
# Get the JobId of the started job
jobId = response['JobId']
# Initial waiting time before checking job status
time.sleep(5)
while True:
response = client.get_document_analysis(JobId=jobId)
status = response['JobStatus']
if status in ['SUCCEEDED', 'FAILED']:
break
print('Waiting for job to complete...')
time.sleep(5)
# The get_document_analysis response contains a 'Blocks' array -
# we can iterate over this to find key-value pairs
all_blocks = []
next_token = None
while True:
if next_token:
response = client.get_document_analysis(JobId=jobId, NextToken=next_token)
else:
response = client.get_document_analysis(JobId=jobId)
all_blocks.extend(response['Blocks'])
next_token = response.get('NextToken', None)
if next_token is None:
break
detected_kv = {}
for block in all_blocks:
if block['BlockType'] == 'KEY_VALUE_SET':
if 'KEY' in block['EntityTypes']:
key, key_confidence = get_text_from_block(block, all_blocks)
if 'VALUE' in block['EntityTypes']:
value, value_confidence = get_text_from_block(block, all_blocks)
# The 'Page' field in the block tells you which page this key-value pair is from
page_number = block['Page']
if page_number not in detected_kv:
detected_kv[page_number] = {}
detected_kv[page_number][key] = {'Value': value, 'KeyConfidence': key_confidence, 'ValueConfidence': value_confidence}
return detected_kv
def get_text_from_block(block, blocks):
text = ''
confidence = 0.0
for relationship in block.get('Relationships', []):
if relationship['Type'] == 'CHILD':
for blockId in relationship['Ids']:
text_blocks = [b for b in blocks if b['Id'] == blockId]
if text_blocks:
text += text_blocks[0].get('Text', '')
confidence = text_blocks[0].get('Confidence', 0.0)
return text, confidence
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team