async def _query_attribute( self, dispatcher: CollectingDispatcher, object_type: Text, attribute: Text, tracker: Tracker, ) -> List[Dict]: """ Queries the knowledge base for the value of the requested attribute of the mentioned object and outputs it to the user. Args: dispatcher: the dispatcher tracker: the tracker Returns: list of slots """ object_name = get_object_name( tracker, self.knowledge_base.ordinal_mention_mapping, self.use_last_object_mention, ) if not object_name or not attribute: dispatcher.utter_message(template="utter_ask_rephrase") return [SlotSet(SLOT_MENTION, None)] object_of_interest = await utils.call_potential_coroutine( self.knowledge_base.get_object(object_type, object_name)) if not object_of_interest or attribute not in object_of_interest: dispatcher.utter_message(template="utter_ask_rephrase") return [SlotSet(SLOT_MENTION, None)] value = object_of_interest[attribute] object_representation = await utils.call_potential_coroutine( self.knowledge_base.get_representation_function_of_object( object_type)) key_attribute = await utils.call_potential_coroutine( self.knowledge_base.get_key_attribute_of_object(object_type)) object_identifier = object_of_interest[key_attribute] await utils.call_potential_coroutine( self.utter_attribute_value(dispatcher, object_representation, attribute, value)) slots = [ SlotSet(SLOT_OBJECT_TYPE, object_type), SlotSet(SLOT_ATTRIBUTE, None), SlotSet(SLOT_MENTION, None), SlotSet(SLOT_LAST_OBJECT, object_identifier), SlotSet(SLOT_LAST_OBJECT_TYPE, object_type), ] return slots
async def _query_join_objects(self, dispatcher: CollectingDispatcher, object_type: Text, last_object_type, tracker: Tracker) -> List[Dict]: """ Performs a join query on the knowledge base by returning the object of type 'object_type' where the key attribute matches the key of the last object in the tracker. Args: dispatcher: the dispatcher tracker: the tracker Returns: list of slots """ object_name = get_object_name( tracker, self.knowledge_base.ordinal_mention_mapping, self.use_last_object_mention, ) last_object = await utils.call_potential_coroutine( self.knowledge_base.get_object(last_object_type, object_name)) if not object_name or not object_type or not last_object: dispatcher.utter_message(template="utter_ask_rephrase") return [SlotSet(SLOT_MENTION, None)] object_of_interest = await utils.call_potential_coroutine( self.knowledge_base.get_object(object_type, object_name)) obj_repr = self.knowledge_base.document_types[object_type].to_string( object_of_interest) last_obj_repr = self.knowledge_base.document_types[ last_object_type].to_string(last_object) dispatcher.utter_message( text= f"{obj_repr} is the {object_type} for {last_object_type} {last_obj_repr}." ) slots = [ SlotSet(SLOT_OBJECT_TYPE, last_object_type), SlotSet(SLOT_MENTION, None), SlotSet(SLOT_ATTRIBUTE, None), SlotSet(SLOT_LAST_OBJECT, object_name), SlotSet(SLOT_LAST_OBJECT_TYPE, last_object_type), SlotSet(SLOT_LIMIT, None), ] return slots
def _query_attribute(self, dispatcher, tracker): # type: (CollectingDispatcher, Tracker) -> List[Dict] """ Queries the knowledge base for the value of the requested attribute of the mentioned object and outputs it to the user. Args: dispatcher: the dispatcher tracker: the tracker Returns: list of slots """ object_type = tracker.get_slot(SLOT_OBJECT_TYPE) attribute = tracker.get_slot(SLOT_ATTRIBUTE) object_name = get_object_name( tracker, self.knowledge_base.ordinal_mention_mapping, self.use_last_object_mention, ) if not object_name or not attribute: dispatcher.utter_template("utter_ask_rephrase", tracker) return [SlotSet(SLOT_MENTION, None)] object_of_interest = self.knowledge_base.get_object(object_type, object_name) if not object_of_interest or attribute not in object_of_interest: dispatcher.utter_template("utter_ask_rephrase", tracker) return [SlotSet(SLOT_MENTION, None)] value = object_of_interest[attribute] repr_function = self.knowledge_base.get_representation_function_of_object( object_type ) object_representation = repr_function(object_of_interest) key_attribute = self.knowledge_base.get_key_attribute_of_object(object_type) object_identifier = object_of_interest[key_attribute] self.utter_attribute_value(dispatcher, object_representation, attribute, value) slots = [ SlotSet(SLOT_OBJECT_TYPE, object_type), SlotSet(SLOT_ATTRIBUTE, None), SlotSet(SLOT_MENTION, None), SlotSet(SLOT_LAST_OBJECT, object_identifier), SlotSet(SLOT_LAST_OBJECT_TYPE, object_type), ] return slots
def test_get_object_name(slots, use_last_object_mention, expected_object_name): ordinal_mention_mapping = { "1": lambda l: l[0], "2": lambda l: l[1], "3": lambda l: l[2], "LAST": lambda l: l[-1], } tracker = Tracker("default", slots, {}, [], False, None, {}, "action_listen") actual_object_name = get_object_name( tracker, ordinal_mention_mapping, use_last_object_mention ) assert actual_object_name == expected_object_name
def _query_attribute( self, dispatcher: CollectingDispatcher, tracker: Tracker ) -> List[Dict]: """ Queries the knowledge base for the value of the requested attribute of the mentioned object and outputs it to the user. Args: dispatcher: the dispatcher tracker: the tracker Returns: list of slots """ object_type = tracker.get_slot(SLOT_OBJECT_TYPE) attribute = tracker.get_slot(SLOT_ATTRIBUTE) object_name = get_object_name( tracker, self.knowledge_base.ordinal_mention_mapping, self.use_last_object_mention, ) if not object_name or not attribute: dispatcher.utter_message(template="utter_ask_rephrase") return [SlotSet(SLOT_MENTION, None)] object_of_interest = self.knowledge_base.get_object(object_type, object_name) if attribute in ["address", "latitude", "longitude", "url"] : base = "https://www.google.com/maps/search/?api=1&query=" url = base + urllib.parse.quote(object_of_interest["name"]) value = object_of_interest[attribute] repr_function = self.knowledge_base.get_representation_function_of_object( object_type ) object_representation = repr_function(object_of_interest) self.utter_attribute_value(dispatcher, object_representation, attribute, value, custom=True, load=url) key_attribute = self.knowledge_base.get_key_attribute_of_object(object_type) object_identifier = object_of_interest[key_attribute] slots = [ SlotSet(SLOT_OBJECT_TYPE, object_type), SlotSet(SLOT_ATTRIBUTE, None), SlotSet(SLOT_MENTION, None), SlotSet(SLOT_LAST_OBJECT, object_identifier), SlotSet(SLOT_LAST_OBJECT_TYPE, object_type), ] return slots else: if not object_of_interest or attribute not in object_of_interest: dispatcher.utter_message(template="utter_ask_rephrase") return [SlotSet(SLOT_MENTION, None)] value = object_of_interest[attribute] repr_function = self.knowledge_base.get_representation_function_of_object( object_type ) object_representation = repr_function(object_of_interest) key_attribute = self.knowledge_base.get_key_attribute_of_object(object_type) object_identifier = object_of_interest[key_attribute] self.utter_attribute_value(dispatcher, object_representation, attribute, value) slots = [ SlotSet(SLOT_OBJECT_TYPE, object_type), SlotSet(SLOT_ATTRIBUTE, None), SlotSet(SLOT_MENTION, None), SlotSet(SLOT_LAST_OBJECT, object_identifier), SlotSet(SLOT_LAST_OBJECT_TYPE, object_type), ] return slots
async def _query_attribute( self, dispatcher: CollectingDispatcher, object_type: Text, attribute: Text, tracker: Tracker, ) -> List[Dict]: """ Queries the knowledge base for the value of the requested attribute of the mentioned object and outputs it to the user. Args: dispatcher: the dispatcher tracker: the tracker Returns: list of slots """ object_name = get_object_name( tracker, self.knowledge_base.ordinal_mention_mapping, self.use_last_object_mention, ) if not object_name or not attribute: dispatcher.utter_message(template="utter_ask_rephrase") return [SlotSet(SLOT_MENTION, None)] if utils.is_coroutine_action(self.knowledge_base.get_object): object_of_interest = await self.knowledge_base.get_object( object_type, object_name) else: # see https://github.com/python/mypy/issues/5206 object_of_interest = cast( Optional[Dict[Text, Any]], self.knowledge_base.get_object(object_type, object_name), ) if not object_of_interest or attribute not in object_of_interest: dispatcher.utter_message(template="utter_ask_rephrase") return [SlotSet(SLOT_MENTION, None)] value = object_of_interest[attribute] if utils.is_coroutine_action( self.knowledge_base.get_representation_function_of_object): repr_function = ( await self.knowledge_base.get_representation_function_of_object( object_type)) else: # see https://github.com/python/mypy/issues/5206 repr_function = cast( Callable, self.knowledge_base.get_representation_function_of_object( object_type), ) object_representation = repr_function(object_of_interest) if utils.is_coroutine_action( self.knowledge_base.get_key_attribute_of_object): key_attribute = await self.knowledge_base.get_key_attribute_of_object( object_type) else: # see https://github.com/python/mypy/issues/5206 key_attribute = cast( Text, self.knowledge_base.get_key_attribute_of_object(object_type)) object_identifier = object_of_interest[key_attribute] if utils.is_coroutine_action(self.utter_attribute_value): await self.utter_attribute_value(dispatcher, object_representation, attribute, value) else: self.utter_attribute_value(dispatcher, object_representation, attribute, value) slots = [ SlotSet(SLOT_OBJECT_TYPE, object_type), SlotSet(SLOT_ATTRIBUTE, None), SlotSet(SLOT_MENTION, None), SlotSet(SLOT_LAST_OBJECT, object_identifier), SlotSet(SLOT_LAST_OBJECT_TYPE, object_type), ] return slots