<commit_before>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import copy
import io
import logging
from collections import deque

import jsonpickle
import typing
from typing import Generator, Dict, Text, Any, Optional, Iterator
from typing import List

from rasa_core import utils
from rasa_core import events
from rasa_core.conversation import Dialogue, Topic
from rasa_core.events import UserUttered, TopicSet, ActionExecuted, \
    Event, SlotSet, Restarted, ActionReverted, UserUtteranceReverted, BotUttered

logger = logging.getLogger(__name__)

if typing.TYPE_CHECKING:
    from rasa_core.actions import Action


class DialogueStateTracker(object):
    """Maintains the state of a conversation."""

    @classmethod
    def from_dict(cls, sender_id, dump_as_dict, domain):
        evts = events.deserialise_events(dump_as_dict, domain)
        tracker = cls(sender_id, domain.slots, domain.topics,
                      domain.default_topic)
        for e in evts:
            tracker.update(e)
        return tracker

    def __init__(self, sender_id, slots,
                 topics=None,
                 default_topic=None,
                 max_event_history=None):
        """Initialize the tracker.

        A set of events can be stored externally, and we will run through all
        of them to get the current state. The tracker will represent all the
        information we captured while processing messages of the dialogue."""

        # maximum number of events to store
        self._max_event_history = max_event_history
        # list of previously seen events
        self.events = self._create_events([])
        # id of the source of the messages
        self.sender_id = sender_id
        # available topics in the domain
        self.topics = topics if topics is not None else []
        # default topic of the domain
        self.default_topic = default_topic
        # slots that can be filled in this domain
        self.slots = {slot.name: copy.deepcopy(slot) for slot in slots}

        ###
        # current state of the tracker - MUST be re-creatable by processing
        # all the events. This only defines the attributes, values are set in
        # `reset()`
        ###
        # if tracker is paused, no actions should be taken
        self._paused = None
        # A deterministically scheduled action to be executed next
        self.follow_up_action = None
        # topic tracking
        self._topic_stack = None
        self.latest_action_name = None
        self.latest_message = None
        self.latest_bot_utterance = None
        self._reset()

    ###
    # Public tracker interface
    ###
    def current_state(self, should_include_events=False):
        # type: (bool) -> Dict[Text, Any]
        """Returns the current tracker state as an object."""

        if should_include_events:
            evts = [e.as_dict() for e in self.events]
        else:
            evts = None

        latest_event_time = None
        if len(self.events) > 0:
            latest_event_time = self.events[-1].timestamp

        return {
            "sender_id": self.sender_id,
            "slots": self.current_slot_values(),
            "latest_message": self.latest_message.parse_data,
            "latest_event_time": latest_event_time,
            "paused": self.is_paused(),
            "events": evts
        }

    def current_slot_values(self):
        """Return the currently set values of the slots"""
        return {key: slot.value for key, slot in self.slots.items()}

    def get_slot(self, key):
        # type: (Text) -> Optional[Any]
        """Retrieves the value of a slot."""

        if key in self.slots:
            return self.slots[key].value
        else:
            logger.info("Tried to access non existent slot '{}'".format(key))
            return None

    def get_latest_entity_values(self, entity_type):<commit_msg>added paraneter to current state of tracker<commit_after>from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

import copy
import io
import logging
from collections import deque

import jsonpickle
import typing
from typing import Generator, Dict, Text, Any, Optional, Iterator
from typing import List

from rasa_core import utils
from rasa_core import events
from rasa_core.conversation import Dialogue, Topic
from rasa_core.events import UserUttered, TopicSet, ActionExecuted, \
    Event, SlotSet, Restarted, ActionReverted, UserUtteranceReverted, BotUttered

logger = logging.getLogger(__name__)

if typing.TYPE_CHECKING:
    from rasa_core.actions import Action


class DialogueStateTracker(object):
    """Maintains the state of a conversation."""

    @classmethod
    def from_dict(cls, sender_id, dump_as_dict, domain):
        evts = events.deserialise_events(dump_as_dict, domain)
        tracker = cls(sender_id, domain.slots, domain.topics,
                      domain.default_topic)
        for e in evts:
            tracker.update(e)
        return tracker

    def __init__(self, sender_id, slots,
                 topics=None,
                 default_topic=None,
                 max_event_history=None):
        """Initialize the tracker.

        A set of events can be stored externally, and we will run through all
        of them to get the current state. The tracker will represent all the
        information we captured while processing messages of the dialogue."""

        # maximum number of events to store
        self._max_event_history = max_event_history
        # list of previously seen events
        self.events = self._create_events([])
        # id of the source of the messages
        self.sender_id = sender_id
        # available topics in the domain
        self.topics = topics if topics is not None else []
        # default topic of the domain
        self.default_topic = default_topic
        # slots that can be filled in this domain
        self.slots = {slot.name: copy.deepcopy(slot) for slot in slots}

        ###
        # current state of the tracker - MUST be re-creatable by processing
        # all the events. This only defines the attributes, values are set in
        # `reset()`
        ###
        # if tracker is paused, no actions should be taken
        self._paused = None
        # A deterministically scheduled action to be executed next
        self.follow_up_action = None
        # topic tracking
        self._topic_stack = None
        self.latest_action_name = None
        self.latest_message = None
        self.latest_bot_utterance = None
        self._reset()

    ###
    # Public tracker interface
    ###
    def current_state(self,
                      should_include_events=False,
                      only_events_after_latest_restart=False):
        # type: (bool, bool) -> Dict[Text, Any]
        """Returns the current tracker state as an object."""

        if should_include_events:
            if only_events_after_latest_restart:
                es = self.events
            else:
                es = self.events_after_latest_restart()
            evts = [e.as_dict() for e in es]
        else:
            evts = None

        latest_event_time = None
        if len(self.events) > 0:
            latest_event_time = self.events[-1].timestamp

        return {
            "sender_id": self.sender_id,
            "slots": self.current_slot_values(),
            "latest_message": self.latest_message.parse_data,
            "latest_event_time": latest_event_time,
            "paused": self.is_paused(),
            "events": evts
        }

    def current_slot_values(self):
        """Return the currently set values of the slots"""
        return {key: slot.value for key, slot in self.slots.items()}

    def get_slot(self, key):
        # type: (Text) -> Optional[Any]
        """Retrieves the value of a slot."""

        if key in self.slots:
            return self.slots[key].value
        else:
            logger.info("Tried to access non existent slot '{}'".format(key))
            return None

    def get_latest_entity_values(self, entity_type):
{'fraction_non_alphanumeric': 0.07275372862859221, 'fraction_numerical': 0.0004850248575239481, 'mean_word_length': 2.387268993839836, 'pattern_counts': {'":': 12, '<': 3, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '315279', 'n_tokens_mistral': 2257, 'n_tokens_neox': 2200, 'n_words': 740}