converters.py
Go to the documentation of this file.
1 import rospy
2 from google.protobuf import struct_pb2
3 from dialogflow_v2beta1.types import Context, EventInput, InputAudioConfig, \
4  OutputAudioConfig, QueryInput, QueryParameters, \
5  SentimentAnalysisRequestConfig, StreamingDetectIntentRequest, TextInput
6 from dialogflow_ros.msg import *
7 from output import print_context_parameters
8 
9 
10 def parameters_struct_to_msg(parameters):
11  """Convert Dialogflow parameter (Google Struct) into ros msg
12  :param parameters:
13  :type parameters: struct_pb2.Struct
14  :return: List of DF Param msgs or empty list
15  :rtype: (list of DialogflowParameter) or None
16  """
17  if parameters.items():
18  param_list = []
19  for name, value in parameters.items():
20  param = DialogflowParameter(param_name=str(name), value=[str(value)])
21  param_list.append(param)
22  return param_list
23  else:
24  return []
25 
26 
27 def params_msg_to_struct(parameters):
28  """Create a DF compatible parameter dictionary
29  :param parameters: DialogflowParameter message
30  :type parameters: list(DialogflowParameter)
31  :return: Parameters as a dictionary (Technically)
32  :rtype: struct_pb2.Struct
33  """
34  google_struct = struct_pb2.Struct()
35  for param in parameters:
36  google_struct[param.param_name] = param.value
37  return google_struct
38 
39 
40 def events_msg_to_struct(event, language_code='en-US'):
41  """Convert ROS Event Msg to DF Event
42  :param event: ROS Event Message
43  :type event: DialogflowEvent
44  :param language_code: Language code of event, default 'en-US'
45  :type language_code: str
46  :return: Dialogflow EventInput to send
47  :rtype: EventInput
48  """
49  parameters = params_msg_to_struct(event.parameters)
50  return EventInput(name=event.event_name,
51  parameters=parameters,
52  language_code=language_code)
53 
54 
55 def contexts_struct_to_msg(contexts):
56  """Utility function that fills the context received from Dialogflow into
57  the ROS msg.
58  :param contexts: The output_context received from Dialogflow.
59  :type contexts: Context
60  :return: The ROS DialogflowContext msg.
61  :rtype: DialogflowContext
62  """
63  context_list = []
64  for context in contexts:
65  df_context_msg = DialogflowContext()
66  df_context_msg.name = str(context.name)
67  df_context_msg.lifespan_count = int(context.lifespan_count)
68  df_context_msg.parameters = parameters_struct_to_msg(context.parameters)
69  context_list.append(df_context_msg)
70  return context_list
71 
72 
73 def contexts_msg_to_struct(contexts):
74  """Utility function that fills the context received from ROS into
75  the Dialogflow msg.
76  :param contexts: The output_context received from ROS.
77  :type contexts: DialogflowContext
78  :return: The Dialogflow Context.
79  :rtype: Context
80  """
81  context_list = []
82  for context in contexts:
83  new_parameters = params_msg_to_struct(context.parameters)
84  new_context = Context(name=context.name,
85  lifespan_count=context.lifespan_count,
86  parameters=new_parameters)
87  context_list.append(new_context)
88  return context_list
89 
90 
91 def create_query_parameters(contexts=None):
92  """Creates a QueryParameter with contexts. Last contexts used if
93  contexts is empty. No contexts if none found.
94  :param contexts: The ROS DialogflowContext message
95  :type contexts: list(DialogflowContext)
96  :return: A Dialogflow query parameters object.
97  :rtype: QueryParameters
98  """
99  # Create a context list is contexts are passed
100  if contexts:
101  rospy.logdebug("DF_CLIENT: Using the following contexts:\n{}".format(
102  print_context_parameters(contexts)))
103  contexts = contexts_msg_to_struct(contexts)
104  return QueryParameters(contexts=contexts)
105 
106 
107 def result_struct_to_msg(query_result):
108  """Utility function that fills the result received from Dialogflow into
109  the ROS msg.
110  :param query_result: The query_result received from Dialogflow.
111  :type query_result: QueryResult
112  :return: The ROS DialogflowResult msg.
113  :rtype: DialogflowResult
114  """
115  df_result_msg = DialogflowResult()
116  df_result_msg.fulfillment_text = str(query_result.fulfillment_text)
117  df_result_msg.query_text = str(query_result.query_text)
118  df_result_msg.action = str(query_result.action)
119  df_result_msg.parameters = parameters_struct_to_msg(
120  query_result.parameters
121  )
122  df_result_msg.contexts = contexts_struct_to_msg(
123  query_result.output_contexts
124  )
125  df_result_msg.intent = str(query_result.intent.display_name)
126  return df_result_msg
def parameters_struct_to_msg(parameters)
Definition: converters.py:10
def params_msg_to_struct(parameters)
Definition: converters.py:27
def contexts_struct_to_msg(contexts)
Definition: converters.py:55
def result_struct_to_msg(query_result)
Definition: converters.py:107
def events_msg_to_struct(event, language_code='en-US')
Definition: converters.py:40
def contexts_msg_to_struct(contexts)
Definition: converters.py:73
def create_query_parameters(contexts=None)
Definition: converters.py:91
def print_context_parameters(contexts)
Definition: output.py:1


dialogflow_ros
Author(s): Anas Abou Allaban
autogenerated on Mon Jun 10 2019 13:02:59