Class: Tabula::Table

Inherits:
Rectangle show all
Defined in:
lib/tabula/table/table.rb

Overview

Represents an extracted table with rows and cells. Provides methods for accessing table data and converting to various formats.

Direct Known Subclasses

WithRulingLines

Defined Under Namespace

Classes: WithRulingLines

Constant Summary

Constants inherited from Rectangle

Rectangle::VERTICAL_COMPARISON_THRESHOLD

Instance Attribute Summary collapse

Attributes inherited from Rectangle

#height, #left, #top, #width

Instance Method Summary collapse

Methods inherited from Rectangle

#<=>, #==, #area, #bottom, #bottom=, bounding_box_of, #bounds, #center, #contains?, #contains_point?, #dup, from_bounds, from_points, #hash, #horizontal_overlap, #horizontally_overlaps?, #intersection, #intersects?, #merge, #merge!, #overlap_ratio, #points, #right, #right=, #vertical_overlap, #vertically_overlaps?, #x, #x=, #y, #y=

Constructor Details

#initialize(extraction_method: 'unknown', page_number: nil) ⇒ Table

Returns a new instance of Table.

Parameters:

  • extraction_method (String) (defaults to: 'unknown') —

    method used for extraction

  • page_number (Integer) (defaults to: nil) —

    page number where table was found



11
12
13
14
15
16
17
18
19
# File 'lib/tabula/table/table.rb', line 11

def initialize(extraction_method: 'unknown', page_number: nil)
  super(0, 0, 0, 0)
  @extraction_method = extraction_method
  @page_number = page_number
  @cells = {} # { [row, col] => Cell }
  @row_count = 0
  @col_count = 0
  @memoized_rows = nil
end

Instance Attribute Details

#col_count ⇒ Integer (readonly)

Get number of columns

Returns:

  • (Integer)


60
61
62
# File 'lib/tabula/table/table.rb', line 60

def col_count
  @col_count
end

#extraction_method ⇒ Object (readonly)

Returns the value of attribute extraction_method.



7
8
9
# File 'lib/tabula/table/table.rb', line 7

def extraction_method
  @extraction_method
end

#page_number ⇒ Object (readonly)

Returns the value of attribute page_number.



7
8
9
# File 'lib/tabula/table/table.rb', line 7

def page_number
  @page_number
end

#row_count ⇒ Integer (readonly)

Get number of rows

Returns:

  • (Integer)


56
57
58
# File 'lib/tabula/table/table.rb', line 56

def row_count
  @row_count
end

Instance Method Details

#add(row, col, cell) ⇒ Object

Add a cell at a specific position

Parameters:

  • row (Integer) —

    row index (0-based)

  • col (Integer) —

    column index (0-based)

  • cell (Cell) —

    cell to add



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/tabula/table/table.rb', line 25

def add(row, col, cell)
  @cells[[row, col]] = cell
  @row_count = [row + 1, @row_count].max
  @col_count = [col + 1, @col_count].max
  @memoized_rows = nil # Invalidate cache

  # Update bounds
  if @cells.size == 1
    @top = cell.top
    @left = cell.left
    @width = cell.width
    @height = cell.height
  else
    merge!(cell)
  end

  self
end

#cells ⇒ Array<Cell>

Get all cells as flat array

Returns:



70
71
72
# File 'lib/tabula/table/table.rb', line 70

def cells
  @cells.values
end

#column(index) ⇒ Array<Cell>

Get a specific column

Parameters:

  • index (Integer) —

    column index

Returns:



84
85
86
# File 'lib/tabula/table/table.rb', line 84

def column(index)
  rows.map { |r| r[index] || Cell.empty }
end

#each_cell {|Integer, Integer, Cell| ... } ⇒ Object

Iterate over cells

Yields:

  • (Integer, Integer, Cell) —

    row, col, cell



145
146
147
148
149
150
151
# File 'lib/tabula/table/table.rb', line 145

def each_cell
  rows.each_with_index do |row, row_idx|
    row.each_with_index do |cell, col_idx|
      yield row_idx, col_idx, cell
    end
  end
end

#each_row {|Array<Cell>| ... } ⇒ Object

Iterate over rows

Yields:

  • (Array<Cell>) —

    each row



139
140
141
# File 'lib/tabula/table/table.rb', line 139

def each_row(&)
  rows.each(&)
end

#empty? ⇒ Boolean

Check if table is empty

Returns:

  • (Boolean)


133
134
135
# File 'lib/tabula/table/table.rb', line 133

def empty?
  @cells.empty?
end

#get_cell(row, col) ⇒ Cell Also known as: []

Get a cell at a specific position

Parameters:

  • row (Integer) —

    row index

  • col (Integer) —

    column index

Returns:

  • (Cell) —

    cell at position, or empty cell if none



48
49
50
# File 'lib/tabula/table/table.rb', line 48

def get_cell(row, col)
  @cells[[row, col]] || Cell.empty
end

#inspect ⇒ Object



157
158
159
# File 'lib/tabula/table/table.rb', line 157

def inspect
  to_s
end

#row(index) ⇒ Array<Cell>

Get a specific row

Parameters:

  • index (Integer) —

    row index

Returns:



77
78
79
# File 'lib/tabula/table/table.rb', line 77

def row(index)
  rows[index] || []
end

#rows ⇒ Array<Array<Cell>>

Get all rows as 2D array

Returns:

  • (Array<Array<Cell>>)


64
65
66
# File 'lib/tabula/table/table.rb', line 64

def rows
  @rows ||= compute_rows
end

#to_a ⇒ Array<Array<String>>

Convert to 2D array of strings

Returns:

  • (Array<Array<String>>)


90
91
92
# File 'lib/tabula/table/table.rb', line 90

def to_a
  rows.map { |row| row.map(&:text) }
end

#to_csv(**options) ⇒ String

Convert to CSV string

Parameters:

  • options (Hash) —

    options for CSV generation

Returns:

  • (String)


97
98
99
100
101
102
# File 'lib/tabula/table/table.rb', line 97

def to_csv(**options)
  require 'csv'
  CSV.generate(**options) do |csv|
    to_a.each { |row| csv << row }
  end
end

#to_h ⇒ Hash

Convert to hash (for JSON serialization)

Returns:

  • (Hash)


112
113
114
115
116
117
118
119
120
121
122
# File 'lib/tabula/table/table.rb', line 112

def to_h
  {
    extraction_method: @extraction_method,
    page_number: @page_number,
    top: top,
    left: left,
    width: width,
    height: height,
    data: to_a
  }
end

#to_json(*args) ⇒ String

Convert to JSON string

Returns:

  • (String)


126
127
128
129
# File 'lib/tabula/table/table.rb', line 126

def to_json(*args)
  require 'json'
  to_h.to_json(*args)
end

#to_s ⇒ Object



153
154
155
# File 'lib/tabula/table/table.rb', line 153

def to_s
  "Table[#{row_count}x#{col_count}](#{left}, #{top}, #{width}, #{height})"
end

#to_tsv ⇒ String

Convert to TSV string

Returns:

  • (String)


106
107
108
# File 'lib/tabula/table/table.rb', line 106

def to_tsv
  to_a.map { |row| row.join("\t") }.join("\n")
end