Пример #1
0
def use_contexts_to_predict_next_action(action_name, tracker):
    contexts_in = in_context_set(action_name)
    contexts_out = out_context_set(action_name)
    could_change_action = 1
    for context in contexts_in:
        if context not in active_conexts_to_influence_actions:
            active_conexts_to_influence_actions[context] = 0

        elif active_conexts_to_influence_actions[context] > 0:
            could_change_action = 0
            break

    if could_change_action:
        intent_ranking = tracker.latest_message.parse_data['intent_ranking']
        for intent in intent_ranking[1:2]:
            if intent['name'] not in intents_to_actions:
                continue
            potential_action_name = intents_to_actions[intent['name']]
            contexts_potential_action = in_context_set(potential_action_name)
            for context in contexts_potential_action:
                if context not in active_conexts_to_influence_actions:
                    active_conexts_to_influence_actions[context] = 0

                elif active_conexts_to_influence_actions[context] > 0:
                    tracker.trigger_follow_up_action(
                        action_name_to_action_object(potential_action_name))
                    contexts_out = out_context_set(potential_action_name)
                    break

    for context in active_conexts_to_influence_actions:
        active_conexts_to_influence_actions[context] = 0

    for context in contexts_out:
        active_conexts_to_influence_actions[context] = 1
Пример #2
0
def contexts_reset(action_name, tracker):
    events = []
    contexts_in = in_context_set(action_name)
    contexts_out = out_context_set(action_name)
    if len(contexts_out) == 0:
        contexts_out = [action_name]
    contexts_in.extend(contexts_out)
    contexts = contexts_in
    should_reset = 1
    for context in contexts:
        if context not in active_contexts:
            active_contexts[context] = 0

        elif active_contexts[context] > 0:
            should_reset = 0
            break

    if should_reset == 1:
        for entity in entities:
            try:
                next(tracker.get_latest_entity_values(entity))
            except:
                # no entities for this entity found in the last massage
                tracker._set_slot(entity, None)
                events.append(SlotSet(entity, None))

    for context in active_contexts:
        active_contexts[context] = 0

    for context in contexts_out:
        active_contexts[context] = 1

    return events
Пример #3
0
    def run(self, dispatcher, tracker, domain):
        index = "works_by_instrument"
        template = dispatcher.retrieve_template("utter_" +
                                                "works_by_instrument")

        # use contexts to influence predicted action
        use_contexts_to_predict_next_action(self.name(), tracker)

        # reset slots if necessary
        events = contexts_reset(self.name(), tracker)

        # standardize the slots
        events.extend(transform_slots_to_standard(tracker))

        # Checking required parameters
        intent = contain.index[index]

        for entity in intent.entities:
            if entity.required == True:
                slot = entity.name
                if slot != None:
                    slot_val = tracker.get_slot(slot)
                    if slot_val is None:
                        logger.info("Uttering the required parameter")
                        dispatcher.utter_template(
                            command_sanitizer("utter_{}_follow_up_{}".format(
                                self.name(), slot)))
                        events.append(SlotSet("requested_slot", slot))
                        return events

        text = template["text"]
        modified_text = ""
        i = 0
        while i < (len(text)):
            if text[i] == '{':
                j = i + 1
                slot = ""
                while (text[j] != '}' and j < len(text)):
                    slot += text[j]
                    j += 1
                modified_text += tracker.get_slot(slot)
                i = j
            else:
                modified_text += text[i]
            i += 1
        dispatcher.utter_message(modified_text)
        contexts = out_context_set(self.name)
        for c in contexts:
            events.append(SlotSet(c, 1))
        events.append(SlotSet("requested_slot", None))
        return events
    def run(self, dispatcher, tracker, domain):
        index = "transfer_money___yes"
        template = dispatcher.retrieve_template("utter_" +
                                                "transfer_money_yes")

        # Checking required parameters
        intent = contain.index[index]
        for entity in intent.entities:
            if entity.required == True:
                slot = entity.name
                if slot != None:
                    slot_val = tracker.get_slot(slot)
                    if slot_val is None:
                        logger.info("Uttering the required parameter")
                        dispatcher.utter_template(
                            command_sanitizer("utter_{}_follow_up_{}".format(
                                self.name(), slot)))
                        events = []
                        events.append(SlotSet("requested_slot", slot))
                        return events

        text = template["text"]
        modified_text = ""
        i = 0
        while i < (len(text)):
            if text[i] == '{':
                j = i + 1
                slot = ""
                while (text[j] != '}' and j < len(text)):
                    slot += text[j]
                    j += 1
                modified_text += tracker.get_slot(slot)
                i = j
            else:
                modified_text += text[i]
            i += 1
        dispatcher.utter_message(modified_text)
        events = []
        contexts = out_context_set(self.name)
        for c in contexts:
            events.append(SlotSet(c, 1))
        events.append(SlotSet("requested_slot", None))
        return events