Class: RTesseract::Command

Inherits:
Object
  • Object
show all
Defined in:
lib/rtesseract/command.rb

Constant Summary collapse

FIXED =
[:command, :psm, :oem, :lang, :tessdata_dir, :user_words, :user_patterns, :config_file]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, output, options) ⇒ Command

Returns a new instance of Command.



9
10
11
12
13
# File 'lib/rtesseract/command.rb', line 9

def initialize(source, output, options)
  @source = source
  @output = output
  @options = options
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



7
8
9
# File 'lib/rtesseract/command.rb', line 7

def options
  @options
end

Instance Method Details

#configsObject



15
16
17
# File 'lib/rtesseract/command.rb', line 15

def configs
  @options.to_h.map { |key, value| ['-c', "#{key}=#{value}"] unless FIXED.include?(key) }.compact
end

#full_commandObject



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/rtesseract/command.rb', line 19

def full_command
  command = [options.command, @source, @output]

  command << ['--psm', options.psm.to_s] if options.psm
  command << ['--oem', options.oem.to_s] if options.oem
  command << ['-l', options.lang] if options.lang

  command << ['--tessdata_dir', options.tessdata_dir] if options.tessdata_dir
  command << ['--user_words', options.user_words] if options.user_words
  command << ['--user_patterns', options.user_patterns] if options.user_patterns

  command << configs

  command << options.config_file.to_s if options.config_file

  command.flatten
end

#runObject



37
38
39
# File 'lib/rtesseract/command.rb', line 37

def run
  Open3.capture2e(*full_command)
end