class Sanzang::Command::Reflow

This class provides a command for text reformatting for CJK languages. This reformatting is typically for use prior to processing the text with the translation commands. The reason for doing this is so that initial text transformations will be done to ensure (1) that terms will be translated reliably, and (2) that the final output of the translation will be readable by the user (i.e. lines not too long).

Attributes

name[R]

The name of the command

Public Class Methods

new() click to toggle source

Create a new instance of the reflow command.

# File lib/sanzang/command/reflow.rb, line 33
def initialize
  @name = "sanzang reflow"
  @encoding = Sanzang::Platform.data_encoding
  @infile = nil
  @outfile = nil
  @verbose = false
end

Public Instance Methods

run(args) click to toggle source

Run the reflow command with the given arguments. The parameter args would typically be an array of command options and parameters. Calling this with the “-h” or “–help” option will print full usage information necessary for running this command.

# File lib/sanzang/command/reflow.rb, line 46
def run(args)
  parser = option_parser
  parser.parse!(args)

  if args.length != 0
    $stderr.puts(parser)
    return 1
  end

  begin
    fin = @infile ? File.open(@infile, "r") : $stdin
    fin.binmode.set_encoding(@encoding)
    fout = @outfile ? File.open(@outfile, "w") : $stdout
    fout.binmode.set_encoding(@encoding)
    fout.write(Sanzang::Formatting.reflow_cjk(fin.read))
  ensure
    if defined?(fin) and fin.class == File
      fin.close if not fin.closed?
    end
    if defined?(fout) and fout.class == File
      fout.close if not fout.closed?
    end
  end

  return 0
rescue SystemExit => err
  return err.status
rescue Interrupt
  puts
  return 0
rescue Errno::EPIPE
  return 0
rescue Exception => err
  if @verbose
    $stderr.puts err.backtrace
  end
  $stderr.puts "#{@name.split[0]}: #{err.inspect}"
  return 1
end

Private Instance Methods

option_parser() click to toggle source

An OptionParser for the command

# File lib/sanzang/command/reflow.rb, line 94
def option_parser
  OptionParser.new do |op|
    op.banner = "Usage: #{@name} [options]\n"

    op.banner << "\nReformat text into lines based on spacing, "
    op.banner << "punctuation, etc. This should work\nfor the CJK "
    op.banner << "languages (Chinese, Japanese, and Korean). By default, "
    op.banner << "text is read\nfrom STDIN and written to STDOUT."
    op.banner << "\n"

    op.banner << "\nOptions:\n"

    op.on("-h", "--help", "show this help message and exit") do |v|
      puts op
      exit 0
    end
    op.on("-E", "--encoding=ENC", "set data encoding to ENC") do |v|
      @encoding = Encoding.find(v)
    end
    op.on("-L", "--list-encodings", "list possible encodings") do |v|
      Sanzang::Platform.valid_encodings.each {|e| puts e.to_s }
      exit 0
    end
    op.on("-i", "--infile=FILE", "read input text from FILE") do |v|
      @infile = v
    end
    op.on("-o", "--outfile=FILE", "write output text to FILE") do |v|
      @outfile = v
    end
    op.on("-v", "--verbose", "verbose mode for debugging") do |v|
      @verbose = true
    end
  end
end