Class: Opener::LanguageIdentifier::CLI

Inherits:
Object
  • Object
show all
Defined in:
lib/opener/language_identifier/cli.rb

Overview

CLI wrapper around Opener::LanguageIdentifier using OptionParser.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ CLI

Returns a new instance of CLI.

Parameters:

  • options (Hash) (defaults to: {})


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/opener/language_identifier/cli.rb', line 18

def initialize(options = {})
  @options = DEFAULT_OPTIONS.merge(options)

  @option_parser = OptionParser.new do |opts|
    opts.program_name   = 'language-identifier'
    opts.summary_indent = '  '

    opts.on('-v', '--version', 'Shows the current version') do
      show_version
    end

    opts.on('-k', '--[no-]kaf', 'Output the language as KAF') do |v|
      @options[:kaf] = v
    end

    opts.on('-p', '--probs', 'Provide probabilities, assumes --no-kaf') do
      @options[:kaf]   = false
      @options[:probs] = true
    end

    opts.on('-b', '--benchmark', 'Include benchmarking output') do
      @options[:benchmark] = true
    end

    opts.separator "\nExamples:\n\n  cat example_text.txt | \#{opts.program_name}    # Basic detection\n\nLanguages:\n\n  * ar  Arabic\n  * bg  Bulgarian\n  * bn  Bengali\n  * cs  Czech\n  * da  Danish\n  * de  German\n  * el  Greek\n  * en  English\n  * es  Spanish\n  * et  Estonian\n  * fa  Persian\n  * fi  Finnish\n  * fr  French\n  * gu  Gujarati\n  * he  Hebrew\n  * hi  Hindi\n  * hr  Croatian\n  * hu  Hungarian\n  * id  Indonesian\n  * it  Italian\n  * ja  Japanese\n  * kn  Kannada\n  * ko  Korean\n  * lt  Lithuanian\n  * lv  Latvian\n  * mk  Macedonian\n  * ml  Malayalam\n  * mr  Marathi\n  * ne  Nepali\n  * nl  Dutch\n  * no  Norwegian\n  * pa  Punjabi\n  * pl  Polish\n  * pt  Portuguese\n  * ro  Romanian\n  * ru  Russian\n  * sk  Slovak\n  * sl  Slovene\n  * so  Somali\n  * sq  Albanian\n  * sv  Swedish\n  * sw  Swahili\n  * ta  Tamil\n  * te  Telugu\n  * th  Thai\n  * tl  Tagalog\n  * tr  Turkish\n  * uk  Ukrainian\n  * ur  Urdu\n  * vi  Vietnamese\n  * zh-cn Simplified Chinese\n  * zh-tw Traditional Chinese\n    EOF\n\n    opts.separator \"\"\n    opts.separator \"Common options:\"\n    # No argument, shows at tail.  This will print an options summary.\n    # Try it and see!\n    opts.on_tail(\"-h\", \"--help\", \"Show this message.\") do\n      puts opts\n      exit\n    end\n  end\nend\n"

Instance Attribute Details

#option_parserOptionParser (readonly)

Returns:

  • (OptionParser)


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/opener/language_identifier/cli.rb', line 12

class CLI
  attr_reader :options, :option_parser

  ##
  # @param [Hash] options
  #
  def initialize(options = {})
    @options = DEFAULT_OPTIONS.merge(options)

    @option_parser = OptionParser.new do |opts|
      opts.program_name   = 'language-identifier'
      opts.summary_indent = '  '

      opts.on('-v', '--version', 'Shows the current version') do
        show_version
      end

      opts.on('-k', '--[no-]kaf', 'Output the language as KAF') do |v|
        @options[:kaf] = v
      end

      opts.on('-p', '--probs', 'Provide probabilities, assumes --no-kaf') do
        @options[:kaf]   = false
        @options[:probs] = true
      end

      opts.on('-b', '--benchmark', 'Include benchmarking output') do
        @options[:benchmark] = true
      end

      opts.separator "\nExamples:\n\n  cat example_text.txt | \#{opts.program_name}    # Basic detection\n\nLanguages:\n\n  * ar  Arabic\n  * bg  Bulgarian\n  * bn  Bengali\n  * cs  Czech\n  * da  Danish\n  * de  German\n  * el  Greek\n  * en  English\n  * es  Spanish\n  * et  Estonian\n  * fa  Persian\n  * fi  Finnish\n  * fr  French\n  * gu  Gujarati\n  * he  Hebrew\n  * hi  Hindi\n  * hr  Croatian\n  * hu  Hungarian\n  * id  Indonesian\n  * it  Italian\n  * ja  Japanese\n  * kn  Kannada\n  * ko  Korean\n  * lt  Lithuanian\n  * lv  Latvian\n  * mk  Macedonian\n  * ml  Malayalam\n  * mr  Marathi\n  * ne  Nepali\n  * nl  Dutch\n  * no  Norwegian\n  * pa  Punjabi\n  * pl  Polish\n  * pt  Portuguese\n  * ro  Romanian\n  * ru  Russian\n  * sk  Slovak\n  * sl  Slovene\n  * so  Somali\n  * sq  Albanian\n  * sv  Swedish\n  * sw  Swahili\n  * ta  Tamil\n  * te  Telugu\n  * th  Thai\n  * tl  Tagalog\n  * tr  Turkish\n  * uk  Ukrainian\n  * ur  Urdu\n  * vi  Vietnamese\n  * zh-cn Simplified Chinese\n  * zh-tw Traditional Chinese\n      EOF\n\n      opts.separator \"\"\n      opts.separator \"Common options:\"\n      # No argument, shows at tail.  This will print an options summary.\n      # Try it and see!\n      opts.on_tail(\"-h\", \"--help\", \"Show this message.\") do\n        puts opts\n        exit\n      end\n    end\n  end\n\n  ##\n  # @param [String] input\n  #\n  def run(input)\n    option_parser.parse!(options[:args])\n    identifier = LanguageIdentifier.new(options)\n\n    output = identifier.run(input)\n    puts output\n  end\n\n  private\n\n  ##\n  # Shows the help message and exits the program.\n  #\n  def show_help\n    abort option_parser.to_s\n  end\n\n  ##\n  # Shows the version and exits the program.\n  #\n  def show_version\n    abort \"\#{option_parser.program_name} v\#{VERSION} on \#{RUBY_DESCRIPTION}\"\n  end\nend\n"

#optionsHash (readonly)

Returns:

  • (Hash)


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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/opener/language_identifier/cli.rb', line 12

class CLI
  attr_reader :options, :option_parser

  ##
  # @param [Hash] options
  #
  def initialize(options = {})
    @options = DEFAULT_OPTIONS.merge(options)

    @option_parser = OptionParser.new do |opts|
      opts.program_name   = 'language-identifier'
      opts.summary_indent = '  '

      opts.on('-v', '--version', 'Shows the current version') do
        show_version
      end

      opts.on('-k', '--[no-]kaf', 'Output the language as KAF') do |v|
        @options[:kaf] = v
      end

      opts.on('-p', '--probs', 'Provide probabilities, assumes --no-kaf') do
        @options[:kaf]   = false
        @options[:probs] = true
      end

      opts.on('-b', '--benchmark', 'Include benchmarking output') do
        @options[:benchmark] = true
      end

      opts.separator "\nExamples:\n\n  cat example_text.txt | \#{opts.program_name}    # Basic detection\n\nLanguages:\n\n  * ar  Arabic\n  * bg  Bulgarian\n  * bn  Bengali\n  * cs  Czech\n  * da  Danish\n  * de  German\n  * el  Greek\n  * en  English\n  * es  Spanish\n  * et  Estonian\n  * fa  Persian\n  * fi  Finnish\n  * fr  French\n  * gu  Gujarati\n  * he  Hebrew\n  * hi  Hindi\n  * hr  Croatian\n  * hu  Hungarian\n  * id  Indonesian\n  * it  Italian\n  * ja  Japanese\n  * kn  Kannada\n  * ko  Korean\n  * lt  Lithuanian\n  * lv  Latvian\n  * mk  Macedonian\n  * ml  Malayalam\n  * mr  Marathi\n  * ne  Nepali\n  * nl  Dutch\n  * no  Norwegian\n  * pa  Punjabi\n  * pl  Polish\n  * pt  Portuguese\n  * ro  Romanian\n  * ru  Russian\n  * sk  Slovak\n  * sl  Slovene\n  * so  Somali\n  * sq  Albanian\n  * sv  Swedish\n  * sw  Swahili\n  * ta  Tamil\n  * te  Telugu\n  * th  Thai\n  * tl  Tagalog\n  * tr  Turkish\n  * uk  Ukrainian\n  * ur  Urdu\n  * vi  Vietnamese\n  * zh-cn Simplified Chinese\n  * zh-tw Traditional Chinese\n      EOF\n\n      opts.separator \"\"\n      opts.separator \"Common options:\"\n      # No argument, shows at tail.  This will print an options summary.\n      # Try it and see!\n      opts.on_tail(\"-h\", \"--help\", \"Show this message.\") do\n        puts opts\n        exit\n      end\n    end\n  end\n\n  ##\n  # @param [String] input\n  #\n  def run(input)\n    option_parser.parse!(options[:args])\n    identifier = LanguageIdentifier.new(options)\n\n    output = identifier.run(input)\n    puts output\n  end\n\n  private\n\n  ##\n  # Shows the help message and exits the program.\n  #\n  def show_help\n    abort option_parser.to_s\n  end\n\n  ##\n  # Shows the version and exits the program.\n  #\n  def show_version\n    abort \"\#{option_parser.program_name} v\#{VERSION} on \#{RUBY_DESCRIPTION}\"\n  end\nend\n"

Instance Method Details

#run(input) ⇒ Object

Parameters:

  • input (String)


118
119
120
121
122
123
124
# File 'lib/opener/language_identifier/cli.rb', line 118

def run(input)
  option_parser.parse!(options[:args])
  identifier = LanguageIdentifier.new(options)

  output = identifier.run(input)
  puts output
end