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
- #add_sample_generation_options(options) ⇒ void
- #add_sample_input_options(options) ⇒ void
- #grammar_for_samples(path) ⇒ IR::Grammar
- #positive_sample_option!(name, value) ⇒ Integer
- #run_samples_command(arguments) ⇒ Integer
- #samples_option_parser ⇒ OptionParser
Instance Method Details
#add_sample_generation_options(options) ⇒ void
This method returns an undefined value.
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 |
# File 'lib/ibex/cli/samples.rb', line 55 def () .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( "--strategy=NAME", %w[random coverage], "random or coverage-oriented production selection" ) do |value| @options[:sample_strategy] = value.to_sym end .on("--path-length=N", Integer, "coverage path length: 1 or 2") do |value| raise OptionParser::InvalidArgument, "--path-length must be 1 or 2" unless [1, 2].include?(value) @options[:sample_path_length] = value end end |
#add_sample_input_options(options) ⇒ void
This method returns an undefined value.
85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ibex/cli/samples.rb', line 85 def () .on("--from=FORMAT", %w[grammar-ir automaton-ir], "read current IR JSON") do |value| @options[:from] = value end .on("--mode=MODE", %w[default extended], "grammar mode") do |value| set_configuration_option(:mode, value.to_sym) end .on("--warnings=CATEGORIES", "all, error, all,error, or none") do |value| @options[:warnings] = warning_categories(value) end end |
#grammar_for_samples(path) ⇒ IR::Grammar
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/ibex/cli/samples.rb', line 98 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
112 113 114 115 116 |
# File 'lib/ibex/cli/samples.rb', line 112 def positive_sample_option!(name, value) return value if value.positive? raise OptionParser::InvalidArgument, "--#{name} must be positive" end |
#run_samples_command(arguments) ⇒ Integer
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/ibex/cli/samples.rb', line 21 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), strategy: @options.fetch(:sample_strategy, :random), path_length: @options.fetch(:sample_path_length, 1) ) generator.generate(count: @options.fetch(:sample_count, 5)).each do |sample| @stdout.puts(JSON.generate(sample)) end 0 end |
#samples_option_parser ⇒ OptionParser
45 46 47 48 49 50 51 52 |
# File 'lib/ibex/cli/samples.rb', line 45 def samples_option_parser OptionParser.new do || . = "Usage: ibex samples [options] grammarfile" () () .on("--help", "show help") { @options[:help] = true } end end |