class NluTools::Test

The subcommand for test

Public Instance Methods

dialogflow() click to toggle source
# File lib/nlu_tools/cli/test.rb, line 40
def dialogflow
  unless File.exist?(options[:file])
    puts "File #{options[:file]} does not exist"
    exit(1)
  end

  intents = JSON.parse(File.read(options[:file]))
  schema = Pathname.new('schema/nlu_testing_data.json')
  schemer = JSONSchemer.schema(schema)
  if schemer.valid?(intents)
    na = NluAdapter.new(:Dialogflow,
                        project_id: options[:project_id],
                        session_id: 'SESSION1')
    new_intents = {}
    intents['testing_data'].each do |intent|
      new_intents[intent['intent']] = intent['utterences']
    end

    run_tests(na, new_intents, options[:output_file], options[:output_type])
  else
    puts "Testing data is not in valid format\nPlease check data/simple_test.json for reference"
    exit(1)
  end
end
lex() click to toggle source
# File lib/nlu_tools/cli/test.rb, line 100
def lex
  unless File.exist?(options[:file])
    puts "File #{options[:file]} does not exist"
    exit(1)
  end

  intents = JSON.parse(File.read(options[:file]))
  schema = Pathname.new('schema/nlu_testing_data.json')
  schemer = JSONSchemer.schema(schema)
  if schemer.valid?(intents)
    na = NluAdapter.new(:Lex,
                        bot_name: options[:botname],
                        bot_alias: 'BotAlias',
                        user_id: 'user-1')
    new_intents = {}
    intents['testing_data'].each do |intent|
      intent_name = intent['intent'].gsub('-', '_')
      new_intents[intent_name] = intent['utterences']
    end

    run_tests(na, new_intents, options[:output_file], options[:output_type])
  else
    puts "Testing data is not in valid format\nPlease check data/simple_test.json for reference"
    exit(1)
  end
end

Private Instance Methods

run_tests(nlu_adapter, intents, output_file, output_type) click to toggle source
# File lib/nlu_tools/cli/test.rb, line 129
def run_tests(nlu_adapter, intents, output_file, output_type)
  ext = output_file.nil? || output_file.empty? || output_file == :STDOUT ? :none : output_file.split('.')[-1].intern
  type = output_type.nil? || output_type.empty? ? :raw : output_type.intern

  case type
  when :raw
    case ext
    when :none
      puts nlu_adapter.parse_test(intents, :csv)
    when :csv, :json, :yaml
      File.open(output_file, 'w') { |file| file.write(nlu_adapter.parse_test(intents, ext)) }
    else
      puts 'Only csv/json/yaml output file format supported'
    end

  when :summary
    case ext
    when :none
      puts nlu_adapter.parse_test_report(intents, :csv)
    when :csv, :json, :yaml
      File.open(output_file, 'w') { |file| file.write(nlu_adapter.parse_test_report(intents, ext)) }
    else
      puts 'Only csv/json/yaml output file format supported'
    end

  else
    puts 'Only raw/summary output type supported'
  end
end