Class: Tabula::Extractors::Spreadsheet
- Inherits:
-
ExtractionAlgorithm
- Object
- ExtractionAlgorithm
- Tabula::Extractors::Spreadsheet
- Defined in:
- lib/tabula/extractors/spreadsheet_extraction_algorithm.rb
Overview
Lattice-mode extraction algorithm. Extracts tables by analyzing ruling lines (cell borders) in the PDF.
Constant Summary collapse
- MIN_CELLS =
Minimum cells required for a valid table
4- TABULAR_RATIO_THRESHOLD =
Magic heuristic for determining tabular content
0.65
Class Method Summary collapse
-
.tabular?(page) ⇒ Boolean
Check if a page contains tabular content.
Instance Method Summary collapse
-
#extract(page) ⇒ Array<Table>
Extract tables from a page.
Methods inherited from ExtractionAlgorithm
Constructor Details
This class inherits a constructor from Tabula::Extractors::ExtractionAlgorithm
Class Method Details
.tabular?(page) ⇒ Boolean
Check if a page contains tabular content
42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/tabula/extractors/spreadsheet_extraction_algorithm.rb', line 42 def self.tabular?(page) extractor = new tables = extractor.extract(page) return false if tables.empty? # Check if tables have reasonable structure tables.any? do |table| ratio = table.row_count.to_f / table.col_count ratio.between?(TABULAR_RATIO_THRESHOLD, 1.0 / TABULAR_RATIO_THRESHOLD) end end |
Instance Method Details
#extract(page) ⇒ Array<Table>
Extract tables from a page
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/tabula/extractors/spreadsheet_extraction_algorithm.rb', line 17 def extract(page) horizontal = page.horizontal_rulings vertical = page.vertical_rulings return [] if horizontal.empty? || vertical.empty? # Find cells from ruling intersections cells = find_cells(horizontal, vertical) return [] if cells.size < MIN_CELLS # Find spreadsheet regions from cells and get cells per region cell_groups = find_spreadsheet_areas_with_cells(cells) return [] if cell_groups.empty? # Extract tables from each region using the found cells tables = cell_groups.map do |region_cells| extract_table_from_cells(page, region_cells, horizontal, vertical) end tables.reject(&:empty?) end |