Exemple #1
0
def test_story_visualization_with_training_data(domain: Domain, tmp_path: Path,
                                                nlu_data_path: Text):
    import rasa.shared.core.training_data.loading as core_loading

    story_steps = core_loading.load_data_from_resource(
        "data/test_yaml_stories/stories.yml", domain)
    out_file = tmp_path / "graph.html"
    test_text = "test text"
    test_intent = "affirm"
    generated_graph = visualization.visualize_stories(
        story_steps,
        domain,
        output_file=str(out_file),
        max_history=3,
        should_merge_nodes=False,
        nlu_training_data=TrainingData(
            [Message({
                TEXT: test_text,
                INTENT: test_intent
            })]),
    )

    assert test_text in out_file.read_text()
    assert test_intent not in out_file.read_text()

    assert len(generated_graph.nodes()) == 51
    assert len(generated_graph.edges()) == 56
Exemple #2
0
    async def visualize(
        self,
        resource_name: Text,
        output_file: Text,
        max_history: Optional[int] = None,
        nlu_training_data: Optional[TrainingData] = None,
        should_merge_nodes: bool = True,
        fontsize: int = 12,
    ) -> None:
        """Visualize the loaded training data from the resource."""
        from rasa.shared.core.training_data.visualization import visualize_stories
        from rasa.shared.core.training_data import loading

        # if the user doesn't provide a max history, we will use the
        # largest value from any policy
        max_history = max_history or self._max_history()

        story_steps = loading.load_data_from_resource(resource_name,
                                                      self.domain)
        await visualize_stories(
            story_steps,
            self.domain,
            output_file,
            max_history,
            self.interpreter,
            nlu_training_data,
            should_merge_nodes,
            fontsize,
        )
Exemple #3
0
def visualize(
    domain_path: Text,
    stories_path: Text,
    nlu_data_path: Text,
    output_path: Text,
    max_history: int,
) -> None:
    """Visualizes stories as graph.

    Args:
        domain_path: Path to the domain file.
        stories_path: Path to the stories files.
        nlu_data_path: Path to the NLU training data which can be used to interpolate
            intents with actual examples in the graph.
        output_path: Path where the created graph should be persisted.
        max_history: Max history to use for the story visualization.
    """
    import rasa.shared.core.training_data.visualization

    try:
        domain = Domain.load(domain_path)
    except InvalidDomain as e:
        print_error(
            f"Could not load domain due to: '{e}'. To specify a valid domain path use "
            f"the '--domain' argument.")
        return

    # this is optional, only needed if the `/greet` type of
    # messages in the stories should be replaced with actual
    # messages (e.g. `hello`)
    if nlu_data_path is not None:
        import rasa.shared.nlu.training_data.loading

        nlu_training_data = rasa.shared.nlu.training_data.loading.load_data(
            nlu_data_path)
    else:
        nlu_training_data = None

    logger.info("Starting to visualize stories...")
    telemetry.track_visualization()

    story_steps = loading.load_data_from_resource(stories_path, domain)
    rasa.shared.core.training_data.visualization.visualize_stories(
        story_steps,
        domain,
        output_path,
        max_history,
        nlu_training_data=nlu_training_data,
    )

    full_output_path = "file://{}".format(os.path.abspath(output_path))
    logger.info(f"Finished graph creation. Saved into {full_output_path}")

    import webbrowser

    webbrowser.open(full_output_path)
Exemple #4
0
def test_story_visualization_with_merging(domain: Domain):
    import rasa.shared.core.training_data.loading as core_loading

    story_steps = core_loading.load_data_from_resource(
        "data/test_yaml_stories/stories.yml", domain)
    generated_graph = visualization.visualize_stories(story_steps,
                                                      domain,
                                                      output_file=None,
                                                      max_history=3,
                                                      should_merge_nodes=True)
    assert 15 < len(generated_graph.nodes()) < 33

    assert 20 < len(generated_graph.edges()) < 33
Exemple #5
0
def test_merge_nodes(domain: Domain, tmp_path: Path):
    from os.path import isfile
    import rasa.shared.core.training_data.loading as core_loading

    story_steps = core_loading.load_data_from_resource(
        "data/test_yaml_stories/stories.yml", domain)
    out_file = str(tmp_path / "graph.html")
    visualization.visualize_stories(
        story_steps,
        domain,
        output_file=out_file,
        max_history=3,
        should_merge_nodes=True,
    )
    assert isfile(out_file)
Exemple #6
0
async def test_story_visualization(domain: Domain, tmp_path: Path):
    import rasa.shared.core.training_data.loading as core_loading

    story_steps = core_loading.load_data_from_resource(
        "data/test_yaml_stories/stories.yml", domain)
    out_file = str(tmp_path / "graph.html")
    generated_graph = await visualize_stories(
        story_steps,
        domain,
        output_file=out_file,
        max_history=3,
        should_merge_nodes=False,
    )

    assert len(generated_graph.nodes()) == 51

    assert len(generated_graph.edges()) == 56
Exemple #7
0
def test_story_visualization(domain: Domain, tmp_path: Path):
    import rasa.shared.core.training_data.loading as core_loading

    story_steps = core_loading.load_data_from_resource(
        "data/test_yaml_stories/stories.yml", domain)
    out_file = tmp_path / "graph.html"
    generated_graph = visualization.visualize_stories(
        story_steps,
        domain,
        output_file=str(out_file),
        max_history=3,
        should_merge_nodes=False,
    )

    assert str(None) not in out_file.read_text()
    assert "/affirm" in out_file.read_text()
    assert len(generated_graph.nodes()) == 51
    assert len(generated_graph.edges()) == 56
Exemple #8
0
def extract_story_graph(
        resource_name: Text,
        domain: "Domain",
        exclusion_percentage: Optional[int] = None) -> "StoryGraph":
    """Loads training stories / rules from file or directory.

    Args:
        resource_name: Path to file or directory.
        domain: The model domain.
        exclusion_percentage: Percentage of stories which should be dropped. `None`
            if all training data should be used.

    Returns:
        The loaded training data as graph.
    """
    from rasa.shared.core.training_data.structures import StoryGraph
    import rasa.shared.core.training_data.loading as core_loading

    story_steps = core_loading.load_data_from_resource(
        resource_name, domain, exclusion_percentage=exclusion_percentage)
    return StoryGraph(story_steps)
Exemple #9
0
def test_graph_persistence(domain: Domain, tmp_path: Path):
    from os.path import isfile
    from networkx.drawing import nx_pydot
    import rasa.shared.core.training_data.loading as core_loading

    story_steps = core_loading.load_data_from_resource(
        "data/test_yaml_stories/stories.yml", domain)
    out_file = str(tmp_path / "graph.html")
    generated_graph = visualization.visualize_stories(
        story_steps,
        domain,
        output_file=out_file,
        max_history=3,
        should_merge_nodes=False,
    )

    generated_graph = nx_pydot.to_pydot(generated_graph)

    assert isfile(out_file)

    content = rasa.shared.utils.io.read_file(out_file)

    assert "isClient = true" in content
    assert "graph = `{}`".format(generated_graph.to_string()) in content