예제 #1
0
    def encode_state(
        self, state: State, interpreter: NaturalLanguageInterpreter
    ) -> Dict[Text, List["Features"]]:
        """Encode the given state with the help of the given interpreter.

        Args:
            state: The state to encode
            interpreter: The interpreter used to encode the state

        Returns:
            A dictionary of state_type to list of features.
        """
        state_features = {}
        for state_type, sub_state in state.items():
            if state_type == PREVIOUS_ACTION:
                state_features.update(
                    self._extract_state_features(sub_state, interpreter, sparse=True)
                )
            # featurize user only if it is "real" user input,
            # i.e. input from a turn after action_listen
            if state_type == USER and is_prev_action_listen_in_state(state):
                state_features.update(
                    self._extract_state_features(sub_state, interpreter, sparse=True)
                )
                if sub_state.get(ENTITIES):
                    state_features[ENTITIES] = self._create_features(
                        sub_state, ENTITIES, sparse=True
                    )

            if state_type in {SLOTS, ACTIVE_LOOP}:
                state_features[state_type] = self._create_features(
                    sub_state, state_type, sparse=True
                )

        return state_features
예제 #2
0
 def freeze_current_state(state: State) -> FrozenState:
     frozen_state = frozenset({
         key: frozenset(values.items())
         if isinstance(values, Dict) else frozenset(values)
         for key, values in state.items()
     }.items())
     return frozen_state
예제 #3
0
    def _does_rule_match_state(rule_state: State,
                               conversation_state: State) -> bool:
        for state_type, rule_sub_state in rule_state.items():
            conversation_sub_state = conversation_state.get(state_type, {})
            for key, value_from_rules in rule_sub_state.items():
                if isinstance(value_from_rules, list):
                    # json dumps and loads tuples as lists,
                    # so we need to convert them back
                    value_from_rules = tuple(value_from_rules)
                value_from_conversation = conversation_sub_state.get(key)
                if (
                        # value should be set, therefore
                        # check whether it is the same as in the state
                        value_from_rules and
                        value_from_rules != SHOULD_NOT_BE_SET and
                        value_from_conversation != value_from_rules
                ) or (
                        # value shouldn't be set, therefore
                        # it should be None or non existent in the state
                        value_from_rules == SHOULD_NOT_BE_SET
                        and value_from_conversation
                        # during training `SHOULD_NOT_BE_SET` is provided. Hence, we also
                        # have to check for the value of the slot state
                        and value_from_conversation != SHOULD_NOT_BE_SET):
                    return False

        return True
예제 #4
0
파일: trackers.py 프로젝트: zoovu/rasa
    def freeze_current_state(state: State) -> FrozenState:
        """Convert State dict into a hashable format FrozenState.

        Args:
            state: The state which should be converted

        Return:
            hashable form of the state of type `FrozenState`
        """
        return frozenset({
            key: frozenset(values.items())
            if isinstance(values, Dict) else frozenset(values)
            for key, values in state.items()
        }.items())
예제 #5
0
    def encode_state(
        self,
        state: State,
        precomputations: Optional[MessageContainerForCoreFeaturization],
    ) -> Dict[Text, List[Features]]:
        """Encode the given state.

        Args:
            state: The state to encode
            precomputations: Contains precomputed features and attributes.

        Returns:
            A dictionary of state_type to list of features.
        """
        state_features = {}
        for state_type, sub_state in state.items():
            if state_type == PREVIOUS_ACTION:
                state_features.update(
                    self._extract_state_features(
                        sub_state, precomputations=precomputations, sparse=True,
                    )
                )
            # featurize user only if it is "real" user input,
            # i.e. input from a turn after action_listen
            if state_type == USER and is_prev_action_listen_in_state(state):

                state_features.update(
                    self._extract_state_features(
                        sub_state, precomputations=precomputations, sparse=True,
                    )
                )
                if sub_state.get(ENTITIES):
                    state_features[ENTITIES] = self._create_features(
                        sub_state, ENTITIES, sparse=True
                    )

            if state_type in {SLOTS, ACTIVE_LOOP}:
                state_features[state_type] = self._create_features(
                    sub_state, state_type, sparse=True
                )

        return state_features
예제 #6
0
    def _does_rule_match_state(rule_state: State,
                               conversation_state: State) -> bool:
        for state_type, rule_sub_state in rule_state.items():
            conversation_sub_state = conversation_state.get(state_type, {})
            for key, value in rule_sub_state.items():
                if isinstance(value, list):
                    # json dumps and loads tuples as lists,
                    # so we need to convert them back
                    value = tuple(value)

                if (
                        # value should be set, therefore
                        # check whether it is the same as in the state
                        value and value != SHOULD_NOT_BE_SET and
                        conversation_sub_state.get(key) != value
                ) or (
                        # value shouldn't be set, therefore
                        # it should be None or non existent in the state
                        value == SHOULD_NOT_BE_SET
                        and conversation_sub_state.get(key)):
                    return False

        return True