Module: Ibex::CLISamples
- Defined in:
- lib/ibex/cli/samples.rb,
sig/ibex/cli/samples.rbs
Overview
CLI entry point for bounded grammar sentence generation.
Instance Method Summary collapse
- #grammar_for_samples(path) ⇒ IR::Grammar
- #positive_sample_option!(name, value) ⇒ Integer
- #run_samples_command(arguments) ⇒ Integer
- #samples_option_parser ⇒ OptionParser
Instance Method Details
#grammar_for_samples(path) ⇒ IR::Grammar
72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'lib/ibex/cli/samples.rb', line 72 def grammar_for_samples(path) return normalize_grammar_path(path) unless @options[:from] value = IR::Validator.validate(File.read(path)) expected = @options[:from] == "grammar-ir" ? IR::Grammar : IR::Automaton raise Ibex::Error, "#{path}:1:1: expected #{@options[:from]} input" unless value.is_a?(expected) return value if value.is_a?(IR::Grammar) return value.grammar if value.is_a?(IR::Automaton) raise Ibex::Error, "#{path}:1:1: expected #{@options[:from]} input" end |
#positive_sample_option!(name, value) ⇒ Integer
86 87 88 89 90 |
# File 'lib/ibex/cli/samples.rb', line 86 def positive_sample_option!(name, value) return value if value.positive? raise OptionParser::InvalidArgument, "--#{name} must be positive" end |
#run_samples_command(arguments) ⇒ Integer
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ibex/cli/samples.rb', line 20 def run_samples_command(arguments) parser = samples_option_parser remaining = parser.parse(arguments) return print_help(parser) if @options[:help] path = input_path(remaining) grammar = grammar_for_samples(path) handle_grammar_warnings(grammar, path) generator = Samples.new( grammar, seed: @options.fetch(:sample_seed, 0), max_tokens: @options.fetch(:sample_max_tokens, 32), max_depth: @options.fetch(:sample_max_depth, 16), max_expansions: @options.fetch(:sample_max_expansions, Samples::DEFAULT_MAX_EXPANSIONS) ) generator.generate(count: @options.fetch(:sample_count, 5)).each do |sample| @stdout.puts(JSON.generate(sample)) end 0 end |
#samples_option_parser ⇒ OptionParser
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 |
# File 'lib/ibex/cli/samples.rb', line 42 def samples_option_parser OptionParser.new do || . = "Usage: ibex samples [options] grammarfile" .on("--count=N", Integer, "number of samples (default 5)") do |value| @options[:sample_count] = positive_sample_option!("count", value) end .on("--seed=N", Integer, "deterministic random seed (default 0)") do |value| @options[:sample_seed] = value end .on("--max-tokens=N", Integer, "maximum tokens per sample (default 32)") do |value| @options[:sample_max_tokens] = positive_sample_option!("max-tokens", value) end .on("--max-depth=N", Integer, "random expansion depth (default 16)") do |value| @options[:sample_max_depth] = positive_sample_option!("max-depth", value) end .on("--max-expansions=N", Integer, "total expansion steps (default 100000)") do |value| @options[:sample_max_expansions] = positive_sample_option!("max-expansions", value) end .on("--from=FORMAT", %w[grammar-ir automaton-ir], "read versioned IR JSON") do |value| @options[:from] = value end .on("--mode=MODE", %w[default extended], "grammar mode") { |value| @options[:mode] = value.to_sym } .on("--warnings=CATEGORIES", "all, error, all,error, or none") do |value| @options[:warnings] = warning_categories(value) end .on("--help", "show help") { @options[:help] = true } end end |