def validate(self, dispatcher, tracker, domain): # type: (CollectingDispatcher, Tracker, Dict[Text, Any]) -> List[Dict] """Validate extracted value of requested slot else reject execution of the form action Subclass this method to add custom validation and rejection logic """ # extract other slots that were not requested # but set by corresponding entity slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update( self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # reject to execute the form action # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection( self.name(), "Failed to validate slot {0} " "with action {1}" "".format(slot_to_fill, self.name())) # validation succeed, set slots to extracted values return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict]: slot_values = self.extract_other_slots(dispatcher, tracker, domain) slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update(self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: raise ActionExecutionRejection(self.name(), "Failed to validate slot {0}" "with action {1}" "".format(slot_to_fill, self.name())) for slot, value in slot_values.items(): if slot == "TypeOfVehicle": if value not in self.TypeOfVehicle_db(): dispatcher.utter_template('utter_wrong_TypeOfVehicle', tracker) # validation failed, set slot to None slot_values[slot] = None # validation succeed, set the slots values to the extracted values return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher, tracker, domain): self.slots_filled.update([entities['entity'] for entities in tracker.latest_message.get('entities')]) slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update(self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # reject form action execution # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection(self.name(), "Failed to validate slot {0} " "with action {1}" "".format(slot_to_fill, self.name())) ''' 预留做数据校验 for slot, value in slot_values.items(): if slot == 'patient-name': pass elif slot == 'num_people': pass elif slot == 'sth...': pass ''' # validation succeed, set the slots values to the extracted values return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher, tracker, domain): # type: (CollectingDispatcher, Tracker, Dict[Text, Any]) -> List[Dict] """Extract and validate value of requested slot. If nothing was extracted reject execution of the form action. Subclass this method to add custom validation and rejection logic """ # extract other slots that were not requested # but set by corresponding entity or trigger intent mapping slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update( self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # reject to execute the form action # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection( self.name(), "Failed to extract slot {0} " "with action {1}" "".format(slot_to_fill, self.name()), ) logger.debug("Validating extracted slots: {}".format(slot_values)) return self.validate_slots(slot_values, dispatcher, tracker, domain)
def validate( self, dispatcher, # : CollectingDispatcher tracker, # : Tracker domain: Dict[Text, Any]) -> List[Dict]: slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update( self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: raise ActionExecutionRejection( self.name(), "Failed to validate slot {0} " "with action {1}" "".format(slot_to_fill, self.name())) for slot, value in slot_values.items(): if slot == 'COLOR': if value.lower() not in self.color_db(): pprint('>> utter_show_colors') dispatcher.utter_template('utter_show_colors', tracker) slot_values[slot] = None elif slot == 'QTY': if not self.is_int(value) or int(value) <= 0: pprint('>> utter_wrong_qty') dispatcher.utter_template('utter_wrong_qty', tracker) slot_values[slot] = None # else: # slot_values[slot] = int(value) return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher, tracker, domain): # type: (CollectingDispatcher, Tracker, Dict[Text, Any]) -> List[Dict] """Validate extracted requested slot else reject the execution of the form action """ # extract other slots that were not requested # but set by corresponding entity slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update( self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # reject form action execution # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection( self.name(), "Failed to validate slot {0} " "with action {1}" "".format(slot_to_fill, self.name())) # we'll check when validation failed in order # to add appropriate utterances for slot, value in slot_values.items(): if slot == 'city': if not self.is_city(value): dispatcher.utter_message("Please enter a valid city") slot_values[slot] = None # validation succeed, set the slots values to the extracted values return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher, tracker, domain): # type: (CollectingDispatcher, Tracker, Dict[Text, Any]) -> List[Dict] """Validate extracted input, if no valid email found but intent is enter_data, re-ask for email, otherwise let other policies take over""" # extract other slots that were not requested # but set by corresponding entity or trigger intent mapping slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update(self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # if no email entity was picked up, but intent was # enter_data, ask again instead of leaving the form intent = tracker.latest_message["intent"].get("name") if slot_to_fill == "business_email" and intent == "enter_data": dispatcher.utter_template("utter_no_email", tracker) return [] # reject to execute the form action # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection( self.name(), "Failed to extract slot {0} " "with action {1}" "".format(slot_to_fill, self.name()), ) # validation succeed, set slots to extracted values return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher, tracker, domain): slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update( self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # reject form action execution # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection( self.name(), "Failed to validate slot {0} " "with action {1}" "".format(slot_to_fill, self.name())) # for slot, value in slot_values.items(): # if slot == 'question': # if "market" in value.lower(): # # validation failed, set slot to None # slot_values[slot] = "mon_job" # elif "maestro" in value.lower(): # slot_values[slot] = "mon_entreprise" # elif "freelance" in value.lower(): # slot_values[slot] = "freelance" # validation succeed, set the slots values to the extracted values return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict]: """Validate extracted requested slot else reject the execution of the form action """ # extract other slots that were not requested # but set by corresponding entity slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update(self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # reject form action execution # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection(self.name(), "Failed to validate slot {0} " "with action {1}" "".format(slot_to_fill, self.name())) # we'll check when validation failed in order # to add appropriate utterances for slot, value in slot_values.items(): if slot == 'cuisine': if value.lower() not in self.cuisine_db(): dispatcher.utter_template('utter_wrong_cuisine', tracker) # validation failed, set slot to None slot_values[slot] = None elif slot == 'num_people': if not self.is_int(value) or int(value) <= 0: dispatcher.utter_template('utter_wrong_num_people', tracker) # validation failed, set slot to None slot_values[slot] = None elif slot == 'outdoor_seating': if isinstance(value, str): if 'out' in value: # convert "out..." to True slot_values[slot] = True elif 'in' in value: # convert "in..." to False slot_values[slot] = False else: dispatcher.utter_template('utter_wrong_outdoor_seating', tracker) # validation failed, set slot to None slot_values[slot] = None # validation succeed, set the slots values to the extracted values return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict]: """Validate extracted requested slot else reject the execution of the form action """ # extract other slots that were not requested # but set by corresponding entity slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update( self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # reject form action execution # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection( self.name(), "Failed to validate slot {0} " "with action {1}" "".format(slot_to_fill, self.name())) # we'll check when validation failed in order # to add appropriate utterances for slot, value in slot_values.items(): if slot == 'account': if value.lower() not in self.account_db(): dispatcher.utter_template('utter_wrong_account', tracker) # validation failed, set slot to None slot_values[slot] = None elif slot == 'accounttype': if value.lower() not in self.accounttype(): dispatcher.utter_template('utter_wrong_account_type', tracker) # validation failed, set slot to None slot_values[slot] = None elif slot == 'producttype': if value.lower() not in self.prodtype(): dispatcher.utter_template('utter_wrong_product_type', tracker) # validation failed, set slot to None slot_values[slot] = None elif slot == 'adjustmenttype': if value.lower() not in self.adjtype(): dispatcher.utter_template('utter_wrong_adjustment_type', tracker) # validation failed, set slot to None slot_values[slot] = None # validation succeed, set the slots values to the extracted values return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict]: slots_values = self.extract_other_slots(dispatcher, tracker, domain) slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slots_values.update( self.extract_requested_slot(dispatcher, tracker, domain)) if not slots_values: raise ActionExecutionRejection( self.name(), "Failed to validate slot {0} \ with action {1}".format( slot_to_fill, self.name)) return [SlotSet(slot, value) for slot, value in slots_values.items()]
def validate(self, dispatcher, tracker, domain): # type: (CollectingDispatcher, Tracker, Dict[Text, Any]) -> List[Dict] """Extract and validate value of requested slot. If nothing was extracted reject execution of the form action. Subclass this method to add custom validation and rejection logic """ # extract other slots that were not requested # but set by corresponding entity or trigger intent mapping slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update(self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # reject to execute the form action # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection( self.name(), "Failed to extract slot {0} " "with action {1}" "".format(slot_to_fill, self.name()), ) for slot, value in list(slot_values.items()): validate_func = getattr( self, "validate_{}".format(slot), lambda *x: {slot: value} ) validation_output = validate_func(value, dispatcher, tracker, domain) if not isinstance(validation_output, dict): logger.warning( "Returning values in helper validation methods is deprecated. " + "Your method should return a dict of {'slot_name': value} instead." ) validation_output = {slot: validation_output} slot_values.update(validation_output) # validation succeed, set slots to extracted values return [SlotSet(slot, value) for slot, value in slot_values.items()]
def validate(self, dispatcher: CollectingDispatcher, tracker: Tracker, domain: Dict[Text, Any]) -> List[Dict]: """Validate extracted requested slot else reject the execution of the form action""" # extract other slots that were not requested # but set by corresponding entity slot_values = self.extract_other_slots(dispatcher, tracker, domain) # extract requested slot slot_to_fill = tracker.get_slot(REQUESTED_SLOT) if slot_to_fill: slot_values.update( self.extract_requested_slot(dispatcher, tracker, domain)) if not slot_values: # reject form action execution # if some slot was requested but nothing was extracted # it will allow other policies to predict another action raise ActionExecutionRejection( self.name(), "Failed to validate slot {0} " "with action {1}" "".format(slot_to_fill, self.name())) return [SlotSet(slot, value) for slot, value in slot_values.items()]