Пример #1
0
    def load(cls, filename, action_factory=None):
        if not os.path.isfile(filename):
            raise Exception(
                    "Failed to load domain specification from '{}'. "
                    "File not found!".format(os.path.abspath(filename)))

        cls.validate_domain_yaml(filename)
        data = read_yaml_file(filename)
        utter_templates = cls.collect_templates(data.get("templates", {}))
        if not action_factory:
            action_factory = data.get("action_factory", None)
        topics = [Topic(name) for name in data.get("topics", [])]
        slots = cls.collect_slots(data.get("slots", {}))
        additional_arguments = data.get("config", {})
        return TemplateDomain(
                data.get("intents", []),
                data.get("entities", []),
                slots,
                utter_templates,
                data.get("actions", []),
                data.get("action_names", []),
                action_factory,
                topics,
                **additional_arguments
        )
Пример #2
0
def tracker_from_dialogue_file(filename, domain=None):
    dialogue = read_dialogue_file(filename)

    dialogue_topics = set(
        [Topic(t.topic) for t in dialogue.events if isinstance(t, TopicSet)])
    if domain is not None:
        domain = domain
    else:
        domain = TemplateDomain.load(DEFAULT_DOMAIN_PATH)
    domain.topics.extend(dialogue_topics)
    tracker = DialogueStateTracker(dialogue.name, domain.slots, domain.topics,
                                   domain.default_topic)
    tracker.recreate_from_dialogue(dialogue)
    return tracker
Пример #3
0
def test_tracker_duplicate():
    filename = "data/test_dialogues/inform_no_change.json"
    dialogue = read_dialogue_file(filename)
    dialogue_topics = set([Topic(t.topic)
                           for t in dialogue.events
                           if isinstance(t, TopicSet)])
    domain.topics.extend(dialogue_topics)
    tracker = DialogueStateTracker(dialogue.name, domain.slots,
                                   domain.topics, domain.default_topic)
    tracker.recreate_from_dialogue(dialogue)
    num_actions = len([event
                       for event in dialogue.events
                       if isinstance(event, ActionExecuted)])

    # There is always one duplicated tracker more than we have actions,
    # as the tracker also gets duplicated for the
    # action that would be next (but isn't part of the operations)
    assert len(list(tracker.generate_all_prior_states())) == num_actions + 1
Пример #4
0
 def load(cls, file_name):
     import yaml
     import io
     if os.path.isfile(file_name):
         with io.open(file_name, encoding="utf-8") as f:
             data = yaml.load(f.read())
             templates = data.get("templates", [])
             action_factory = data.get("action_factory", None)
             topics = [Topic(name) for name in data.get("topics", [])]
             slots = TemplateDomain.collect_slots(data.get("slots", {}))
             additional_arguments = data.get("config", {})
             return TemplateDomain(data.get("intents", []),
                                   data.get("entities",
                                            []), slots, templates,
                                   data.get("actions", []), action_factory,
                                   topics, **additional_arguments)
     else:
         raise Exception("Failed to load domain specification from '{}'. "
                         "File not found!".format(
                             os.path.abspath(file_name)))
Пример #5
0
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals

from rasa_core import utils
from rasa_core.conversation import Topic, DefaultTopic

dummy_topics = [Topic("topic_{0}".format(i)) for i in range(4)]


def test_default():
    stack = utils.TopicStack(dummy_topics, [], DefaultTopic)
    assert stack.top.name == DefaultTopic.name, \
        "default topic not correctly set"


def test_push_pop():
    stack = utils.TopicStack(dummy_topics, [], DefaultTopic)
    stack.push(dummy_topics[0])
    assert stack.top.name == dummy_topics[0].name, \
        "pushing a topic should put that topic at the top"
    stack.push(dummy_topics[0])
    assert stack.top.name == dummy_topics[0].name, \
        "pushing a topic a second time should have no effect"
    stack.pop()
    assert len(stack) == 0, \
        "popping the last topic should leave the stack empty"
    assert stack.top.name == DefaultTopic.name, \
        "popping a the last topic should leave default on top"
    stack.pop()