Packages
kreuzberg
4.7.4
4.10.2
4.10.1
4.10.0
4.9.9
4.9.7
4.9.5
4.9.4
4.9.3
4.9.2
4.9.1
4.8.6
4.8.5
4.8.4
4.8.3
4.8.2
4.8.1
4.8.0
4.7.4
4.7.3
4.7.2
4.7.1
4.7.0
4.6.3
4.6.2
4.6.1
4.6.0
4.5.4
4.5.3
4.5.2
4.5.1
4.4.6
4.4.5
4.4.4
4.4.3
4.4.2
4.4.1
4.4.0
4.3.8
4.3.7
4.3.6
4.3.5
4.3.4
4.3.3
4.3.2
4.3.0
4.2.15
4.2.14
4.2.13
4.2.12
4.2.11
4.2.10
4.2.9
4.2.8
4.2.7
4.2.6
4.2.5
4.2.4
4.2.3
4.2.2
4.2.1
4.2.0
4.1.2
4.1.1
4.1.0
4.0.8
4.0.7
4.0.6
4.0.4
4.0.3
4.0.2
4.0.1
4.0.0
4.0.0-rc.27
4.0.0-rc.26
High-performance document intelligence library with OCR support
Current section
Files
Jump to
Current section
Files
lib/kreuzberg/table.ex
defmodule Kreuzberg.Table do
@moduledoc """
Structure representing an extracted table from a document.
Matches the Rust `Table` struct with cells, markdown, page number, and optional bounding box.
## Fields
* `:cells` - Two-dimensional list of table cells [[cell1, cell2], ...]
* `:markdown` - Markdown representation of the table
* `:page_number` - Page number where table appears (0-indexed)
* `:bounding_box` - Bounding box coordinates {x0, y0, x1, y1} if available, nil otherwise
## Examples
iex> table = %Kreuzberg.Table{
...> cells: [["Name", "Age"], ["Alice", "30"]],
...> markdown: "| Name | Age |\\n|------|-----|\\n| Alice | 30 |",
...> page_number: 0
...> }
iex> table.cells
[["Name", "Age"], ["Alice", "30"]]
"""
@type t :: %__MODULE__{
cells: list(list(String.t())),
markdown: String.t(),
page_number: non_neg_integer(),
bounding_box: map() | nil
}
defstruct cells: [], markdown: "", page_number: 0, bounding_box: nil
@doc """
Creates a Table struct from a map.
## Examples
iex> Kreuzberg.Table.from_map(%{"cells" => [["A", "B"]], "markdown" => "| A | B |", "page_number" => 0})
%Kreuzberg.Table{cells: [["A", "B"]], markdown: "| A | B |", page_number: 0}
"""
@spec from_map(map()) :: t()
def from_map(data) when is_map(data) do
%__MODULE__{
cells: data["cells"] || [],
markdown: data["markdown"] || "",
page_number: data["page_number"] || 0,
bounding_box: data["bounding_box"]
}
end
@doc """
Converts a Table struct to a map.
"""
@spec to_map(t()) :: map()
def to_map(%__MODULE__{} = table) do
%{
"cells" => table.cells,
"markdown" => table.markdown,
"page_number" => table.page_number,
"bounding_box" => table.bounding_box
}
end
@doc """
Returns the number of rows in the table.
## Examples
iex> table = %Kreuzberg.Table{cells: [["A", "B"], ["1", "2"]]}
iex> Kreuzberg.Table.row_count(table)
2
"""
@spec row_count(t()) :: non_neg_integer()
def row_count(%__MODULE__{cells: cells}) when is_list(cells), do: length(cells)
@doc """
Returns the number of columns in the table.
## Examples
iex> table = %Kreuzberg.Table{cells: [["A", "B"], ["1", "2"]]}
iex> Kreuzberg.Table.column_count(table)
2
"""
@spec column_count(t()) :: non_neg_integer()
def column_count(%__MODULE__{cells: []}), do: 0
def column_count(%__MODULE__{cells: [first | _]}) when is_list(first), do: length(first)
def column_count(%__MODULE__{cells: _}), do: 0
end