class NluTools::Import

The subcommand import

Public Instance Methods

dialogflow() click to toggle source
# File lib/nlu_tools/cli/import.rb, line 28
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_training_data.json')
  schemer = JSONSchemer.schema(schema)
  if schemer.valid?(intents)
    d = NluAdapter.new(:Dialogflow,
                       project_id: options[:project_id],
                       session_id: 'SESSION1')
    intents['training_data'].each do |intent|
      i = d.new_intent(intent['intent'], intent['utterences'])
      d.create_intent(i)
    end
  else
    puts "Training data is not in valid format\nPlease check data/simple_train.json for reference"
    exit(1)
  end
end
lex() click to toggle source
# File lib/nlu_tools/cli/import.rb, line 70
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_training_data.json')
  schemer = JSONSchemer.schema(schema)
  if schemer.valid?(intents)
    l = NluAdapter.new(:Lex,
                       bot_name: options[:botname],
                       bot_alias: 'BotAlias',
                       user_id: 'user-1')
    lex_intents = []
    intents['training_data'].each do |intent|
      intent_name = intent['intent'].gsub('-', '_')
      i = l.new_intent(intent_name, intent['utterences'])
      lex_intents << i
    end

    ic = l.new_intent_collection(options[:botname], lex_intents)
    l.create_intent_collection(ic)
  else
    puts "Training data is not in valid format\nPlease check data/simple_train.json for reference"
    exit(1)
  end
end