Class: Precise::CLI

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCLI

Returns a new instance of CLI.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/precise.rb', line 11

def initialize
  opts = Slop::Options.new
  opts.banner = "Usage: precise [options] <string(s)>\n"
  opts.separator "    where options can be:\n"
  alif_variants = Precise::Transcription::AlifVariants
  opts.bool "-s", "--show-rules", "print the list of rules which are applied for transcription"
  opts.bool "-c", "--confidence", "also print the percentage of output words appearing in a large corpus of Arabic"
  opts.bool "-A", "--no-alif-variants", "all of #{alif_variants.join("، ")} will be merged into ا"
  opts.bool "-T", "--no-tashkeel", "diacritics (and non printables, such as tatweel) will be removed from output"
  opts.bool "-P", "--no-punctuation", "all punctuation characters will be discarded from output"
  opts.bool "-v", "--verbose", "instruct the backend classes to output debugging and plausibility information"
  opts.bool "-h", "--help", "display this message"
  opts.separator "\n    Transcription direction is determined by presence of characters from the 'Arabic' Unicode block.\n" \
    "    At present, Arabic-to-Roman transcription is only rudimentary."
  opts = Slop::Parser.new(opts)

  begin
    @opts = opts.parse(ARGV)
    usage if @opts[:help] || ARGV.size == 0
    rules if @opts.to_h[:show_rules]
  rescue
    @opts = opts.parse([])
    usage
  end

  options = {verbose: @opts[:verbose]}
  options[:alif_variants] = false if @opts.to_h[:no_alif_variants]
  options[:tashkeel] = false if @opts.to_h[:no_tashkeel]
  options[:punctuation] = false if @opts.to_h[:no_punctuation]

  instr = @opts.arguments.join(' ')
  if instr.match?(/\p{Arabic}/)
    outstr = Precise::Transcription.transcribe(instr.dup, options)
  else
    outstr = Precise::Transcription.reverse(instr.dup, options)
    outstr += " (#{Precise::TypesList::percentage_of_tokens_present(outstr)}%)" if @opts[:confidence]
  end
  puts outstr.pretty_inspect.gsub(/(^"|"$)/, "").strip
end

Class Method Details

.startObject



65
# File 'lib/precise.rb', line 65

def self.start; new; end

Instance Method Details

#noptsObject



61
62
63
# File 'lib/precise.rb', line 61

def nopts
  @opts.to_h.values.map { |o| o || nil }.compact.size
end

#rulesObject



56
57
58
59
# File 'lib/precise.rb', line 56

def rules
  puts Precise::Transcription::R2ATables.map{|k,v| Hash[k.to_s,v.map{|kk,vv| Hash[kk.to_s,vv]}]}.to_yaml.gsub(/---\n/,'')
  exit
end

#usageObject



51
52
53
54
# File 'lib/precise.rb', line 51

def usage
  warn @opts
  exit
end