class Sanzang::BatchTranslator

BatchTranslator can handle batches of files for translation, and may also be able to translate them in parallel using multiprocessing, if your Ruby virtual machine supports it. This class inherits from Translator.

Public Instance Methods

translate_batch(fpath_pairs, verbose = true, jobs = nil) click to toggle source

Translate a batch of files. The main parameter is an array, each element of which should be a two-dimensional array with the first element being the input file path, and the second element being the output file path. If the verbose parameter is true, then print progress to STDERR. The return value is an array containing all the output file paths.

# File lib/sanzang/batch_translator.rb, line 32
def translate_batch(fpath_pairs, verbose = true, jobs = nil)
  options = {}
  if RUBY_PLATFORM =~ /java/
    options[:in_threads] = jobs || Sanzang::Platform.processor_count
  elsif Sanzang::Platform.unix_processes?
    options[:in_processes] = jobs || Sanzang::Platform.processor_count
  else
    options[:in_processes] = 0
  end
  Parallel.map(fpath_pairs, options) do |f1,f2|
    translate_io(f1, f2)
    if verbose
      $stderr.write "[#{Process.pid}] #{File.expand_path(f2)} \n"
      $stderr.flush
    end
    f2
  end
end
translate_to_dir(in_fpaths, out_dir, verbose = true, jobs = nil) click to toggle source

Translate a list of files to some output directory. The names of the files written to the output directory will be the same as those of their respective input files. If the verbose parameter is true, then print progress to STDERR.

# File lib/sanzang/batch_translator.rb, line 56
def translate_to_dir(in_fpaths, out_dir, verbose = true, jobs = nil)
  pairs = []
  in_fpaths.each do |f1|
    pairs << [f1, File.join(out_dir, File.basename(f1))]
  end
  translate_batch(pairs, verbose, jobs)
end