class NluAdapter::Adapters::Dialogflow
Dialogflow
wrapper class
Public Class Methods
new(options = {})
click to toggle source
Constructor
# File lib/nlu_adapter/dialogflow.rb, line 11 def initialize(options = {}) @project_id = options[:project_id] @session_id = options[:session_id] end
Public Instance Methods
create_intent(intent)
click to toggle source
Given an Intent
object, create/update it in Dialogflow
@param intent [Intent] Intent
object @return [Intent] Intent
object
@todo convert response -> Intent
# File lib/nlu_adapter/dialogflow.rb, line 77 def create_intent(intent) intents_client = Google::Cloud::Dialogflow::Intents.new(version: :v2) formatted_parent = Google::Cloud::Dialogflow::V2::IntentsClient.project_agent_path(@project_id) #check: to create / update if !intent.id i = intent.to_h response = intents_client.create_intent(formatted_parent, i) else i = intent.to_h language_code = 'en' response = intents_client.update_intent(i, language_code) end end
create_intent_collection(collection)
click to toggle source
Not implemented @todo check back
# File lib/nlu_adapter/dialogflow.rb, line 107 def create_intent_collection(collection) end
get_intent(name)
click to toggle source
Get an instance of Intent
, if it exists else nil
@param name [String] name of the intent @return [Intent] intent object
# File lib/nlu_adapter/dialogflow.rb, line 41 def get_intent(name) intents_client = Google::Cloud::Dialogflow::Intents.new(version: :v2) formatted_parent = Google::Cloud::Dialogflow::V2::IntentsClient.project_agent_path(@project_id) #Iterate over all results. #todo: should be cached for better performance intents_client.list_intents(formatted_parent).each do |intent| if intent.display_name == name return Intent.new({id: intent.name, display_name: intent.display_name}) end end return nil end
get_intent_collection(name)
click to toggle source
Not implemented @todo check back
# File lib/nlu_adapter/dialogflow.rb, line 95 def get_intent_collection(name) end
new_intent(name, utterences = [])
click to toggle source
Get a new instance of Intent
@param name [String] name of the intent @param utterences [Array] phrases for training @return [Intent] Intent
object
# File lib/nlu_adapter/dialogflow.rb, line 61 def new_intent(name, utterences = []) i = get_intent(name) i = Intent.new if !i i.name = name i.utterences = utterences return i end
new_intent_collection(name, intents)
click to toggle source
Not implemented @todo check back
# File lib/nlu_adapter/dialogflow.rb, line 101 def new_intent_collection(name, intents) end
parse(text)
click to toggle source
Understand a given text
@param text [String] a text to parse using the NLU provider @return [Json] return the identified intent name
# File lib/nlu_adapter/dialogflow.rb, line 21 def parse(text) sessions_client = Google::Cloud::Dialogflow::Sessions.new(version: :v2) formatted_session = Google::Cloud::Dialogflow::V2::SessionsClient.session_path(@project_id, @session_id) language_code = 'en' intent_name = :NO_INTENT_FOUND query_input = Google::Cloud::Dialogflow::V2::QueryInput.new({text: {language_code: language_code, text: text}}) response = sessions_client.detect_intent(formatted_session, query_input) unless response.nil? || response.query_result.nil? || response.query_result.intent.nil? || response.query_result.intent.display_name.empty? intent_name = response.query_result.intent.display_name end return { intent_name: intent_name } end