Kreuzberg.OcrElement (kreuzberg v4.4.2)

Copy Markdown View Source

OCR-extracted text element with detailed positioning and confidence information.

Represents a text element detected by OCR with its content, geometry, confidence scores, and hierarchical relationships within a document.

Fields

  • :text - The recognized text content
  • :geometry - Bounding geometry for the element, or nil
  • :confidence - Confidence scores for detection and recognition, or nil
  • :level - Hierarchical level of the element (e.g., "page", "block", "line", "word"), or nil
  • :rotation - Rotation information for the text, or nil
  • :page_number - Page number where element appears (1-indexed), or nil
  • :parent_id - ID of parent element in hierarchy, or nil
  • :backend_metadata - Backend-specific metadata map, or nil

Examples

iex> element = %Kreuzberg.OcrElement{
...>   text: "Hello World",
...>   geometry: %Kreuzberg.OcrBoundingGeometry{type: "rect", left: 10.0, top: 20.0, width: 100.0, height: 30.0},
...>   confidence: %Kreuzberg.OcrConfidence{detection: 0.95, recognition: 0.92},
...>   level: "line",
...>   page_number: 1
...> }
iex> element.text
"Hello World"

Summary

Functions

Creates an OcrElement struct from a map.

Converts an OcrElement struct to a map.

Types

t()

@type t() :: %Kreuzberg.OcrElement{
  backend_metadata: map() | nil,
  confidence: Kreuzberg.OcrConfidence.t() | nil,
  geometry: Kreuzberg.OcrBoundingGeometry.t() | nil,
  level: String.t() | nil,
  page_number: integer() | nil,
  parent_id: String.t() | nil,
  rotation: Kreuzberg.OcrRotation.t() | nil,
  text: String.t()
}

Functions

from_map(data)

@spec from_map(map()) :: t()

Creates an OcrElement struct from a map.

Converts a plain map (typically from NIF/Rust) into a proper struct, handling nested geometry, confidence, and rotation data.

Parameters

  • data - A map containing OCR element fields

Returns

An OcrElement struct with properly typed fields.

Examples

iex> element_map = %{
...>   "text" => "Hello World",
...>   "geometry" => %{"type" => "rect", "left" => 10.0, "top" => 20.0, "width" => 100.0, "height" => 30.0},
...>   "confidence" => %{"detection" => 0.95, "recognition" => 0.92},
...>   "level" => "line",
...>   "page_number" => 1
...> }
iex> element = Kreuzberg.OcrElement.from_map(element_map)
iex> element.text
"Hello World"

to_map(element)

@spec to_map(t()) :: map()

Converts an OcrElement struct to a map.

Useful for serialization and passing to external systems.

Parameters

  • element - An OcrElement struct

Returns

A map with string keys representing all fields.

Examples

iex> element = %Kreuzberg.OcrElement{
...>   text: "Hello World",
...>   geometry: %Kreuzberg.OcrBoundingGeometry{type: "rect", left: 10.0, top: 20.0, width: 100.0, height: 30.0},
...>   confidence: %Kreuzberg.OcrConfidence{detection: 0.95, recognition: 0.92},
...>   level: "line",
...>   page_number: 1
...> }
iex> map = Kreuzberg.OcrElement.to_map(element)
iex> map["text"]
"Hello World"