Class: Tabula::Table
Overview
Represents an extracted table with rows and cells. Provides methods for accessing table data and converting to various formats.
Direct Known Subclasses
Defined Under Namespace
Classes: WithRulingLines
Constant Summary
Constants inherited from Rectangle
Rectangle::VERTICAL_COMPARISON_THRESHOLD
Instance Attribute Summary collapse
-
#col_count ⇒ Integer
readonly
Get number of columns.
-
#extraction_method ⇒ Object
readonly
Returns the value of attribute extraction_method.
-
#page_number ⇒ Object
readonly
Returns the value of attribute page_number.
-
#row_count ⇒ Integer
readonly
Get number of rows.
Attributes inherited from Rectangle
Instance Method Summary collapse
-
#add(row, col, cell) ⇒ Object
Add a cell at a specific position.
-
#cells ⇒ Array<Cell>
Get all cells as flat array.
-
#column(index) ⇒ Array<Cell>
Get a specific column.
-
#each_cell {|Integer, Integer, Cell| ... } ⇒ Object
Iterate over cells.
-
#each_row {|Array<Cell>| ... } ⇒ Object
Iterate over rows.
-
#empty? ⇒ Boolean
Check if table is empty.
-
#get_cell(row, col) ⇒ Cell
(also: #[])
Get a cell at a specific position.
-
#initialize(extraction_method: 'unknown', page_number: nil) ⇒ Table
constructor
A new instance of Table.
- #inspect ⇒ Object
-
#row(index) ⇒ Array<Cell>
Get a specific row.
-
#rows ⇒ Array<Array<Cell>>
Get all rows as 2D array.
-
#to_a ⇒ Array<Array<String>>
Convert to 2D array of strings.
-
#to_csv(**options) ⇒ String
Convert to CSV string.
-
#to_h ⇒ Hash
Convert to hash (for JSON serialization).
-
#to_json(*args) ⇒ String
Convert to JSON string.
- #to_s ⇒ Object
-
#to_tsv ⇒ String
Convert to TSV string.
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.
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
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
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
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
70 71 72 |
# File 'lib/tabula/table/table.rb', line 70 def cells @cells.values end |
#column(index) ⇒ Array<Cell>
Get a specific column
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
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
139 140 141 |
# File 'lib/tabula/table/table.rb', line 139 def each_row(&) rows.each(&) end |
#empty? ⇒ Boolean
Check if table is empty
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
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
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
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
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
97 98 99 100 101 102 |
# File 'lib/tabula/table/table.rb', line 97 def to_csv(**) require 'csv' CSV.generate(**) do |csv| to_a.each { |row| csv << row } end end |
#to_h ⇒ Hash
Convert to hash (for JSON serialization)
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
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
106 107 108 |
# File 'lib/tabula/table/table.rb', line 106 def to_tsv to_a.map { |row| row.join("\t") }.join("\n") end |