Exemple #1
0
def _revert_single_affirmation_events() -> List[Event]:
    return [
        UserUtteranceReverted(),  # revert affirmation and request
        # revert original intent (has to be re-added later)
        UserUtteranceReverted(),
        # add action listen intent
        ActionExecuted(action_name=ACTION_LISTEN_NAME),
    ]
Exemple #2
0
def _revert_rephrasing_events() -> List[Event]:
    return [
        UserUtteranceReverted(),  # remove rephrasing
        # remove feedback and rephrase request
        UserUtteranceReverted(),
        # remove affirmation request and false intent
        UserUtteranceReverted(),
        # replace action with action listen
        ActionExecuted(action_name=ACTION_LISTEN_NAME),
    ]
Exemple #3
0
    def _log_action_on_tracker(self, tracker, action_name, events, policy,
                               confidence):
        # Ensures that the code still works even if a lazy programmer missed
        # to type `return []` at the end of an action or the run method
        # returns `None` for some other reason.
        if events is None:
            events = []

        logger.debug("Action '{}' ended with events '{}'".format(
            action_name, ["{}".format(e) for e in events]))

        self._warn_about_new_slots(tracker, action_name, events)

        if action_name is not None:
            # log the action and its produced events
            tracker.update(ActionExecuted(action_name, policy, confidence))

        for e in events:
            # this makes sure the events are ordered by timestamp -
            # since the event objects are created somewhere else,
            # the timestamp would indicate a time before the time
            # of the action executed
            e.timestamp = time.time()
            tracker.update(e)
 def _add_action_listen(self, events):
     if not events or not self._is_action_listen(events[-1]):
         # do not add second action_listen
         events.append(ActionExecuted(ACTION_LISTEN_NAME))
    def as_story_string(self, flat=False, e2e=False):
        # if the result should be flattened, we
        # will exclude the caption and any checkpoints.

        for s in self.start_checkpoints:
            if s.name == STORY_START:
                # first story step in the story, so reset helper
                self.story_string_helper = StoryStringHelper()

        if flat:
            result = ""
        else:
            result = "\n## {}\n".format(self.block_name)
            for s in self.start_checkpoints:
                if s.name != STORY_START:
                    result += self._checkpoint_string(s)

        for s in self.events:
            if isinstance(s, UserUttered):
                if self.story_string_helper.active_form is None:
                    result += self._user_string(s, e2e)
                else:
                    # form is active
                    # it is not known whether the form will be
                    # successfully executed, so store this
                    # story string for later
                    self._store_user_strings(s, e2e, FORM_PREFIX)

            elif isinstance(s, Form):
                # form got either activated or deactivated
                self.story_string_helper.active_form = s.name

                if self.story_string_helper.active_form is None:
                    # form deactivated, so form succeeded,
                    # so add story string with form prefix
                    result += self.story_string_helper.form_prefix_string
                    # remove all stored story strings
                    self._reset_stored_strings()

                result += self._bot_string(s)

            elif isinstance(s, FormValidation):
                self.story_string_helper.form_validation = s.validate

            elif isinstance(s, ActionExecutionRejected):
                if s.action_name == self.story_string_helper.active_form:
                    # form rejected
                    self.story_string_helper.form_rejected = True

            elif isinstance(s, ActionExecuted):
                if self._is_action_listen(s):
                    pass
                elif self.story_string_helper.active_form is None:
                    result += self._bot_string(s)
                else:
                    # form is active
                    if self.story_string_helper.form_rejected:
                        if (self.story_string_helper.form_validation
                                and s.action_name
                                == self.story_string_helper.active_form):
                            result += self._bot_string(
                                ActionExecuted(ACTION_LISTEN_NAME))
                            result += self.story_string_helper.form_prefix_string
                        else:
                            result += self.story_string_helper.no_form_prefix_string
                        # form rejected, add story string without form prefix
                        result += self._bot_string(s)
                    else:
                        # form succeeded, so add story string with form prefix
                        result += self.story_string_helper.form_prefix_string
                        result += self._bot_string(s, FORM_PREFIX)

                    # remove all stored story strings
                    self._reset_stored_strings()

                    if s.action_name == self.story_string_helper.active_form:
                        # form was successfully executed
                        self.story_string_helper.form_rejected = False

                self.story_string_helper.form_validation = True

            elif isinstance(s, SlotSet):
                if self.story_string_helper.active_form is None:
                    result += self._bot_string(s)
                else:
                    # form is active
                    # it is not known whether the form will be
                    # successfully executed, so store this
                    # story string for later
                    # slots should be always printed without prefix
                    self._store_bot_strings(s)

            elif isinstance(s, Event):
                converted = s.as_story_string()
                if converted:
                    if self.story_string_helper.active_form is None:
                        result += self._bot_string(s)
                    else:
                        # form is active
                        # it is not known whether the form will be
                        # successfully executed, so store this
                        # story string for later
                        self._store_bot_strings(s, FORM_PREFIX)

            else:
                raise Exception(
                    "Unexpected element in story step: {}".format(s))

        if (not self.end_checkpoints
                and self.story_string_helper.active_form is not None):
            # there are no end checkpoints
            # form is active
            # add story string with form prefix
            result += self.story_string_helper.form_prefix_string
            # remove all stored story strings
            self._reset_stored_strings()

        if not flat:
            for e in self.end_checkpoints:
                result += "> {}\n".format(e.as_story_string())
        return result