Class: NluAdapter::Adapters::Dialogflow

Inherits:
Object
  • Object
show all
Includes:
ParseHelper
Defined in:
lib/nlu_adapter/dialogflow.rb

Overview

Dialogflow wrapper class

Defined Under Namespace

Classes: Intent, IntentCollection

Instance Method Summary collapse

Methods included from ParseHelper

#bulk_parse, #parse_test, #parse_test_report

Constructor Details

#initialize(options = {}) ⇒ Dialogflow

Constructor



11
12
13
14
# File 'lib/nlu_adapter/dialogflow.rb', line 11

def initialize(options = {})
	@project_id = options[:project_id]
	@session_id = options[:session_id]
end

Instance Method Details

#create_intent(intent) ⇒ Intent

TODO:

convert response -> Intent

Given an Intent object, create/update it in Dialogflow

Parameters:

  • intent (Intent)

    Intent object

Returns:



77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 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) ⇒ Object

TODO:

check back

Not implemented



107
108
# File 'lib/nlu_adapter/dialogflow.rb', line 107

def create_intent_collection(collection)
end

#get_intent(name) ⇒ Intent

Get an instance of Intent, if it exists else nil

Parameters:

  • name (String)

    name of the intent

Returns:



41
42
43
44
45
46
47
48
49
50
51
52
53
# 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) ⇒ Object

TODO:

check back

Not implemented



95
96
# File 'lib/nlu_adapter/dialogflow.rb', line 95

def get_intent_collection(name)
end

#new_intent(name, utterences = []) ⇒ Intent

Get a new instance of Intent

Parameters:

  • name (String)

    name of the intent

  • utterences (Array) (defaults to: [])

    phrases for training

Returns:



61
62
63
64
65
66
67
68
# 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) ⇒ Object

TODO:

check back

Not implemented



101
102
# File 'lib/nlu_adapter/dialogflow.rb', line 101

def new_intent_collection(name, intents)
end

#parse(text) ⇒ Json

Understand a given text

Parameters:

  • text (String)

    a text to parse using the NLU provider

Returns:

  • (Json)

    return the identified intent name



21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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