Amazon Textract Response Parser

repository·master·Indexed 42 days ago

https://github.com/aws-samples/amazon-textract-response-parser

A library (version 0.4.3) that simplifies parsing JSON responses from Amazon Textract by providing high-level, language-specific abstractions for document elements such as forms, tables, lines, and words. It supports Python, JavaScript/TypeScript, and C# (.NET Core), allowing developers to easily access document data without manually traversing complex JSON structures.

Tokens
24.7K
Snippets
73
Records
104
Agent score
53%

What's inside Amazon Textract Response Parser

  1. Overview of Textract Response Parser for A2I

    master
    The Textract-Amazon Augmented AI (A2I) response parser library allows you to easily parse the JSON returned by an A2I human review workflow. The library processes the JSON and provides language-specific constructs (available in Python, JavaScript/TypeScript, and C#) to interact with different parts of the document, such as forms and tables.
  2. Overview of Textract Response Parser

    master
    The Textract Response Parser is a library designed to simplify the process of parsing JSON responses returned by Amazon Textract. Instead of manually traversing complex JSON structures, the library provides language-specific constructs (Python, JavaScript/TypeScript, and C#) to easily access and work with different parts of a document, such as lines, words, forms, and tables.
  3. Navigate document text using TRP helper methods

    master

    The library provides structured ways to navigate the document hierarchy instead of exposing raw arrays directly. This helps avoid accidental mutations and provides cleaner access patterns:

    • .n*** properties: Count items (e.g., doc.nPages).
    • .list***(): Returns a copy of the underlying array (e.g., doc.listPages()).
    • .iter***(): Returns an iterator for collections (e.g., page.iterLines()).
    • .***At***(): Fetches a specific item by index (e.g., table.cellAt(2, 4)).
    // Navigate the document hierarchy:
    console.log(`Opened doc with ${doc.nPages} pages`);
    console.log(
      `The first word of the first line is ${doc.pageNumber(1).lineAtIndex(0).wordAtIndex(0).text}`
    );
    
    // Iterate through content:
    for (const page of doc.iterPages()) {
      for (const line of page.iterLines()) {
        for (const word of line.iterWords()) {
          console.log(word.text);
        }
      }
    }
    
    // Get snapshot arrays instead of iterators:
    const linesArrsByPage = doc.listPages().map((p) => p.listLines());
  4. How mutation operations work in Textract Response Parser

    master

    The library allows you to augment or edit Textract JSON objects using its classes (e.g., TextractDocument, Page, Word). When using mutation operations provided by the library, keep the following behaviors in mind:

    • In-place modification: Mutation operations provided by library classes modify the source API JSON object in-place and attempt to ensure self-consistency.
    • Accessing raw JSON: You can access the underlying API JSON object for any class via the .dict property (e.g., word.dict, table.dict). If you modify the JSON directly via .dict, you are responsible for updating any required references in other objects to maintain consistency.
    • Dynamic reflection: Individual block-level changes made to the underlying API JSON (like overriding word text or coordinates) are dynamically reflected in the parsed TRP objects. However, changes affecting inter-block relationships may cause staleness issues.
    • Array properties: To prevent unsupported arbitrary mutations (like table.rows.pop()), the library generally exposes getters and iterators rather than direct access to lists. Use methods like table.nRows, table.iterRows(), table.listRows(), or table.cellsAt() instead of attempting to manipulate arrays directly.
  5. How Serializer/Deserializer works with Textract JSON

    master

    The library uses the marshmallow framework to convert raw Textract JSON dictionaries into structured Python objects (and vice versa).

    • Use TDocumentSchema().load(j) to deserialize a standard Textract JSON dictionary j into a TDocument object.
    • Use TDocumentSchema().dump(t_doc) to serialize a TDocument object back into a JSON-compatible dictionary.
    • For AnalyzeID responses, use TAnalyzeIdDocumentSchema instead of TDocumentSchema.
    # Deserialize standard Textract JSON
    from trp.trp2 import TDocument, TDocumentSchema
    t_doc = TDocumentSchema().load(j)
    
    # Serialize back to JSON
    t_doc = TDocumentSchema().dump(t_doc)
    
    # For AnalyzeID
    from trp.trp2_analyzeid import TAnalyzeIdDocument, TAnalyzeIdDocumentSchema
    t_doc = TAnalyzeIdDocumentSchema().load(json.loads(j))
  6. Handle multi-page documents with Asynchronous Textract APIs

    master

    The synchronous request/response APIs used in many basic examples are limited to images or single-page documents. For multi-page documents or large batches, you must use the Asynchronous Textract APIs.

    Workflow for Asynchronous Processing:

    1. Upload: Upload the source document to an Amazon S3 bucket.
    2. Start Analysis: Call StartDocumentAnalysis (or similar async commands) which returns a job ID.
    3. Retrieve Results: Use the job ID with GetDocumentAnalysis to retrieve the results once the job is complete.

    Best Practices for Scale:

    • Avoid Polling: Instead of repeatedly calling GetDocumentAnalysis, use Amazon SNS notifications to trigger your application when the analysis is ready. This is more quota-efficient.
    • Manage Quotas: Be aware of Amazon Textract quota limits. For spiky workloads, implement a queuing mechanism to manage request rates.
    • Infrastructure: A production-ready solution typically involves AWS Lambda and Amazon SNS topics.
  7. Understand the parsed Form data structure

    master

    When parsing Amazon Textract responses for Forms, the output provides a structured mapping of keys to their corresponding values. Each field entry typically contains:

    • key: A dictionary representing the label (e.g., 'Phone Number:'), including its block metadata (type, confidence, geometry) and content (the individual text segments making up the key).
    • value: A dictionary representing the extracted value (e.g., '555-0100'), including its block metadata and content.

    This structure allows you to programmatically access specific fields by their text label or by navigating the block relationships.

  8. Parse Amazon Textract A2I Human Review responses in Python

    master

    The Textract-Augmented AI (A2I) response parser library allows you to parse the JSON returned by an A2I human review workflow. It provides language-specific constructs to navigate document elements like forms and fields.

    To use the parser, you must first obtain the human review results from S3 (which are published after the human review is complete) and extract the specific Textract response segment from the A2I JSON structure. Once extracted, you can initialize a Document object to iterate through pages and fields.

    # 1. Extract the Textract response from the A2I JSON
    # The path in the JSON is typically: a2i_json["humanAnswers"][0]["answerContent"]["AWS/Textract/AnalyzeDocument/Forms/V1"]
    a2i_response = a2i_json["humanAnswers"][0]["answerContent"]["AWS/Textract/AnalyzeDocument/Forms/V1"]
    
    # 2. Initialize the Document object
    doc = Document(a2i_response)
    
    # 3. Iterate over elements
    for page in doc.pages:
        for field in page.form.fields:
            k = field.key.text if field.key else ""
            v = field.value.text if field.value else ""
            print(f"Field: Key: {k}, Value: {v}")
  9. Import Textract Response Parser in Node.js or Browser

    master

    Depending on your environment, you can import the library using CommonJS require or ES6 import statements. For direct browser usage without a module framework, you can link to a CDN like unpkg.

    // With CommonJS-style require:
    const { TextractDocument, TextractIdentity } = require("amazon-textract-response-parser");
    
    // Or ES-style module imports:
    import { TextractDocument, TextractExpense } from "amazon-textract-response-parser";
    <script src="https://unpkg.com/amazon-textract-response-parser@x.y.z"></script>
    
    <script>
      // Use the main parser classes:
      var doc = new trp.TextractDocument(...);
      // Or other exported utility functions/classes/enums/etc:
      var avg = trp.aggregate([1, 2, 3], trp.AggregationMethod.Mean);
    </script>
  10. Run tests for Textract Response Parser

    master

    You can run tests using npm to validate the library. Note that the full test suite includes integration tests that call Amazon Textract APIs.

    Full Integration Tests

    To run npm run test, your environment must be configured with AWS credentials (e.g., via the AWS CLI). Warning: This will make billable API requests to Amazon Textract.

    Unit Tests

    To run only the local unit tests without making AWS API calls, use:

    npm run test:unit
  11. Switch from local TRP.js build to published version

    master

    To use a published version of amazon-textract-response-parser instead of the local source code used in these examples, follow these steps based on your environment:

    NodeJS Projects

    In your package.json, replace the relative file path with a version specifier and reinstall dependencies:

    From: "amazon-textract-response-parser": "file:../.."

    To: "amazon-textract-response-parser": "^0.4.3" (or your desired version)

    Then run:

    npm install

    Browser IIFE Projects

    Edit the <script> tag in your HTML file to point to a CDN URL or a local path to a downloaded trp.min.js file.

    {
      "dependencies": {
        "amazon-textract-response-parser": "^0.4.3"
      }
    }