Class: NluTools::Import

Inherits:
Thor
  • Object
show all
Defined in:
lib/nlu_tools/cli/import.rb

Overview

The subcommand import

Instance Method Summary collapse

Instance Method Details

#dialogflowObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# 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

#lexObject



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
# 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