Class: Arrolio::Harness::TextExtractor

Inherits:
Object
  • Object
show all
Defined in:
lib/arrolio/harness/text_extractor.rb

Overview

Extracts text from PDF files for diff/matching. Uses poppler's pdftotext as the primary backend (reliable with CIDFont subsets); falls back to returning an empty string if the tool is unavailable.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ TextExtractor

Returns a new instance of TextExtractor.



14
15
16
# File 'lib/arrolio/harness/text_extractor.rb', line 14

def initialize(path)
  @path = path.to_s
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



12
13
14
# File 'lib/arrolio/harness/text_extractor.rb', line 12

def path
  @path
end

Instance Method Details

#full_text(layout: false) ⇒ Object

Returns all text as a single String (pages joined by "\f").



35
36
37
# File 'lib/arrolio/harness/text_extractor.rb', line 35

def full_text(layout: false)
  pages(layout: layout).join("\f")
end

#pages(layout: false) ⇒ Object

Returns an Array of Strings — one per page. Uses pdftotext in raw mode (no layout reconstruction) for maximum text recovery. For layout-aware extraction, pass layout: true.



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/arrolio/harness/text_extractor.rb', line 21

def pages(layout: false)
  return [] unless pdftotext_available?

  args = ['pdftotext']
  args << '-layout' if layout
  args << '-q' << @path << '-'
  stdout, _stderr, status = Open3.capture3(*args)
  return [] unless status.success?

  text = stdout.force_encoding('UTF-8').scrub
  split_by_page(text)
end

#word_countObject

Word count across all pages.



40
41
42
# File 'lib/arrolio/harness/text_extractor.rb', line 40

def word_count
  pages.sum { |text| text.split.length }
end