yomitoku

repository·main·Indexed 23 days ago

https://github.com/kotaro-kinoshita/yomitoku

An AI-powered document image analysis engine specialized for the Japanese language. It provides high-precision OCR, layout analysis, and table semantic parsing with support for Markdown, HTML, JSON, and searchable PDF outputs. Features include a Table Semantic Parser CLI (`yomitoku_table`) for extracting grids and key-value pairs, and an Extractor that supports both rule-based (via YAML schema) and LLM-based data extraction from fixed and non-fixed-form documents.

Tokens
37.2K
Snippets
100
Records
200
Agent score
81%

What's inside yomitoku

  1. Overview of YomiToku capabilities

    main

    YomiToku is a Document AI engine specialized in Japanese document image analysis. It provides full OCR (optical character recognition) and layout analysis to recognize, extract, and convert text and diagrams from images.

    Key Features:

    • Specialized AI Models: Uses four independent models for text detection, text recognition, layout analysis, and table structure recognition, all optimized for Japanese documents.
    • Japanese Language Support: Supports over 7,000 Japanese characters, including vertical text and unique layout structures. It also supports English documents.
    • Semantic Extraction: Leverages layout analysis, table structure parsing, and reading order estimation to preserve the semantic structure of the document.
    • Versatile Output: Supports conversion to HTML, Markdown, JSON, CSV, and text-searchable PDFs. It can also extract diagrams and images.
    • Hardware Efficiency: Optimized for GPU environments (requires < 8GB VRAM). It also features an efficient mode for fast inference on CPUs.
  2. Use the Table Semantic Parser to convert tables to structured data

    main

    TableSemanticParser is an end-to-end pipeline that detects tables in document images and converts them into structured data. It performs table region detection, cell segmentation, OCR, cell role estimation (header vs. cell), and semantic structure estimation.

    Key Features:

    • Cell Roles: Identifies header vs cell.
    • Key-Value Pairs: Extracts 1:1 relationships (e.g., Label $\rightarrow$ Value).
    • Grid Structures: Extracts matrix-like tabular records (rows and columns).
    • Searchable IDs: Tables are identified as t0, t1, etc., and cells use position-based IDs like r{row}c{col} (e.g., r0c0).

    Note: This module only supports forms that have separator lines (ruled lines). Tables without separator lines are not supported.

  3. Compare Rule-based vs LLM-based extraction modes

    main

    Yomitoku Extractor provides two modes for extracting structured data from document images and PDFs. Choosing the right mode depends on your document format and infrastructure availability.

    yomitoku_extract (Rule-based)

    • Best for: Fixed-format documents (e.g., application forms, reports, slips) where layout is consistent.
    • Mechanism: Uses KV search, grid matching, cell_id, bbox, or regex to pinpoint values.
    • Pros: Fast, high accuracy for well-defined patterns, no LLM/GPU server required.
    • Cons: Requires manual schema design based on layout or patterns.

    yomitoku_extract_with_llm (LLM-based)

    • Best for: Variable-format documents (e.g., business cards, receipts, invoices) where layout varies by issuer.
    • Mechanism: Uses an LLM server (like vLLM) to extract fields based on semantic description.
    • Pros: Highly flexible, handles irregular text and natural language, easy for rapid prototyping.
    • Cons: Slower, requires an LLM server and GPU infrastructure.

    Recommended Workflow: Start with LLM-based extraction to understand a document's structure, then switch to rule-based extraction once the extraction rules (like cell_id or regex) are established to optimize cost and speed.

  4. How YomiToku Extractor works: Rule-based vs LLM-based

    main

    YomiToku Extractor extracts structured data from images or PDFs based on a YAML schema. You can choose between two extraction modes:

    1. Rule-based (yomitoku_extract): Uses Key-Value search, grid matching, and regular expressions. It does not require an LLM. This is fast and ideal for fixed-form documents (e.g., applications, reports, slips) where positions and text patterns are predictable.

    2. LLM-based (yomitoku_extract_with_llm): Utilizes an LLM server (like vLLM). This is more flexible and ideal for non-fixed-form documents (e.g., business cards, receipts, invoices) where layouts and value patterns vary, as it can understand context.

  5. Extracting Table fields

    main

    To extract tabular data, set structure: table and define the columns using the columns key. Column matching (via cell_id, bbox, or description) is performed against the grid's header cells.

    - name: order_items
      description: Order Details
      structure: table
      columns:
        - name: product
          description: Product Name
          type: string
        - name: quantity
          description: Quantity
          type: number
          normalize: numeric
        - name: price
          description: Amount
          type: number
          normalize: numeric
  6. Understand the two semantic structures: kv_items and grids

    main

    The TableSemanticParser does more than just detect cells; it estimates the semantic structure of a table using two distinct concepts:

    1. kv_items (Key-Value): Used for data where a header (key) and a value have a 1:1 relationship. This is ideal for treating the table as a dictionary (e.g., Name $\rightarrow$ John Doe).
    2. grids (Grid Data): Used for data that follows a row/column matrix structure. This is ideal for tables where multiple records exist as rows, and each row shares the same set of attributes/columns (e.g., a list of dates, times, and amounts).

    Both kv_items and grids contain cell IDs, which can be used to look up detailed cell information (text, role, coordinates) in the cells object.

  7. How to specify extraction methods and their priority

    main

    You can specify how to locate a target cell using four methods. If multiple methods are provided, they are evaluated in the following priority order:

    1. cell_id: Direct specification using the ID assigned by TableSemanticParser (e.g., c12).
    2. bbox: Bounding box specification [x1, y1, x2, y2]. Matches if overlap is 50% or more.
    3. description: Text search (default). Performs a partial match against KV keys or table column headers.
    4. regex: Regular expression pattern. Used for scalar fields to extract a match from cell or paragraph text when the key is uncertain but the format is known.

    Note on Fallback Behavior: If cell_id is provided with description, cell_id takes precedence. If cell_id is not found, it falls back to description. regex is treated as the final resort if other methods fail.

  8. Choose between rule-based and LLM-based extraction

    main

    Yomitoku Extractor provides two extraction methods depending on your document type and requirements:

    1. Rule-based (yomitoku_extract): Best for fixed-layout documents (applications, reports, slips) where items appear in predictable locations. It uses KV search and grid matching, making it fast and requiring no LLM/GPU server. You define targets using cell_id, bbox, or regex.

    2. LLM-based (yomitoku_extract_with_llm): Best for non-fixed layout documents (business cards, receipts, invoices) where layouts vary by issuer. It uses an LLM server (like vLLM) to understand context. You define targets using a description of the field's meaning, letting the LLM handle the logic.

    Recommended Workflow: Start with LLM-based extraction to understand the document structure and refine your schema. Once the extraction rules are stable, switch to rule-based extraction to optimize for speed and cost.

  9. Understand the default structured JSON output format

    main

    The default output resolves cell IDs in kv_items and grids into text, embedding the originating cell IDs and coordinates in key_cells and value_cells.

    Key Concepts:

    • Cell IDs: Position-based in the form r{row}c{col} (e.g., r1c0). This is robust to coordinate jitter. For strict cross-run matching, use coordinate-based matching (match_policy: bbox) in templates.
    • Merging: Multiple values for one key are joined in spatial order (auto-detected vertical/horizontal) with line breaks.
    • Independence: Merging is decided by cell IDs, not text, so distinct fields with the same label are not merged.
    • Standalone Cells: Cells without a key are included with an empty key array.
    {
        "tables": [
            {
                "id": "t0",
                "box": [150, 500, 1500, 840],
                "style": "border",
                "kv_items": [
                    {
                        "key": ["Usage information", "Facility name"],
                        "value": "MLism Inc.",
                        "key_cells": [{"id": "r1c0", "box": [150, 550, 365, 645]}],
                        "value_cells": [{"id": "r1c1", "box": [365, 550, 1499, 645]}]
                    }
                ],
                "grids": [
                    {
                        "id": "g0",
                        "box": [150, 840, 1500, 1370],
                        "n_row": 6,
                        "n_col": 4,
                        "rows": [
                            {
                                "cells": [
                                    {
                                        "key": ["Date"],
                                        "value": "2025-01-30 (Mon)",
                                        "key_cells": [{"id": "r4c1", "box": [365, 840, 947, 888]}],
                                        "value_cells": [{"id": "r5c1", "box": [365, 888, 947, 968]}]
                                    }
                                ]
                            }
                        ]
                    }
                ]
            }
        ],
        "paragraphs": [
            {
                "id": "p0",
                "box": [669, 226, 983, 274],
                "score": 0.97,
                "role": "section_headings",
                "contents": "Facility Use Application"
            }
        ]
    }
  10. How extraction methods are prioritized

    main

    Each field and table column supports four methods for identifying target cells. When multiple methods are provided, they are evaluated in the following priority order:

    1. cell_id: Direct specification of a cell ID (e.g., c12) assigned by the TableSemanticParser.
    2. bbox: Bounding box position specification [x1, y1, x2, y2]. Matches by 50% or more overlap.
    3. description: Text search. Performs a partial match against KV item key text or grid header text.
    4. regex: Regular expression pattern. Used for scalar fields to extract the first matching string from cell/paragraph text.

    Note: If description and cell_id are both specified, cell_id takes priority. If cell_id is not found, it falls back to description. regex is evaluated as a last resort.

  11. Understand kv_items and grids semantic structures

    main

    The parser provides two types of semantic structured outputs beyond raw cells:

    1. kv_items (Key-Value): Represents data where a header (label) and a value correspond 1:1. This is ideal for dictionary-like data (e.g., Name $\rightarrow$ John Doe).
    2. grids (Grid / Tabular Records): Represents data that follows a row/column matrix structure. This is ideal for repeating records with multiple attributes (e.g., a list of dates, times, and descriptions).

    Both kv_items and grids store cell IDs, which can be used to look up detailed CellSchema information.

  12. Extracting KV (Key-Value) fields and merging values

    main

    KV fields are used for forms. You can set structure: kv to explicitly signal to LLMs that the field is a single scalar value.

    Merging Multiple Values

    If a single key is associated with multiple value cells (e.g., an address spanning multiple rows), set merge_values: true. This collects all values, sorts them by position, and joins them with the specified separator.

    Sorting Logic:

    • If vertical spread (Y) $\ge$ horizontal spread (X): Sort top-to-bottom.
    • Otherwise: Sort left-to-right.
    - name: address
      structure: kv
      description: Address
      type: string
      merge_values: true
      separator: ""